src/misc/st/st.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  st_table_entry
struct  st_table
struct  st_generator

Defines

#define st_is_member(table, key)   st_lookup(table,key,(char **) 0)
#define st_count(table)   ((table)->num_entries)
#define ST_DEFAULT_MAX_DENSITY   5
#define ST_DEFAULT_INIT_TABLE_SIZE   11
#define ST_DEFAULT_GROW_FACTOR   2.0
#define ST_DEFAULT_REORDER_FLAG   0
#define st_foreach_item(table, gen, key, value)   for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)
#define st_foreach_item_int(table, gen, key, value)   for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)
#define ST_OUT_OF_MEM   -10000

Typedefs

typedef struct st_table_entry st_table_entry
typedef struct st_table st_table
typedef struct st_generator st_generator
typedef int(* ST_PFI )()

Enumerations

enum  st_retval { ST_CONTINUE, ST_STOP, ST_DELETE }

Functions

st_tablest_init_table_with_params (ST_PFI, ST_PFI, int, int, double, int)
st_tablest_init_table (ST_PFI, ST_PFI)
void st_free_table (st_table *)
int st_lookup (st_table *, char *, char **)
int st_lookup_int (st_table *, char *, int *)
int st_insert (st_table *, char *, char *)
int st_add_direct (st_table *, char *, char *)
int st_find_or_add (st_table *, char *, char ***)
int st_find (st_table *, char *, char ***)
st_tablest_copy (st_table *)
int st_delete (st_table *, char **, char **)
int st_delete_int (st_table *, long *, char **)
int st_foreach (st_table *, ST_PFSR, char *)
int st_strhash (char *, int)
int st_numhash (char *, int)
int st_ptrhash (char *, int)
int st_numcmp (char *, char *)
int st_ptrcmp (char *, char *)
st_generatorst_init_gen (st_table *)
int st_gen (st_generator *, char **, char **)
int st_gen_int (st_generator *, char **, long *)
void st_free_gen (st_generator *)

Variables

enum st_retval(* ST_PFSR )()

Define Documentation

#define st_count ( table   )     ((table)->num_entries)

Definition at line 48 of file st.h.

#define ST_DEFAULT_GROW_FACTOR   2.0

Definition at line 81 of file st.h.

#define ST_DEFAULT_INIT_TABLE_SIZE   11

Definition at line 80 of file st.h.

#define ST_DEFAULT_MAX_DENSITY   5

Definition at line 79 of file st.h.

#define ST_DEFAULT_REORDER_FLAG   0

Definition at line 82 of file st.h.

#define st_foreach_item ( table,
gen,
key,
value   )     for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)

Definition at line 84 of file st.h.

#define st_foreach_item_int ( table,
gen,
key,
value   )     for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)

Definition at line 87 of file st.h.

#define st_is_member ( table,
key   )     st_lookup(table,key,(char **) 0)

Definition at line 47 of file st.h.

#define ST_OUT_OF_MEM   -10000

Definition at line 90 of file st.h.


Typedef Documentation

typedef struct st_generator st_generator

Definition at line 40 of file st.h.

typedef int(* ST_PFI)()

Definition at line 53 of file st.h.

typedef struct st_table st_table

Definition at line 28 of file st.h.

Definition at line 21 of file st.h.


Enumeration Type Documentation

enum st_retval
Enumerator:
ST_CONTINUE 
ST_STOP 
ST_DELETE 

Definition at line 50 of file st.h.


Function Documentation

int st_add_direct ( st_table ,
char *  ,
char *   
)

Definition at line 233 of file st.c.

00237 {
00238     int hash_val;
00239     st_table_entry *new;
00240     
00241     hash_val = do_hash(key, table);
00242     if (table->num_entries / table->num_bins >= table->max_density) {
00243         if (rehash(table) == ST_OUT_OF_MEM) {
00244             return ST_OUT_OF_MEM;
00245         }
00246     }
00247     hash_val = do_hash(key, table);
00248     new = ALLOC(st_table_entry, 1);
00249     if (new == NULL) {
00250         return ST_OUT_OF_MEM;
00251     }
00252     new->key = key;
00253     new->record = value;
00254     new->next = table->bins[hash_val];
00255     table->bins[hash_val] = new;
00256     table->num_entries++;
00257     return 1;
00258 }

st_table* st_copy ( st_table  ) 

Definition at line 368 of file st.c.

