diff --git a/tommyhash.h b/tommyhash.h index d584923e..38d642bf 100644 --- a/tommyhash.h +++ b/tommyhash.h @@ -43,30 +43,38 @@ typedef tommy_key_t tommy_hash_t; /** - * Hash function. - * Robert Jenkins "lookup3" hash, + * Hash function with a 32 bits result. + * Implementation of the Robert Jenkins "lookup3" hash 32 bits version, * from http://www.burtleburtle.net/bob/hash/doobs.html, function hashlittle(). + * \param init_val Initialization value. + * Using a different initialization value, you can generate a completely different set of hash values. + * Use 0 if not relevalt. + * \param void_key Pointer at the data to hash. + * \param key_len Size of the data to hash. * \note - * This function is platform independent as it read always in little endian format. - * \param init_val Initial value. You can use this to concatenate hash. Otherwise set it at 0. - * \param void_key Key to hash. - * \param key_len Length of the key. - * \return The 32 bit hash value. + * This function is endianess independent. + * \return The hash value of 32 bits. */ tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tommy_size_t key_len); /** - * Hash function for 64 bits. - * Robert Jenkins "lookup3" hash, + * Hash function with a 64 bits result. + * Implementation of the Robert Jenkins "lookup3" hash 64 bits versions, * from http://www.burtleburtle.net/bob/hash/doobs.html, function hashlittle2(). + * \param init_val Initialization value. + * Using a different initialization value, you can generate a completely different set of hash values. + * Use 0 if not relevalt. + * \param void_key Pointer at the data to hash. + * \param key_len Size of the data to hash. * \note - * This function is platform independent as it read always in little endian format. + * This function is endianess independent. + * \return The hash value of 64 bits. */ tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tommy_size_t key_len); /** * Integer hash of 32 bits. - * Robert Jenkins "4-byte Integer Hashing", + * Implementation of the Robert Jenkins "4-byte Integer Hashing", * from http://burtleburtle.net/bob/hash/integer.html */ tommy_inline tommy_uint32_t tommy_inthash_u32(tommy_uint32_t key) @@ -84,7 +92,7 @@ tommy_inline tommy_uint32_t tommy_inthash_u32(tommy_uint32_t key) /** * Integer hash of 64 bits. - * Thomas Wang "Integer Hash Function", + * Implementation of the Thomas Wang "Integer Hash Function", * from http://www.cris.com/~Ttwang/tech/inthash.htm */ tommy_inline tommy_uint64_t tommy_inthash_u64(tommy_uint64_t key) diff --git a/tommyhashdyn.c b/tommyhashdyn.c index 0b96ef5d..f5acec15 100644 --- a/tommyhashdyn.c +++ b/tommyhashdyn.c @@ -114,6 +114,28 @@ static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, unsigned new_bucket_bit hashdyn->bucket = new_bucket; } +/** + * Grow. + */ +tommy_inline void hashdyn_grow_step(tommy_hashdyn* hashdyn) +{ + /* grow if more than 50% full */ + if (hashdyn->count >= hashdyn->bucket_max / 2) { + tommy_hashdyn_resize(hashdyn, hashdyn->bucket_bit + 1); + } +} + +/** + * Shrink. + */ +tommy_inline void hashdyn_shrink_step(tommy_hashdyn* hashdyn) +{ + /* shrink if less than 12.5% full */ + if (hashdyn->count <= hashdyn->bucket_max / 8 && hashdyn->bucket_bit > TOMMY_HASHDYN_BIT) { + tommy_hashdyn_resize(hashdyn, hashdyn->bucket_bit - 1); + } +} + void tommy_hashdyn_insert(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node, void* data, tommy_hash_t hash) { unsigned pos = hash & hashdyn->bucket_mask; @@ -124,10 +146,7 @@ void tommy_hashdyn_insert(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node, void ++hashdyn->count; - /* grow if more than 50% full */ - if (hashdyn->count >= hashdyn->bucket_max / 2) { - tommy_hashdyn_resize(hashdyn, hashdyn->bucket_bit + 1); - } + hashdyn_grow_step(hashdyn); } void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node) @@ -138,10 +157,7 @@ void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* --hashdyn->count; - /* shrink if less than 12.5% full */ - if (hashdyn->count <= hashdyn->bucket_max / 8 && hashdyn->bucket_bit > TOMMY_HASHDYN_BIT) { - tommy_hashdyn_resize(hashdyn, hashdyn->bucket_bit - 1); - } + hashdyn_shrink_step(hashdyn); return node->data; } @@ -158,6 +174,8 @@ void* tommy_hashdyn_remove(tommy_hashdyn* hashdyn, tommy_compare_func* cmp, cons --hashdyn->count; + hashdyn_shrink_step(hashdyn); + return i->data; } i = i->next; diff --git a/tommyhashdyn.h b/tommyhashdyn.h index b22175e9..b1e1e61c 100644 --- a/tommyhashdyn.h +++ b/tommyhashdyn.h @@ -242,7 +242,7 @@ tommy_inline unsigned tommy_hashdyn_count(tommy_hashdyn* hashdyn) /** * Gets the size of allocated memory. - * It includes the size of the ::tommy_trie_node of the stored elements. + * It includes the size of the ::tommy_hashdyn_node of the stored elements. */ tommy_size_t tommy_hashdyn_memory_usage(tommy_hashdyn* hashdyn);