src/st/st.c File Reference

#include "util.h"
#include "st.h"
Include dependency graph for st.c:

Go to the source code of this file.

Defines

#define ST_NUMCMP(x, y)   ((x) != (y))
#define ST_NUMHASH(x, size)   (ABS((long)x)%(size))
#define st_shift   2
#define ST_PTRHASH(x, size)   ((unsigned int)((unsigned long)(x)>>st_shift)%size)
#define EQUAL(func, x, y)
#define do_hash(key, table)
#define PTR_NOT_EQUAL(table, ptr, user_key)   (ptr != NIL(st_table_entry) && !EQUAL(table->compare, (char *)user_key, (ptr)->key))
#define FIND_ENTRY(table, hash_val, key, ptr, last)
#define ADD_DIRECT(table, key, value, hash_val, newt)

Functions

static int rehash (st_table *)
st_tablest_init_table (ST_PFICPCP compare, ST_PFICPI hash)
st_tablest_init_table_with_params (ST_PFICPCP compare, ST_PFICPI hash, int size, int density, double grow_factor, int reorder_flag)
void st_free_table (st_table *table)
int st_lookup (st_table *table, void *key, void *value)
int st_lookup_int (st_table *table, void *key, int *value)
int st_insert (st_table *table, void *key, void *value)
int st_add_direct (st_table *table, void *key, void *value)
int st_find_or_add (st_table *table, void *key, void *slot)
int st_find (st_table *table, void *key, void *slot)
st_tablest_copy (st_table *old_table)
int st_delete (st_table *table, void *keyp, void *value)
int st_delete_int (st_table *table, void *keyp, int *value)
int st_foreach (st_table *table, ST_PFSR func, char *arg)
int st_strhash (char *string, int modulus)
int st_numhash (char *x, int size)
int st_ptrhash (char *x, int size)
int st_numcmp (const char *x, const char *y)
int st_ptrcmp (const char *x, const char *y)
st_generatorst_init_gen (st_table *table)
int st_gen (st_generator *gen, void *key_p, void *value_p)
int st_gen_int (st_generator *gen, void *key_p, int *value_p)
void st_free_gen (st_generator *gen)

Variables

static char rcsid[] UNUSED = " $Id: st.c,v 1.8 2005/04/13 05:02:20 fabio Exp $"

Define Documentation

#define ADD_DIRECT ( table,
key,
value,
hash_val,
newt   ) 
Value:
{\
    if (table->num_entries/table->num_bins >= table->max_density) {\
        rehash(table);\
        hash_val = do_hash(key,table);\
    }\
    \
    newt = ALLOC(st_table_entry, 1);\
    \
    newt->key = (char *)key;\
    newt->record = value;\
    newt->next = table->bins[hash_val];\
    table->bins[hash_val] = newt;\
    table->num_entries++;\
}

Definition at line 84 of file st.c.

#define do_hash ( key,
table   ) 
Value:
((int)((table->hash == st_ptrhash) ? ST_PTRHASH((char *)(key),(table)->num_bins) :\
     (table->hash == st_numhash) ? ST_NUMHASH((char *)(key), (table)->num_bins) :\
     (*table->hash)((char *)(key), (table)->num_bins)))

Definition at line 63 of file st.c.

#define EQUAL ( func,
x,
 ) 
Value:
((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\
      (ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))

Definition at line 59 of file st.c.

#define FIND_ENTRY ( table,
hash_val,
key,
ptr,
last   ) 
Value:
(last) = &(table)->bins[hash_val];\
    (ptr) = *(last);\
    while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
        (last) = &(ptr)->next; (ptr) = *(last);\
    }\
    if ((ptr) != NIL(st_table_entry) && (table)->reorder_flag) {\
        *(last) = (ptr)->next;\
        (ptr)->next = (table)->bins[hash_val];\
        (table)->bins[hash_val] = (ptr);\
    }

Definition at line 71 of file st.c.

