2000-01-05 07:41:21 +03:00
|
|
|
/* This is a public domain general purpose hash table package written by Peter Moore @ UCB. */
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible";
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "st.h"
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NT
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
typedef struct st_table_entry st_table_entry;
|
|
|
|
|
|
|
|
struct st_table_entry {
|
|
|
|
unsigned int hash;
|
|
|
|
char *key;
|
|
|
|
char *record;
|
|
|
|
st_table_entry *next;
|
|
|
|
};
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#define ST_DEFAULT_MAX_DENSITY 5
|
|
|
|
#define ST_DEFAULT_INIT_TABLE_SIZE 11
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DEFAULT_MAX_DENSITY is the default for the largest we allow the
|
|
|
|
* average number of items per bin before increasing the number of
|
|
|
|
* bins
|
|
|
|
*
|
|
|
|
* DEFAULT_INIT_TABLE_SIZE is the default for the number of bins
|
|
|
|
* allocated initially
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int numcmp();
|
|
|
|
static int numhash();
|
|
|
|
static struct st_hash_type type_numhash = {
|
|
|
|
numcmp,
|
|
|
|
numhash,
|
|
|
|
};
|
|
|
|
|
|
|
|
extern int strcmp();
|
|
|
|
static int strhash();
|
|
|
|
static struct st_hash_type type_strhash = {
|
|
|
|
strcmp,
|
|
|
|
strhash,
|
|
|
|
};
|
|
|
|
|
2000-05-16 06:46:57 +04:00
|
|
|
#ifdef RUBY_PLATFORM
|
|
|
|
#define xmalloc ruby_xmalloc
|
|
|
|
#define xcalloc ruby_xcalloc
|
|
|
|
#define xrealloc ruby_xrealloc
|
|
|
|
#define xfree ruby_xfree
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void *xmalloc();
|
|
|
|
void *xcalloc();
|
|
|
|
void *xrealloc();
|
2000-05-16 06:46:57 +04:00
|
|
|
void xfree();
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
2000-05-16 06:46:57 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void rehash();
|
|
|
|
|
|
|
|
#define alloc(type) (type*)xmalloc((unsigned)sizeof(type))
|
|
|
|
#define Calloc(n,s) (char*)xcalloc((n),(s))
|
|
|
|
|
|
|
|
#define EQUAL(table, x, y) ((*table->type->compare)(x, y) == 0)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define do_hash(key, table) (unsigned int)(*(table)->type->hash)((key))
|
2000-02-25 06:51:23 +03:00
|
|
|
#define do_hash_bin(key, table) (do_hash(key, table)&(table)->num_bins)
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MINSIZE is the minimum size of a dictionary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MINSIZE 8
|
|
|
|
|
|
|
|
/*
|
|
|
|
Table of prime numbers 2^n+a, 2<=n<=30.
|
|
|
|
*/
|
|
|
|
static long primes[] = {
|
|
|
|
8 + 3,
|
|
|
|
16 + 3,
|
|
|
|
32 + 5,
|
|
|
|
64 + 3,
|
|
|
|
128 + 3,
|
1999-10-13 10:44:42 +04:00
|
|
|
256 + 27,
|
|
|
|
512 + 9,
|
1999-01-20 07:59:39 +03:00
|
|
|
1024 + 9,
|
|
|
|
2048 + 5,
|
|
|
|
4096 + 83,
|
|
|
|
8192 + 27,
|
|
|
|
16384 + 43,
|
|
|
|
32768 + 3,
|
|
|
|
65536 + 45,
|
|
|
|
131072 + 9,
|
|
|
|
262144 + 39,
|
|
|
|
524288 + 39,
|
|
|
|
1048576 + 9,
|
|
|
|
2097152 + 5,
|
|
|
|
4194304 + 3,
|
|
|
|
8388608 + 33,
|
|
|
|
16777216 + 27,
|
|
|
|
33554432 + 9,
|
|
|
|
67108864 + 71,
|
|
|
|
134217728 + 39,
|
|
|
|
268435456 + 9,
|
|
|
|
536870912 + 5,
|
|
|
|
1073741824 + 83,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
new_size(size)
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
int i, newsize;
|
|
|
|
|
2000-02-23 08:44:18 +03:00
|
|
|
#if 1
|
2000-02-25 06:51:23 +03:00
|
|
|
for (i=3; i<31; i++) {
|
|
|
|
if ((1<<i) > size) return 1<<i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#else
|
1999-01-20 07:59:39 +03:00
|
|
|
for (i = 0, newsize = MINSIZE;
|
|
|
|
i < sizeof(primes)/sizeof(primes[0]);
|
|
|
|
i++, newsize <<= 1)
|
|
|
|
{
|
|
|
|
if (newsize > size) return primes[i];
|
|
|
|
}
|
|
|
|
/* Ran out of polynomials */
|
|
|
|
return -1; /* should raise exception */
|
2000-02-23 08:23:12 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
#ifdef HASH_LOG
|
2000-02-23 08:23:12 +03:00
|
|
|
static int collision = 0;
|
|
|
|
static int init_st = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
stat_col()
|
|
|
|
{
|
|
|
|
FILE *f = fopen("/tmp/col", "w");
|
|
|
|
fprintf(f, "collision: %d\n", collision);
|
|
|
|
fclose(f);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-02-25 06:51:23 +03:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
st_table*
|
|
|
|
st_init_table_with_size(type, size)
|
|
|
|
struct st_hash_type *type;
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
st_table *tbl;
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
#ifdef HASH_LOG
|
2000-02-23 08:23:12 +03:00
|
|
|
if (init_st == 0) {
|
|
|
|
init_st = 1;
|
|
|
|
atexit(stat_col);
|
|
|
|
}
|
2000-02-23 08:43:57 +03:00
|
|
|
#endif
|
2000-02-23 08:23:12 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
size = new_size(size); /* round up to prime number */
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
tbl = alloc(st_table);
|
|
|
|
tbl->type = type;
|
|
|
|
tbl->num_entries = 0;
|
2000-02-25 06:51:23 +03:00
|
|
|
tbl->num_bins = size-1;
|
1998-01-16 15:13:05 +03:00
|
|
|
tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return tbl;
|
|
|
|
}
|
|
|
|
|
|
|
|
st_table*
|
|
|
|
st_init_table(type)
|
|
|
|
struct st_hash_type *type;
|
|
|
|
{
|
|
|
|
return st_init_table_with_size(type, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
st_table*
|
|
|
|
st_init_numtable()
|
|
|
|
{
|
|
|
|
return st_init_table(&type_numhash);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
st_table*
|
|
|
|
st_init_numtable_with_size(size)
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
return st_init_table_with_size(&type_numhash, size);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
st_table*
|
|
|
|
st_init_strtable()
|
|
|
|
{
|
|
|
|
return st_init_table(&type_strhash);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
st_table*
|
|
|
|
st_init_strtable_with_size(size)
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
return st_init_table_with_size(&type_strhash, size);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
st_free_table(table)
|
|
|
|
st_table *table;
|
|
|
|
{
|
|
|
|
register st_table_entry *ptr, *next;
|
|
|
|
int i;
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
for(i = 0; i <= table->num_bins; i++) {
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = table->bins[i];
|
1999-01-20 07:59:39 +03:00
|
|
|
while (ptr != 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
next = ptr->next;
|
1999-01-20 07:59:39 +03:00
|
|
|
free(ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = next;
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
free(table->bins);
|
|
|
|
free(table);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
|
1999-08-13 09:45:20 +04:00
|
|
|
((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
#ifdef HASH_LOG
|
|
|
|
#define COLLISION collision++
|
|
|
|
#else
|
|
|
|
#define COLLISION
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) \
|
2000-02-25 06:51:23 +03:00
|
|
|
bin_pos = hash_val&(table)->num_bins;\
|
1999-01-20 07:59:39 +03:00
|
|
|
ptr = (table)->bins[bin_pos];\
|
|
|
|
if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\
|
2000-02-25 06:51:23 +03:00
|
|
|
COLLISION;\
|
1999-01-20 07:59:39 +03:00
|
|
|
while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = ptr->next;\
|
|
|
|
}\
|
|
|
|
ptr = ptr->next;\
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
st_lookup(table, key, value)
|
|
|
|
st_table *table;
|
|
|
|
register char *key;
|
|
|
|
char **value;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned int hash_val, bin_pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
register st_table_entry *ptr;
|
|
|
|
|
|
|
|
hash_val = do_hash(key, table);
|
1999-01-20 07:59:39 +03:00
|
|
|
FIND_ENTRY(table, ptr, hash_val, bin_pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ptr == 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (value != 0) *value = ptr->record;
|
1998-01-16 15:13:05 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
|
1998-01-16 15:13:05 +03:00
|
|
|
{\
|
1999-08-13 09:45:20 +04:00
|
|
|
st_table_entry *entry;\
|
2000-02-25 06:51:23 +03:00
|
|
|
if (table->num_entries/(table->num_bins+1) > ST_DEFAULT_MAX_DENSITY) {\
|
1998-01-16 15:13:05 +03:00
|
|
|
rehash(table);\
|
2000-02-25 06:51:23 +03:00
|
|
|
bin_pos = hash_val & table->num_bins;\
|
1998-01-16 15:13:05 +03:00
|
|
|
}\
|
|
|
|
\
|
1999-08-13 09:45:20 +04:00
|
|
|
entry = alloc(st_table_entry);\
|
1998-01-16 15:13:05 +03:00
|
|
|
\
|
1999-08-13 09:45:20 +04:00
|
|
|
entry->hash = hash_val;\
|
|
|
|
entry->key = key;\
|
|
|
|
entry->record = value;\
|
|
|
|
entry->next = table->bins[bin_pos];\
|
|
|
|
table->bins[bin_pos] = entry;\
|
1998-01-16 15:13:05 +03:00
|
|
|
table->num_entries++;\
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
st_insert(table, key, value)
|
|
|
|
register st_table *table;
|
|
|
|
register char *key;
|
|
|
|
char *value;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned int hash_val, bin_pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
register st_table_entry *ptr;
|
|
|
|
|
|
|
|
hash_val = do_hash(key, table);
|
1999-01-20 07:59:39 +03:00
|
|
|
FIND_ENTRY(table, ptr, hash_val, bin_pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ptr == 0) {
|
|
|
|
ADD_DIRECT(table, key, value, hash_val, bin_pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
ptr->record = value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
st_add_direct(table, key, value)
|
|
|
|
st_table *table;
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned int hash_val, bin_pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
hash_val = do_hash(key, table);
|
2000-02-25 06:51:23 +03:00
|
|
|
bin_pos = hash_val & table->num_bins;
|
1999-01-20 07:59:39 +03:00
|
|
|
ADD_DIRECT(table, key, value, hash_val, bin_pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rehash(table)
|
|
|
|
register st_table *table;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
register st_table_entry *ptr, *next, **new_bins;
|
|
|
|
int i, old_num_bins = table->num_bins, new_num_bins;
|
|
|
|
unsigned int hash_val;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
new_num_bins = new_size(old_num_bins+1);
|
1999-01-20 07:59:39 +03:00
|
|
|
new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
new_num_bins--;
|
|
|
|
for(i = 0; i <= old_num_bins; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
ptr = table->bins[i];
|
|
|
|
while (ptr != 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
next = ptr->next;
|
2000-02-25 06:51:23 +03:00
|
|
|
hash_val = ptr->hash & new_num_bins;
|
1999-01-20 07:59:39 +03:00
|
|
|
ptr->next = new_bins[hash_val];
|
|
|
|
new_bins[hash_val] = ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = next;
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
free(table->bins);
|
|
|
|
table->num_bins = new_num_bins;
|
|
|
|
table->bins = new_bins;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
st_table*
|
|
|
|
st_copy(old_table)
|
|
|
|
st_table *old_table;
|
|
|
|
{
|
|
|
|
st_table *new_table;
|
1999-08-13 09:45:20 +04:00
|
|
|
st_table_entry *ptr, *entry;
|
2000-02-25 06:51:23 +03:00
|
|
|
int i, num_bins = old_table->num_bins+1;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
new_table = alloc(st_table);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (new_table == 0) {
|
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*new_table = *old_table;
|
|
|
|
new_table->bins = (st_table_entry**)
|
|
|
|
Calloc((unsigned)num_bins, sizeof(st_table_entry*));
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (new_table->bins == 0) {
|
|
|
|
free(new_table);
|
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
for(i = 0; i < num_bins; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
new_table->bins[i] = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = old_table->bins[i];
|
1999-01-20 07:59:39 +03:00
|
|
|
while (ptr != 0) {
|
1999-08-13 09:45:20 +04:00
|
|
|
entry = alloc(st_table_entry);
|
|
|
|
if (entry == 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
free(new_table->bins);
|
|
|
|
free(new_table);
|
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
*entry = *ptr;
|
|
|
|
entry->next = new_table->bins[i];
|
|
|
|
new_table->bins[i] = entry;
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
st_delete(table, key, value)
|
|
|
|
register st_table *table;
|
|
|
|
register char **key;
|
|
|
|
char **value;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned int hash_val;
|
1998-01-16 15:13:05 +03:00
|
|
|
st_table_entry *tmp;
|
|
|
|
register st_table_entry *ptr;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
hash_val = do_hash_bin(*key, table);
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = table->bins[hash_val];
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ptr == 0) {
|
|
|
|
if (value != 0) *value = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EQUAL(table, *key, ptr->key)) {
|
|
|
|
table->bins[hash_val] = ptr->next;
|
|
|
|
table->num_entries--;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (value != 0) *value = ptr->record;
|
1998-01-16 15:13:05 +03:00
|
|
|
*key = ptr->key;
|
1999-01-20 07:59:39 +03:00
|
|
|
free(ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
for(; ptr->next != 0; ptr = ptr->next) {
|
1998-01-16 15:13:05 +03:00
|
|
|
if (EQUAL(table, ptr->next->key, *key)) {
|
|
|
|
tmp = ptr->next;
|
|
|
|
ptr->next = ptr->next->next;
|
|
|
|
table->num_entries--;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (value != 0) *value = tmp->record;
|
1998-01-16 15:13:05 +03:00
|
|
|
*key = tmp->key;
|
1999-01-20 07:59:39 +03:00
|
|
|
free(tmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
st_delete_safe(table, key, value, never)
|
|
|
|
register st_table *table;
|
|
|
|
register char **key;
|
|
|
|
char **value;
|
|
|
|
char *never;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned int hash_val;
|
1998-01-16 15:13:05 +03:00
|
|
|
register st_table_entry *ptr;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
hash_val = do_hash_bin(*key, table);
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr = table->bins[hash_val];
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ptr == 0) {
|
|
|
|
if (value != 0) *value = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
for(; ptr != 0; ptr = ptr->next) {
|
2000-03-23 11:37:35 +03:00
|
|
|
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
table->num_entries--;
|
|
|
|
*key = ptr->key;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (value != 0) *value = ptr->record;
|
1998-01-16 15:13:05 +03:00
|
|
|
ptr->key = ptr->record = never;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static int
|
|
|
|
delete_never(key, value, never)
|
|
|
|
char *key, *value, *never;
|
|
|
|
{
|
|
|
|
if (value == never) return ST_DELETE;
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
st_cleanup_safe(table, never)
|
|
|
|
st_table *table;
|
|
|
|
char *never;
|
|
|
|
{
|
|
|
|
int num_entries = table->num_entries;
|
|
|
|
|
|
|
|
st_foreach(table, delete_never, never);
|
|
|
|
table->num_entries = num_entries;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
st_foreach(table, func, arg)
|
|
|
|
st_table *table;
|
|
|
|
enum st_retval (*func)();
|
|
|
|
char *arg;
|
|
|
|
{
|
|
|
|
st_table_entry *ptr, *last, *tmp;
|
|
|
|
enum st_retval retval;
|
|
|
|
int i;
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
for(i = 0; i <= table->num_bins; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
last = 0;
|
|
|
|
for(ptr = table->bins[i]; ptr != 0;) {
|
1998-01-16 15:13:05 +03:00
|
|
|
retval = (*func)(ptr->key, ptr->record, arg);
|
|
|
|
switch (retval) {
|
|
|
|
case ST_CONTINUE:
|
|
|
|
last = ptr;
|
|
|
|
ptr = ptr->next;
|
|
|
|
break;
|
|
|
|
case ST_STOP:
|
|
|
|
return;
|
|
|
|
case ST_DELETE:
|
|
|
|
tmp = ptr;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (last == 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
table->bins[i] = ptr->next;
|
|
|
|
} else {
|
|
|
|
last->next = ptr->next;
|
|
|
|
}
|
|
|
|
ptr = ptr->next;
|
1999-01-20 07:59:39 +03:00
|
|
|
free(tmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
table->num_entries--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-20 07:59:39 +03:00
|
|
|
strhash(string)
|
1998-01-16 15:13:05 +03:00
|
|
|
register char *string;
|
|
|
|
{
|
|
|
|
register int c;
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
#ifdef HASH_ELFHASH
|
|
|
|
register unsigned int h = 0, g;
|
|
|
|
|
|
|
|
while ((c = *string++) != '\0') {
|
|
|
|
h = ( h << 4 ) + c;
|
|
|
|
if ( g = h & 0xF0000000 )
|
|
|
|
h ^= g >> 24;
|
|
|
|
h &= ~g;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
#elif HASH_PERL
|
|
|
|
register int val = 0;
|
|
|
|
|
|
|
|
while ((c = *string++) != '\0') {
|
|
|
|
val = val*33 + c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val + (val>>5);
|
|
|
|
#else
|
|
|
|
register int val = 0;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while ((c = *string++) != '\0') {
|
|
|
|
val = val*997 + c;
|
|
|
|
}
|
|
|
|
|
2000-02-25 06:51:23 +03:00
|
|
|
return val + (val>>5);
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
numcmp(x, y)
|
|
|
|
int x, y;
|
|
|
|
{
|
|
|
|
return x != y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-20 07:59:39 +03:00
|
|
|
numhash(n)
|
1998-01-16 15:13:05 +03:00
|
|
|
int n;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return n;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|