Data Structures | Typedefs | Functions
tommyhashdyn.h File Reference

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...
 

Detailed Description

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().

tommy_hashdyn hashdyn;
Hashtable container type.
Definition: tommyhashdyn.h:182
TOMMY_API void tommy_hashdyn_init(tommy_hashdyn *hashdyn)
Initializes the hashtable.

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.

struct element {
int value;
// other fields
tommy_node node;
};
struct element* elem = malloc(sizeof(struct element)); // creates the element
elem->value = ...; // initializes the element
tommy_hashdyn_insert(&hashdyn, &elem->node, elem, tommy_inthash_u32(elem->value)); // inserts the element
Data structure node.
Definition: tommytypes.h:242
tommy_uint32_t tommy_inthash_u32(tommy_uint32_t key)
Reversible 32-bit integer hash function.
Definition: tommyhash.h:141
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.

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.

int compare(const void* arg, const void* elem)
{
return *(const int*)arg != ((const struct element*)elem)->value;
}
int value_to_find = 1;
struct element* elem = tommy_hashdyn_search(&hashdyn, compare, &value_to_find, tommy_inthash_u32(value_to_find));
if (!elem) {
// not found
} else {
// found
}
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.
Definition: tommyhashdyn.h:349

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.

int value_to_find = 1;
tommy_node* i = tommy_hashdyn_bucket(&hashdyn, tommy_inthash_u32(value_to_find));
while (i) {
struct element* elem = i->data; // gets the element pointer
if (elem->value == value_to_find) {
printf("%d\n", elem->value); // process the element
}
i = i->next; // goes to the next element
}
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:247
void * data
Pointer to the element containing the node.
Definition: tommytypes.h:260
tommy_hashdyn_node * tommy_hashdyn_bucket(tommy_hashdyn *hashdyn, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashdyn.h:309

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.

struct element* elem = tommy_hashdyn_remove(&hashdyn, compare, &value_to_remove, tommy_inthash_u32(value_to_remove));
if (elem) {
free(elem); // frees the element allocated memory
}
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.

To destroy the hashtable you have to remove all the elements, and deinitialize the hashtable calling tommy_hashdyn_done().

TOMMY_API void tommy_hashdyn_done(tommy_hashdyn *hashdyn)
Deinitializes the hashtable.

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.

while (i) {
tommy_hashdyn_node* next = tommy_hashdyn_next(&hashdyn, i);
if (should_remove(i->data)) {
free(tommy_hashdyn_remove_existing(&hashdyn, i));
}
i = next;
}
TOMMY_API tommy_hashdyn_node * tommy_hashdyn_head(tommy_hashdyn *hashdyn)
Gets the first node in iteration order, or 0 if empty.
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.
TOMMY_API void * tommy_hashdyn_remove_existing(tommy_hashdyn *hashdyn, tommy_hashdyn_node *node)
Removes an element from the hashtable.

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 Documentation

◆ tommy_hashdyn_node

Hashtable node.

This is the node that you have to include inside your elements.

◆ tommy_hashdyn

Hashtable container type.

Note
Don't use internal fields directly, but access the container only using functions.

Function Documentation

◆ tommy_hashdyn_init()

TOMMY_API void tommy_hashdyn_init ( tommy_hashdyn *  hashdyn)

Initializes the hashtable.

Parameters
hashdynThe hashtable.

◆ tommy_hashdyn_done()

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.

Parameters
hashdynThe hashtable.

◆ tommy_hashdyn_swap()

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.

Parameters
firstThe first hashtable.
secondThe second hashtable.
Note
This operation is O(1) with respect to the number of elements.

◆ tommy_hashdyn_clear()

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.

Parameters
hashdynThe hashtable.
Note
This operation is O(b), where b is the number of buckets.

◆ tommy_hashdyn_reserve()

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.

Parameters
hashdynThe hashtable.
countNumber of elements to reserve space for.

◆ tommy_hashdyn_shrink()

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.

Parameters
hashdynThe hashtable.
Note
This operation is O(b), where b is the previous number of buckets.

◆ tommy_hashdyn_insert()

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.

Parameters
hashdynThe hashtable.
nodePointer to the node embedded into the element to insert.
dataPointer to the element to insert.
hashHash of the element to insert.

◆ tommy_hashdyn_insert_unique()

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.

Parameters
hashdynThe hashtable.
nodeThe candidate node. It must not belong to any container.
dataPointer to the element to insert.
cmpCompare 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_argCompare argument describing the candidate key.
hashHash of the candidate key, consistent with the comparison function.
Returns
The first equal element, or data if the candidate was inserted.

◆ tommy_hashdyn_remove()

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.

Parameters
hashdynThe hashtable.
cmpCompare 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_argCompare argument passed as first argument of the compare function.
hashHash of the element to find and remove.
Returns
The removed element, or 0 if not found.

◆ tommy_hashdyn_bucket()

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.

Parameters
hashdynThe hashtable.
hashHash of the element to find.
Returns
The head of the bucket, or 0 if empty.

◆ tommy_hashdyn_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.

Parameters
hashdynThe hashtable.
cmpCompare 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_argCompare argument passed as first argument of the compare function.
hashHash of the element to find.
Returns
The first node found, or 0 if none.

◆ tommy_hashdyn_search()

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.

Parameters
hashdynThe hashtable.
cmpCompare 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_argCompare argument passed as first argument of the compare function.
hashHash of the element to find.
Returns
The first element found, or 0 if none.

◆ tommy_hashdyn_remove_existing()

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.

Parameters
hashdynThe hashtable.
nodeThe node to remove.
Returns
The removed element.

◆ tommy_hashdyn_head()

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.

Parameters
hashdynThe hashtable.
Returns
The first node in iteration order, or 0 if empty.
Note
This operation is expected O(1) under suitable hash distribution and normal dynamic resizing. In the worst case, scanning bucket contents and empty buckets takes O(n + b), where n is the number of elements and b is the number of buckets.

◆ tommy_hashdyn_tail()

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.

Parameters
hashdynThe hashtable.
Returns
The last node in iteration order, or 0 if empty.
Note
This operation is expected O(1) under suitable hash distribution and normal dynamic resizing. In the worst case, scanning bucket contents and empty buckets takes O(n + b), where n is the number of elements and b is the number of buckets.

◆ tommy_hashdyn_next()

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.

Parameters
hashdynThe hashtable.
nodeNode currently contained in the hashtable. It must not be 0.
Returns
The following node in iteration order, or 0 for the tail.
Note
This operation is expected O(1) under suitable hash distribution and normal dynamic resizing. In the worst case, scanning bucket contents and empty buckets takes O(n + b), where n is the number of elements and b is the number of buckets.

◆ tommy_hashdyn_prev()

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.

Parameters
hashdynThe hashtable.
nodeNode currently contained in the hashtable. It must not be 0.
Returns
The preceding node in iteration order, or 0 for the head.
Note
This operation is expected O(1) under suitable hash distribution and normal dynamic resizing. In the worst case, scanning bucket contents and empty buckets takes O(n + b), where n is the number of elements and b is the number of buckets.

◆ tommy_hashdyn_rehash_existing()

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.

Parameters
hashdynThe hashtable.
nodeThe node whose hash is updated.
hashThe new hash of the element.
Note
This operation is O(1).

◆ tommy_hashdyn_foreach()

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.

tommy_hashdyn hashdyn;
// initializes the hashtable
...
// creates an element
struct element* elem = malloc(sizeof(struct element));
...
// insert it in the hashtable
tommy_hashdyn_insert(&hashdyn, &elem->node, elem, tommy_inthash_u32(elem->value));
...
// deallocates all the elements iterating the hashtable
tommy_hashdyn_foreach(&hashdyn, free);
// deallocates the hashtable

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.

Parameters
hashdynThe hashtable.
funcFunction to call with each element.

◆ tommy_hashdyn_foreach_arg()

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.

Parameters
hashdynThe hashtable.
funcFunction to call with each element.
argArgument to pass to the function.

◆ tommy_hashdyn_count()

tommy_size_t tommy_hashdyn_count ( const tommy_hashdyn *  hashdyn)

Gets the number of elements.

Parameters
hashdynThe hashtable.
Returns
The number of elements in the hashtable.
Note
This operation is O(1).

◆ tommy_hashdyn_empty()

tommy_bool_t tommy_hashdyn_empty ( const tommy_hashdyn *  hashdyn)

Checks whether the hashtable is empty.

Parameters
hashdynThe hashtable.
Returns
Nonzero if the hashtable is empty, 0 otherwise.
Note
This operation is O(1).

◆ tommy_hashdyn_bucket_count()

tommy_size_t tommy_hashdyn_bucket_count ( const tommy_hashdyn *  hashdyn)

Gets the number of buckets.

Parameters
hashdynThe hashtable.
Returns
The number of buckets.
Note
This operation is O(1).

◆ tommy_hashdyn_memory_usage()

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.

Parameters
hashdynThe hashtable.
Returns
Total memory allocated by the hashtable, in bytes.

◆ tommy_hashdyn_to_list()

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 hashtable is left empty and initialized, preserving its allocated buckets
  • the target list contains all the elements that were previously in the hashtable

The tommy_node::data and tommy_node::index fields are left unchanged.

This function is useful when you need to:

  • extract all elements to process/sort them outside the hashtable
  • convert the hashtable into a list for sequential iteration
  • prepare for a full clear + re-insertion with different hash/ordering
  • move ownership of the nodes to a list-based container

Typical usage pattern:

tommy_list all_elements;
tommy_list_init(&all_elements);
// move everything out of the hashtable into the list
tommy_hashdyn_to_list(&hashdyn, &all_elements);
// now you can sort, filter, process sequentially, etc.
tommy_list_sort(&all_elements, compare_by_value);
TOMMY_API void tommy_hashdyn_to_list(tommy_hashdyn *hashdyn, tommy_list *list)
Transfers all elements from the hashtable into a tommy_list.
void tommy_list_init(tommy_list *list)
Initializes the list.
Definition: tommylist.h:190
TOMMY_API void tommy_list_sort(tommy_list *list, tommy_compare_func *cmp)
Sorts a list.
Parameters
hashdynThe hashtable to drain.
listThe destination list. It must be initialized and must not share nodes with the hashtable. Existing elements remain at the head of the list.
Note
This operation is O(b), where b is the number of buckets.
No memory allocation or deallocation is performed.
The relative order of elements that were in the same bucket is preserved, but the order among different buckets is bucket-order dependent.