00370 {
00371     st_table *new_table;
00372     st_table_entry *ptr, *newptr, *next, *new;
00373     int i, j, num_bins = old_table->num_bins;
00374 
00375     new_table = ALLOC(st_table, 1);
00376     if (new_table == NULL) {
00377         return NULL;
00378     }
00379     
00380     *new_table = *old_table;
00381     new_table->bins = ALLOC(st_table_entry *, num_bins);
00382     if (new_table->bins == NULL) {
00383         FREE(new_table);
00384         return NULL;
00385     }
00386     for(i = 0; i < num_bins ; i++) {
00387         new_table->bins[i] = NULL;
00388         ptr = old_table->bins[i];
00389         while (ptr != NULL) {
00390             new = ALLOC(st_table_entry, 1);
00391             if (new == NULL) {
00392                 for (j = 0; j <= i; j++) {
00393                     newptr = new_table->bins[j];
00394                     while (newptr != NULL) {
00395                         next = newptr->next;
00396                         FREE(newptr);
00397                         newptr = next;
00398                     }
00399                 }
00400                 FREE(new_table->bins);
00401                 FREE(new_table);
00402                 return NULL;
00403             }
00404             *new = *ptr;
00405             new->next = new_table->bins[i];
00406             new_table->bins[i] = new;
00407             ptr = ptr->next;
00408         }
00409     }
00410     return new_table;
00411 }

int st_delete ( st_table ,
char **  ,
char **   
)

Definition at line 414 of file st.c.

00418 {
00419     int hash_val;
00420     char *key = *keyp;
00421     register st_table_entry *ptr, **last;
00422 
00423     hash_val = do_hash(key, table);
00424 
00425     FIND_ENTRY(table, hash_val, key, ptr ,last);
00426     
00427     if (ptr == NULL) {
00428         return 0;
00429     }
00430 
00431     *last = ptr->next;
00432     if (value != NULL) *value = ptr->record;
00433     *keyp = ptr->key;
00434     FREE(ptr);
00435     table->num_entries--;
00436     return 1;
00437 }

int st_delete_int ( st_table ,
long *  ,
char **   
)

Definition at line 440 of file st.c.

00444 {
00445     int hash_val;
00446     char *key = (char *) *keyp;
00447     register st_table_entry *ptr, **last;
00448 
00449     hash_val = do_hash(key, table);
00450 
00451     FIND_ENTRY(table, hash_val, key, ptr ,last);
00452 
00453     if (ptr == NULL) {
00454         return 0;
00455     }
00456 
00457     *last = ptr->next;
00458     if (value != NULL) *value = ptr->record;
00459     *keyp = (long) ptr->key;
00460     FREE(ptr);
00461     table->num_entries--;
00462     return 1;
00463 }

int st_find ( st_table ,
char *  ,
char ***   
)

Definition at line 298 of file st.c.

00302 {
00303     int hash_val;
00304     st_table_entry *ptr, **last;
00305 
00306     hash_val = do_hash(key, table);
00307 
00308     FIND_ENTRY(table, hash_val, key, ptr, last);
00309 
00310     if (ptr == NULL) {
00311         return 0;
00312     } else {
00313         if (slot != NULL) {
00314             *slot = &ptr->record;
00315         }
00316         return 1;
00317     }
00318 }

int st_find_or_add ( st_table ,
char *  ,
char ***   
)

Definition at line 261 of file st.c.

00265 {
00266     int hash_val;
00267     st_table_entry *new, *ptr, **last;
00268 
00269     hash_val = do_hash(key, table);
00270 
00271     FIND_ENTRY(table, hash_val, key, ptr, last);
00272 
00273     if (ptr == NULL) {
00274         if (table->num_entries / table->num_bins >= table->max_density) {
00275             if (rehash(table) == ST_OUT_OF_MEM) {
00276                 return ST_OUT_OF_MEM;
00277             }
00278             hash_val = do_hash(key, table);
00279         }
00280         new = ALLOC(st_table_entry, 1);
00281         if (new == NULL) {
00282             return ST_OUT_OF_MEM;
00283         }
00284         new->key = key;
00285         new->record = (char *) 0;
00286         new->next = table->bins[hash_val];
00287         table->bins[hash_val] = new;
00288         table->num_entries++;
00289         if (slot != NULL) *slot = &new->record;
00290         return 0;
00291     } else {
00292         if (slot != NULL) *slot = &ptr->record;
00293         return 1;
00294     }
00295 }

int st_foreach ( st_table ,
ST_PFSR  ,
char *   
)
void st_free_gen ( st_generator  ) 

Definition at line 621 of file st.c.

00623 {
00624     FREE(gen);
00625 }

void st_free_table ( st_table  ) 

Definition at line 99 of file st.c.

00101 {
00102     register st_table_entry *ptr, *next;
00103     int i;
00104 
00105     for(i = 0; i < table->num_bins ; i++) {
00106         ptr = table->bins[i];
00107         while (ptr != NULL) {
00108             next = ptr->next;
00109             FREE(ptr);
00110             ptr = next;
00111         }
00112     }
00113     FREE(table->bins);
00114     FREE(table);
00115 }

int st_gen ( st_generator ,
char **  ,
char **   
)

Definition at line 561 of file st.c.

00565 {
00566     register int i;
00567 
00568     if (gen->entry == NULL) {
00569         /* try to find next entry */
00570         for(i = gen->index; i < gen->table->num_bins; i++) {
00571             if (gen->table->bins[i] != NULL) {
00572                 gen->index = i+1;
00573                 gen->entry = gen->table->bins[i];
00574                 break;
00575             }
00576         }
00577         if (gen->entry == NULL) {
00578             return 0;           /* that's all folks ! */
00579         }
00580     }
00581     *key_p = gen->entry->key;
00582     if (value_p != 0) {
00583         *value_p = gen->entry->record;
00584     }
00585     gen->entry = gen->entry->next;
00586     return 1;
00587 }