#define PTR_NOT_EQUAL ( table,
ptr,
user_key   )     (ptr != NIL(st_table_entry) && !EQUAL(table->compare, (char *)user_key, (ptr)->key))

Definition at line 68 of file st.c.

#define ST_NUMCMP ( x,
 )     ((x) != (y))

Definition at line 47 of file st.c.

#define ST_NUMHASH ( x,
size   )     (ABS((long)x)%(size))

Definition at line 49 of file st.c.

#define ST_PTRHASH ( x,
size   )     ((unsigned int)((unsigned long)(x)>>st_shift)%size)

Definition at line 57 of file st.c.

#define st_shift   2

Definition at line 54 of file st.c.


Function Documentation

static int rehash ( st_table table  )  [static]

AutomaticStart

Function********************************************************************

Synopsis [Rehashes a symbol table.]

Description [Rehashes a symbol table.]

SideEffects [None]

SeeAlso [st_insert]

Definition at line 1021 of file st.c.

01022 {
01023     st_table_entry *ptr, *next, **old_bins;
01024     int             i, old_num_bins, hash_val, old_num_entries;
01025 
01026     /* save old values */
01027     old_bins = table->bins;
01028     old_num_bins = table->num_bins;
01029     old_num_entries = table->num_entries;
01030 
01031     /* rehash */
01032     table->num_bins = (int) (table->grow_factor * old_num_bins);
01033     if (table->num_bins % 2 == 0) {
01034         table->num_bins += 1;
01035     }
01036     table->num_entries = 0;
01037     table->bins = ALLOC(st_table_entry *, table->num_bins);
01038     if (table->bins == NIL(st_table_entry *)) {
01039         table->bins = old_bins;
01040         table->num_bins = old_num_bins;
01041         table->num_entries = old_num_entries;
01042         return ST_OUT_OF_MEM;
01043     }
01044     /* initialize */
01045     for (i = 0; i < table->num_bins; i++) {
01046         table->bins[i] = 0;
01047     }
01048 
01049     /* copy data over */
01050     for (i = 0; i < old_num_bins; i++) {
01051         ptr = old_bins[i];
01052         while (ptr != NIL(st_table_entry)) {
01053             next = ptr->next;
01054             hash_val = do_hash(ptr->key, table);
01055             ptr->next = table->bins[hash_val];
01056             table->bins[hash_val] = ptr;
01057             table->num_entries++;
01058             ptr = next;
01059         }
01060     }
01061     FREE(old_bins);
01062 
01063     return 1;
01064 
01065 } /* rehash */

int st_add_direct ( st_table table,
void *  key,
void *  value 
)

Function********************************************************************

Synopsis [Place 'value' in 'table' under the key 'key'.]

Description [Place 'value' in 'table' under the key 'key'. This is done without checking if 'key' is in 'table' already. This should only be used if you are sure there is not already an entry for 'key', since it is undefined which entry you would later get from st_lookup or st_find_or_add. Returns 1 if successful; ST_OUT_OF_MEM otherwise.]

SideEffects [None]

SeeAlso []

Definition at line 410 of file st.c.

00411 {
00412     int hash_val;
00413     st_table_entry *newt;
00414     
00415     hash_val = do_hash(key, table);
00416     if (table->num_entries / table->num_bins >= table->max_density) {
00417         if (rehash(table) == ST_OUT_OF_MEM) {
00418             return ST_OUT_OF_MEM;
00419         }
00420     }
00421     hash_val = do_hash(key, table);
00422     newt = ALLOC(st_table_entry, 1);
00423     if (newt == NIL(st_table_entry)) {
00424         return ST_OUT_OF_MEM;
00425     }
00426     newt->key = (char *)key;
00427     newt->record = (char *)value;
00428     newt->next = table->bins[hash_val];
00429     table->bins[hash_val] = newt;
00430     table->num_entries++;
00431     return 1;
00432 
00433 } /* st_add_direct */

