2
* This is a public domain general purpose hash table package written by
7
* static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible";
10
#include "avro_private.h"
11
#include "avro/allocation.h"
18
typedef struct st_table_entry st_table_entry;
20
struct st_table_entry {
27
#define ST_DEFAULT_MAX_DENSITY 5
28
#define ST_DEFAULT_INIT_TABLE_SIZE 11
31
* DEFAULT_MAX_DENSITY is the default for the largest we allow the
32
* average number of items per bin before increasing the number of
35
* DEFAULT_INIT_TABLE_SIZE is the default for the number of bins
39
static int numcmp(long, long);
40
static int numhash(long);
41
static struct st_hash_type type_numhash = {
42
HASH_FUNCTION_CAST numcmp,
43
HASH_FUNCTION_CAST numhash
47
* extern int strcmp(const char *, const char *);
49
static int strhash(const char *);
50
static struct st_hash_type type_strhash = {
51
HASH_FUNCTION_CAST strcmp,
52
HASH_FUNCTION_CAST strhash
55
static void rehash(st_table *);
58
#define malloc xmalloc
59
#define calloc xcalloc
62
#define Calloc(n,s) (char*)avro_calloc((n),(s))
64
#define free_bins(tbl) \
65
avro_free(tbl->bins, tbl->num_bins * sizeof(st_table_entry *))
67
#define EQUAL(table,x,y) ((x)==(y) || (*table->type->compare)((x),(y)) == 0)
69
#define do_hash(key,table) (unsigned int)(*(table)->type->hash)((key))
70
#define do_hash_bin(key,table) (do_hash(key, table)%(table)->num_bins)
73
* MINSIZE is the minimum size of a dictionary.
79
* Table of prime numbers 2^n+a, 2<=n<=30.
81
static long primes[] = {
113
static int new_size(int size)
118
for (i = 3; i < 31; i++) {
126
for (i = 0, newsize = MINSIZE;
127
i < sizeof(primes) / sizeof(primes[0]); i++, newsize <<= 1) {
132
* Ran out of polynomials
134
return -1; /* should raise exception */
139
static int collision = 0;
140
static int init_st = 0;
142
static void stat_col()
144
FILE *f = fopen("/tmp/col", "w");
145
fprintf(f, "collision: %d\n", collision);
150
st_table *st_init_table_with_size(struct st_hash_type *type, int size)
161
size = new_size(size); /* round up to prime number */
163
tbl = (st_table *) avro_new(st_table);
165
tbl->num_entries = 0;
166
tbl->num_bins = size;
167
tbl->bins = (st_table_entry **) Calloc(size, sizeof(st_table_entry *));
172
st_table *st_init_table(struct st_hash_type *type)
174
return st_init_table_with_size(type, 0);
177
st_table *st_init_numtable(void)
179
return st_init_table(&type_numhash);
182
st_table *st_init_numtable_with_size(int size)
184
return st_init_table_with_size(&type_numhash, size);
187
st_table *st_init_strtable(void)
189
return st_init_table(&type_strhash);
192
st_table *st_init_strtable_with_size(int size)
194
return st_init_table_with_size(&type_strhash, size);
197
void st_free_table(st_table *table)
199
register st_table_entry *ptr, *next;
202
for (i = 0; i < table->num_bins; i++) {
203
ptr = table->bins[i];
206
avro_freet(st_table_entry, ptr);
211
avro_freet(st_table, table);
214
#define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
215
((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
218
#define COLLISION collision++
223
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) do {\
224
bin_pos = hash_val%(table)->num_bins;\
225
ptr = (table)->bins[bin_pos];\
226
if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\
228
while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\
235
int st_lookup(st_table *table, register st_data_t key, st_data_t *value)
237
unsigned int hash_val, bin_pos;
238
register st_table_entry *ptr;
240
hash_val = do_hash(key, table);
241
FIND_ENTRY(table, ptr, hash_val, bin_pos);
247
*value = ptr->record;
252
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
254
st_table_entry *entry;\
255
if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\
257
bin_pos = hash_val % table->num_bins;\
260
entry = (st_table_entry *) avro_new(st_table_entry);\
262
entry->hash = hash_val;\
264
entry->record = value;\
265
entry->next = table->bins[bin_pos];\
266
table->bins[bin_pos] = entry;\
267
table->num_entries++;\
270
int st_insert(register st_table *table, register st_data_t key, st_data_t value)
272
unsigned int hash_val, bin_pos;
273
register st_table_entry *ptr;
275
hash_val = do_hash(key, table);
276
FIND_ENTRY(table, ptr, hash_val, bin_pos);
279
ADD_DIRECT(table, key, value, hash_val, bin_pos);
287
void st_add_direct(st_table *table,st_data_t key,st_data_t value)
289
unsigned int hash_val, bin_pos;
291
hash_val = do_hash(key, table);
292
bin_pos = hash_val % table->num_bins;
293
ADD_DIRECT(table, key, value, hash_val, bin_pos);
296
static void rehash(register st_table *table)
298
register st_table_entry *ptr, *next, **new_bins;
299
int i, old_num_bins = table->num_bins, new_num_bins;
300
unsigned int hash_val;
302
new_num_bins = new_size(old_num_bins + 1);
304
(st_table_entry **) Calloc(new_num_bins, sizeof(st_table_entry *));
306
for (i = 0; i < old_num_bins; i++) {
307
ptr = table->bins[i];
310
hash_val = ptr->hash % new_num_bins;
311
ptr->next = new_bins[hash_val];
312
new_bins[hash_val] = ptr;
317
table->num_bins = new_num_bins;
318
table->bins = new_bins;
321
st_table *st_copy(st_table *old_table)
324
st_table_entry *ptr, *entry;
325
int i, num_bins = old_table->num_bins;
327
new_table = (st_table *) avro_new(st_table);
328
if (new_table == 0) {
332
*new_table = *old_table;
333
new_table->bins = (st_table_entry **)
334
Calloc((unsigned)num_bins, sizeof(st_table_entry *));
336
if (new_table->bins == 0) {
337
avro_freet(st_table, new_table);
341
for (i = 0; i < num_bins; i++) {
342
new_table->bins[i] = 0;
343
ptr = old_table->bins[i];
345
entry = (st_table_entry *) avro_new(st_table_entry);
347
free_bins(new_table);
348
avro_freet(st_table, new_table);
352
entry->next = new_table->bins[i];
353
new_table->bins[i] = entry;
360
int st_delete(register st_table *table,register st_data_t *key,st_data_t *value)
362
unsigned int hash_val;
364
register st_table_entry *ptr;
366
hash_val = do_hash_bin(*key, table);
367
ptr = table->bins[hash_val];
375
if (EQUAL(table, *key, ptr->key)) {
376
table->bins[hash_val] = ptr->next;
377
table->num_entries--;
379
*value = ptr->record;
381
avro_freet(st_table_entry, ptr);
385
for (; ptr->next != 0; ptr = ptr->next) {
386
if (EQUAL(table, ptr->next->key, *key)) {
388
ptr->next = ptr->next->next;
389
table->num_entries--;
391
*value = tmp->record;
393
avro_freet(st_table_entry, tmp);
401
int st_delete_safe(register st_table *table,register st_data_t *key,st_data_t *value,st_data_t never)
403
unsigned int hash_val;
404
register st_table_entry *ptr;
406
hash_val = do_hash_bin(*key, table);
407
ptr = table->bins[hash_val];
415
for (; ptr != 0; ptr = ptr->next) {
416
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
417
table->num_entries--;
420
*value = ptr->record;
421
ptr->key = ptr->record = never;
429
static int delete_never(st_data_t key, st_data_t value, st_data_t never)
438
void st_cleanup_safe(st_table *table,st_data_t never)
440
int num_entries = table->num_entries;
442
st_foreach(table, HASH_FUNCTION_CAST delete_never, never);
443
table->num_entries = num_entries;
446
int st_foreach(st_table *table,int (*func) (ANYARGS),st_data_t arg)
448
st_table_entry *ptr, *last, *tmp;
449
enum st_retval retval;
452
for (i = 0; i < table->num_bins; i++) {
454
for (ptr = table->bins[i]; ptr != 0;) {
455
retval = (enum st_retval) (*func) (ptr->key, ptr->record, arg);
457
case ST_CHECK: /* check if hash is modified during
460
if (i < table->num_bins) {
461
for (tmp = table->bins[i]; tmp;
469
* call func with error notice
485
table->bins[i] = ptr->next;
487
last->next = ptr->next;
490
avro_freet(st_table_entry, tmp);
491
table->num_entries--;
498
static int strhash(register const char *string)
503
register unsigned int h = 0, g;
505
while ((c = *string++) != '\0') {
507
if (g = h & 0xF0000000)
512
#elif defined(HASH_PERL)
513
register int val = 0;
515
while ((c = *string++) != '\0') {
523
return val + (val << 15);
525
register int val = 0;
527
while ((c = *string++) != '\0') {
531
return val + (val >> 5);
535
static int numcmp(long x, long y)
540
static int numhash(long n)