From 39883a6b046c53855a30fbe81f0b849b0b2b428e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Wed, 24 Dec 2014 22:12:33 +0100 Subject: [PATCH] Updates TommyDS to the latest version. --- tommyds/tommychain.h | 5 ++--- tommyds/tommyhash.h | 4 ++-- tommyds/tommyhashdyn.c | 8 +++---- tommyds/tommyhashdyn.h | 3 +-- tommyds/tommylist.c | 3 +-- tommyds/tommylist.h | 49 +++++++++++++++++------------------------- tommyds/tommytypes.h | 11 +++++----- 7 files changed, 35 insertions(+), 48 deletions(-) diff --git a/tommyds/tommychain.h b/tommyds/tommychain.h index a9c05f85..71d3cccf 100644 --- a/tommyds/tommychain.h +++ b/tommyds/tommychain.h @@ -210,11 +210,10 @@ tommy_inline void tommy_chain_mergesort(tommy_chain* chain, tommy_compare_func* mask = counter >> i; while (mask != 1) { mask >>= 1; - if (mask & 1) { + if (mask & 1) tommy_chain_merge_degenerated(&bit[i + 1], &bit[i], cmp); - } else { + else bit[i + 1] = bit[i]; - } ++i; } diff --git a/tommyds/tommyhash.h b/tommyds/tommyhash.h index 329ef9d1..702e8ef4 100644 --- a/tommyds/tommyhash.h +++ b/tommyds/tommyhash.h @@ -48,7 +48,7 @@ typedef tommy_key_t tommy_hash_t; * 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. + * Use 0 if not relevant. * \param void_key Pointer at the data to hash. * \param key_len Size of the data to hash. * \note @@ -63,7 +63,7 @@ tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tom * 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. + * Use 0 if not relevant. * \param void_key Pointer at the data to hash. * \param key_len Size of the data to hash. * \note diff --git a/tommyds/tommyhashdyn.c b/tommyds/tommyhashdyn.c index 4da3fe63..8aa3e53a 100644 --- a/tommyds/tommyhashdyn.c +++ b/tommyds/tommyhashdyn.c @@ -120,9 +120,8 @@ static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, tommy_count_t new_bucke tommy_inline void hashdyn_grow_step(tommy_hashdyn* hashdyn) { /* grow if more than 50% full */ - if (hashdyn->count >= hashdyn->bucket_max / 2) { + if (hashdyn->count >= hashdyn->bucket_max / 2) tommy_hashdyn_resize(hashdyn, hashdyn->bucket_bit + 1); - } } /** @@ -131,9 +130,8 @@ tommy_inline void hashdyn_grow_step(tommy_hashdyn* hashdyn) 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) { + 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) @@ -164,7 +162,7 @@ void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* void* tommy_hashdyn_remove(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash) { - tommy_count_t pos = hash % hashdyn->bucket_max; + tommy_count_t pos = hash & hashdyn->bucket_mask; tommy_hashdyn_node* node = hashdyn->bucket[pos]; while (node) { diff --git a/tommyds/tommyhashdyn.h b/tommyds/tommyhashdyn.h index 11f24473..afbdfabb 100644 --- a/tommyds/tommyhashdyn.h +++ b/tommyds/tommyhashdyn.h @@ -189,7 +189,6 @@ void tommy_hashdyn_insert(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node, void * You have to provide a compare function and the hash of the element you want to remove. * If the element is not found, 0 is returned. * If more equal elements are present, the first one is removed. - * This operation is faster than calling tommy_hashdyn_bucket() and tommy_hashdyn_remove_existing() separately. * \param cmp Compare function called with cmp_arg as first argument and with the element to compare as a second one. * The function should return 0 for equal elements, anything other for different elements. * \param cmp_arg Compare argument passed as first argument of the compare function. @@ -274,7 +273,7 @@ void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* void tommy_hashdyn_foreach(tommy_hashdyn* hashdyn, tommy_foreach_func* func); /** - * Calls the specified function with argument for each element in the hashtable. + * Calls the specified function with an argument for each element in the hashtable. */ void tommy_hashdyn_foreach_arg(tommy_hashdyn* hashdyn, tommy_foreach_arg_func* func, void* arg); diff --git a/tommyds/tommylist.c b/tommyds/tommylist.c index 891d6553..ea85b08c 100644 --- a/tommyds/tommylist.c +++ b/tommyds/tommylist.c @@ -34,9 +34,8 @@ void tommy_list_concat(tommy_list* first, tommy_list* second) tommy_node* first_tail; tommy_node* second_head; - if (tommy_list_empty(second)) { + if (tommy_list_empty(second)) return; - } if (tommy_list_empty(first)) { *first = *second; diff --git a/tommyds/tommylist.h b/tommyds/tommylist.h index 98e43035..ccee71af 100644 --- a/tommyds/tommylist.h +++ b/tommyds/tommylist.h @@ -28,18 +28,18 @@ /** \file * Double linked list for collisions into hashtables. * - * This list is a double linked list mainly targetted for collisions into an hashtables, - * but useable also as a generic list. + * This list is a double linked list mainly targetted for handling collisions + * into an hashtables, but useable also as a generic list. * * The main feature of this list is to require only one pointer to represent the - * list, compared to a classic implementation requiring an head an a tail pointers. + * list, compared to a classic implementation requiring a head an a tail pointers. * This reduces the memory usage in hashtables. * * Another feature is to support the insertion at the end of the list. This allow to store * collisions in a stable order. Where for stable order we mean that equal elements keep * their insertion order. * - * To initialize the list, you have to call tommy_list_init(), or simply assign + * To initialize the list, you have to call tommy_list_init(), or to simply assign * to it NULL, as an empty list is represented by the NULL value. * * \code @@ -69,7 +69,7 @@ * tommy_list_insert_tail(&list, &obj->node, obj); // inserts the object * \endcode * - * To iterates over all the elements in the list you have to call + * To iterate over all the elements in the list you have to call * tommy_list_head() to get the head of the list and follow the * tommy_node::next pointer until NULL. * @@ -84,18 +84,13 @@ * } * \endcode * - * To destroy the list you have only to remove all the elements, as the list is - * completely inplace and it doesn't allocate memory. + * To destroy the list you have to remove all the elements, + * as the list is completely inplace and it doesn't allocate memory. + * This can be done with the tommy_list_foreach() function. * * \code - * tommy_node* i = tommy_list_head(&list); - * while (i) { - * tommy_node* i_next = i->next; // saves the next element before freeing - * - * free(i->data); // frees the object allocated memory - * - * i = i_next; // goes to the next element - * } + * // deallocates all the objects iterating the list + * tommy_list_foreach(&list, free); * \endcode */ @@ -206,11 +201,10 @@ tommy_inline void tommy_list_insert_head(tommy_list* list, tommy_node* node, voi { tommy_node* head = tommy_list_head(list); - if (head) { + if (head) tommy_list_insert_head_not_empty(list, node); - } else { + else tommy_list_insert_first(list, node); - } node->data = data; } @@ -224,11 +218,10 @@ tommy_inline void tommy_list_insert_tail(tommy_list* list, tommy_node* node, voi { tommy_node* head = tommy_list_head(list); - if (head) { + if (head) tommy_list_insert_tail_not_empty(head, node); - } else { + else tommy_list_insert_first(list, node); - } node->data = data; } @@ -264,18 +257,16 @@ tommy_inline void* tommy_list_remove_existing(tommy_list* list, tommy_node* node tommy_node* head = tommy_list_head(list); /* remove from the "circular" prev list */ - if (node->next) { + if (node->next) node->next->prev = node->prev; - } else { + else head->prev = node->prev; /* the last */ - } /* remove from the "0 terminated" next list */ - if (head == node) { + if (head == node) *list = node->next; /* the new head, in case 0 */ - } else { + else node->prev->next = node->next; - } return node->data; } @@ -294,7 +285,7 @@ void tommy_list_concat(tommy_list* first, tommy_list* second); * It's a stable merge sort with O(N*log(N)) worst complexity. * It's faster on degenerated cases like partially ordered lists. * \param cmp Compare function called with two elements. - * The function should return <0 if the first element is less than the second, == 0 if equal, and > 0 if greather. + * The function should return <0 if the first element is less than the second, ==0 if equal, and >0 if greather. */ void tommy_list_sort(tommy_list* list, tommy_compare_func* cmp); @@ -347,7 +338,7 @@ tommy_inline void tommy_list_foreach(tommy_list* list, tommy_foreach_func* func) } /** - * Calls the specified function with argument for each element in the list. + * Calls the specified function with an argument for each element in the list. */ tommy_inline void tommy_list_foreach_arg(tommy_list* list, tommy_foreach_arg_func* func, void* arg) { diff --git a/tommyds/tommytypes.h b/tommyds/tommytypes.h index 5c81f3ca..45d3d1a5 100644 --- a/tommyds/tommytypes.h +++ b/tommyds/tommytypes.h @@ -243,7 +243,7 @@ typedef int tommy_compare_func(const void* obj_a, const void* obj_b); * Search function for elements. * \param arg Pointer at the value to search. * \param obj Pointer at the object to compare to. - * \return ==0 if the value matches the element. != 0 if different. + * \return ==0 if the value matches the element. !=0 if different. * * Note that the first argument is a pointer to the value to search and * the second one is a pointer to the object to compare. @@ -310,9 +310,9 @@ typedef void tommy_foreach_arg_func(void* arg, void* obj); * Return the bit index of the most significant 1 bit. * * If no bit is set, the result is undefined. - * To force a return 0 in this case, you can use tommy_ilog2(value | 1). + * To force a return 0 in this case, you can use tommy_ilog2_u32(value | 1). * - * Other interesting ways for bitscan can be found at: + * Other interesting ways for bitscan are at: * * Bit Twiddling Hacks * http://graphics.stanford.edu/~seander/bithacks.html @@ -321,7 +321,7 @@ typedef void tommy_foreach_arg_func(void* arg, void* obj); * http://chessprogramming.wikispaces.com/BitScan * * \param value Value to scan. 0 is not allowed. - * \return The index of the most significan bit set. + * \return The index of the most significant bit set. */ tommy_inline tommy_uint_t tommy_ilog2_u32(tommy_uint32_t value) { @@ -336,7 +336,8 @@ tommy_inline tommy_uint_t tommy_ilog2_u32(tommy_uint32_t value) * Where "x ^ 31 = 31 - x", but gcc does not optimize "31 - __builtin_clz(x)" to bsr(x), * but generates 31 - (bsr(x) xor 31). * - * So we write "__builtin_clz(x) ^ 31" instead of "31 - __builtin_clz(x)". + * So we write "__builtin_clz(x) ^ 31" instead of "31 - __builtin_clz(x)", + * to allow the double xor to be optimized out. */ return __builtin_clz(value) ^ 31; #else