From 995dfd20e78ad16cec678df25422ce032650e3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 23 Jul 2017 10:18:42 +0200 Subject: New upstream version 6.4.0 --- src/st.c | 484 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 241 insertions(+), 243 deletions(-) (limited to 'src/st.c') diff --git a/src/st.c b/src/st.c index d4fe867..e5fd1a1 100644 --- a/src/st.c +++ b/src/st.c @@ -108,17 +108,16 @@ new_size(size) #if 0 for (i=3; i<31; i++) { - if ((1< size) return 1< size) return 1< size) return primes[i]; + i < (int )(sizeof(primes)/sizeof(primes[0])); + i++, newsize <<= 1) { + if (newsize > size) return primes[i]; } /* Ran out of polynomials */ return -1; /* should raise exception */ @@ -145,82 +144,82 @@ st_init_table_with_size(type, size) struct st_hash_type *type; int size; { - st_table *tbl; + st_table *tbl; #ifdef HASH_LOG - if (init_st == 0) { - init_st = 1; - atexit(stat_col); - } + if (init_st == 0) { + init_st = 1; + atexit(stat_col); + } #endif - size = new_size(size); /* round up to prime number */ + size = new_size(size); /* round up to prime number */ - tbl = alloc(st_table); - if (tbl == 0) return 0; + tbl = alloc(st_table); + if (tbl == 0) return 0; - tbl->type = type; - tbl->num_entries = 0; - tbl->num_bins = size; - tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*)); - if (tbl->bins == 0) { - free(tbl); - return 0; - } + tbl->type = type; + tbl->num_entries = 0; + tbl->num_bins = size; + tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*)); + if (tbl->bins == 0) { + free(tbl); + return 0; + } - return tbl; + return tbl; } st_table* st_init_table(type) struct st_hash_type *type; { - return st_init_table_with_size(type, 0); + return st_init_table_with_size(type, 0); } st_table* st_init_numtable(void) { - return st_init_table(&type_numhash); + return st_init_table(&type_numhash); } st_table* st_init_numtable_with_size(size) int size; { - return st_init_table_with_size(&type_numhash, size); + return st_init_table_with_size(&type_numhash, size); } st_table* st_init_strtable(void) { - return st_init_table(&type_strhash); + return st_init_table(&type_strhash); } st_table* st_init_strtable_with_size(size) int size; { - return st_init_table_with_size(&type_strhash, size); + return st_init_table_with_size(&type_strhash, size); } void st_free_table(table) st_table *table; { - register st_table_entry *ptr, *next; - int i; + register st_table_entry *ptr, *next; + int i; - for(i = 0; i < table->num_bins; i++) { - ptr = table->bins[i]; - while (ptr != 0) { + for(i = 0; i < table->num_bins; i++) { + ptr = table->bins[i]; + while (ptr != 0) { next = ptr->next; free(ptr); ptr = next; - } } - free(table->bins); - free(table); + } + free(table->bins); + free(table); } #define PTR_NOT_EQUAL(table, ptr, hash_val, key) \ @@ -236,187 +235,186 @@ st_free_table(table) bin_pos = hash_val%(table)->num_bins;\ ptr = (table)->bins[bin_pos];\ if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\ - COLLISION;\ - while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\ - ptr = ptr->next;\ - }\ - ptr = ptr->next;\ + COLLISION;\ + while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\ + ptr = ptr->next;\ + }\ + ptr = ptr->next;\ }\ } while (0) int st_lookup(table, key, value) - st_table *table; - register st_data_t key; - st_data_t *value; + st_table *table; + register st_data_t key; + st_data_t *value; { - unsigned int hash_val, bin_pos; - register st_table_entry *ptr; + unsigned int hash_val, bin_pos; + register st_table_entry *ptr; - hash_val = do_hash(key, table); - FIND_ENTRY(table, ptr, hash_val, bin_pos); + hash_val = do_hash(key, table); + FIND_ENTRY(table, ptr, hash_val, bin_pos); - if (ptr == 0) { - return 0; - } - else { - if (value != 0) *value = ptr->record; - return 1; - } + if (ptr == 0) { + return 0; + } + else { + if (value != 0) *value = ptr->record; + return 1; + } } -#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\ +#define ADD_DIRECT(table, key, value, hash_val, bin_pos, ret) \ do {\ - st_table_entry *entry;\ - if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\ - rehash(table);\ - bin_pos = hash_val % table->num_bins;\ - }\ - \ - entry = alloc(st_table_entry);\ - \ - entry->hash = hash_val;\ - entry->key = key;\ - entry->record = value;\ - entry->next = table->bins[bin_pos];\ - table->bins[bin_pos] = entry;\ - table->num_entries++;\ + st_table_entry *entry;\ + if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\ + rehash(table);\ + bin_pos = hash_val % table->num_bins;\ + }\ + entry = alloc(st_table_entry);\ + if (IS_NULL(entry)) return ret;\ + entry->hash = hash_val;\ + entry->key = key;\ + entry->record = value;\ + entry->next = table->bins[bin_pos];\ + table->bins[bin_pos] = entry;\ + table->num_entries++;\ } while (0) int st_insert(table, key, value) - register st_table *table; - register st_data_t key; - st_data_t value; + register st_table *table; + register st_data_t key; + st_data_t value; { - unsigned int hash_val, bin_pos; - register st_table_entry *ptr; + unsigned int hash_val, bin_pos; + register st_table_entry *ptr; - hash_val = do_hash(key, table); - FIND_ENTRY(table, ptr, hash_val, bin_pos); + hash_val = do_hash(key, table); + FIND_ENTRY(table, ptr, hash_val, bin_pos); - if (ptr == 0) { - ADD_DIRECT(table, key, value, hash_val, bin_pos); - return 0; - } - else { - ptr->record = value; - return 1; - } + if (ptr == 0) { + ADD_DIRECT(table, key, value, hash_val, bin_pos, ONIGERR_MEMORY); + return 0; + } + else { + ptr->record = value; + return 1; + } } void st_add_direct(table, key, value) - st_table *table; - st_data_t key; - st_data_t value; + st_table *table; + st_data_t key; + st_data_t value; { - unsigned int hash_val, bin_pos; + unsigned int hash_val, bin_pos; - hash_val = do_hash(key, table); - bin_pos = hash_val % table->num_bins; - ADD_DIRECT(table, key, value, hash_val, bin_pos); + hash_val = do_hash(key, table); + bin_pos = hash_val % table->num_bins; + ADD_DIRECT(table, key, value, hash_val, bin_pos,); } static void rehash(table) - register st_table *table; + register st_table *table; { - register st_table_entry *ptr, *next, **new_bins; - int i, old_num_bins = table->num_bins, new_num_bins; - unsigned int hash_val; - - new_num_bins = new_size(old_num_bins+1); - new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*)); - if (new_bins == 0) { - return ; - } - - for(i = 0; i < old_num_bins; i++) { - ptr = table->bins[i]; - while (ptr != 0) { + register st_table_entry *ptr, *next, **new_bins; + int i, old_num_bins = table->num_bins, new_num_bins; + unsigned int hash_val; + + new_num_bins = new_size(old_num_bins+1); + new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*)); + if (new_bins == 0) { + return ; + } + + for(i = 0; i < old_num_bins; i++) { + ptr = table->bins[i]; + while (ptr != 0) { next = ptr->next; hash_val = ptr->hash % new_num_bins; ptr->next = new_bins[hash_val]; new_bins[hash_val] = ptr; ptr = next; - } } - free(table->bins); - table->num_bins = new_num_bins; - table->bins = new_bins; + } + free(table->bins); + table->num_bins = new_num_bins; + table->bins = new_bins; } st_table* st_copy(old_table) - st_table *old_table; + st_table *old_table; { - st_table *new_table; - st_table_entry *ptr, *entry; - int i, num_bins = old_table->num_bins; + st_table *new_table; + st_table_entry *ptr, *entry; + int i, num_bins = old_table->num_bins; - new_table = alloc(st_table); - if (new_table == 0) { - return 0; - } + new_table = alloc(st_table); + if (new_table == 0) { + return 0; + } - *new_table = *old_table; - new_table->bins = (st_table_entry**) - Calloc((unsigned)num_bins, sizeof(st_table_entry*)); + *new_table = *old_table; + new_table->bins = (st_table_entry**) + Calloc((unsigned)num_bins, sizeof(st_table_entry*)); - if (new_table->bins == 0) { - free(new_table); - return 0; - } + if (new_table->bins == 0) { + free(new_table); + return 0; + } - for(i = 0; i < num_bins; i++) { - new_table->bins[i] = 0; - ptr = old_table->bins[i]; - while (ptr != 0) { + for(i = 0; i < num_bins; i++) { + new_table->bins[i] = 0; + ptr = old_table->bins[i]; + while (ptr != 0) { entry = alloc(st_table_entry); if (entry == 0) { - free(new_table->bins); - free(new_table); - return 0; + free(new_table->bins); + free(new_table); + return 0; } *entry = *ptr; entry->next = new_table->bins[i]; new_table->bins[i] = entry; ptr = ptr->next; - } } - return new_table; + } + return new_table; } int st_delete(table, key, value) - register st_table *table; - register st_data_t *key; - st_data_t *value; + register st_table *table; + register st_data_t *key; + st_data_t *value; { - unsigned int hash_val; - st_table_entry *tmp; - register st_table_entry *ptr; + unsigned int hash_val; + st_table_entry *tmp; + register st_table_entry *ptr; - hash_val = do_hash_bin(*key, table); - ptr = table->bins[hash_val]; + hash_val = do_hash_bin(*key, table); + ptr = table->bins[hash_val]; - if (ptr == 0) { - if (value != 0) *value = 0; - return 0; - } - - if (EQUAL(table, *key, ptr->key)) { - table->bins[hash_val] = ptr->next; - table->num_entries--; - if (value != 0) *value = ptr->record; - *key = ptr->key; - free(ptr); - return 1; - } - - for(; ptr->next != 0; ptr = ptr->next) { - if (EQUAL(table, ptr->next->key, *key)) { + if (ptr == 0) { + if (value != 0) *value = 0; + return 0; + } + + if (EQUAL(table, *key, ptr->key)) { + table->bins[hash_val] = ptr->next; + table->num_entries--; + if (value != 0) *value = ptr->record; + *key = ptr->key; + free(ptr); + return 1; + } + + for(; ptr->next != 0; ptr = ptr->next) { + if (EQUAL(table, ptr->next->key, *key)) { tmp = ptr->next; ptr->next = ptr->next->next; table->num_entries--; @@ -424,41 +422,41 @@ st_delete(table, key, value) *key = tmp->key; free(tmp); return 1; - } } + } - return 0; + return 0; } int st_delete_safe(table, key, value, never) - register st_table *table; - register st_data_t *key; - st_data_t *value; - st_data_t never; + register st_table *table; + register st_data_t *key; + st_data_t *value; + st_data_t never; { - unsigned int hash_val; - register st_table_entry *ptr; + unsigned int hash_val; + register st_table_entry *ptr; - hash_val = do_hash_bin(*key, table); - ptr = table->bins[hash_val]; + hash_val = do_hash_bin(*key, table); + ptr = table->bins[hash_val]; - if (ptr == 0) { - if (value != 0) *value = 0; - return 0; - } + if (ptr == 0) { + if (value != 0) *value = 0; + return 0; + } - for(; ptr != 0; ptr = ptr->next) { - if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { + for(; ptr != 0; ptr = ptr->next) { + if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { table->num_entries--; *key = ptr->key; if (value != 0) *value = ptr->record; ptr->key = ptr->record = never; return 1; - } } + } - return 0; + return 0; } static int @@ -476,114 +474,114 @@ delete_never(key, value, never) void st_cleanup_safe(table, never) - st_table *table; - st_data_t never; + st_table *table; + st_data_t never; { - int num_entries = table->num_entries; + int num_entries = table->num_entries; - st_foreach(table, delete_never, never); - table->num_entries = num_entries; + st_foreach(table, delete_never, never); + table->num_entries = num_entries; } int st_foreach(table, func, arg) - st_table *table; - int (*func)(); - st_data_t arg; + st_table *table; + int (*func)(); + st_data_t arg; { - st_table_entry *ptr, *last, *tmp; - enum st_retval retval; - int i; + st_table_entry *ptr, *last, *tmp; + enum st_retval retval; + int i; - for(i = 0; i < table->num_bins; i++) { - last = 0; - for(ptr = table->bins[i]; ptr != 0;) { + for(i = 0; i < table->num_bins; i++) { + last = 0; + for(ptr = table->bins[i]; ptr != 0;) { retval = (*func)(ptr->key, ptr->record, arg); switch (retval) { case ST_CHECK: /* check if hash is modified during iteration */ - tmp = 0; - if (i < table->num_bins) { - for (tmp = table->bins[i]; tmp; tmp=tmp->next) { - if (tmp == ptr) break; - } - } - if (!tmp) { - /* call func with error notice */ - return 1; - } - /* fall through */ + tmp = 0; + if (i < table->num_bins) { + for (tmp = table->bins[i]; tmp; tmp=tmp->next) { + if (tmp == ptr) break; + } + } + if (!tmp) { + /* call func with error notice */ + return 1; + } + /* fall through */ case ST_CONTINUE: - last = ptr; - ptr = ptr->next; - break; + last = ptr; + ptr = ptr->next; + break; case ST_STOP: - return 0; + return 0; case ST_DELETE: - tmp = ptr; - if (last == 0) { - table->bins[i] = ptr->next; - } - else { - last->next = ptr->next; - } - ptr = ptr->next; - free(tmp); - table->num_entries--; + tmp = ptr; + if (last == 0) { + table->bins[i] = ptr->next; + } + else { + last->next = ptr->next; + } + ptr = ptr->next; + free(tmp); + table->num_entries--; } - } } - return 0; + } + return 0; } static int strhash(string) - register const char *string; + register const char *string; { - register int c; + register int c; #ifdef HASH_ELFHASH - register unsigned int h = 0, g; + register unsigned int h = 0, g; - while ((c = *string++) != '\0') { - h = ( h << 4 ) + c; - if ( g = h & 0xF0000000 ) + while ((c = *string++) != '\0') { + h = ( h << 4 ) + c; + if ( g = h & 0xF0000000 ) h ^= g >> 24; - h &= ~g; - } - return h; + h &= ~g; + } + return h; #elif HASH_PERL - register int val = 0; + register int val = 0; - while ((c = *string++) != '\0') { - val += c; - val += (val << 10); - val ^= (val >> 6); - } - val += (val << 3); - val ^= (val >> 11); + while ((c = *string++) != '\0') { + val += c; + val += (val << 10); + val ^= (val >> 6); + } + val += (val << 3); + val ^= (val >> 11); - return val + (val << 15); + return val + (val << 15); #else - register int val = 0; + register int val = 0; - while ((c = *string++) != '\0') { - val = val*997 + c; - } + while ((c = *string++) != '\0') { + val = val*997 + c; + } - return val + (val>>5); + return val + (val>>5); #endif } static int numcmp(x, y) - long x, y; + long x, y; { - return x != y; + return x != y; } static int numhash(n) - long n; + long n; { - return n; + return n; } -- cgit v1.2.3