From: Stephan Soller Date: Sat, 30 Apr 2016 11:17:18 +0000 (+0200) Subject: Made sh_put_ptr() comapre the key additionally to the hash. X-Git-Url: https://git.owens.tech/rss.xml/rss.xml/git?a=commitdiff_plain;h=3a9a13f670a41dfa7522ed7b630e0ac9b5921997;p=forks%2Fsingle-header-file-c-libs.git Made sh_put_ptr() comapre the key additionally to the hash. --- diff --git a/slim_hash.h b/slim_hash.h index 452c854..2828221 100644 --- a/slim_hash.h +++ b/slim_hash.h @@ -197,25 +197,31 @@ SH_GEN_HASH_DEF(con_list, int32_t, void*) hashmap->slots = NULL; \ } \ \ - value_t* prefix##_put_ptr(prefix##_p hashmap, key_t key) { \ - /* add the +1 to the capacity doubling to avoid beeing stuck on a capacity of 0 */ \ - if (hashmap->length + hashmap->deleted + 1 > hashmap->capacity * 0.5) \ - prefix##_resize(hashmap, (hashmap->capacity + 1) * 2); \ - \ - uint32_t hash = (hash_expr) | SH_SLOT_FILLED; \ - size_t index = hash % hashmap->capacity; \ - while ( !(hashmap->slots[index].hash_and_flags == hash || hashmap->slots[index].hash_and_flags == SH_SLOT_FREE || hashmap->slots[index].hash_and_flags == SH_SLOT_DELETED) ) { \ - index = (index + 1) % hashmap->capacity; \ - } \ - \ - if (hashmap->slots[index].hash_and_flags == SH_SLOT_DELETED) \ - hashmap->deleted--; \ - hashmap->length++; \ - hashmap->slots[index].hash_and_flags = hash; \ - hashmap->slots[index].key = (key_put_expr); \ - return &hashmap->slots[index].value; \ - } \ - \ + value_t* prefix##_put_ptr(prefix##_p hashmap, key_t key) { \ + /* add the +1 to the capacity doubling to avoid beeing stuck on a capacity of 0 */ \ + if (hashmap->length + hashmap->deleted + 1 > hashmap->capacity * 0.5) \ + prefix##_resize(hashmap, (hashmap->capacity + 1) * 2); \ + \ + uint32_t hash = (hash_expr) | SH_SLOT_FILLED; \ + size_t index = hash % hashmap->capacity; \ + while ( !(hashmap->slots[index].hash_and_flags == SH_SLOT_FREE || hashmap->slots[index].hash_and_flags == SH_SLOT_DELETED) ) { \ + if (hashmap->slots[index].hash_and_flags == hash) { \ + key_t a = hashmap->slots[index].key; \ + key_t b = key; \ + if (key_cmp_expr) \ + break; \ + } \ + index = (index + 1) % hashmap->capacity; \ + } \ + \ + if (hashmap->slots[index].hash_and_flags == SH_SLOT_DELETED) \ + hashmap->deleted--; \ + hashmap->length++; \ + hashmap->slots[index].hash_and_flags = hash; \ + hashmap->slots[index].key = (key_put_expr); \ + return &hashmap->slots[index].value; \ + } \ + \ value_t* prefix##_get_ptr(prefix##_p hashmap, key_t key) { \ uint32_t hash = (hash_expr) | SH_SLOT_FILLED; \ size_t index = hash % hashmap->capacity; \