40 static char rcsid[]
UTIL_UNUSED =
" $Id: st.c,v 1.12 2010/04/22 19:00:55 fabio Exp fabio $";
47 #define ST_NUMCMP(x,y) ((x) != (y)) 49 #define ST_NUMHASH(x,size) ((unsigned long)(x)%(size)) 51 #if SIZEOF_VOID_P == 8 57 #define ST_PTRHASH(x,size) ((unsigned int)((unsigned long)(x)>>st_shift)%size) 59 #define EQUAL(func, x, y) \ 60 ((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\ 61 (ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0)) 63 #define do_hash(key, table)\ 64 ((int)((table->hash == st_ptrhash) ? ST_PTRHASH((char *)(key),(table)->num_bins) :\ 65 (table->hash == st_numhash) ? ST_NUMHASH((char *)(key), (table)->num_bins) :\ 66 (*table->hash)((char *)(key), (table)->num_bins))) 68 #define PTR_NOT_EQUAL(table, ptr, user_key)\ 69 (ptr != NIL(st_table_entry) && !EQUAL(table->compare, (char *)user_key, (ptr)->key)) 71 #define FIND_ENTRY(table, hash_val, key, ptr, last) \ 72 (last) = &(table)->bins[hash_val];\ 74 while (PTR_NOT_EQUAL((table), (ptr), (key))) {\ 75 (last) = &(ptr)->next; (ptr) = *(last);\ 77 if ((ptr) != NIL(st_table_entry) && (table)->reorder_flag) {\ 78 *(last) = (ptr)->next;\ 79 (ptr)->next = (table)->bins[hash_val];\ 80 (table)->bins[hash_val] = (ptr);\ 84 #define ADD_DIRECT(table, key, value, hash_val, newt)\ 86 if (table->num_entries/table->num_bins >= table->max_density) {\ 88 hash_val = do_hash(key,table);\ 91 newt = ALLOC(st_table_entry, 1);\ 93 newt->key = (char *)key;\ 94 newt->record = value;\ 95 newt->next = table->bins[hash_val];\ 96 table->bins[hash_val] = newt;\ 97 table->num_entries++;\ 229 for(i = 0; i < size; i++) {
257 for(i = 0; i < table->
num_bins ; i++) {
258 ptr = table->
bins[i];
291 hash_val =
do_hash(key, table);
298 if (value !=
NIL(
void)) {
299 *(
char **)value = ptr->
record;
327 hash_val =
do_hash(key, table);
334 if (value !=
NIL(
int)) {
335 *value = (int) (
long) ptr->
record;
364 hash_val =
do_hash(key, table);
373 hash_val =
do_hash(key, table);
379 newt->
key = (
char *)key;
380 newt->
record = (
char *)value;
382 table->
bins[hash_val] = newt;
386 ptr->
record = (
char *)value;
415 hash_val =
do_hash(key, table);
421 hash_val =
do_hash(key, table);
426 newt->
key = (
char *)key;
427 newt->
record = (
char *)value;
429 table->
bins[hash_val] = newt;
493 hash_val =
do_hash(key, table);
502 hash_val =
do_hash(key, table);
508 newt->
key = (
char *)key;
509 newt->
record = (
char *) 0;
511 table->
bins[hash_val] = newt;
513 if (slot !=
NIL(
void)) *(
char ***)slot = &newt->
record;
516 if (slot !=
NIL(
void)) *(
char ***)slot = &ptr->
record;
541 hash_val =
do_hash(key, table);
548 if (slot !=
NIL(
void)) {
549 *(
char ***)slot = &ptr->
record;
575 int i, j, num_bins = old_table->
num_bins;
582 *new_table = *old_table;
588 for(i = 0; i < num_bins ; i++) {
590 ptr = old_table->
bins[i];
594 for (j = 0; j <= i; j++) {
595 newptr = new_table->
bins[j];
608 new_table->
bins[i] = newt;
637 char *key = *(
char **)keyp;
640 hash_val =
do_hash(key, table);
649 if (value !=
NIL(
void)) *(
char **)value = ptr->
record;
650 *(
char **)keyp = ptr->
key;
678 char *key = *(
char **)keyp;
681 hash_val =
do_hash(key, table);
690 if (value !=
NIL(
int)) *value = (int) (
long) ptr->
record;
691 *(
char **)keyp = ptr->
key;
731 for(i = 0; i < table->
num_bins; i++) {
732 last = &table->
bins[i]; ptr = *last;
734 retval = (*func)(ptr->
key, ptr->
record, arg);
737 last = &ptr->
next; ptr = *last;
771 while ((c = *
string++) !=
'\0') {
775 return ((val < 0) ? -val : val)%modulus;
914 for(i = gen->
index; i < gen->table->num_bins; i++) {
926 if (value_p !=
NIL(
void)) {
959 for(i = gen->
index; i < gen->table->num_bins; i++) {
971 if (value_p !=
NIL(
int)) {
1024 int i, old_num_bins, hash_val, old_num_entries;
1027 old_bins = table->
bins;
1039 table->
bins = old_bins;
1045 for (i = 0; i < table->
num_bins; i++) {
1050 for (i = 0; i < old_num_bins; i++) {
1056 table->
bins[hash_val] = ptr;
#define ST_DEFAULT_REORDER_FLAG
int st_delete_int(st_table *table, void *keyp, int *value)
st_table * st_init_table_with_params(ST_PFICPCP compare, ST_PFICPI hash, int size, int density, double grow_factor, int reorder_flag)
int st_ptrhash(char *x, int size)
#define FIND_ENTRY(table, hash_val, key, ptr, last)
#define ST_DEFAULT_MAX_DENSITY
#define ST_DEFAULT_GROW_FACTOR
int st_strhash(char *string, int modulus)
int(* ST_PFICPI)(char *, int)
enum st_retval(* ST_PFSR)(char *, char *, char *)
int st_lookup_int(st_table *table, void *key, int *value)
int st_ptrcmp(const char *x, const char *y)
static char rcsid [] UTIL_UNUSED
int st_gen_int(st_generator *gen, void *key_p, int *value_p)
st_generator * st_init_gen(st_table *table)
int(* compare)(const char *, const char *)
int st_find_or_add(st_table *table, void *key, void *slot)
int st_gen(st_generator *gen, void *key_p, void *value_p)
int st_lookup(st_table *table, void *key, void *value)
int(* ST_PFICPCP)(const char *, const char *)
int st_insert(st_table *table, void *key, void *value)
st_table * st_init_table(ST_PFICPCP compare, ST_PFICPI hash)
st_table * st_copy(st_table *old_table)
#define do_hash(key, table)
void st_free_gen(st_generator *gen)
#define ST_PTRHASH(x, size)
int st_numcmp(const char *x, const char *y)
int st_delete(st_table *table, void *keyp, void *value)
#define ST_NUMHASH(x, size)
int st_numhash(char *x, int size)
void st_free_table(st_table *table)
#define ST_DEFAULT_INIT_TABLE_SIZE
int st_foreach(st_table *table, ST_PFSR func, char *arg)
static int rehash(st_table *)
int st_add_direct(st_table *table, void *key, void *value)
int st_find(st_table *table, void *key, void *slot)