Trie optimized for cache utilization. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_trie_struct |
| Trie container type. More... | |
Macros | |
| #define | TOMMY_TRIE_BIT 32 |
| Number of bits supported for trie keys. More... | |
| #define | TOMMY_TRIE_TREE_MAX (64 / sizeof(void*)) |
| Number of branches on each inner node. More... | |
| #define | TOMMY_TRIE_BLOCK_SIZE (TOMMY_TRIE_TREE_MAX * sizeof(void*)) |
| Trie block size. More... | |
Typedefs | |
| typedef tommy_node | tommy_trie_node |
| Trie node. More... | |
| typedef struct tommy_trie_struct | tommy_trie |
| Trie container type. More... | |
Functions | |
| TOMMY_API void | tommy_trie_init (tommy_trie *trie, tommy_allocator *alloc) |
| Initializes the trie. More... | |
| void | tommy_trie_swap (tommy_trie *first, tommy_trie *second) |
| Exchanges the complete state of two initialized tries, including their allocator references. More... | |
| TOMMY_API void | tommy_trie_clear (tommy_trie *trie) |
| Removes all elements, preserving the allocator reference. More... | |
| TOMMY_API void | tommy_trie_insert (tommy_trie *trie, tommy_trie_node *node, void *data, tommy_key_t key) |
| Inserts an element in the trie. More... | |
| TOMMY_API void * | tommy_trie_insert_unique (tommy_trie *trie, tommy_trie_node *node, void *data, tommy_key_t key) |
| Inserts an element only if no element with the same numeric key is already contained. More... | |
| TOMMY_API void * | tommy_trie_remove (tommy_trie *trie, tommy_key_t key) |
| Searches and removes the first element with the specified key. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_bucket (tommy_trie *trie, tommy_key_t key) |
| Gets the bucket of the specified key. More... | |
| tommy_trie_node * | tommy_trie_find (tommy_trie *trie, tommy_key_t key) |
| Finds the first node with the specified key. More... | |
| void * | tommy_trie_search (tommy_trie *trie, tommy_key_t key) |
| Searches an element in the trie. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_find_less (tommy_trie *trie, tommy_key_t key) |
| Finds the last node in key order with key strictly less than the specified one. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_find_less_equal (tommy_trie *trie, tommy_key_t key) |
| Finds the last node in key order with key less or equal than the specified one. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_find_greater_equal (tommy_trie *trie, tommy_key_t key) |
| Finds the first node in key order with key greater or equal than the specified one. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_find_greater (tommy_trie *trie, tommy_key_t key) |
| Finds the first node in key order with key strictly greater than the specified one. More... | |
| TOMMY_API void * | tommy_trie_search_less (tommy_trie *trie, tommy_key_t key) |
| Searches the last element in key order with key strictly less than the specified one. More... | |
| TOMMY_API void * | tommy_trie_search_less_equal (tommy_trie *trie, tommy_key_t key) |
| Searches the last element in key order with key less or equal than the specified one. More... | |
| TOMMY_API void * | tommy_trie_search_greater_equal (tommy_trie *trie, tommy_key_t key) |
| Searches the first element in key order with key greater or equal than the specified one. More... | |
| TOMMY_API void * | tommy_trie_search_greater (tommy_trie *trie, tommy_key_t key) |
| Searches the first element in key order with key strictly greater than the specified one. More... | |
| TOMMY_API void * | tommy_trie_remove_existing (tommy_trie *trie, tommy_trie_node *node) |
| Removes an element from the trie. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_head (tommy_trie *trie) |
| Gets the head (smallest key) node in the trie. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_tail (tommy_trie *trie) |
| Gets the tail (greatest key) node in the trie. More... | |
| void * | tommy_trie_remove_head (tommy_trie *trie) |
| Removes and returns the head (smallest key) element. More... | |
| void * | tommy_trie_remove_tail (tommy_trie *trie) |
| Removes and returns the tail (greatest key) element. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_next (tommy_trie *trie, tommy_trie_node *node) |
| Gets the node following the specified one by increasing numeric key. More... | |
| TOMMY_API tommy_trie_node * | tommy_trie_prev (tommy_trie *trie, tommy_trie_node *node) |
| Gets the node preceding the specified one by increasing numeric key. More... | |
| TOMMY_API void | tommy_trie_to_list (tommy_trie *trie, tommy_list *list) |
| Transfers all elements from the trie to the tail of a list by increasing numeric key. More... | |
| TOMMY_API void | tommy_trie_foreach (tommy_trie *trie, tommy_foreach_func *func) |
| Calls the specified function for each element in the trie. More... | |
| TOMMY_API void | tommy_trie_foreach_arg (tommy_trie *trie, tommy_foreach_arg_func *func, void *arg) |
| Calls the specified function with an argument for each element in the trie. More... | |
| tommy_size_t | tommy_trie_count (const tommy_trie *trie) |
| Gets the number of elements. More... | |
| tommy_bool_t | tommy_trie_empty (const tommy_trie *trie) |
| Checks whether the trie is empty. More... | |
| TOMMY_API tommy_size_t | tommy_trie_memory_usage (const tommy_trie *trie) |
| Gets the size of allocated memory. More... | |
Trie optimized for cache utilization.
This trie is a standard implementation that stores elements in the order defined by the key.
It needs an external allocator for the inner nodes in the trie.
You can control the number of branches of each node using the TOMMY_TRIE_TREE_MAX define. More branches imply more speed, but a bigger memory occupation.
Compared to tommy_trie_inplace you have to provide a tommy_allocator allocator. Note that the C malloc() is too slow to fulfill this role.
To initialize the trie you have to call tommy_allocator_init() to initialize the allocator, and tommy_trie_init() for the trie.
To insert elements in the trie you have to call tommy_trie_insert() for each element. In the insertion call you have to specify the address of the node, the address of the element, and the key value to use. The address of the element is used to initialize the tommy_node::data field of the node, and the key to initialize the tommy_node::index field.
To find an element in the trie you have to call tommy_trie_search() providing the key to search.
To find a key bound, use tommy_trie_search_less(), tommy_trie_search_less_equal(), tommy_trie_search_greater_equal(), or tommy_trie_search_greater(). Like the corresponding tommy_tree searches, the less searches return the last qualifying element in key and insertion order, and the greater searches return the first. They return the element pointer, or 0 if no key qualifies, and accept the same key range as tommy_trie_search().
To iterate over all the elements in the trie with the same key, you have to use tommy_trie_bucket() and follow the tommy_node::next pointer until 0.
To iterate over all elements by increasing key, start with tommy_trie_head() and advance with tommy_trie_next(). Equal keys are visited in insertion order. Use tommy_trie_tail() and tommy_trie_prev() for the reverse order.
Iterators are pointers to nodes contained in the trie. Inserting elements or removing other nodes between iterator calls does not invalidate pointers to nodes that remain in the trie. Subsequent next/prev calls use the current contents and ordering; inserted elements may become visible and removed elements are skipped.
Removing the pointed node invalidates its use as an iterator, even if its storage has not been freed. To continue traversal after removing the current node, obtain the next or previous node before removal.
Keys and fields used for ordering must not be modified while contained. Concurrent traversal and modification require external synchronization.
The container passed to next/prev must currently contain the node. After swapping containers, use the container that now owns the node.
To remove an element from the trie you have to call tommy_trie_remove() providing the key to search and remove.
To remove the smallest or greatest element, use tommy_trie_remove_head() or tommy_trie_remove_tail(). With equal keys, they remove the first or last element in insertion order, respectively, like the corresponding tommy_tree operations. They return the removed element, or 0 if empty.
To visit all the elements use tommy_trie_foreach() or tommy_trie_foreach_arg(). Elements are visited by increasing key, with equal keys in insertion order.
To destroy the trie you can deallocate all the elements with tommy_trie_foreach(), and deinitialize the allocator using tommy_allocator_done(). The allocator must no longer be used by other tries.
| #define TOMMY_TRIE_BIT 32 |
Number of bits supported for trie keys.
All keys must fit in TOMMY_TRIE_BIT bits, even if tommy_key_t is wider. This requirement is checked with assert(), and is not enforced when assertions are disabled.
Increase this value to support larger keys. Keeping it small improves trie performance.
| #define TOMMY_TRIE_TREE_MAX (64 / sizeof(void*)) |
Number of branches on each inner node.
It must be a power of 2. Suggested values are 8, 16 and 32. Any inner node, excluding leafs, contains a pointer to each branch.
The default size is chosen to exactly fit a typical cache line of 64 bytes.
| #define TOMMY_TRIE_BLOCK_SIZE (TOMMY_TRIE_TREE_MAX * sizeof(void*)) |
Trie block size.
You must use this value to initialize the allocator.
| typedef tommy_node tommy_trie_node |
Trie node.
This is the node that you have to include inside your elements.
| typedef struct tommy_trie_struct tommy_trie |
Trie container type.
| TOMMY_API void tommy_trie_init | ( | tommy_trie * | trie, |
| tommy_allocator * | alloc | ||
| ) |
Initializes the trie.
You have to provide an allocator initialized with both the size and align with TOMMY_TRIE_BLOCK_SIZE. You can share this allocator with other tries.
The trie is completely allocated through the allocator, and it doesn't need to be deinitialized.
| trie | The trie. |
| alloc | Allocator initialized with both the size and align with TOMMY_TRIE_BLOCK_SIZE. |
| void tommy_trie_swap | ( | tommy_trie * | first, |
| tommy_trie * | second | ||
| ) |
Exchanges the complete state of two initialized tries, including their allocator references.
Each allocator reference moves with its internal nodes so subsequent operations use the associated allocator. The tries must not share nodes. Nodes and elements are not accessed or modified. Existing node pointers remain valid and belong to the other trie. Referenced allocators must remain initialized. Passing the same trie twice has no effect. Both tries remain usable; their memory is released through their allocators.
| first | The first trie. |
| second | The second trie. |
| TOMMY_API void tommy_trie_clear | ( | tommy_trie * | trie | ) |
Removes all elements, preserving the allocator reference.
The trie remains initialized and can be reused immediately with the same allocator. 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_trie_foreach() has freed the elements. Internal nodes are returned to the allocator, leaving the trie empty. Other tries sharing the allocator are unaffected. No memory is allocated; free blocks remain available for reuse in the allocator.
| trie | The trie. |
| TOMMY_API void tommy_trie_insert | ( | tommy_trie * | trie, |
| tommy_trie_node * | node, | ||
| void * | data, | ||
| tommy_key_t | key | ||
| ) |
Inserts an element in the trie.
| trie | The trie. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| key | Key to use to insert the element. |
| TOMMY_API void * tommy_trie_insert_unique | ( | tommy_trie * | trie, |
| tommy_trie_node * | node, | ||
| void * | data, | ||
| tommy_key_t | key | ||
| ) |
Inserts an element only if no element with the same numeric key is already contained.
If found, the first element with that key in insertion order is returned. The candidate node and trie, including its allocator, are left unchanged, and no memory is allocated. Otherwise, the candidate is inserted using the normal insertion policy and data is returned.
| trie | The trie. |
| node | The candidate node. It must not belong to any container. |
| data | Pointer to the element to insert. |
| key | Numeric key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_remove | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches and removes the first element with the specified key.
If the element is not found, 0 is returned. If more equal elements are present, the first one is removed. This operation is faster than calling tommy_trie_bucket() and tommy_trie_remove_existing() separately.
| trie | The trie. |
| key | Key of the element to find and remove. |
| TOMMY_API tommy_trie_node * tommy_trie_bucket | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Gets the bucket of the specified key.
The bucket is guaranteed to contain ALL and ONLY the elements with the specified key. You can access elements in the bucket following the tommy_node::next pointer until 0.
| trie | The trie. |
| key | Key of the element to find. |
| tommy_trie_node * tommy_trie_find | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Finds the first node with the specified key.
If multiple elements have the key, the first in insertion order is returned.
| trie | The trie. |
| key | Key of the element to find. |
| void * tommy_trie_search | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches an element in the trie.
You have to provide the key of the element you want to find. If more elements with the same key are present, the first one is returned.
| trie | The trie. |
| key | Key of the element to find. |
| TOMMY_API tommy_trie_node * tommy_trie_find_less | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Finds the last node in key order with key strictly less than the specified one.
If multiple elements have the selected key, the last in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API tommy_trie_node * tommy_trie_find_less_equal | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Finds the last node in key order with key less or equal than the specified one.
If multiple elements have the selected key, the last in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API tommy_trie_node * tommy_trie_find_greater_equal | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Finds the first node in key order with key greater or equal than the specified one.
If multiple elements have the selected key, the first in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API tommy_trie_node * tommy_trie_find_greater | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Finds the first node in key order with key strictly greater than the specified one.
If multiple elements have the selected key, the first in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_search_less | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches the last element in key order with key strictly less than the specified one.
If multiple elements have the selected key, the last in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_search_less_equal | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches the last element in key order with key less or equal than the specified one.
If multiple elements have the selected key, the last in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_search_greater_equal | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches the first element in key order with key greater or equal than the specified one.
If multiple elements have the selected key, the first in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_search_greater | ( | tommy_trie * | trie, |
| tommy_key_t | key | ||
| ) |
Searches the first element in key order with key strictly greater than the specified one.
If multiple elements have the selected key, the first in insertion order is returned.
| trie | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_BIT bits. |
| TOMMY_API void * tommy_trie_remove_existing | ( | tommy_trie * | trie, |
| tommy_trie_node * | node | ||
| ) |
Removes an element from the trie.
You must already have the address of the node to remove.
| trie | The trie. |
| node | The node to remove. |
| TOMMY_API tommy_trie_node * tommy_trie_head | ( | tommy_trie * | trie | ) |
Gets the head (smallest key) node in the trie.
If multiple elements have the smallest key, the first in insertion order is returned.
| trie | The trie. |
| TOMMY_API tommy_trie_node * tommy_trie_tail | ( | tommy_trie * | trie | ) |
Gets the tail (greatest key) node in the trie.
If multiple elements have the greatest key, the last in insertion order is returned.
| trie | The trie. |
| void * tommy_trie_remove_head | ( | tommy_trie * | trie | ) |
Removes and returns the head (smallest key) element.
If multiple elements have the smallest key, the first in insertion order is removed.
| trie | The trie. |
| void * tommy_trie_remove_tail | ( | tommy_trie * | trie | ) |
Removes and returns the tail (greatest key) element.
If multiple elements have the greatest key, the last in insertion order is removed.
| trie | The trie. |
| TOMMY_API tommy_trie_node * tommy_trie_next | ( | tommy_trie * | trie, |
| tommy_trie_node * | node | ||
| ) |
Gets the node following the specified one by increasing numeric key.
Elements with equal keys are visited in insertion order. See tommy_trie_iterator_validity for iterator validity across insertions and removals.
| trie | The trie. |
| node | Node currently contained in the trie. It must not be 0. |
| TOMMY_API tommy_trie_node * tommy_trie_prev | ( | tommy_trie * | trie, |
| tommy_trie_node * | node | ||
| ) |
Gets the node preceding the specified one by increasing numeric key.
Elements with equal keys are visited in reverse insertion order. See tommy_trie_iterator_validity for iterator validity across insertions and removals.
| trie | The trie. |
| node | Node currently contained in the trie. It must not be 0. |
| TOMMY_API void tommy_trie_to_list | ( | tommy_trie * | trie, |
| tommy_list * | list | ||
| ) |
Transfers all elements from the trie to the tail of a list by increasing numeric key.
Elements with equal keys retain their insertion order. Existing list elements remain before the transferred elements; an initially empty list is therefore sorted. The list must be initialized and must not share nodes with the trie. Elements are not freed, and the node data and index fields are left unchanged. Internal nodes are returned to the allocator, leaving the trie empty and reusable with the same allocator. Other tries sharing the allocator are unaffected. No memory is allocated; free blocks remain available for reuse in the allocator.
| trie | The trie to drain. |
| list | The destination list. |
| TOMMY_API void tommy_trie_foreach | ( | tommy_trie * | trie, |
| tommy_foreach_func * | func | ||
| ) |
Calls the specified function for each element in the trie.
Elements are visited by increasing key, with equal keys in insertion order. The callback receives the data field of each node. An empty trie does not invoke the callback.
The callback may deallocate the current element, including its embedded node. It must not add or remove elements, modify keys or node links, or deallocate other elements, the trie, or its allocator. This operation does not remove elements or update the count.
No memory is allocated. Stack space is bounded by the number of key bits.
If the callback frees the elements, the trie retains dangling node pointers. Call tommy_trie_clear() before reusing it to release its internal nodes to the allocator, or tommy_allocator_done() to release its storage.
| trie | The trie. |
| func | Function to call with each element. |
| TOMMY_API void tommy_trie_foreach_arg | ( | tommy_trie * | trie, |
| tommy_foreach_arg_func * | func, | ||
| void * | arg | ||
| ) |
Calls the specified function with an argument for each element in the trie.
The iteration order and callback rules are the same as tommy_trie_foreach(). The callback receives arg followed by the data field of each node.
| trie | The trie. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |
| tommy_size_t tommy_trie_count | ( | const tommy_trie * | trie | ) |
Gets the number of elements.
| trie | The trie. |
| tommy_bool_t tommy_trie_empty | ( | const tommy_trie * | trie | ) |
Checks whether the trie is empty.
| trie | The trie. |
| TOMMY_API tommy_size_t tommy_trie_memory_usage | ( | const tommy_trie * | trie | ) |
Gets the size of allocated memory.
It includes the size of the tommy_trie_node of the stored elements.
| trie | The trie. |