Some style cleanups in khash.c - I just realized that we have two different
hash implementations in that file... we should really remove one of them. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1747 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+88
-56
@@ -1,5 +1,5 @@
|
|||||||
/* Generic hash table. No real clue where hash_elem is defined...*/
|
/* Generic hash table
|
||||||
/*
|
**
|
||||||
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
||||||
** Distributed under the terms of the NewOS License.
|
** Distributed under the terms of the NewOS License.
|
||||||
*/
|
*/
|
||||||
@@ -10,17 +10,21 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <khash.h>
|
#include <khash.h>
|
||||||
|
|
||||||
|
// ToDo: this file apparently contains two different hash implementations
|
||||||
|
// get rid of one of them, and update the external code.
|
||||||
|
|
||||||
|
// ToDo: remove these
|
||||||
#define malloc kmalloc
|
#define malloc kmalloc
|
||||||
#define free kfree
|
#define free kfree
|
||||||
|
|
||||||
struct hash_table {
|
struct hash_table {
|
||||||
struct hash_elem **table;
|
struct hash_elem **table;
|
||||||
int next_ptr_offset;
|
int next_ptr_offset;
|
||||||
unsigned int table_size;
|
unsigned int table_size;
|
||||||
int num_elems;
|
int num_elems;
|
||||||
int flags;
|
int flags;
|
||||||
int (*compare_func)(void *e, const void *key);
|
int (*compare_func)(void *e, const void *key);
|
||||||
unsigned int (*hash_func)(void *e, const void *key, unsigned int range);
|
unsigned int (*hash_func)(void *e, const void *key, unsigned int range);
|
||||||
};
|
};
|
||||||
|
|
||||||
// XXX gross hack
|
// XXX gross hack
|
||||||
@@ -28,7 +32,9 @@ struct hash_table {
|
|||||||
#define NEXT(t, e) ((void *)(*(unsigned long *)NEXT_ADDR(t, e)))
|
#define NEXT(t, e) ((void *)(*(unsigned long *)NEXT_ADDR(t, e)))
|
||||||
#define PUT_IN_NEXT(t, e, val) (*(unsigned long *)NEXT_ADDR(t, e) = (long)(val))
|
#define PUT_IN_NEXT(t, e, val) (*(unsigned long *)NEXT_ADDR(t, e) = (long)(val))
|
||||||
|
|
||||||
void *hash_init(unsigned int table_size, int next_ptr_offset,
|
|
||||||
|
void *
|
||||||
|
hash_init(unsigned int table_size, int next_ptr_offset,
|
||||||
int compare_func(void *e, const void *key),
|
int compare_func(void *e, const void *key),
|
||||||
unsigned int hash_func(void *e, const void *key, unsigned int range))
|
unsigned int hash_func(void *e, const void *key, unsigned int range))
|
||||||
{
|
{
|
||||||
@@ -36,12 +42,11 @@ void *hash_init(unsigned int table_size, int next_ptr_offset,
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
t = (struct hash_table *)malloc(sizeof(struct hash_table));
|
t = (struct hash_table *)malloc(sizeof(struct hash_table));
|
||||||
if(t == NULL) {
|
if (t == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
t->table = (struct hash_elem **)malloc(sizeof(void *) * table_size);
|
t->table = (struct hash_elem **)malloc(sizeof(void *) * table_size);
|
||||||
for(i = 0; i<table_size; i++)
|
for (i = 0; i<table_size; i++)
|
||||||
t->table[i] = NULL;
|
t->table[i] = NULL;
|
||||||
t->table_size = table_size;
|
t->table_size = table_size;
|
||||||
t->next_ptr_offset = next_ptr_offset;
|
t->next_ptr_offset = next_ptr_offset;
|
||||||
@@ -56,7 +61,9 @@ void *hash_init(unsigned int table_size, int next_ptr_offset,
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hash_uninit(void *_hash_table)
|
|
||||||
|
int
|
||||||
|
hash_uninit(void *_hash_table)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
|
|
||||||
@@ -72,7 +79,9 @@ int hash_uninit(void *_hash_table)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hash_insert(void *_hash_table, void *e)
|
|
||||||
|
int
|
||||||
|
hash_insert(void *_hash_table, void *e)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
@@ -87,6 +96,7 @@ int hash_insert(void *_hash_table, void *e)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int hash_remove(void *_hash_table, void *e)
|
int hash_remove(void *_hash_table, void *e)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
@@ -95,9 +105,9 @@ int hash_remove(void *_hash_table, void *e)
|
|||||||
|
|
||||||
hash = t->hash_func(e, NULL, t->table_size);
|
hash = t->hash_func(e, NULL, t->table_size);
|
||||||
last_i = NULL;
|
last_i = NULL;
|
||||||
for(i = t->table[hash]; i != NULL; last_i = i, i = NEXT(t, i)) {
|
for (i = t->table[hash]; i != NULL; last_i = i, i = NEXT(t, i)) {
|
||||||
if(i == e) {
|
if (i == e) {
|
||||||
if(last_i != NULL)
|
if (last_i != NULL)
|
||||||
PUT_IN_NEXT(t, last_i, NEXT(t, i));
|
PUT_IN_NEXT(t, last_i, NEXT(t, i));
|
||||||
else
|
else
|
||||||
t->table[hash] = (struct hash_elem *)NEXT(t, i);
|
t->table[hash] = (struct hash_elem *)NEXT(t, i);
|
||||||
@@ -109,48 +119,52 @@ int hash_remove(void *_hash_table, void *e)
|
|||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hash_find(void *_hash_table, void *e)
|
|
||||||
|
void *
|
||||||
|
hash_find(void *_hash_table, void *e)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
void *i;
|
void *i;
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
|
|
||||||
hash = t->hash_func(e, NULL, t->table_size);
|
hash = t->hash_func(e, NULL, t->table_size);
|
||||||
for(i = t->table[hash]; i != NULL; i = NEXT(t, i)) {
|
for (i = t->table[hash]; i != NULL; i = NEXT(t, i)) {
|
||||||
if(i == e) {
|
if (i == e)
|
||||||
return i;
|
return i;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hash_lookup(void *_hash_table, const void *key)
|
|
||||||
|
void *
|
||||||
|
hash_lookup(void *_hash_table, const void *key)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
void *i;
|
void *i;
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
|
|
||||||
if(t->compare_func == NULL)
|
if (t->compare_func == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
hash = t->hash_func(NULL, key, t->table_size);
|
hash = t->hash_func(NULL, key, t->table_size);
|
||||||
for(i = t->table[hash]; i != NULL; i = NEXT(t, i)) {
|
for (i = t->table[hash]; i != NULL; i = NEXT(t, i)) {
|
||||||
if(t->compare_func(i, key) == 0) {
|
if (t->compare_func(i, key) == 0)
|
||||||
return i;
|
return i;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct hash_iterator *hash_open(void *_hash_table, struct hash_iterator *i)
|
|
||||||
|
struct hash_iterator *
|
||||||
|
hash_open(void *_hash_table, struct hash_iterator *i)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
|
|
||||||
if(i == NULL) {
|
if (i == NULL) {
|
||||||
i = (struct hash_iterator *)malloc(sizeof(struct hash_iterator));
|
i = (struct hash_iterator *)malloc(sizeof(struct hash_iterator));
|
||||||
if(i == NULL)
|
if (i == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,27 +173,33 @@ struct hash_iterator *hash_open(void *_hash_table, struct hash_iterator *i)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash_close(void *_hash_table, struct hash_iterator *i, bool free_iterator)
|
|
||||||
|
void
|
||||||
|
hash_close(void *_hash_table, struct hash_iterator *i, bool freeIterator)
|
||||||
{
|
{
|
||||||
if(free_iterator)
|
if (freeIterator)
|
||||||
free(i);
|
free(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash_rewind(void *_hash_table, struct hash_iterator *i)
|
|
||||||
|
void
|
||||||
|
hash_rewind(void *_hash_table, struct hash_iterator *i)
|
||||||
{
|
{
|
||||||
i->ptr = NULL;
|
i->ptr = NULL;
|
||||||
i->bucket = -1;
|
i->bucket = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hash_next(void *_hash_table, struct hash_iterator *i)
|
|
||||||
|
void *
|
||||||
|
hash_next(void *_hash_table, struct hash_iterator *i)
|
||||||
{
|
{
|
||||||
struct hash_table *t = (struct hash_table *)_hash_table;
|
struct hash_table *t = (struct hash_table *)_hash_table;
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
|
|
||||||
restart:
|
restart:
|
||||||
if(!i->ptr) {
|
if (!i->ptr) {
|
||||||
for(index = (unsigned int)(i->bucket + 1); index < t->table_size; index++) {
|
for (index = (unsigned int)(i->bucket + 1); index < t->table_size; index++) {
|
||||||
if(t->table[index]) {
|
if (t->table[index]) {
|
||||||
i->bucket = index;
|
i->bucket = index;
|
||||||
i->ptr = t->table[index];
|
i->ptr = t->table[index];
|
||||||
break;
|
break;
|
||||||
@@ -187,25 +207,27 @@ restart:
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i->ptr = NEXT(t, i->ptr);
|
i->ptr = NEXT(t, i->ptr);
|
||||||
if(!i->ptr)
|
if (!i->ptr)
|
||||||
goto restart;
|
goto restart;
|
||||||
}
|
}
|
||||||
|
|
||||||
return i->ptr;
|
return i->ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int hash_hash_str( const char *str )
|
|
||||||
|
unsigned int
|
||||||
|
hash_hash_str( const char *str )
|
||||||
{
|
{
|
||||||
char ch;
|
char ch;
|
||||||
unsigned int hash = 0;
|
unsigned int hash = 0;
|
||||||
|
|
||||||
// we assume hash to be at least 32 bits
|
// we assume hash to be at least 32 bits
|
||||||
while( (ch = *str++) != 0 ) {
|
while ((ch = *str++) != 0) {
|
||||||
hash ^= hash >> 28;
|
hash ^= hash >> 28;
|
||||||
hash <<= 4;
|
hash <<= 4;
|
||||||
hash ^= ch;
|
hash ^= ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,12 +243,13 @@ static void nhash_this(hash_table_index *hi, const void **key, ssize_t *klen,
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
new_hash_table *hash_make(void)
|
|
||||||
|
new_hash_table *
|
||||||
|
hash_make(void)
|
||||||
{
|
{
|
||||||
new_hash_table *nn;
|
new_hash_table *nn;
|
||||||
|
|
||||||
nn = (new_hash_table *)kmalloc(sizeof(new_hash_table));
|
|
||||||
|
|
||||||
|
nn = (new_hash_table *)kmalloc(sizeof(new_hash_table));
|
||||||
if (!nn)
|
if (!nn)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -241,7 +264,9 @@ new_hash_table *hash_make(void)
|
|||||||
return nn;
|
return nn;
|
||||||
}
|
}
|
||||||
|
|
||||||
static hash_index *new_hash_next(hash_index *hi)
|
|
||||||
|
static hash_index *
|
||||||
|
new_hash_next(hash_index *hi)
|
||||||
{
|
{
|
||||||
hi->this_idx = hi->next;
|
hi->this_idx = hi->next;
|
||||||
while (!hi->this_idx) {
|
while (!hi->this_idx) {
|
||||||
@@ -253,7 +278,9 @@ static hash_index *new_hash_next(hash_index *hi)
|
|||||||
return hi;
|
return hi;
|
||||||
}
|
}
|
||||||
|
|
||||||
static hash_index *new_hash_first(new_hash_table *nh)
|
|
||||||
|
static hash_index *
|
||||||
|
new_hash_first(new_hash_table *nh)
|
||||||
{
|
{
|
||||||
hash_index *hi = &nh->iterator;
|
hash_index *hi = &nh->iterator;
|
||||||
hi->nh = nh;
|
hi->nh = nh;
|
||||||
@@ -262,7 +289,9 @@ static hash_index *new_hash_first(new_hash_table *nh)
|
|||||||
return new_hash_next(hi);
|
return new_hash_next(hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expand_array(new_hash_table *nh)
|
|
||||||
|
static void
|
||||||
|
expand_array(new_hash_table *nh)
|
||||||
{
|
{
|
||||||
hash_index *hi;
|
hash_index *hi;
|
||||||
hash_entry **new_array;
|
hash_entry **new_array;
|
||||||
@@ -282,8 +311,8 @@ static void expand_array(new_hash_table *nh)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static hash_entry **find_entry(new_hash_table *nh, const void *key,
|
static hash_entry **
|
||||||
ssize_t klen, const void *val)
|
find_entry(new_hash_table *nh, const void *key, ssize_t klen, const void *val)
|
||||||
{
|
{
|
||||||
hash_entry **hep;
|
hash_entry **hep;
|
||||||
hash_entry *he;
|
hash_entry *he;
|
||||||
@@ -294,11 +323,10 @@ static hash_entry **find_entry(new_hash_table *nh, const void *key,
|
|||||||
if (!nh)
|
if (!nh)
|
||||||
return 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;
|
for (hep = &nh->array[hash & nh->max], he = *hep; he; hep = &he->next, he = *hep) {
|
||||||
hep = &he->next, he = *hep) {
|
|
||||||
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;
|
||||||
@@ -320,17 +348,21 @@ static hash_entry **find_entry(new_hash_table *nh, const void *key,
|
|||||||
return hep;
|
return hep;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hash_get(new_hash_table *nh, const void *key, ssize_t klen)
|
|
||||||
|
void *
|
||||||
|
hash_get(new_hash_table *nh, const void *key, ssize_t klen)
|
||||||
{
|
{
|
||||||
hash_entry *he;
|
hash_entry *he;
|
||||||
he = *find_entry(nh, key, klen, NULL);
|
he = *find_entry(nh, key, klen, NULL);
|
||||||
if (he)
|
if (he)
|
||||||
return (void*)he->val;
|
return (void*)he->val;
|
||||||
else
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash_set(new_hash_table *nh, const void *key, ssize_t klen, const void *val)
|
|
||||||
|
void
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user