Fixed-size chained hashtable. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_hashtable_struct |
| Hashtable container type. More... | |
Typedefs | |
| typedef tommy_node | tommy_hashtable_node |
| Hashtable node. More... | |
| typedef struct tommy_hashtable_struct | tommy_hashtable |
| Hashtable container type. More... | |
Functions | |
| TOMMY_API void | tommy_hashtable_init (tommy_hashtable *hashtable, tommy_size_t bucket_max) |
| Initializes the hashtable. More... | |
| TOMMY_API void | tommy_hashtable_done (tommy_hashtable *hashtable) |
| Deinitializes the hashtable. More... | |
| void | tommy_hashtable_swap (tommy_hashtable *first, tommy_hashtable *second) |
| Exchanges the complete state of two initialized hashtables, including bucket allocations. More... | |
| TOMMY_API void | tommy_hashtable_clear (tommy_hashtable *hashtable) |
| Removes all elements, preserving the allocated buckets. More... | |
| TOMMY_API void | tommy_hashtable_insert (tommy_hashtable *hashtable, tommy_hashtable_node *node, void *data, tommy_hash_t hash) |
| Inserts an element in the hashtable. More... | |
| TOMMY_API void * | tommy_hashtable_insert_unique (tommy_hashtable *hashtable, tommy_hashtable_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_hashtable_remove (tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Searches and removes an element from the hashtable. More... | |
| tommy_hashtable_node * | tommy_hashtable_bucket (tommy_hashtable *hashtable, tommy_hash_t hash) |
| Gets the bucket of the specified hash. More... | |
| tommy_hashtable_node * | tommy_hashtable_find (tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Finds a node in the hashtable. More... | |
| void * | tommy_hashtable_search (tommy_hashtable *hashtable, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash) |
| Searches an element in the hashtable. More... | |
| TOMMY_API void * | tommy_hashtable_remove_existing (tommy_hashtable *hashtable, tommy_hashtable_node *node) |
| Removes an element from the hashtable. More... | |
| TOMMY_API tommy_hashtable_node * | tommy_hashtable_head (tommy_hashtable *hashtable) |
| Gets the first node in iteration order, or 0 if empty. More... | |
| TOMMY_API tommy_hashtable_node * | tommy_hashtable_tail (tommy_hashtable *hashtable) |
| Gets the last node in iteration order, or 0 if empty. More... | |
| TOMMY_API tommy_hashtable_node * | tommy_hashtable_next (tommy_hashtable *hashtable, tommy_hashtable_node *node) |
| Gets the following node in iteration order, or 0 for the tail. More... | |
| TOMMY_API tommy_hashtable_node * | tommy_hashtable_prev (tommy_hashtable *hashtable, tommy_hashtable_node *node) |
| Gets the preceding node in iteration order, or 0 for the head. More... | |
| TOMMY_API void | tommy_hashtable_rehash_existing (tommy_hashtable *hashtable, tommy_hashtable_node *node, tommy_hash_t hash) |
| Updates the hash of an element already contained in the hashtable. More... | |
| TOMMY_API void | tommy_hashtable_foreach (tommy_hashtable *hashtable, tommy_foreach_func *func) |
| Calls the specified function for each element in the hashtable. More... | |
| TOMMY_API void | tommy_hashtable_foreach_arg (tommy_hashtable *hashtable, 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_hashtable_count (const tommy_hashtable *hashtable) |
| Gets the number of elements. More... | |
| tommy_bool_t | tommy_hashtable_empty (const tommy_hashtable *hashtable) |
| Checks whether the hashtable is empty. More... | |
| tommy_size_t | tommy_hashtable_bucket_count (const tommy_hashtable *hashtable) |
| Gets the number of buckets. More... | |
| TOMMY_API tommy_size_t | tommy_hashtable_memory_usage (const tommy_hashtable *hashtable) |
| Gets the size of allocated memory. More... | |
| TOMMY_API void | tommy_hashtable_to_list (tommy_hashtable *hashtable, tommy_list *list) |
| Transfers all elements from the hashtable into a tommy_list. More... | |
Fixed-size chained hashtable.
This hashtable is a standard implementation of a chained hashtable with a fixed size.
Note that performance starts to degenerate after reaching a load factor greater than 0.75. The tommy_hashdyn and tommy_hashlin hashtables fix this problem growing dynamically.
To initialize the hashtable you have to call tommy_hashtable_init() specifying the fixed bucket size.
To insert elements in the hashtable you have to call tommy_hashtable_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_hashtable_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_hashtable_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_hashtable_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_hashtable_done().
If you need to iterate over all the elements in the hashtable, you can use tommy_hashtable_foreach() or tommy_hashtable_foreach_arg(). For traversal that permits insertions and removals, use tommy_hashtable_head() and tommy_hashtable_next(), or tommy_hashtable_tail() and tommy_hashtable_prev(). Buckets are visited by increasing bucket index, and elements within each bucket are visited in insertion order.
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. 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_hashtable_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 follow node links directly without allocating memory. Advancing within a bucket is O(1); crossing between buckets scans empty buckets. Under suitable occupancy and distribution, each iterator step is expected O(1). Sparse tables can result from a large initial allocation, removals, tommy_hashtable_clear(), or tommy_hashtable_to_list(). A full traversal runs in O(n + b) time, where n is the number of elements and b is the number of buckets. Use foreach when no insertions or removals are needed.
| typedef tommy_node tommy_hashtable_node |
Hashtable node.
This is the node that you have to include inside your elements.
| typedef struct tommy_hashtable_struct tommy_hashtable |
Hashtable container type.
| TOMMY_API void tommy_hashtable_init | ( | tommy_hashtable * | hashtable, |
| tommy_size_t | bucket_max | ||
| ) |
Initializes the hashtable.
| hashtable | The hashtable. |
| bucket_max | Minimum number of buckets to allocate. The effective number is rounded up to a power of 2, with a minimum of 16 buckets. |
| TOMMY_API void tommy_hashtable_done | ( | tommy_hashtable * | hashtable | ) |
Deinitializes the hashtable.
You can call this function with elements still contained, but such elements are not going to be freed by this call.
| hashtable | The hashtable. |
| void tommy_hashtable_swap | ( | tommy_hashtable * | first, |
| tommy_hashtable * | 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_hashtable_clear | ( | tommy_hashtable * | hashtable | ) |
Removes all elements, preserving the allocated buckets.
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_hashtable_foreach() has freed the elements.
| hashtable | The hashtable. |
| TOMMY_API void tommy_hashtable_insert | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_node * | node, | ||
| void * | data, | ||
| tommy_hash_t | hash | ||
| ) |
Inserts an element in the hashtable.
| hashtable | 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_hashtable_insert_unique | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_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.
| hashtable | 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_hashtable_remove | ( | tommy_hashtable * | hashtable, |
| 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.
| hashtable | 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_hashtable_node * tommy_hashtable_bucket | ( | tommy_hashtable * | hashtable, |
| 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.
| hashtable | The hashtable. |
| hash | Hash of the element to find. |
| tommy_hashtable_node * tommy_hashtable_find | ( | tommy_hashtable * | hashtable, |
| 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.
| hashtable | 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_hashtable_search | ( | tommy_hashtable * | hashtable, |
| 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.
| hashtable | 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_hashtable_remove_existing | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_node * | node | ||
| ) |
Removes an element from the hashtable.
You must already have the address of the node to remove.
| hashtable | The hashtable. |
| node | The node to remove. |
| TOMMY_API tommy_hashtable_node * tommy_hashtable_head | ( | tommy_hashtable * | hashtable | ) |
Gets the first node in iteration order, or 0 if empty.
See tommy_hashtable_iterator_validity for ordering and iterator validity.
| hashtable | The hashtable. |
| TOMMY_API tommy_hashtable_node * tommy_hashtable_tail | ( | tommy_hashtable * | hashtable | ) |
Gets the last node in iteration order, or 0 if empty.
See tommy_hashtable_iterator_validity for ordering and iterator validity.
| hashtable | The hashtable. |
| TOMMY_API tommy_hashtable_node * tommy_hashtable_next | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_node * | node | ||
| ) |
Gets the following node in iteration order, or 0 for the tail.
See tommy_hashtable_iterator_validity for ordering and iterator validity.
| hashtable | The hashtable. |
| node | Node currently contained in the hashtable. It must not be 0. |
| TOMMY_API tommy_hashtable_node * tommy_hashtable_prev | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_node * | node | ||
| ) |
Gets the preceding node in iteration order, or 0 for the head.
See tommy_hashtable_iterator_validity for ordering and iterator validity.
| hashtable | The hashtable. |
| node | Node currently contained in the hashtable. It must not be 0. |
| TOMMY_API void tommy_hashtable_rehash_existing | ( | tommy_hashtable * | hashtable, |
| tommy_hashtable_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.
| hashtable | The hashtable. |
| node | The node whose hash is updated. |
| hash | The new hash of the element. |
| TOMMY_API void tommy_hashtable_foreach | ( | tommy_hashtable * | hashtable, |
| 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_hashtable_clear() before reusing it, or tommy_hashtable_done() to release its storage.
| hashtable | The hashtable. |
| func | Function to call with each element. |
| TOMMY_API void tommy_hashtable_foreach_arg | ( | tommy_hashtable * | hashtable, |
| 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_hashtable_foreach(). The callback may deallocate the current element. Adding or removing elements from inside the callback is not allowed.
| hashtable | The hashtable. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |
| tommy_size_t tommy_hashtable_count | ( | const tommy_hashtable * | hashtable | ) |
Gets the number of elements.
| hashtable | The hashtable. |
| tommy_bool_t tommy_hashtable_empty | ( | const tommy_hashtable * | hashtable | ) |
Checks whether the hashtable is empty.
| hashtable | The hashtable. |
| tommy_size_t tommy_hashtable_bucket_count | ( | const tommy_hashtable * | hashtable | ) |
Gets the number of buckets.
| hashtable | The hashtable. |
| TOMMY_API tommy_size_t tommy_hashtable_memory_usage | ( | const tommy_hashtable * | hashtable | ) |
Gets the size of allocated memory.
It includes the size of the tommy_hashtable_node of the stored elements.
| hashtable | The hashtable. |
| TOMMY_API void tommy_hashtable_to_list | ( | tommy_hashtable * | hashtable, |
| tommy_list * | list | ||
| ) |
Transfers all elements from the hashtable into a tommy_list.
Removes every element from the 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:
| hashtable | 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. |