st_table* st_copy ( st_table old_table  ) 

Function********************************************************************

Synopsis [Return a copy of old_table and all its members.]

Description [Return a copy of old_table and all its members. (st_table *) 0 is returned if there was insufficient memory to do the copy.]

SideEffects [None]

SeeAlso []

Definition at line 571 of file st.c.

00572 {
00573     st_table *new_table;
00574     st_table_entry *ptr, *newptr, *next, *newt;
00575     int i, j, num_bins = old_table->num_bins;
00576 
00577     new_table = ALLOC(st_table, 1);
00578     if (new_table == NIL(st_table)) {
00579         return NIL(st_table);
00580     }
00581     
00582     *new_table = *old_table;
00583     new_table->bins = ALLOC(st_table_entry *, num_bins);
00584     if (new_table->bins == NIL(st_table_entry *)) {
00585         FREE(new_table);
00586         return NIL(st_table);
00587     }
00588     for(i = 0; i < num_bins ; i++) {
00589         new_table->bins[i] = NIL(st_table_entry);
00590         ptr = old_table->bins[i];
00591         while (ptr != NIL(st_table_entry)) {
00592             newt = ALLOC(st_table_entry, 1);
00593             if (newt == NIL(st_table_entry)) {
00594                 for (j = 0; j <= i; j++) {
00595                     newptr = new_table->bins[j];
00596                     while (newptr != NIL(st_table_entry)) {
00597                         next = newptr->next;
00598                         FREE(newptr);
00599                         newptr = next;
00600                     }
00601                 }
00602                 FREE(new_table->bins);
00603                 FREE(new_table);
00604                 return NIL(st_table);
00605             }
00606             *newt = *ptr;
00607             newt->next = new_table->bins[i];
00608             new_table->bins[i] = newt;
00609             ptr = ptr->next;
00610         }
00611     }
00612     return new_table;
00613 
00614 } /* st_copy */

int st_delete ( st_table table,
void *  keyp,
void *  value 
)

Function********************************************************************

