LCOV - code coverage report
Current view: top level - tommyds/tommyds - tommyhashlin.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 30 30 100.0 %
Date: 2026-11-12 12:42:49 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-2-Clause
       2             : // Copyright (C) 2010 Andrea Mazzoleni
       3             : 
       4             : /** \file
       5             :  * Linear chained hashtable.
       6             :  *
       7             :  * This hashtable resizes dynamically and progressively using a variation of the
       8             :  * linear hashing algorithm described in https://en.wikipedia.org/wiki/Linear_hashing
       9             :  *
      10             :  * It starts with the minimal size of 64 buckets, it doubles the size when it
      11             :  * reaches a load factor greater than 0.5, and it halves the size when the load
      12             :  * factor is lower than 0.125.
      13             :  *
      14             :  * The progressive resize spreads resizing work across insert and delete
      15             :  * operations, reducing pauses caused by resizing the entire table at once.
      16             :  * Resizing may traverse a whole bucket, so many collisions can still make
      17             :  * an individual operation expensive.
      18             :  *
      19             :  * For resizing, a dynamic array that supports access to non-contiguous
      20             :  * segments is used.
      21             :  * In this way, we only allocate additional table segments on the heap, without
      22             :  * freeing the previous table, and thus not increasing the heap fragmentation.
      23             :  *
      24             :  * The resize takes place inside tommy_hashlin_insert() and tommy_hashlin_remove().
      25             :  * No resize is done in the tommy_hashlin_search() operation.
      26             :  *
      27             :  * To initialize the hashtable you have to call tommy_hashlin_init().
      28             :  *
      29             :  * \code
      30             :  * tommy_hashlin hashlin;
      31             :  *
      32             :  * tommy_hashlin_init(&hashlin);
      33             :  * \endcode
      34             :  *
      35             :  * To insert elements in the hashtable you have to call tommy_hashlin_insert() for
      36             :  * each element.
      37             :  * In the insertion call you have to specify the address of the node, the
      38             :  * address of the element, and the hash value of the key to use.
      39             :  * The address of the element is used to initialize the tommy_node::data field
      40             :  * of the node, and the hash to initialize the tommy_node::index field.
      41             :  *
      42             :  * \code
      43             :  * struct element {
      44             :  *     int value;
      45             :  *     // other fields
      46             :  *     tommy_node node;
      47             :  * };
      48             :  *
      49             :  * struct element* elem = malloc(sizeof(struct element)); // creates the element
      50             :  *
      51             :  * elem->value = ...; // initializes the element
      52             :  *
      53             :  * tommy_hashlin_insert(&hashlin, &elem->node, elem, tommy_inthash_u32(elem->value)); // inserts the element
      54             :  * \endcode
      55             :  *
      56             :  * To find an element in the hashtable you have to call tommy_hashlin_search()
      57             :  * providing a comparison function, its argument, and the hash of the key to search.
      58             :  *
      59             :  * \code
      60             :  * int compare(const void* arg, const void* elem)
      61             :  * {
      62             :  *     return *(const int*)arg != ((const struct element*)elem)->value;
      63             :  * }
      64             :  *
      65             :  * int value_to_find = 1;
      66             :  * struct element* elem = tommy_hashlin_search(&hashlin, compare, &value_to_find, tommy_inthash_u32(value_to_find));
      67             :  * if (!elem) {
      68             :  *     // not found
      69             :  * } else {
      70             :  *     // found
      71             :  * }
      72             :  * \endcode
      73             :  *
      74             :  * To iterate over all the elements in the hashtable with the same key, you have to
      75             :  * use tommy_hashlin_bucket() and follow the tommy_node::next pointer until 0.
      76             :  * You have also to check explicitly for the key, as the bucket may contain
      77             :  * different keys.
      78             :  *
      79             :  * \code
      80             :  * int value_to_find = 1;
      81             :  * tommy_node* i = tommy_hashlin_bucket(&hashlin, tommy_inthash_u32(value_to_find));
      82             :  * while (i) {
      83             :  *     struct element* elem = i->data; // gets the element pointer
      84             :  *
      85             :  *     if (elem->value == value_to_find) {
      86             :  *         printf("%d\n", elem->value); // process the element
      87             :  *     }
      88             :  *
      89             :  *     i = i->next; // goes to the next element
      90             :  * }
      91             :  * \endcode
      92             :  *
      93             :  * To remove an element from the hashtable you have to call tommy_hashlin_remove()
      94             :  * providing a comparison function, its argument, and the hash of the key to search
      95             :  * and remove.
      96             :  *
      97             :  * \code
      98             :  * struct element* elem = tommy_hashlin_remove(&hashlin, compare, &value_to_remove, tommy_inthash_u32(value_to_remove));
      99             :  * if (elem) {
     100             :  *     free(elem); // frees the element allocated memory
     101             :  * }
     102             :  * \endcode
     103             :  *
     104             :  * To destroy the hashtable you have to deinitialize it calling tommy_hashlin_done().
     105             :  * Elements can still be contained when calling this function, but their memory
     106             :  * is not freed by it and must be managed separately (for example, freed with
     107             :  * tommy_hashlin_foreach()).
     108             :  *
     109             :  * \code
     110             :  * tommy_hashlin_done(&hashlin);
     111             :  * \endcode
     112             :  *
     113             :  * If you need to iterate over all the elements in the hashtable, you can use
     114             :  * tommy_hashlin_foreach() or tommy_hashlin_foreach_arg().
     115             :  * For traversal that permits insertions and removals, use tommy_hashlin_head()
     116             :  * and tommy_hashlin_next(), or tommy_hashlin_tail() and tommy_hashlin_prev().
     117             :  * Hashes are compared from the least significant bit to the most significant
     118             :  * bit, with 0 before 1. Equal hashes are visited in insertion order.
     119             :  * This order is independent of the number of buckets and progressive resizing.
     120             :  * It differs from the bucket order used by foreach and to_list.
     121             :  *
     122             :  * \anchor tommy_hashlin_iterator_validity
     123             :  * Iterators are pointers to nodes currently contained in the hashtable.
     124             :  * Inserting elements or removing other nodes does not invalidate them or
     125             :  * change the relative iteration order of the remaining elements.
     126             :  * A forward or backward traversal visits every original element that remains
     127             :  * contained, even if removals cause buckets to merge or insertions split them.
     128             :  * New elements can be visited if inserted after the current position in the
     129             :  * traversal direction; elements inserted before it need not be visited.
     130             :  *
     131             :  * Removing the pointed node invalidates its use as an iterator, even if its
     132             :  * storage has not been freed. Obtain the next or previous node before removal,
     133             :  * and keep that saved node contained until continuing the traversal.
     134             :  *
     135             :  * \code
     136             :  * tommy_hashlin_node* i = tommy_hashlin_head(&hashlin);
     137             :  * while (i) {
     138             :  *     tommy_hashlin_node* next = tommy_hashlin_next(&hashlin, i);
     139             :  *     if (should_remove(i->data)) {
     140             :  *         free(tommy_hashlin_remove_existing(&hashlin, i));
     141             :  *     }
     142             :  *     i = next;
     143             :  * }
     144             :  * \endcode
     145             :  *
     146             :  * Changing a hash with tommy_hashlin_rehash_existing() can change iteration
     147             :  * order, so the traversal guarantee does not cover rehashing during traversal.
     148             :  * After swapping containers, use the container that now contains the node.
     149             :  * Clearing or deinitializing the container invalidates all its iterators.
     150             :  * Concurrent traversal and modification require external synchronization.
     151             :  *
     152             :  * Iterator calls scan bucket contents without allocating or modifying memory.
     153             :  * With a good hash function and the table's low load factor, a full traversal
     154             :  * runs in expected linear O(n) time.
     155             :  * In pathological cases with severe hash collisions, traversal degrades to
     156             :  * quadratic O(n^2) time.
     157             :  * Use foreach for guaranteed linear traversal when no insertions or removals
     158             :  * are needed.
     159             :  */
     160             : 
     161             : #ifndef TOMMY_HASHLIN_H
     162             : #define TOMMY_HASHLIN_H
     163             : 
     164             : #include "tommyhash.h"
     165             : #include "tommylist.h"
     166             : 
     167             : /******************************************************************************/
     168             : /* hashlin */
     169             : 
     170             : /** \internal
     171             :  * Initial and minimal size of the hashtable expressed as a power of 2.
     172             :  * The initial size is 2^TOMMY_HASHLIN_BIT.
     173             :  */
     174             : #define TOMMY_HASHLIN_BIT 6
     175             : 
     176             : /**
     177             :  * Hashtable node.
     178             :  * This is the node that you have to include inside your elements.
     179             :  */
     180             : typedef tommy_node tommy_hashlin_node;
     181             : 
     182             : /**
     183             :  * Hashtable container type.
     184             :  * \note Don't use internal fields directly, but access the container only using functions.
     185             :  */
     186             : typedef struct tommy_hashlin_struct {
     187             :         tommy_hashlin_node** bucket[TOMMY_SIZE_BIT]; /**< Dynamic array of hash buckets. One list for each hash modulus. */
     188             :         tommy_size_t bucket_max; /**< Number of buckets. */
     189             :         tommy_size_t bucket_mask; /**< Bit mask to access the buckets. */
     190             :         tommy_size_t low_max; /**< Low order max value. */
     191             :         tommy_size_t low_mask; /**< Low order mask value. */
     192             :         tommy_size_t split; /**< Split position. */
     193             :         tommy_size_t count; /**< Number of elements. */
     194             :         tommy_uint_t bucket_bit; /**< Bits used in the bit mask. */
     195             :         tommy_uint_t state; /**< Reallocation state. */
     196             : } tommy_hashlin;
     197             : 
     198             : /**
     199             :  * Initializes the hashtable.
     200             :  * \param hashlin The hashtable.
     201             :  */
     202             : TOMMY_API void tommy_hashlin_init(tommy_hashlin* hashlin);
     203             : 
     204             : /**
     205             :  * Deinitializes the hashtable.
     206             :  *
     207             :  * You can call this function with elements still contained,
     208             :  * but such elements are not going to be freed by this call.
     209             :  * \param hashlin The hashtable.
     210             :  */
     211             : TOMMY_API void tommy_hashlin_done(tommy_hashlin* hashlin);
     212             : 
     213             : /**
     214             :  * Exchanges the complete state of two initialized hashtables, including bucket allocations and progressive resize state.
     215             :  * The hashtables must not share nodes. Nodes and elements are not accessed or modified.
     216             :  * Existing node pointers remain valid and belong to the other hashtable.
     217             :  * Passing the same hashtable twice has no effect. Both hashtables remain usable and deinitializable.
     218             :  * \param first The first hashtable.
     219             :  * \param second The second hashtable.
     220             :  * \note This operation is O(1) with respect to the number of elements.
     221             :  */
     222          34 : tommy_inline void tommy_hashlin_swap(tommy_hashlin* first, tommy_hashlin* second)
     223             : {
     224          34 :         tommy_hashlin tmp = *first;
     225          34 :         *first = *second;
     226          34 :         *second = tmp;
     227          34 : }
     228             : 
     229             : /**
     230             :  * Removes all elements and resets the hashtable to its initial state.
     231             :  * As after tommy_hashlin_init(), it can be reused immediately.
     232             :  * Resetting the hashtable is intentional. Keeping a large empty table
     233             :  * would make subsequent removals expensive, as progressive shrinking could
     234             :  * require scanning many empty buckets.
     235             :  * Any pending resize is canceled. No new memory is allocated.
     236             :  * Elements are not freed and nodes are not accessed or modified.
     237             :  * Their links must not be used to traverse the previous contents.
     238             :  * You can call this function after tommy_hashlin_foreach() has freed the elements.
     239             :  * Subsequent insertions and removals retain the normal resizing policy.
     240             :  * \param hashlin The hashtable.
     241             :  * \note This operation is O(m + log b), where m is the initial number of buckets
     242             :  * and b is the number of allocated buckets before the call. Nodes are not traversed.
     243             :  */
     244             : TOMMY_API void tommy_hashlin_clear(tommy_hashlin* hashlin);
     245             : 
     246             : /**
     247             :  * Inserts an element in the hashtable.
     248             :  * \param hashlin The hashtable.
     249             :  * \param node Pointer to the node embedded into the element to insert.
     250             :  * \param data Pointer to the element to insert.
     251             :  * \param hash Hash of the element to insert.
     252             :  */
     253             : TOMMY_API void tommy_hashlin_insert(tommy_hashlin* hashlin, tommy_hashlin_node* node, void* data, tommy_hash_t hash);
     254             : 
     255             : /**
     256             :  * Inserts an element only if no equal element is already contained.
     257             :  * If found, the first equal element is returned,
     258             :  * and the hashtable and candidate node are left unchanged.
     259             :  * Otherwise, the candidate is inserted using the normal insertion policy,
     260             :  * and data is returned.
     261             :  * Elements are not freed by this call.
     262             :  * \param hashlin The hashtable.
     263             :  * \param node The candidate node. It must not belong to any container.
     264             :  * \param data Pointer to the element to insert.
     265             :  * \param cmp Compare function called with cmp_arg as first argument and with the element to compare as a second one.
     266             :  * The function should return 0 for equal elements, non-zero for different elements.
     267             :  * \param cmp_arg Compare argument describing the candidate key.
     268             :  * \param hash Hash of the candidate key, consistent with the comparison function.
     269             :  * \return The first equal element, or data if the candidate was inserted.
     270             :  */
     271             : TOMMY_API void* tommy_hashlin_insert_unique(tommy_hashlin* hashlin, tommy_hashlin_node* node, void* data, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash);
     272             : 
     273             : /**
     274             :  * Searches and removes an element from the hashtable.
     275             :  * You have to provide a compare function and the hash of the element you want to remove.
     276             :  * If the element is not found, 0 is returned.
     277             :  * If more equal elements are present, the first one is removed.
     278             :  * \param hashlin The hashtable.
     279             :  * \param cmp Compare function called with cmp_arg as first argument and with the element to compare as a second one.
     280             :  * The function should return 0 for equal elements, non-zero for different elements.
     281             :  * \param cmp_arg Compare argument passed as first argument of the compare function.
     282             :  * \param hash Hash of the element to find and remove.
     283             :  * \return The removed element, or 0 if not found.
     284             :  */
     285             : TOMMY_API void* tommy_hashlin_remove(tommy_hashlin* hashlin, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash);
     286             : 
     287             : /** \internal
     288             :  * Returns the bucket at the specified position.
     289             :  */
     290    18410901 : tommy_inline tommy_hashlin_node** tommy_hashlin_pos(tommy_hashlin* hashlin, tommy_hash_t pos)
     291             : {
     292             :         tommy_uint_t bsr;
     293             : 
     294             :         /* get the highest bit set, in case of all 0, return 0 */
     295    18410901 :         bsr = tommy_ilog2(pos | 1);
     296             : 
     297    18410901 :         return &hashlin->bucket[bsr][pos];
     298             : }
     299             : 
     300             : /** \internal
     301             :  * Returns a pointer to the bucket of the specified hash.
     302             :  */
     303    11753051 : tommy_inline tommy_hashlin_node** tommy_hashlin_bucket_ref(tommy_hashlin* hashlin, tommy_hash_t hash)
     304             : {
     305    11753051 :         tommy_size_t pos = hash & hashlin->low_mask;
     306    11753051 :         tommy_size_t high_pos = hash & hashlin->bucket_mask;
     307             : 
     308             :         /* if this position is already allocated in the high half */
     309    11753051 :         if (pos < hashlin->split) {
     310             :                 /* the following assignment is expected to be implemented */
     311             :                 /* with a conditional move instruction */
     312             :                 /* that results in a little better and constant performance */
     313             :                 /* regardless of the split position. */
     314             :                 /* this affects mostly the worst case, when the split value */
     315             :                 /* is near at its half, resulting in a totally unpredictable */
     316             :                 /* condition by the CPU. */
     317             :                 /* in such case, the use of the conditional move is generally faster. */
     318             : 
     319             :                 /* use also the high bit */
     320     1427629 :                 pos = high_pos;
     321             :         }
     322             : 
     323    11753051 :         return tommy_hashlin_pos(hashlin, pos);
     324             : }
     325             : 
     326             : /**
     327             :  * Gets the bucket of the specified hash.
     328             :  * The bucket is guaranteed to contain ALL the elements with the specified hash,
     329             :  * but it can also contain others.
     330             :  * You can access elements in the bucket following the tommy_node::next pointer until 0.
     331             :  * \param hashlin The hashtable.
     332             :  * \param hash Hash of the element to find.
     333             :  * \return The head of the bucket, or 0 if empty.
     334             :  */
     335       64957 : tommy_inline tommy_hashlin_node* tommy_hashlin_bucket(tommy_hashlin* hashlin, tommy_hash_t hash)
     336             : {
     337       64957 :         return *tommy_hashlin_bucket_ref(hashlin, hash);
     338             : }
     339             : 
     340             : /**
     341             :  * Finds a node in the hashtable.
     342             :  * You have to provide a compare function and the hash of the element you want to find.
     343             :  * If more equal elements are present, the first one is returned.
     344             :  * \param hashlin The hashtable.
     345             :  * \param cmp Compare function called with cmp_arg as first argument and with the element to compare as a second one.
     346             :  * The function should return 0 for equal elements, non-zero for different elements.
     347             :  * \param cmp_arg Compare argument passed as first argument of the compare function.
     348             :  * \param hash Hash of the element to find.
     349             :  * \return The first node found, or 0 if none.
     350             :  */
     351       53431 : tommy_inline tommy_hashlin_node* tommy_hashlin_find(tommy_hashlin* hashlin, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
     352             : {
     353       53431 :         tommy_hashlin_node* i = tommy_hashlin_bucket(hashlin, hash);
     354             : 
     355     1597328 :         while (i) {
     356             :                 /* we first check if the hash matches, as in the same bucket we may have multiple hash values */
     357     1587889 :                 if (i->index == hash && cmp(cmp_arg, i->data) == 0)
     358       43992 :                         return i;
     359     1543897 :                 i = i->next;
     360             :         }
     361        9439 :         return 0;
     362             : }
     363             : 
     364             : /**
     365             :  * Searches an element in the hashtable.
     366             :  * You have to provide a compare function and the hash of the element you want to find.
     367             :  * If more equal elements are present, the first one is returned.
     368             :  * \param hashlin The hashtable.
     369             :  * \param cmp Compare function called with cmp_arg as first argument and with the element to compare as a second one.
     370             :  * The function should return 0 for equal elements, non-zero for different elements.
     371             :  * \param cmp_arg Compare argument passed as first argument of the compare function.
     372             :  * \param hash Hash of the element to find.
     373             :  * \return The first element found, or 0 if none.
     374             :  */
     375       46733 : tommy_inline void* tommy_hashlin_search(tommy_hashlin* hashlin, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
     376             : {
     377       46733 :         tommy_hashlin_node* node = tommy_hashlin_find(hashlin, cmp, cmp_arg, hash);
     378             : 
     379       46733 :         return node ? node->data : 0;
     380             : }
     381             : 
     382             : /**
     383             :  * Removes an element from the hashtable.
     384             :  * You must already have the address of the node to remove.
     385             :  * \param hashlin The hashtable.
     386             :  * \param node The node to remove.
     387             :  * \return The removed element.
     388             :  */
     389             : TOMMY_API void* tommy_hashlin_remove_existing(tommy_hashlin* hashlin, tommy_hashlin_node* node);
     390             : 
     391             : /**
     392             :  * Gets the first node in iteration order, or 0 if empty.
     393             :  * See \ref tommy_hashlin_iterator_validity for ordering and iterator validity.
     394             :  * \param hashlin The hashtable.
     395             :  * \return The first node in iteration order, or 0 if empty.
     396             :  * \note This operation is expected O(1) under suitable hash distribution and
     397             :  * normal dynamic resizing. In the worst case, scanning bucket contents and
     398             :  * empty buckets takes O(n + b), where n is the number of elements and b is
     399             :  * the number of buckets.
     400             :  */
     401             : TOMMY_API tommy_hashlin_node* tommy_hashlin_head(tommy_hashlin* hashlin);
     402             : 
     403             : /**
     404             :  * Gets the last node in iteration order, or 0 if empty.
     405             :  * See \ref tommy_hashlin_iterator_validity for ordering and iterator validity.
     406             :  * \param hashlin The hashtable.
     407             :  * \return The last node in iteration order, or 0 if empty.
     408             :  * \note This operation is expected O(1) under suitable hash distribution and
     409             :  * normal dynamic resizing. In the worst case, scanning bucket contents and
     410             :  * empty buckets takes O(n + b), where n is the number of elements and b is
     411             :  * the number of buckets.
     412             :  */
     413             : TOMMY_API tommy_hashlin_node* tommy_hashlin_tail(tommy_hashlin* hashlin);
     414             : 
     415             : /**
     416             :  * Gets the following node in iteration order, or 0 for the tail.
     417             :  * See \ref tommy_hashlin_iterator_validity for ordering and iterator validity.
     418             :  * \param hashlin The hashtable.
     419             :  * \param node Node currently contained in the hashtable. It must not be 0.
     420             :  * \return The following node in iteration order, or 0 for the tail.
     421             :  * \note This operation is expected O(1) under suitable hash distribution and
     422             :  * normal dynamic resizing. In the worst case, scanning bucket contents and
     423             :  * empty buckets takes O(n + b), where n is the number of elements and b is
     424             :  * the number of buckets.
     425             :  */
     426             : TOMMY_API tommy_hashlin_node* tommy_hashlin_next(tommy_hashlin* hashlin, tommy_hashlin_node* node);
     427             : 
     428             : /**
     429             :  * Gets the preceding node in iteration order, or 0 for the head.
     430             :  * See \ref tommy_hashlin_iterator_validity for ordering and iterator validity.
     431             :  * \param hashlin The hashtable.
     432             :  * \param node Node currently contained in the hashtable. It must not be 0.
     433             :  * \return The preceding node in iteration order, or 0 for the head.
     434             :  * \note This operation is expected O(1) under suitable hash distribution and
     435             :  * normal dynamic resizing. In the worst case, scanning bucket contents and
     436             :  * empty buckets takes O(n + b), where n is the number of elements and b is
     437             :  * the number of buckets.
     438             :  */
     439             : TOMMY_API tommy_hashlin_node* tommy_hashlin_prev(tommy_hashlin* hashlin, tommy_hashlin_node* node);
     440             : 
     441             : /**
     442             :  * Updates the hash of an element already contained in the hashtable.
     443             :  * The node must belong to this hashtable. The caller updates the element key
     444             :  * and provides its new hash, without modifying tommy_node::index directly.
     445             :  * If the hash is unchanged, the node and its position are left unchanged.
     446             :  * Otherwise, the node is moved to the tail of the destination bucket,
     447             :  * even if the old and new hashes identify the same bucket.
     448             :  * The tommy_node::data field and the number of elements are left unchanged.
     449             :  * No memory allocation, deallocation or resize is performed.
     450             :  * The progressive resize state is left unchanged.
     451             :  * Equal keys are allowed; no uniqueness check is performed.
     452             :  * \param hashlin The hashtable.
     453             :  * \param node The node whose hash is updated.
     454             :  * \param hash The new hash of the element.
     455             :  * \note This operation is O(1).
     456             :  */
     457             : TOMMY_API void tommy_hashlin_rehash_existing(tommy_hashlin* hashlin, tommy_hashlin_node* node, tommy_hash_t hash);
     458             : 
     459             : /**
     460             :  * Calls the specified function for each element in the hashtable.
     461             :  *
     462             :  * You cannot add or remove elements from the inside of the callback,
     463             :  * but can use it to deallocate them.
     464             :  *
     465             :  * \code
     466             :  * tommy_hashlin hashlin;
     467             :  *
     468             :  * // initializes the hashtable
     469             :  * tommy_hashlin_init(&hashlin);
     470             :  *
     471             :  * ...
     472             :  *
     473             :  * // creates an element
     474             :  * struct element* elem = malloc(sizeof(struct element));
     475             :  *
     476             :  * ...
     477             :  *
     478             :  * // insert it in the hashtable
     479             :  * tommy_hashlin_insert(&hashlin, &elem->node, elem, tommy_inthash_u32(elem->value));
     480             :  *
     481             :  * ...
     482             :  *
     483             :  * // deallocates all the elements iterating the hashtable
     484             :  * tommy_hashlin_foreach(&hashlin, free);
     485             :  *
     486             :  * // deallocates the hashtable
     487             :  * tommy_hashlin_done(&hashlin);
     488             :  * \endcode
     489             :  *
     490             :  * If the callback frees the elements, the hashtable retains dangling
     491             :  * node pointers. Call tommy_hashlin_clear() before reusing it, or
     492             :  * tommy_hashlin_done() to release its storage.
     493             :  * \param hashlin The hashtable.
     494             :  * \param func Function to call with each element.
     495             :  */
     496             : TOMMY_API void tommy_hashlin_foreach(tommy_hashlin* hashlin, tommy_foreach_func* func);
     497             : 
     498             : /**
     499             :  * Calls the specified function with an argument for each element in the hashtable.
     500             :  * The iteration order and callback rules are the same as tommy_hashlin_foreach().
     501             :  * The callback may deallocate the current element.
     502             :  * Adding or removing elements from inside the callback is not allowed.
     503             :  * \param hashlin The hashtable.
     504             :  * \param func Function to call with each element.
     505             :  * \param arg Argument to pass to the function.
     506             :  */
     507             : TOMMY_API void tommy_hashlin_foreach_arg(tommy_hashlin* hashlin, tommy_foreach_arg_func* func, void* arg);
     508             : 
     509             : /**
     510             :  * Gets the number of elements.
     511             :  * \param hashlin The hashtable.
     512             :  * \return The number of elements in the hashtable.
     513             :  * \note This operation is O(1).
     514             :  */
     515       40323 : tommy_inline tommy_size_t tommy_hashlin_count(const tommy_hashlin* hashlin)
     516             : {
     517       40323 :         return hashlin->count;
     518             : }
     519             : 
     520             : /**
     521             :  * Checks whether the hashtable is empty.
     522             :  * \param hashlin The hashtable.
     523             :  * \return Nonzero if the hashtable is empty, 0 otherwise.
     524             :  * \note This operation is O(1).
     525             :  */
     526          22 : tommy_inline tommy_bool_t tommy_hashlin_empty(const tommy_hashlin* hashlin)
     527             : {
     528          22 :         return hashlin->count == 0;
     529             : }
     530             : 
     531             : /**
     532             :  * Gets the number of active buckets.
     533             :  * During a progressive resize, this can be less than the number of allocated buckets.
     534             :  * \param hashlin The hashtable.
     535             :  * \return The number of active buckets.
     536             :  * \note This operation is O(1).
     537             :  */
     538             : tommy_inline tommy_size_t tommy_hashlin_bucket_count(const tommy_hashlin* hashlin)
     539             : {
     540             :         return hashlin->low_max + hashlin->split;
     541             : }
     542             : 
     543             : /**
     544             :  * Gets the size of allocated memory.
     545             :  * It includes the size of the ::tommy_hashlin_node of the stored elements.
     546             :  * \param hashlin The hashtable.
     547             :  * \return Total memory allocated by the hashtable, in bytes.
     548             :  */
     549             : TOMMY_API tommy_size_t tommy_hashlin_memory_usage(const tommy_hashlin* hashlin);
     550             : 
     551             : /**
     552             :  * Transfers all elements from the hashtable into a tommy_list.
     553             :  *
     554             :  * Removes every element from the \p hashlin hashtable and inserts them
     555             :  * into the provided \p list (at the tail), preserving the per-bucket order
     556             :  * but not guaranteeing any particular global order.
     557             :  * Buckets are visited by increasing active bucket index.
     558             :  *
     559             :  * After the call:
     560             :  * - the hashtable is left in the same state as after tommy_hashlin_init()
     561             :  * - the target list contains all the elements that were previously in the hashtable
     562             :  *
     563             :  * The tommy_node::data and tommy_node::index fields are left unchanged.
     564             :  *
     565             :  * This function is useful when you need to:
     566             :  * - extract all elements to process/sort them outside the hashtable
     567             :  * - convert the hashtable into a list for sequential iteration
     568             :  * - prepare for a full clear + re-insertion with different hash/ordering
     569             :  * - move ownership of the nodes to a list-based container
     570             :  *
     571             :  * Typical usage pattern:
     572             :  * \code
     573             :  * tommy_list all_elements;
     574             :  * tommy_list_init(&all_elements);
     575             :  *
     576             :  * // move everything out of the hashtable into the list
     577             :  * tommy_hashlin_to_list(&hashlin, &all_elements);
     578             :  *
     579             :  * // now you can sort, filter, process sequentially, etc.
     580             :  * tommy_list_sort(&all_elements, compare_by_value);
     581             :  * \endcode
     582             :  *
     583             :  * \param hashlin The hashtable to drain.
     584             :  * \param list The destination list. It must be initialized and must not share
     585             :  * nodes with the hashtable. Existing elements remain at the head of the list.
     586             :  * \note This operation is O(b), where b is the number of active buckets before the call.
     587             :  * \note No new memory is allocated. Additional bucket segments are deallocated.
     588             :  * \note Any pending resize is canceled. The hashtable and list can be reused immediately.
     589             :  * \note The relative order of elements that were in the same bucket is preserved,
     590             :  *       but the order among different buckets is bucket-order dependent.
     591             :  */
     592             : TOMMY_API void tommy_hashlin_to_list(tommy_hashlin* hashlin, tommy_list* list);
     593             : 
     594             : #endif
     595             : 

Generated by: LCOV version 1.0