int st_gen_int ( st_generator ,
char **  ,
long *   
)

Definition at line 591 of file st.c.

00595 {
00596     register int i;
00597 
00598     if (gen->entry == NULL) {
00599         /* try to find next entry */
00600         for(i = gen->index; i < gen->table->num_bins; i++) {
00601             if (gen->table->bins[i] != NULL) {
00602                 gen->index = i+1;
00603                 gen->entry = gen->table->bins[i];
00604                 break;
00605             }
00606         }
00607         if (gen->entry == NULL) {
00608             return 0;           /* that's all folks ! */
00609         }
00610     }
00611     *key_p = gen->entry->key;
00612     if (value_p != 0) {
00613         *value_p = (long) gen->entry->record;
00614     }
00615     gen->entry = gen->entry->next;
00616     return 1;
00617 }

st_generator* st_init_gen ( st_table  ) 

Definition at line 544 of file st.c.

00546 {
00547     st_generator *gen;
00548 
00549     gen = ALLOC(st_generator, 1);
00550     if (gen == NULL) {
00551         return NULL;
00552     }
00553     gen->table = table;
00554     gen->entry = NULL;
00555     gen->index = 0;
00556     return gen;
00557 }

st_table* st_init_table ( ST_PFI  ,
ST_PFI   
)
st_table* st_init_table_with_params ( ST_PFI  ,
ST_PFI  ,
int  ,
int  ,
double  ,
int   
)
int st_insert ( st_table ,
char *  ,
char *   
)

Definition at line 196 of file st.c.

00200 {
00201     int hash_val;
00202     st_table_entry *new;
00203     register st_table_entry *ptr, **last;
00204 
00205     hash_val = do_hash(key, table);
00206 
00207     FIND_ENTRY(table, hash_val, key, ptr, last);
00208 
00209     if (ptr == NULL) {
00210         if (table->num_entries/table->num_bins >= table->max_density) {
00211             if (rehash(table) == ST_OUT_OF_MEM) {
00212                 return ST_OUT_OF_MEM;
00213             }
00214             hash_val = do_hash(key, table);
00215         }
00216         new = ALLOC(st_table_entry, 1);
00217         if (new == NULL) {
00218             return ST_OUT_OF_MEM;
00219         }
00220         new->key = key;
00221         new->record = value;
00222         new->next = table->bins[hash_val];
00223         table->bins[hash_val] = new;
00224         table->num_entries++;
00225         return 0;
00226     } else {
00227         ptr->record = value;
00228         return 1;
00229     }
00230 }

int st_lookup ( st_table ,
char *  ,
char **   
)

Definition at line 133 of file st.c.

00137 {
00138     int hash_val;
00139     register st_table_entry *ptr, **last;
00140 
00141     hash_val = do_hash(key, table);
00142 
00143     FIND_ENTRY(table, hash_val, key, ptr, last);
00144     
00145     if (ptr == NULL) {
00146         return 0;
00147     } else {
00148         if (value != NULL) {
00149             *value = ptr->record; 
00150         }
00151         return 1;
00152     }
00153 }

int st_lookup_int ( st_table ,
char *  ,
int *   
)

Definition at line 156 of file st.c.

00160 {
00161     int hash_val;
00162     register st_table_entry *ptr, **last;
00163 
00164     hash_val = do_hash(key, table);
00165 
00166     FIND_ENTRY(table, hash_val, key, ptr, last);
00167     
00168     if (ptr == NULL) {
00169         return 0;
00170     } else {
00171         if (value != 0) {
00172             *value = (long) ptr->record;
00173         }
00174         return 1;
00175     }
00176 }

int st_numcmp ( char *  ,
char *   
)

Definition at line 528 of file st.c.

00531 {
00532     return ST_NUMCMP(x, y);
00533 }

int st_numhash ( char *  ,
int   
)

Definition at line 512 of file st.c.

00515 {
00516     return ST_NUMHASH(x, size);
00517 }

int st_ptrcmp ( char *  ,
char *   
)

Definition at line 536 of file st.c.

00539 {
00540     return ST_NUMCMP(x, y);
00541 }

int st_ptrhash ( char *  ,
int   
)

Definition at line 520 of file st.c.

00523 {
00524     return ST_PTRHASH(x, size);
00525 }

int st_strhash ( char *  ,
int   
)

Definition at line 497 of file st.c.

00500 {
00501     register int val = 0;
00502     register int c;
00503     
00504     while ((c = *string++) != '\0') {
00505         val = val*997 + c;
00506     }
00507 
00508     return ((val < 0) ? -val : val)%modulus;
00509 }


Variable Documentation

enum st_retval(* ST_PFSR)()

Generated on Tue Jan 5 12:19:19 2010 for abc70930 by  doxygen 1.6.1