Synopsis [Delete the entry with the key pointed to by `keyp'.]

Description [Delete the entry with the key pointed to by `keyp'. If the entry is found, 1 is returned, the variable pointed by `keyp' is set to the actual key and the variable pointed by `value' is set to the corresponding entry. (This allows the freeing of the associated storage.) If the entry is not found, then 0 is returned and nothing is changed.]

SideEffects [None]

SeeAlso [st_delete_int]

Definition at line 634 of file st.c.

00635 {
00636     int hash_val;
00637     char *key = *(char **)keyp;
00638     st_table_entry *ptr, **last;
00639 
00640     hash_val = do_hash(key, table);
00641 
00642     FIND_ENTRY(table, hash_val, key, ptr ,last);
00643     
00644     if (ptr == NIL(st_table_entry)) {
00645         return 0;
00646     }
00647 
00648     *last = ptr->next;
00649     if (value != NIL(void)) *(char **)value = ptr->record;
00650     *(char **)keyp = ptr->key;
00651     FREE(ptr);
00652     table->num_entries--;
00653     return 1;
00654 
00655 } /* st_delete */

int st_delete_int ( st_table table,
void *  keyp,
int *  value 
)

Function********************************************************************

Synopsis [Delete the entry with the key pointed to by `keyp'.]

Description [Delete the entry with the key pointed to by `keyp'. `value' must be a pointer to an integer. If the entry is found, 1 is returned, the variable pointed by `keyp' is set to the actual key and the variable pointed by `value' is set to the corresponding entry. (This allows the freeing of the associated storage.) If the entry is not found, then 0 is returned and nothing is changed.]

SideEffects [None]

SeeAlso [st_delete]

Definition at line 675 of file st.c.

00676 {
00677     int hash_val;
00678     char *key = *(char **)keyp;
00679     st_table_entry *ptr, **last;
00680 
00681     hash_val = do_hash(key, table);
00682 
00683     FIND_ENTRY(table, hash_val, key, ptr ,last);
00684 
00685     if (ptr == NIL(st_table_entry)) {
00686         return 0;
00687     }
00688 
00689     *last = ptr->next;
00690     if (value != NIL(int)) *value = (int) (long) ptr->record;
00691     *(char **)keyp = ptr->key;
00692     FREE(ptr);
00693     table->num_entries--;
00694     return 1;
00695 
00696 } /* st_delete_int */

int st_find ( st_table table,
void *  key,
void *  slot 
)

Function********************************************************************

Synopsis [Lookup `key' in `table'.]

Description [Like st_find_or_add, but does not create an entry if one is not found.]

SideEffects [None]

SeeAlso [st_find_or_add]

Definition at line 536 of file st.c.

00537 {
00538     int hash_val;
00539     st_table_entry *ptr, **last;
00540 
00541     hash_val = do_hash(key, table);
00542 
00543     FIND_ENTRY(table, hash_val, key, ptr, last);
00544 
00545     if (ptr == NIL(st_table_entry)) {
00546         return 0;
00547     } else {
00548         if (slot != NIL(void)) {
00549             *(char ***)slot = &ptr->record;
00550         }
00551         return 1;
00552     }
00553 
00554 } /* st_find */

int st_find_or_add ( st_table table,
void *  key,
void *  slot 
)

Function********************************************************************

Synopsis [Lookup `key' in `table'.]

Description [Lookup `key' in `table'. If not found, create an entry. In either case set slot to point to the field in the entry where the value is stored. The value associated with `key' may then be changed by accessing directly through slot. Returns 1 if an entry already existed, 0 if it did not exist and creation was successful; ST_OUT_OF_MEM otherwise. As an example:

      char **slot;
  
      char *key;
  
      char *value = (char *) item_ptr <-- ptr to a malloc'd structure
  
      if (st_find_or_add(table, key, &slot) == 1) {
  
	 FREE(*slot); <-- free the old value of the record
  
      }
  
slot = value;  <-- attach the new value to the record
  

This replaces the equivelent code:

      if (st_lookup(table, key, &ovalue) == 1) {
  
         FREE(ovalue);
  
      }
  
      st_insert(table, key, value);
  

]

SideEffects [None]

SeeAlso [st_find]

Definition at line 488 of file st.c.

00489 {
00490     int hash_val;
00491     st_table_entry *newt, *ptr, **last;
00492 
00493     hash_val = do_hash(key, table);
00494 
00495     FIND_ENTRY(table, hash_val, key, ptr, last);
00496 
00497     if (ptr == NIL(st_table_entry)) {
00498         if (table->num_entries / table->num_bins >= table->max_density) {
00499             if (rehash(table) == ST_OUT_OF_MEM) {
00500                 return ST_OUT_OF_MEM;
00501             }
00502             hash_val = do_hash(key, table);
00503         }
00504         newt = ALLOC(st_table_entry, 1);
00505         if (newt == NIL(st_table_entry)) {
00506             return ST_OUT_OF_MEM;
00507         }
00508         newt->key = (char *)key;
00509         newt->record = (char *) 0;
00510         newt->next = table->bins[hash_val];
00511         table->bins[hash_val] = newt;
00512         table->num_entries++;
00513         if (slot != NIL(void)) *(char ***)slot = &newt->record;
00514         return 0;
00515     } else {
00516         if (slot != NIL(void)) *(char ***)slot = &ptr->record;
00517         return 1;
00518     }
00519 
00520 } /* st_find_or_add */

int st_foreach ( st_table table,
ST_PFSR  func,
char *  arg 
)

Function********************************************************************

Synopsis [Iterates over the elements of a table.]

Description [For each (key, value) record in `table', st_foreach call func with the arguments

	  (*func)(key, value, arg)
  

If func returns ST_CONTINUE, st_foreach continues processing entries. If func returns ST_STOP, st_foreach stops processing and returns immediately. If func returns ST_DELETE, then the entry is deleted from the symbol table and st_foreach continues. In the case of ST_DELETE, it is func's responsibility to free the key and value, if necessary.

The routine returns 1 if all items in the table were generated and 0 if the generation sequence was aborted using ST_STOP. The order in which the records are visited will be seemingly random.]

SideEffects [None]

SeeAlso [st_foreach_item st_foreach_item_int]

Definition at line 725 of file st.c.

00726 {
00727     st_table_entry *ptr, **last;
00728     enum st_retval retval;
00729     int i;
00730 
00731     for(i = 0; i < table->num_bins; i++) {
00732         last = &table->bins[i]; ptr = *last;
00733         while (ptr != NIL(st_table_entry)) {
00734             retval = (*func)(ptr->key, ptr->record, arg);
00735             switch (retval) {
00736             case ST_CONTINUE:
00737                 last = &ptr->next; ptr = *last;
00738                 break;
00739             case ST_STOP:
00740                 return 0;
00741             case ST_DELETE:
00742                 *last = ptr->next;
00743                 table->num_entries--;   /* cstevens@ic */
00744                 FREE(ptr);
00745                 ptr = *last;
00746             }
00747         }
00748     }
00749     return 1;
00750 
00751 } /* st_foreach */

void st_free_gen ( st_generator gen  ) 

Function********************************************************************

Synopsis [Reclaims the resources associated with `gen'.]

Description [After generating all items in a generation sequence, this routine must be called to reclaim the resources associated with `gen'.]

SideEffects [None]

SeeAlso [st_init_gen]

Definition at line 994 of file st.c.

00995 {
00996     FREE(gen);
00997 
00998 } /* st_free_gen */

void st_free_table ( st_table table  ) 

Function********************************************************************

Synopsis [Free a table.]

Description [Any internal storage associated with table is freed. It is the user's responsibility to free any storage associated with the pointers he placed in the table (by perhaps using st_foreach).]

SideEffects [None]

SeeAlso [st_init_table st_init_table_with_params]

Definition at line 252 of file st.c.

00253 {
00254     st_table_entry *ptr, *next;
00255     int i;
00256 
00257     for(i = 0; i < table->num_bins ; i++) {
00258         ptr = table->bins[i];
00259         while (ptr != NIL(st_table_entry)) {
00260             next = ptr->next;
00261             FREE(ptr);
00262             ptr = next;
00263         }
00264     }
00265     FREE(table->bins);
00266     FREE(table);
00267 
00268 } /* st_free_table */

int st_gen ( st_generator gen,
void *  key_p,
void *  value_p 
)

Function********************************************************************

Synopsis [returns the next (key, value) pair in the generation sequence. ]

Description [Given a generator returned by st_init_gen(), this routine returns the next (key, value) pair in the generation sequence. The pointer `value_p' can be zero which means no value will be returned. When there are no more items in the generation sequence, the routine returns 0.

While using a generation sequence, deleting any (key, value) pair other than the one just generated may cause a fatal error when st_gen() is called later in the sequence and is therefore not recommended.]

SideEffects [None]

SeeAlso [st_gen_int]

Definition at line 908 of file st.c.

00909 {
00910     int i;
00911 
00912     if (gen->entry == NIL(st_table_entry)) {
00913         /* try to find next entry */
00914         for(i = gen->index; i < gen->table->num_bins; i++) {
00915             if (gen->table->bins[i] != NIL(st_table_entry)) {
00916                 gen->index = i+1;
00917                 gen->entry = gen->table->bins[i];
00918                 break;
00919             }
00920         }
00921         if (gen->entry == NIL(st_table_entry)) {
00922             return 0;           /* that's all folks ! */
00923         }
00924     }
00925     *(char **)key_p = gen->entry->key;
00926     if (value_p != NIL(void)) {
00927         *(char **)value_p = gen->entry->record;
00928     }
00929     gen->entry = gen->entry->next;
00930     return 1;
00931 
00932 } /* st_gen */

int st_gen_int ( st_generator gen,
void *  key_p,
int *  value_p 
)

Function********************************************************************

Synopsis [Returns the next (key, value) pair in the generation sequence.]

Description [Given a generator returned by st_init_gen(), this routine returns the next (key, value) pair in the generation sequence. `value_p' must be a pointer to an integer. The pointer `value_p' can be zero which means no value will be returned. When there are no more items in the generation sequence, the routine returns 0.]

SideEffects [None]

SeeAlso [st_gen]

Definition at line 953 of file st.c.

00954 {
00955     int i;
00956 
00957     if (gen->entry == NIL(st_table_entry)) {
00958         /* try to find next entry */
00959         for(i = gen->index; i < gen->table->num_bins; i++) {
00960             if (gen->table->bins[i] != NIL(st_table_entry)) {
00961                 gen->index = i+1;
00962                 gen->entry = gen->table->bins[i];
00963                 break;
00964             }
00965         }
00966         if (gen->entry == NIL(st_table_entry)) {
00967             return 0;           /* that's all folks ! */
00968         }
00969     }
00970     *(char **)key_p = gen->entry->key;
00971     if (value_p != NIL(int)) {
00972         *value_p = (int) (long) gen->entry->record;
00973     }
00974     gen->entry = gen->entry->next;
00975     return 1;
00976 
00977 } /* st_gen_int */

st_generator* st_init_gen ( st_table table  ) 

Function********************************************************************

Synopsis [Initializes a generator.]

Description [Returns a generator handle which when used with st_gen() will progressively return each (key, value) record in `table'.]

SideEffects [None]

SeeAlso [st_free_gen]

Definition at line 870 of file st.c.

00871 {
00872     st_generator *gen;
00873 
00874     gen = ALLOC(st_generator, 1);
00875     if (gen == NIL(st_generator)) {
00876         return NIL(st_generator);
00877     }
00878     gen->table = table;
00879     gen->entry = NIL(st_table_entry);
00880     gen->index = 0;
00881     return gen;
00882 
00883 } /* st_init_gen */

st_table* st_init_table ( ST_PFICPCP  compare,
ST_PFICPI  hash 
)

AutomaticEnd Function********************************************************************

Synopsis [Create and initialize a table.]

Description [Create and initialize a table with the comparison function compare_fn and hash function hash_fn. compare_fn is

	int compare_fn(const char *key1, const char *key2)
  

It returns <,=,> 0 depending on whether key1 <,=,> key2 by some measure.

hash_fn is

	int hash_fn(char *key, int modulus)
  

It returns a integer between 0 and modulus-1 such that if compare_fn(key1,key2) == 0 then hash_fn(key1) == hash_fn(key2).

There are five predefined hash and comparison functions in st. For keys as numbers:

	 st_numhash(key, modulus) { return (unsigned int) key % modulus; }
  
	 st_numcmp(x,y) { return (int) x - (int) y; }
  

For keys as pointers:

	 st_ptrhash(key, modulus) { return ((unsigned int) key/4) % modulus }
  
	 st_ptrcmp(x,y) { return (int) x - (int) y; }
  

For keys as strings:

         st_strhash(x,y) - a reasonable hashing function for strings
  
	 strcmp(x,y) - the standard library function
  

It is recommended to use these particular functions if they fit your needs, since st will recognize certain of them and run more quickly because of it.]

SideEffects [None]

SeeAlso [st_init_table_with_params st_free_table]

Definition at line 163 of file st.c.

00164 {
00165     return st_init_table_with_params(compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
00166                                      ST_DEFAULT_MAX_DENSITY,
00167                                      ST_DEFAULT_GROW_FACTOR,
00168                                      ST_DEFAULT_REORDER_FLAG);
00169 
00170 } /* st_init_table */

st_table* st_init_table_with_params ( ST_PFICPCP  compare,
ST_PFICPI  hash,
int  size,
int  density,
double  grow_factor,
int  reorder_flag 
)

Function********************************************************************

Synopsis [Create a table with given parameters.]

Description [The full blown table initializer. compare and hash are the same as in st_init_table. density is the largest the average number of entries per hash bin there should be before the table is grown. grow_factor is the factor the table is grown by when it becomes too full. size is the initial number of bins to be allocated for the hash table. If reorder_flag is non-zero, then every time an entry is found, it is moved to the top of the chain.

st_init_table(compare, hash) is equivelent to

  st_init_table_with_params(compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
			    ST_DEFAULT_MAX_DENSITY,
			    ST_DEFAULT_GROW_FACTOR,
			    ST_DEFAULT_REORDER_FLAG);
  

]

SideEffects [None]

SeeAlso [st_init_table st_free_table]

Definition at line 199 of file st.c.

00206 {
00207     int i;
00208     st_table *newt;
00209 
00210     newt = ALLOC(st_table, 1);
00211     if (newt == NIL(st_table)) {
00212         return NIL(st_table);
00213     }
00214     newt->compare = compare;
00215     newt->hash = hash;
00216     newt->num_entries = 0;
00217     newt->max_density = density;
00218     newt->grow_factor = grow_factor;
00219     newt->reorder_flag = reorder_flag;
00220     if (size <= 0) {
00221         size = 1;
00222     }
00223     newt->num_bins = size;
00224     newt->bins = ALLOC(st_table_entry *, size);
00225     if (newt->bins == NIL(st_table_entry *)) {
00226         FREE(newt);
00227         return NIL(st_table);
00228     }
00229     for(i = 0; i < size; i++) {
00230         newt->bins[i] = 0;
00231     }
00232     return newt;
00233 
00234 } /* st_init_table_with_params */

int st_insert ( st_table table,
void *  key,
void *  value 
)

Function********************************************************************

Synopsis [Insert value in table under the key 'key'.]

Description [Insert value in table under the key 'key'. Returns 1 if there was an entry already under the key; 0 if there was no entry under the key and insertion was successful; ST_OUT_OF_MEM otherwise. In either of the first two cases the new value is added.]

SideEffects [None]

SeeAlso []

Definition at line 358 of file st.c.

00359 {
00360     int hash_val;
00361     st_table_entry *newt;
00362     st_table_entry *ptr, **last;
00363 
00364     hash_val = do_hash(key, table);
00365 
00366     FIND_ENTRY(table, hash_val, key, ptr, last);
00367 
00368     if (ptr == NIL(st_table_entry)) {
00369         if (table->num_entries/table->num_bins >= table->max_density) {
00370             if (rehash(table) == ST_OUT_OF_MEM) {
00371                 return ST_OUT_OF_MEM;
00372             }
00373             hash_val = do_hash(key, table);
00374         }
00375         newt = ALLOC(st_table_entry, 1);
00376         if (newt == NIL(st_table_entry)) {
00377             return ST_OUT_OF_MEM;
00378         }
00379         newt->key = (char *)key;
00380         newt->record = (char *)value;
00381         newt->next = table->bins[hash_val];
00382         table->bins[hash_val] = newt;
00383         table->num_entries++;
00384         return 0;
00385     } else {
00386         ptr->record = (char *)value;
00387         return 1;
00388     }
00389 
00390 } /* st_insert */

int st_lookup ( st_table table,
void *  key,
void *  value 
)

Function********************************************************************

Synopsis [Lookup up `key' in `table'.]

Description [Lookup up `key' in `table'. If an entry is found, 1 is returned and if `value' is not nil, the variable it points to is set to the associated value. If an entry is not found, 0 is returned and the variable pointed by value is unchanged.]

SideEffects [None]

SeeAlso [st_lookup_int]

Definition at line 286 of file st.c.

00287 {
00288     int hash_val;
00289     st_table_entry *ptr, **last;
00290 
00291     hash_val = do_hash(key, table);
00292 
00293     FIND_ENTRY(table, hash_val, key, ptr, last);
00294 
00295     if (ptr == NIL(st_table_entry)) {
00296         return 0;
00297     } else {
00298         if (value != NIL(void)) {
00299             *(char **)value = ptr->record;
00300         }
00301         return 1;
00302     }
00303 
00304 } /* st_lookup */

int st_lookup_int ( st_table table,
void *  key,
int *  value 
)

Function********************************************************************

Synopsis [Lookup up `key' in `table'.]

Description [Lookup up `key' in `table'. If an entry is found, 1 is returned and if `value' is not nil, the variable it points to is set to the associated integer value. If an entry is not found, 0 is return and the variable pointed by `value' is unchanged.]

SideEffects [None]

SeeAlso [st_lookup]

Definition at line 322 of file st.c.

00323 {
00324     int hash_val;
00325     st_table_entry *ptr, **last;
00326 
00327     hash_val = do_hash(key, table);
00328 
00329     FIND_ENTRY(table, hash_val, key, ptr, last);
00330     
00331     if (ptr == NIL(st_table_entry)) {
00332         return 0;
00333     } else {
00334         if (value != NIL(int)) {
00335             *value = (int) (long) ptr->record;
00336         }
00337         return 1;
00338     }
00339 
00340 } /* st_lookup_int */

int st_numcmp ( const char *  x,
const char *  y 
)

Function********************************************************************

Synopsis [Number comparison function.]

Description [integer number comparison function.]

SideEffects [None]

SeeAlso [st_init_table st_numhash]

Definition at line 830 of file st.c.

00831 {
00832     return ST_NUMCMP(x, y);
00833 
00834 } /* st_numcmp */

int st_numhash ( char *  x,
int  size 
)

Function********************************************************************

Synopsis [Number hash function.]

Description [Integer number hash function.]

SideEffects [None]

SeeAlso [st_init_table st_numcmp]

Definition at line 792 of file st.c.

00793 {
00794     return ST_NUMHASH(x, size);
00795 
00796 } /* st_numhash */

int st_ptrcmp ( const char *  x,
const char *  y 
)

Function********************************************************************

Synopsis [Pointer comparison function.]

Description [Pointer comparison function.]

SideEffects [None]

SeeAlso [st_init_table st_ptrhash]

Definition at line 849 of file st.c.

00850 {
00851     return ST_NUMCMP(x, y);
00852 
00853 } /* st_ptrcmp */

int st_ptrhash ( char *  x,
int  size 
)

Function********************************************************************

Synopsis [Pointer hash function.]

Description [Pointer hash function.]

SideEffects [None]

SeeAlso [st_init_table st_ptrcmp]

Definition at line 811 of file st.c.

00812 {
00813     return ST_PTRHASH(x, size);
00814 
00815 } /* st_ptrhash */

int st_strhash ( char *  string,
int  modulus 
)

Function********************************************************************

Synopsis [String hash function.]

Description [String hash function.]

SideEffects [None]

SeeAlso [st_init_table]

Definition at line 766 of file st.c.

00767 {
00768     int val = 0;
00769     int c;
00770     
00771     while ((c = *string++) != '\0') {
00772         val = val*997 + c;
00773     }
00774 
00775     return ((val < 0) ? -val : val)%modulus;
00776 
00777 } /* st_strhash */


Variable Documentation

char rcsid [] UNUSED = " $Id: st.c,v 1.8 2005/04/13 05:02:20 fabio Exp $" [static]

CFile***********************************************************************

FileName [st.c]

PackageName [st]

Synopsis [Symbol table package.]

Description [The st library provides functions to create, maintain, and query symbol tables.]

SeeAlso []

Author []

Copyright []

Definition at line 40 of file st.c.


Generated on Tue Jan 12 13:57:28 2010 for glu-2.2 by  doxygen 1.6.1