Archived
1
0
Fork 0

hash: Replace struct with a primitive type for hash values.

Remove the struct hash_entry and define a new type for hash values.
Most likely we will only need one member in this struct. and instead
of just using unsinged int. let's name a new type for clarity.
This commit is contained in:
Henrik Hautakoski 2012-08-22 15:16:27 +02:00
parent 569d0167d8
commit 223706d52f
3 changed files with 23 additions and 25 deletions

32
hash.c
View file

@ -23,12 +23,12 @@
#define TABLE_MIN_SIZE 128 #define TABLE_MIN_SIZE 128
unsigned hash_sdbm(const char *s) { hash_t hash_sdbm(const char *s) {
unsigned h; hash_t h;
for(h = 0; *s; s++) for(h = 0; *s; s++)
h = ((unsigned)*s) + (h << 6) + (h << 16) - h; h = ((hash_t)*s) + (h << 6) + (h << 16) - h;
return h; return h;
} }
@ -46,33 +46,33 @@ void hash_free(struct hash_table *table) {
hash_init(table); hash_init(table);
} }
static struct hash_entry** lookup(const struct hash_table *t, unsigned hash) { static hash_t** lookup(const struct hash_table *t, hash_t hash) {
unsigned i = hash % t->size; unsigned i = hash % t->size;
struct hash_entry **table = t->ptr; hash_t **table = t->ptr;
while(table[i]) { while(table[i]) {
if (table[i]->hash == hash) if (*table[i] == hash)
break; break;
i = (i + 1) % t->size; i = (i + 1) % t->size;
} }
return table + i; return table + i;
} }
void* hash_lookup(const struct hash_table *t, unsigned hash) { void* hash_lookup(const struct hash_table *t, hash_t hash) {
if (t->size < 1) if (t->size < 1)
return NULL; return NULL;
return *lookup(t, hash); return *lookup(t, hash);
} }
static void* insert(struct hash_table *t, unsigned hash, void *ptr) { static void* insert(struct hash_table *t, hash_t hash, void *ptr) {
struct hash_entry **dest = lookup(t, hash); hash_t **dest = lookup(t, hash);
if (!*dest) { if (!*dest) {
*dest = ptr; *dest = ptr;
(*dest)->hash = hash; **dest = hash;
t->count++; t->count++;
return NULL; return NULL;
} }
@ -92,7 +92,7 @@ static unsigned needs_resize(const struct hash_table *t) {
static void resize(struct hash_table *t) { static void resize(struct hash_table *t) {
unsigned int i, old_size = t->size; unsigned int i, old_size = t->size;
struct hash_entry **old = t->ptr; hash_t **old = t->ptr;
/* /*
* set size to a load factor that is in the * set size to a load factor that is in the
@ -107,24 +107,24 @@ static void resize(struct hash_table *t) {
if (old) { if (old) {
for(i=0; i < old_size; i++) { for(i=0; i < old_size; i++) {
struct hash_entry *entry = old[i]; hash_t *entry = old[i];
if (entry) if (entry)
insert(t, entry->hash, entry); insert(t, *entry, entry);
} }
free(old); free(old);
} }
} }
void* hash_insert(struct hash_table *t, unsigned hash, void *ptr) { void* hash_insert(struct hash_table *t, hash_t hash, void *ptr) {
if (needs_resize(t)) if (needs_resize(t))
resize(t); resize(t);
return insert(t, hash, ptr); return insert(t, hash, ptr);
} }
void* hash_remove(struct hash_table *t, unsigned hash) { void* hash_remove(struct hash_table *t, hash_t hash) {
struct hash_entry **dest = lookup(t, hash); hash_t **dest = lookup(t, hash);
if (*dest) { if (*dest) {
void *ptr = *dest; void *ptr = *dest;

14
hash.h
View file

@ -22,12 +22,10 @@
#include <stddef.h> #include <stddef.h>
struct hash_entry { typedef unsigned int hash_t;
unsigned hash;
};
struct hash_table { struct hash_table {
struct hash_entry **ptr; hash_t **ptr;
unsigned size; unsigned size;
unsigned count; unsigned count;
}; };
@ -38,16 +36,16 @@ struct hash_table {
((void*) *((char**) (t)->ptr + (i))) ((void*) *((char**) (t)->ptr + (i)))
/* general hash functions */ /* general hash functions */
unsigned hash_sdbm(const char *s); hash_t hash_sdbm(const char *s);
void hash_init(struct hash_table *table); void hash_init(struct hash_table *table);
void hash_free(struct hash_table *table); void hash_free(struct hash_table *table);
void* hash_lookup(const struct hash_table *t, unsigned hash); void* hash_lookup(const struct hash_table *t, hash_t hash);
void* hash_insert(struct hash_table *t, unsigned hash, void *ptr); void* hash_insert(struct hash_table *t, hash_t hash, void *ptr);
void* hash_remove(struct hash_table *t, unsigned hash); void* hash_remove(struct hash_table *t, hash_t hash);
#endif /* HASH_H */ #endif /* HASH_H */

View file

@ -44,7 +44,7 @@ struct header {
}; };
union hash { union hash {
unsigned int index; hash_t index;
unsigned char sha1[20]; unsigned char sha1[20];
}; };