Dynamic chained hashtable. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_hashdyn_struct |
| Hashtable container type. More... | |
Typedefs | |
| typedef tommy_node | tommy_hashdyn_node |
| Hashtable node. More... | |
| typedef struct tommy_hashdyn_struct | tommy_hashdyn |
| Hashtable container type. More... | |
Functions | |
| TOMMY_API void | tommy_hashdyn_init (tommy_hashdyn *hashdyn) |
| Initializes the hashtable. More... | |
| TOMMY_API void | tommy_hashdyn_done (tommy_hashdyn *hashdyn) |
| Deinitializes the hashtable. More... | |
| void | tommy_hashdyn_swap (tommy_hashdyn *first, tommy_hashdyn *second) |
| Exchanges the complete state of two initialized hashtables, including bucket allocations. More... | |
| TOMMY_API void | tommy_hashdyn_clear (tommy_hashdyn *hashdyn) |
| Removes all elements, preserving the allocated buckets. More... | |
| TOMMY_API void | tommy_hashdyn_reserve (tommy_hashdyn *hashdyn, tommy_size_t count) |
| Pre-allocates buckets for the specified number of elements. More... | |
| TOMMY_API void | tommy_hashdyn_shrink (tommy_hashdyn *hashdyn) |
| Shrinks the allocated buckets to fit the current number of elements. More... | |
| TOMMY_API void | tommy_hashdyn_insert (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node, void *data, tommy_hash_t hash) |
| Inserts an element in the hashtable. More... | |
| TOMMY_API void * | tommy_hashdyn_insert_unique (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node, void *data, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Inserts an element only if no equal element is already contained. More... | |
| TOMMY_API void * | tommy_hashdyn_remove (tommy_hashdyn *hashdyn, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Searches and removes an element from the hashtable. More... | |
| tommy_hashdyn_node * | tommy_hashdyn_bucket (tommy_hashdyn *hashdyn, tommy_hash_t hash) |
| Gets the bucket of the specified hash. More... | |
| tommy_hashdyn_node * | tommy_hashdyn_find (tommy_hashdyn *hashdyn, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Finds a node in the hashtable. More... | |
| void * | tommy_hashdyn_search (tommy_hashdyn *hashdyn, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Searches an element in the hashtable. More... | |
| TOMMY_API void * | tommy_hashdyn_remove_existing (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node) |
| Removes an element from the hashtable. More... | |
| TOMMY_API tommy_hashdyn_node * | tommy_hashdyn_head (tommy_hashdyn *hashdyn) |
| Gets the first node in iteration order, or 0 if empty. More... | |
| TOMMY_API tommy_hashdyn_node * | tommy_hashdyn_tail (tommy_hashdyn *hashdyn) |
| Gets the last node in iteration order, or 0 if empty. More... | |
| TOMMY_API tommy_hashdyn_node * | tommy_hashdyn_next (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node) |
| Gets the following node in iteration order, or 0 for the tail. More... | |
| TOMMY_API tommy_hashdyn_node * | tommy_hashdyn_prev (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node) |
| Gets the preceding node in iteration order, or 0 for the head. More... | |
| TOMMY_API void | tommy_hashdyn_rehash_existing (tommy_hashdyn *hashdyn, tommy_hashdyn_node *node, tommy_hash_t hash) |
| Updates the hash of an element already contained in the hashtable. More... | |
| TOMMY_API void | tommy_hashdyn_foreach (tommy_hashdyn *hashdyn, tommy_foreach_func *func) |
| Calls the specified function for each element in the hashtable. More... | |
| TOMMY_API void | tommy_hashdyn_foreach_arg (tommy_hashdyn *hashdyn, tommy_foreach_arg_func *func, void *arg) |
| Calls the specified function with an argument for each element in the hashtable. More... | |
| tommy_size_t | tommy_hashdyn_count (const tommy_hashdyn *hashdyn) |
| Gets the number of elements. More... | |
| tommy_bool_t | tommy_hashdyn_empty (const tommy_hashdyn *hashdyn) |
| Checks whether the hashtable is empty. More... | |
| tommy_size_t | tommy_hashdyn_bucket_count (const tommy_hashdyn *hashdyn) |
| Gets the number of buckets. More... | |
| TOMMY_API tommy_size_t | tommy_hashdyn_memory_usage (const tommy_hashdyn *hashdyn) |
| Gets the size of allocated memory. More... | |
| TOMMY_API void | tommy_hashdyn_to_list (tommy_hashdyn *hashdyn, tommy_list *list) |
| Transfers all elements from the hashtable into a tommy_list. More... | |
Dynamic chained hashtable.
This hashtable resizes dynamically. It starts with the minimal size of 16 buckets. It doubles the size when it reaches a load factor greater than or equal to 0.5 and it halves the size when the load factor is less than or equal to 0.125.
All the elements are reallocated in a single resize operation done inside tommy_hashdyn_insert() or tommy_hashdyn_remove().
Note that the resize operation takes approximately 100 [ms] with 1 million elements, and 1 [second] with 10 million elements. This could be a problem in real-time applications.
The resize also fragments the heap, as it involves allocating a double-sized table, copying elements, and deallocating the older table, leaving a big hole in the heap.
The tommy_hashlin hashtable fixes both problems.
To initialize the hashtable you have to call tommy_hashdyn_init().
To insert elements in the hashtable you have to call tommy_hashdyn_insert() for each element. In the insertion call you have to specify the address of the node, the address of the element, and the hash value of the key to use. The address of the element is used to initialize the tommy_node::data field of the node, and the hash to initialize the tommy_node::index field.
To find an element in the hashtable you have to call tommy_hashdyn_search() providing a comparison function, its argument, and the hash of the key to search.
To iterate over all the elements in the hashtable with the same key, you have to use tommy_hashdyn_bucket() and follow the tommy_node::next pointer until 0. You have also to check explicitly for the key, as the bucket may contain different keys.
To remove an element from the hashtable you have to call tommy_hashdyn_remove() providing a comparison function, its argument, and the hash of the key to search and remove.
To destroy the hashtable you have to remove all the elements, and deinitialize the hashtable calling tommy_hashdyn_done().
If you need to iterate over all the elements in the hashtable, you can use tommy_hashdyn_foreach() or tommy_hashdyn_foreach_arg(). For traversal that permits insertions and removals, use tommy_hashdyn_head() and tommy_hashdyn_next(), or tommy_hashdyn_tail() and tommy_hashdyn_prev(). Hashes are compared from the least significant bit to the most significant bit, with 0 before 1. Equal hashes are visited in insertion order. This order is independent of the number of buckets and progressive resizing. It differs from the bucket order used by foreach and to_list.
Iterators are pointers to nodes currently contained in the hashtable. Inserting elements or removing other nodes does not invalidate them or change the relative iteration order of the remaining elements. A forward or backward traversal visits every original element that remains contained, even if removals cause the table to shrink or insertions grow it. New elements can be visited if inserted after the current position in the traversal direction; elements inserted before it need not be visited.
Removing the pointed node invalidates its use as an iterator, even if its storage has not been freed. Obtain the next or previous node before removal, and keep that saved node contained until continuing the traversal.
Changing a hash with tommy_hashdyn_rehash_existing() can change iteration order, so the traversal guarantee does not cover rehashing during traversal. After swapping containers, use the container that now contains the node. Clearing or deinitializing the container invalidates all its iterators. Concurrent traversal and modification require external synchronization.
Iterator calls scan bucket contents without allocating or modifying memory. With a good hash function and normal dynamic resizing, a full traversal runs in expected O(n) time. Under the same hash distribution assumption, sparse tables require expected O(n + b) time, where n is the number of elements and b is the number of buckets. Such tables can arise after tommy_hashdyn_reserve(), tommy_hashdyn_clear(), or tommy_hashdyn_to_list(). In pathological cases with severe hash collisions, traversal can take O(n^2 + b) time. Use foreach for guaranteed O(n + b) traversal when no insertions or removals are needed.
| typedef tommy_node tommy_hashdyn_node |
Hashtable node.
This is the node that you have to include inside your elements.
| typedef struct tommy_hashdyn_struct tommy_hashdyn |
Hashtable container type.
| TOMMY_API void tommy_hashdyn_init | ( | tommy_hashdyn * | hashdyn | ) |
Initializes the hashtable.
| hashdyn | The hashtable. |
| TOMMY_API void tommy_hashdyn_done | ( | tommy_hashdyn * | hashdyn | ) |
Deinitializes the hashtable.
You can call this function with elements still contained, but such elements are not going to be freed by this call.
| hashdyn | The hashtable. |
| void tommy_hashdyn_swap | ( | tommy_hashdyn * | first, |
| tommy_hashdyn * | second | ||
| ) |
Exchanges the complete state of two initialized hashtables, including bucket allocations.
The hashtables must not share nodes. Nodes and elements are not accessed or modified. Existing node pointers remain valid and belong to the other hashtable. Passing the same hashtable twice has no effect. Both hashtables remain usable and deinitializable.
| first | The first hashtable. |
| second | The second hashtable. |
| TOMMY_API void tommy_hashdyn_clear | ( | tommy_hashdyn * | hashdyn | ) |
Removes all elements, preserving the allocated buckets.
Call tommy_hashdyn_shrink() after clearing to restore the initial capacity. The hashtable remains initialized and can be reused immediately. Elements are not freed and nodes are not accessed or modified. Their links must not be used to traverse the previous contents. You can call this function after tommy_hashdyn_foreach() has freed the elements. Subsequent insertions and removals retain the normal resizing policy.
| hashdyn | The hashtable. |
| TOMMY_API void tommy_hashdyn_reserve | ( | tommy_hashdyn * | hashdyn, |
| tommy_size_t | count | ||
| ) |
Pre-allocates buckets for the specified number of elements.
It ensures that the hashtable has enough buckets allocated to contain the specified number of elements without triggering a dynamic resize on insertion. Subsequent removals retain the normal shrinking policy and may reduce the allocated capacity.
| hashdyn | The hashtable. |
| count | Number of elements to reserve space for. |
| TOMMY_API void tommy_hashdyn_shrink | ( | tommy_hashdyn * | hashdyn | ) |
Shrinks the allocated buckets to fit the current number of elements.
The target is the smallest power of 2, at least 16, for which the number of elements is strictly less than half the number of buckets. If the current allocation is already no larger than the target, nothing is done. An empty hashtable is reduced to 16 buckets. Elements are not freed, and the tommy_node::data and tommy_node::index fields are left unchanged. The order of elements with the same hash is preserved.
| hashdyn | The hashtable. |
| TOMMY_API void tommy_hashdyn_insert | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node, | ||
| void * | data, | ||
| tommy_hash_t | hash | ||
| ) |
Inserts an element in the hashtable.
| hashdyn | The hashtable. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| hash | Hash of the element to insert. |
| TOMMY_API void * tommy_hashdyn_insert_unique | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node, | ||
| void * | data, | ||
| tommy_search_func * | cmp, | ||
| const void * | cmp_arg, | ||
| tommy_hash_t | hash | ||
| ) |
Inserts an element only if no equal element is already contained.
If found, the first equal element is returned, and the hashtable and candidate node are left unchanged. Otherwise, the candidate is inserted using the normal insertion policy, and data is returned. Elements are not freed by this call.
| hashdyn | The hashtable. |
| node | The candidate node. It must not belong to any container. |
| data | Pointer to the element to insert. |
| 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, non-zero for different elements. |
| cmp_arg | Compare argument describing the candidate key. |
| hash | Hash of the candidate key, consistent with the comparison function. |
| TOMMY_API void * tommy_hashdyn_remove | ( | tommy_hashdyn * | hashdyn, |
| tommy_search_func * | cmp, | ||
| const void * | cmp_arg, | ||
| tommy_hash_t | hash | ||
| ) |
Searches and removes an element from the hashtable.
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.
| hashdyn | The hashtable. |
| 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, non-zero for different elements. |
| cmp_arg | Compare argument passed as first argument of the compare function. |
| hash | Hash of the element to find and remove. |
| tommy_hashdyn_node * tommy_hashdyn_bucket | ( | tommy_hashdyn * | hashdyn, |
| tommy_hash_t | hash | ||
| ) |
Gets the bucket of the specified hash.
The bucket is guaranteed to contain ALL the elements with the specified hash, but it can also contain others. You can access elements in the bucket following the tommy_node::next pointer until 0.
| hashdyn | The hashtable. |
| hash | Hash of the element to find. |
| tommy_hashdyn_node * tommy_hashdyn_find | ( | tommy_hashdyn * | hashdyn, |
| tommy_search_func * | cmp, | ||
| const void * | cmp_arg, | ||
| tommy_hash_t | hash | ||
| ) |
Finds a node in the hashtable.
You have to provide a compare function and the hash of the element you want to find. If more equal elements are present, the first one is returned.
| hashdyn | The hashtable. |
| 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, non-zero for different elements. |
| cmp_arg | Compare argument passed as first argument of the compare function. |
| hash | Hash of the element to find. |
| void * tommy_hashdyn_search | ( | tommy_hashdyn * | hashdyn, |
| tommy_search_func * | cmp, | ||
| const void * | cmp_arg, | ||
| tommy_hash_t | hash | ||
| ) |
Searches an element in the hashtable.
You have to provide a compare function and the hash of the element you want to find. If more equal elements are present, the first one is returned.
| hashdyn | The hashtable. |
| 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, non-zero for different elements. |
| cmp_arg | Compare argument passed as first argument of the compare function. |
| hash | Hash of the element to find. |
| TOMMY_API void * tommy_hashdyn_remove_existing | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node | ||
| ) |
Removes an element from the hashtable.
You must already have the address of the node to remove.
| hashdyn | The hashtable. |
| node | The node to remove. |
| TOMMY_API tommy_hashdyn_node * tommy_hashdyn_head | ( | tommy_hashdyn * | hashdyn | ) |
Gets the first node in iteration order, or 0 if empty.
See tommy_hashdyn_iterator_validity for ordering and iterator validity.
| hashdyn | The hashtable. |
| TOMMY_API tommy_hashdyn_node * tommy_hashdyn_tail | ( | tommy_hashdyn * | hashdyn | ) |
Gets the last node in iteration order, or 0 if empty.
See tommy_hashdyn_iterator_validity for ordering and iterator validity.
| hashdyn | The hashtable. |
| TOMMY_API tommy_hashdyn_node * tommy_hashdyn_next | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node | ||
| ) |
Gets the following node in iteration order, or 0 for the tail.
See tommy_hashdyn_iterator_validity for ordering and iterator validity.
| hashdyn | The hashtable. |
| node | Node currently contained in the hashtable. It must not be 0. |
| TOMMY_API tommy_hashdyn_node * tommy_hashdyn_prev | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node | ||
| ) |
Gets the preceding node in iteration order, or 0 for the head.
See tommy_hashdyn_iterator_validity for ordering and iterator validity.
| hashdyn | The hashtable. |
| node | Node currently contained in the hashtable. It must not be 0. |
| TOMMY_API void tommy_hashdyn_rehash_existing | ( | tommy_hashdyn * | hashdyn, |
| tommy_hashdyn_node * | node, | ||
| tommy_hash_t | hash | ||
| ) |
Updates the hash of an element already contained in the hashtable.
The node must belong to this hashtable. The caller updates the element key and provides its new hash, without modifying tommy_node::index directly. If the hash is unchanged, the node and its position are left unchanged. Otherwise, the node is moved to the tail of the destination bucket, even if the old and new hashes identify the same bucket. The tommy_node::data field and the number of elements are left unchanged. No memory allocation, deallocation or resize is performed. Equal keys are allowed; no uniqueness check is performed.
| hashdyn | The hashtable. |
| node | The node whose hash is updated. |
| hash | The new hash of the element. |
| TOMMY_API void tommy_hashdyn_foreach | ( | tommy_hashdyn * | hashdyn, |
| tommy_foreach_func * | func | ||
| ) |
Calls the specified function for each element in the hashtable.
You cannot add or remove elements from the inside of the callback, but can use it to deallocate them.
If the callback frees the elements, the hashtable retains dangling node pointers. Call tommy_hashdyn_clear() before reusing it, or tommy_hashdyn_done() to release its storage.
| hashdyn | The hashtable. |
| func | Function to call with each element. |
| TOMMY_API void tommy_hashdyn_foreach_arg | ( | tommy_hashdyn * | hashdyn, |
| tommy_foreach_arg_func * | func, | ||
| void * | arg | ||
| ) |
Calls the specified function with an argument for each element in the hashtable.
The iteration order and callback rules are the same as tommy_hashdyn_foreach(). The callback may deallocate the current element. Adding or removing elements from inside the callback is not allowed.
| hashdyn | The hashtable. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |
| tommy_size_t tommy_hashdyn_count | ( | const tommy_hashdyn * | hashdyn | ) |
Gets the number of elements.
| hashdyn | The hashtable. |
| tommy_bool_t tommy_hashdyn_empty | ( | const tommy_hashdyn * | hashdyn | ) |
Checks whether the hashtable is empty.
| hashdyn | The hashtable. |
| tommy_size_t tommy_hashdyn_bucket_count | ( | const tommy_hashdyn * | hashdyn | ) |
Gets the number of buckets.
| hashdyn | The hashtable. |
| TOMMY_API tommy_size_t tommy_hashdyn_memory_usage | ( | const tommy_hashdyn * | hashdyn | ) |
Gets the size of allocated memory.
It includes the size of the tommy_hashdyn_node of the stored elements.
| hashdyn | The hashtable. |
| TOMMY_API void tommy_hashdyn_to_list | ( | tommy_hashdyn * | hashdyn, |
| tommy_list * | list | ||
| ) |
Transfers all elements from the hashtable into a tommy_list.
Removes every element from the hashdyn hashtable and inserts them into the provided list (at the tail), preserving the per-bucket order but not guaranteeing any particular global order.
After the call:
The tommy_node::data and tommy_node::index fields are left unchanged.
This function is useful when you need to:
Typical usage pattern:
| hashdyn | The hashtable to drain. |
| list | The destination list. It must be initialized and must not share nodes with the hashtable. Existing elements remain at the head of the list. |