From 4e12ff1c949245bcd6f17e8fd1bb627ca881cc30 Mon Sep 17 00:00:00 2001 From: nohar Date: Fri, 8 Dec 2006 21:08:23 +0000 Subject: [PATCH] Hash fonction just for fun. --- src/util.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/util.c b/src/util.c index 44f5640..4e1c479 100644 --- a/src/util.c +++ b/src/util.c @@ -396,7 +396,7 @@ void list_append(list_t *src, list_t *dest) struct hash_item { char *key; void *item; -}; +}; static int hash_item_nocase_cmp(struct hash_item *a, char *b) { @@ -453,12 +453,15 @@ hash_t *hash_new(int options) return h; } +/* Now we have a real hash, but we use only the last byte of it :p */ static unsigned char hash_func(char *pkey) { - unsigned char i = 0; - while (*pkey) - i += (unsigned char)toupper(*pkey++); - return i; + char c; + unsigned long hash = 5381; /* 5381 & 0xff makes more sense */ + + while (c = *pkey++) + hash = ((hash << 5) + hash) ^ c; + return (unsigned char)hash; } void hash_insert(hash_t *hash, char *key, void *ptr) @@ -467,7 +470,7 @@ void hash_insert(hash_t *hash, char *key, void *ptr) if (hash_get(hash, key)) fatal("Element with key %s already in hash %x\n", key, hash); - + it = malloc(sizeof(struct hash_item)); if (!it) fatal("malloc");