the new_hash_table code has a larger number of problems

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2108 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
beveloper
2002-11-28 17:34:55 +00:00
parent c78d05895c
commit 0010bfc64f
+26 -8
View File
@@ -232,6 +232,9 @@ hash_hash_str( const char *str )
return hash; return hash;
} }
#define MAX_INITIAL 15; #define MAX_INITIAL 15;
/* /*
@@ -331,28 +334,36 @@ find_entry(new_hash_table *nh, const void *key, ssize_t klen, const void *val)
int hash = 0; int hash = 0;
ssize_t i; ssize_t i;
if (!nh) ASSERT(nh != NULL);
return NULL;
for (p = key, i = klen; i; i--, p++) for (p = key, i = klen; i; i--, p++)
hash = hash * 33 + *p; hash = hash * 33 + *p;
for (hep = &nh->array[hash & nh->max], he = *hep; he; hep = &he->next, he = *hep) { for (hep = &nh->array[hash & nh->max], he = *hep; he; hep = &he->next, he = *hep) {
// dprintf("khash, find_entry looking at hep %p, he %p\n", hep, he); // dprintf("khash, find_entry looking at hep %p, he %p\n", hep, he);
if (he->hash == hash && he->klen == klen if (he->hash == hash && he->klen == klen && memcmp(he->key, key, klen) == 0) {
&& memcmp(he->key, key, klen) == 0) { break;
break;
} }
} }
if (he || !val) if (val == 0) /* val == 0 means we only lookup, and don't insert */
return hep; return he ? hep : NULL;
/* add a new linked-list entry */ /* add a new linked-list entry */
he = (hash_entry *)pool_get(nh->pool); he = (hash_entry *)pool_get(nh->pool);
if (he == NULL)
return NULL;
/* copy the key */
he->key = malloc(klen);
if (he->key == NULL) {
pool_put(nh->pool, he);
return NULL;
}
memcpy(he->key, key, klen);
he->next = NULL; he->next = NULL;
he->hash = hash; he->hash = hash;
he->key = key;
he->klen = klen; he->klen = klen;
he->val = val; he->val = val;
*hep = he; *hep = he;
@@ -366,6 +377,8 @@ hash_get(new_hash_table *nh, const void *key, ssize_t klen)
{ {
hash_entry **hepp; hash_entry **hepp;
hash_entry *hep; hash_entry *hep;
ASSERT(nh != NULL);
hepp = find_entry(nh, key, klen, NULL); hepp = find_entry(nh, key, klen, NULL);
// dprintf("khash, find_entry returned %p\n", hepp); // dprintf("khash, find_entry returned %p\n", hepp);
@@ -385,7 +398,11 @@ hash_set(new_hash_table *nh, const void *key, ssize_t klen, const void *val)
{ {
hash_entry **hep; hash_entry **hep;
hash_entry *old; hash_entry *old;
ASSERT(nh != NULL);
hep = find_entry(nh, key, klen, val); hep = find_entry(nh, key, klen, val);
ASSERT(hep != NULL);
if (*hep) { if (*hep) {
if (!val) { if (!val) {
@@ -401,4 +418,5 @@ hash_set(new_hash_table *nh, const void *key, ssize_t klen, const void *val)
expand_array(nh); expand_array(nh);
} }
} }
else ASSERT(false);
} }