tommyhashdyn.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2010 Andrea Mazzoleni
3
157#ifndef TOMMY_HASHDYN_H
158#define TOMMY_HASHDYN_H
159
160#include "tommyhash.h"
161#include "tommylist.h"
162
163/******************************************************************************/
164/* hashdyn */
165
170#define TOMMY_HASHDYN_BIT 4
171
177
182typedef struct tommy_hashdyn_struct {
189
194TOMMY_API void tommy_hashdyn_init(tommy_hashdyn* hashdyn);
195
203TOMMY_API void tommy_hashdyn_done(tommy_hashdyn* hashdyn);
204
214tommy_inline void tommy_hashdyn_swap(tommy_hashdyn* first, tommy_hashdyn* second)
215{
216 tommy_hashdyn tmp = *first;
217 *first = *second;
218 *second = tmp;
219}
220
232TOMMY_API void tommy_hashdyn_clear(tommy_hashdyn* hashdyn);
233
244TOMMY_API void tommy_hashdyn_reserve(tommy_hashdyn* hashdyn, tommy_size_t count);
245
257TOMMY_API void tommy_hashdyn_shrink(tommy_hashdyn* hashdyn);
258
266TOMMY_API void tommy_hashdyn_insert(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node, void* data, tommy_hash_t hash);
267
284TOMMY_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);
285
298TOMMY_API void* tommy_hashdyn_remove(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash);
299
310{
311 return hashdyn->bucket[hash & hashdyn->bucket_mask];
312}
313
325tommy_inline tommy_hashdyn_node* tommy_hashdyn_find(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
326{
327 tommy_hashdyn_node* i = tommy_hashdyn_bucket(hashdyn, hash);
328
329 while (i) {
330 /* we first check if the hash matches, as in the same bucket we may have multiple hash values */
331 if (i->index == hash && cmp(cmp_arg, i->data) == 0)
332 return i;
333 i = i->next;
334 }
335 return 0;
336}
337
349tommy_inline void* tommy_hashdyn_search(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
350{
351 tommy_hashdyn_node* node = tommy_hashdyn_find(hashdyn, cmp, cmp_arg, hash);
352
353 return node ? node->data : 0;
354}
355
364
376
388
401
414
431
470
480TOMMY_API void tommy_hashdyn_foreach_arg(tommy_hashdyn* hashdyn, tommy_foreach_arg_func* func, void* arg);
481
489{
490 return hashdyn->count;
491}
492
500{
501 return hashdyn->count == 0;
502}
503
511{
512 return hashdyn->bucket_max;
513}
514
522
562TOMMY_API void tommy_hashdyn_to_list(tommy_hashdyn* hashdyn, tommy_list* list);
563
564#endif
565
Hashtable container type.
Definition: tommyhashdyn.h:182
tommy_size_t count
Number of elements.
Definition: tommyhashdyn.h:186
tommy_uint_t bucket_bit
Bits used in the bit mask.
Definition: tommyhashdyn.h:187
tommy_hashdyn_node ** bucket
Hash buckets.
Definition: tommyhashdyn.h:183
tommy_size_t bucket_mask
Bit mask to access the buckets.
Definition: tommyhashdyn.h:185
tommy_size_t bucket_max
Number of buckets.
Definition: tommyhashdyn.h:184
Data structure node.
Definition: tommytypes.h:242
tommy_size_t index
Index of the node.
Definition: tommytypes.h:269
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:247
void * data
Pointer to the element containing the node.
Definition: tommytypes.h:260
Hash functions for use with tommy_hashtable, tommy_hashdyn and tommy_hashlin.
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
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.
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_tail(tommy_hashdyn *hashdyn)
Gets the last node in iteration order, or 0 if empty.
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.
void tommy_hashdyn_swap(tommy_hashdyn *first, tommy_hashdyn *second)
Exchanges the complete state of two initialized hashtables, including bucket allocations.
Definition: tommyhashdyn.h:214
TOMMY_API void tommy_hashdyn_shrink(tommy_hashdyn *hashdyn)
Shrinks the allocated buckets to fit the current number of elements.
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.
TOMMY_API tommy_size_t tommy_hashdyn_memory_usage(const tommy_hashdyn *hashdyn)
Gets the size of allocated memory.
tommy_bool_t tommy_hashdyn_empty(const tommy_hashdyn *hashdyn)
Checks whether the hashtable is empty.
Definition: tommyhashdyn.h:499
TOMMY_API void tommy_hashdyn_to_list(tommy_hashdyn *hashdyn, tommy_list *list)
Transfers all elements from the hashtable into a tommy_list.
tommy_hashdyn_node * tommy_hashdyn_bucket(tommy_hashdyn *hashdyn, tommy_hash_t hash)
Gets the bucket of the specified hash.
Definition: tommyhashdyn.h:309
TOMMY_API void tommy_hashdyn_clear(tommy_hashdyn *hashdyn)
Removes all elements, preserving the allocated buckets.
TOMMY_API void tommy_hashdyn_foreach(tommy_hashdyn *hashdyn, tommy_foreach_func *func)
Calls the specified function for each element in the hashtable.
TOMMY_API void tommy_hashdyn_done(tommy_hashdyn *hashdyn)
Deinitializes the hashtable.
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.
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_size_t tommy_hashdyn_count(const tommy_hashdyn *hashdyn)
Gets the number of elements.
Definition: tommyhashdyn.h:488
TOMMY_API void * tommy_hashdyn_remove_existing(tommy_hashdyn *hashdyn, tommy_hashdyn_node *node)
Removes an element from the hashtable.
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.
tommy_node tommy_hashdyn_node
Hashtable node.
Definition: tommyhashdyn.h:176
TOMMY_API void tommy_hashdyn_reserve(tommy_hashdyn *hashdyn, tommy_size_t count)
Pre-allocates buckets for the specified number of elements.
struct tommy_hashdyn_struct tommy_hashdyn
Hashtable container type.
TOMMY_API void tommy_hashdyn_init(tommy_hashdyn *hashdyn)
Initializes the hashtable.
tommy_size_t tommy_hashdyn_bucket_count(const tommy_hashdyn *hashdyn)
Gets the number of buckets.
Definition: tommyhashdyn.h:510
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.
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.
Definition: tommyhashdyn.h:325
Doubly linked list for collisions into hashtables.
void tommy_foreach_func(void *elem)
Foreach function.
Definition: tommytypes.h:346
int tommy_search_func(const void *arg, const void *elem)
Search function for elements.
Definition: tommytypes.h:334
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:40
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
tommy_size_t tommy_hash_t
Type used in hashtables to store the hash of an element.
Definition: tommytypes.h:224
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:61
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53