Data Structures | Typedefs | Functions
tommyhashtbl.h File Reference

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

Detailed Description

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.

tommy_hashtable hashtable;
tommy_hashtable_init(&hashtable, 1024);
Hashtable container type.
Definition: tommyhashtbl.h:162
TOMMY_API void tommy_hashtable_init(tommy_hashtable *hashtable, tommy_size_t bucket_max)
Initializes the hashtable.

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.

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_hashtable_insert(&hashtable, &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_hashtable_insert(tommy_hashtable *hashtable, tommy_hashtable_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_hashtable_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_hashtable_search(&hashtable, compare, &value_to_find, tommy_inthash_u32(value_to_find));
if (!elem) {
// not found
} else {
// found
}
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.
Definition: tommyhashtbl.h:303

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.

tommy_node* i = tommy_hashtable_bucket(&hashtable, 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_hashtable_node * tommy_hashtable_bucket(tommy_hashtable *hashtable, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashtbl.h:263

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.

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

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

TOMMY_API void tommy_hashtable_done(tommy_hashtable *hashtable)
Deinitializes the hashtable.

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.

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

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 Documentation

◆ tommy_hashtable_node

Hashtable node.

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

◆ tommy_hashtable

Hashtable container type.

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

Function Documentation

◆ tommy_hashtable_init()

TOMMY_API void tommy_hashtable_init ( tommy_hashtable *  hashtable,
tommy_size_t  bucket_max 
)

Initializes the hashtable.

Parameters
hashtableThe hashtable.
bucket_maxMinimum number of buckets to allocate. The effective number is rounded up to a power of 2, with a minimum of 16 buckets.

◆ tommy_hashtable_done()

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.

Parameters
hashtableThe hashtable.

◆ tommy_hashtable_swap()

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.

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

◆ tommy_hashtable_clear()

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.

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

◆ tommy_hashtable_insert()

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.

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

◆ tommy_hashtable_insert_unique()

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.

Parameters
hashtableThe 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_hashtable_remove()

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.

Parameters
hashtableThe 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_hashtable_bucket()

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.

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

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

Parameters
hashtableThe 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_hashtable_search()

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.

Parameters
hashtableThe 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_hashtable_remove_existing()

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.

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

◆ tommy_hashtable_head()

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.

Parameters
hashtableThe hashtable.
Returns
The first node in iteration order, or 0 if empty.
Note
This operation is expected O(1) under suitable occupancy and distribution assumptions. In the worst case, scanning empty buckets takes O(b), where b is the number of buckets.

◆ tommy_hashtable_tail()

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.

Parameters
hashtableThe hashtable.
Returns
The last node in iteration order, or 0 if empty.
Note
This operation is expected O(1) under suitable occupancy and distribution assumptions. In the worst case, scanning empty buckets takes O(b), where b is the number of buckets.

◆ tommy_hashtable_next()

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.

Parameters
hashtableThe 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 occupancy and distribution assumptions. In the worst case, scanning empty buckets takes O(b), where b is the number of buckets.

◆ tommy_hashtable_prev()

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.

Parameters
hashtableThe 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 occupancy and distribution assumptions. In the worst case, scanning empty buckets takes O(b), where b is the number of buckets.

◆ tommy_hashtable_rehash_existing()

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.

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

◆ tommy_hashtable_foreach()

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.

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

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.

Parameters
hashtableThe hashtable.
funcFunction to call with each element.

◆ tommy_hashtable_foreach_arg()

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.

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

◆ tommy_hashtable_count()

tommy_size_t tommy_hashtable_count ( const tommy_hashtable *  hashtable)

Gets the number of elements.

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

◆ tommy_hashtable_empty()

tommy_bool_t tommy_hashtable_empty ( const tommy_hashtable *  hashtable)

Checks whether the hashtable is empty.

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

◆ tommy_hashtable_bucket_count()

tommy_size_t tommy_hashtable_bucket_count ( const tommy_hashtable *  hashtable)

Gets the number of buckets.

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

◆ tommy_hashtable_memory_usage()

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.

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

◆ tommy_hashtable_to_list()

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 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_hashtable_to_list(&hashtable, &all_elements);
// now you can sort, filter, process sequentially, etc.
tommy_list_sort(&all_elements, compare_by_value);
TOMMY_API void tommy_hashtable_to_list(tommy_hashtable *hashtable, 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
hashtableThe 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.