From fa92714018ad2134de97bf403cbebeee6d863b3f Mon Sep 17 00:00:00 2001 From: mquinson Date: Tue, 22 Aug 2006 22:11:24 +0000 Subject: [PATCH] Let xbt_dict_hash work also with non-null terminated strings (and declare it static) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2726 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/xbt/dict.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/xbt/dict.c b/src/xbt/dict.c index 2f6101b5ce..c97f224a32 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -127,12 +127,13 @@ void xbt_dict_hashsize_set(xbt_dict_t dict, int hashsize) { /** * Returns the hash code of a string. */ -unsigned int xbt_dict_hash(const char *str) { +static unsigned int xbt_dict_hash(const char *str, int str_len) { /* fast implementation of djb2 algorithm */ unsigned int hash = 5381; int c; - while ((c = *str++)) { + while (str_len--) { + c = *str++; hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ } @@ -158,7 +159,7 @@ void xbt_dict_set_ext(xbt_dict_t dict, void_f_pvoid_t *free_ctn) { xbt_assert(dict); - unsigned int hash_code = xbt_dict_hash(key) % dict->table_size; + unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size; xbt_dictelm_t current, previous = NULL; current = dict->table[hash_code]; @@ -226,7 +227,7 @@ void *xbt_dict_get_ext(xbt_dict_t dict, int key_len) { xbt_assert(dict); - unsigned int hash_code = xbt_dict_hash(key) % dict->table_size; + unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size; xbt_dictelm_t current; current = dict->table[hash_code]; @@ -305,7 +306,7 @@ void xbt_dict_remove_ext(xbt_dict_t dict, int key_len) { xbt_assert(dict); - unsigned int hash_code = xbt_dict_hash(key) % dict->table_size; + unsigned int hash_code = xbt_dict_hash(key,key_len) % dict->table_size; xbt_dictelm_t current, previous = NULL; current = dict->table[hash_code]; @@ -384,7 +385,7 @@ int xbt_dict_length(xbt_dict_t dict) { void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) { xbt_assert(dict); - int hashcode = xbt_dict_hash(element->key) % dict->table_size; + int hashcode = xbt_dict_hash(element->key,element->key_len) % dict->table_size; element->next = dict->table[hashcode]; dict->table[hashcode] = element; } -- 2.20.1