In-place trie. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_trie_inplace_node_struct |
| Trie node. More... | |
| struct | tommy_trie_inplace_struct |
| Trie container type. More... | |
Macros | |
| #define | TOMMY_TRIE_INPLACE_BIT 32 |
| Number of bits supported for in-place trie keys. More... | |
| #define | TOMMY_TRIE_INPLACE_TREE_MAX 4 |
| Number of branches on each node. More... | |
Typedefs | |
| typedef struct tommy_trie_inplace_node_struct | tommy_trie_inplace_node |
| Trie node. More... | |
| typedef struct tommy_trie_inplace_struct | tommy_trie_inplace |
| Trie container type. More... | |
Functions | |
| TOMMY_API void | tommy_trie_inplace_init (tommy_trie_inplace *trie_inplace) |
| Initializes the trie. More... | |
| TOMMY_API void | tommy_trie_inplace_clear (tommy_trie_inplace *trie_inplace) |
| Removes all elements. More... | |
| void | tommy_trie_inplace_swap (tommy_trie_inplace *first, tommy_trie_inplace *second) |
| Exchanges the complete state of two initialized tries. More... | |
| TOMMY_API void | tommy_trie_inplace_insert (tommy_trie_inplace *trie_inplace, tommy_trie_inplace_node *node, void *data, tommy_key_t key) |
| Inserts an element in the trie. More... | |
| TOMMY_API void * | tommy_trie_inplace_insert_unique (tommy_trie_inplace *trie_inplace, tommy_trie_inplace_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_inplace_remove (tommy_trie_inplace *trie_inplace, tommy_key_t key) |
| Searches and removes the first element with the specified key. More... | |
| tommy_trie_inplace_node * | tommy_trie_inplace_bucket (tommy_trie_inplace *trie_inplace, tommy_key_t key) |
| Gets the bucket of the specified key. More... | |
| tommy_trie_inplace_node * | tommy_trie_inplace_find (tommy_trie_inplace *trie_inplace, tommy_key_t key) |
| Finds the first node with the specified key. More... | |
| void * | tommy_trie_inplace_search (tommy_trie_inplace *trie_inplace, tommy_key_t key) |
| Searches an element in the trie. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_find_less (tommy_trie_inplace *trie_inplace, tommy_key_t key) |
| Finds the last node in key order with key strictly less than the specified one. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_find_less_equal (tommy_trie_inplace *trie_inplace, 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_inplace_node * | tommy_trie_inplace_find_greater_equal (tommy_trie_inplace *trie_inplace, 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_inplace_node * | tommy_trie_inplace_find_greater (tommy_trie_inplace *trie_inplace, 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_inplace_search_less (tommy_trie_inplace *trie_inplace, 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_inplace_search_less_equal (tommy_trie_inplace *trie_inplace, 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_inplace_search_greater_equal (tommy_trie_inplace *trie_inplace, 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_inplace_search_greater (tommy_trie_inplace *trie_inplace, 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_inplace_remove_existing (tommy_trie_inplace *trie_inplace, tommy_trie_inplace_node *node) |
| Removes an element from the trie. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_head (tommy_trie_inplace *trie_inplace) |
| Gets the head (smallest key) node in the trie. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_tail (tommy_trie_inplace *trie_inplace) |
| Gets the tail (greatest key) node in the trie. More... | |
| void * | tommy_trie_inplace_remove_head (tommy_trie_inplace *trie_inplace) |
| Removes and returns the head (smallest key) element. More... | |
| void * | tommy_trie_inplace_remove_tail (tommy_trie_inplace *trie_inplace) |
| Removes and returns the tail (greatest key) element. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_next (tommy_trie_inplace *trie_inplace, tommy_trie_inplace_node *node) |
| Gets the node following the specified one by increasing numeric key. More... | |
| TOMMY_API tommy_trie_inplace_node * | tommy_trie_inplace_prev (tommy_trie_inplace *trie_inplace, tommy_trie_inplace_node *node) |
| Gets the node preceding the specified one by increasing numeric key. More... | |
| TOMMY_API void | tommy_trie_inplace_foreach (tommy_trie_inplace *trie_inplace, tommy_foreach_func *func) |
| Calls the specified function for each element in the trie. More... | |
| TOMMY_API void | tommy_trie_inplace_foreach_arg (tommy_trie_inplace *trie_inplace, 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_inplace_count (const tommy_trie_inplace *trie_inplace) |
| Gets the number of elements. More... | |
| tommy_bool_t | tommy_trie_inplace_empty (const tommy_trie_inplace *trie_inplace) |
| Checks whether the trie is empty. More... | |
| TOMMY_API tommy_size_t | tommy_trie_inplace_memory_usage (const tommy_trie_inplace *trie_inplace) |
| Gets the size of allocated memory. More... | |
In-place trie.
This trie is an in-place implementation not needing any external allocation.
Unlike tommy_trie, elements also represent the inner nodes and are not stored in key order. Ordered traversal merges each node's elements with its child subtrees.
You can control the number of branches of each node using the TOMMY_TRIE_INPLACE_TREE_MAX define. More branches imply more speed, but a bigger memory occupation.
Compared to tommy_trie you should use a lower number of branches to limit the unused memory occupation of the leaf nodes. This implies a lower speed, but without the need of an external allocator.
To initialize the trie you have to call tommy_trie_inplace_init().
To insert elements in the trie you have to call tommy_trie_inplace_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_trie_inplace_node::data field of the node, and the key to initialize the tommy_trie_inplace_node::key field.
To find an element in the trie you have to call tommy_trie_inplace_search() providing the key to search.
To find a key bound, use tommy_trie_inplace_search_less(), tommy_trie_inplace_search_less_equal(), tommy_trie_inplace_search_greater_equal(), or tommy_trie_inplace_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_inplace_search().
To iterate over all the elements in the trie with the same key, you have to use tommy_trie_inplace_bucket() and follow the tommy_trie_inplace_node::next pointer until 0.
To iterate over all elements by increasing key, start with tommy_trie_inplace_head() and advance with tommy_trie_inplace_next(). Equal keys are visited in insertion order. Use tommy_trie_inplace_tail() and tommy_trie_inplace_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_inplace_remove() providing the key to search and remove.
To remove the smallest or greatest element, use tommy_trie_inplace_remove_head() or tommy_trie_inplace_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_inplace_foreach() or tommy_trie_inplace_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_inplace_foreach(), as the trie doesn't allocate memory.
| #define TOMMY_TRIE_INPLACE_BIT 32 |
Number of bits supported for in-place trie keys.
All keys must fit in TOMMY_TRIE_INPLACE_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_INPLACE_TREE_MAX 4 |
Number of branches on each node.
It must be a power of 2. Suggested values are 2, 4 and 8. Any node, including leafs, contains a pointer to each branch.
| typedef struct tommy_trie_inplace_node_struct tommy_trie_inplace_node |
Trie node.
This is the node that you have to include inside your elements.
| typedef struct tommy_trie_inplace_struct tommy_trie_inplace |
Trie container type.
| TOMMY_API void tommy_trie_inplace_init | ( | tommy_trie_inplace * | trie_inplace | ) |
Initializes the trie.
The trie is completely in-place, and it doesn't need to be deinitialized.
| trie_inplace | The trie. |
| TOMMY_API void tommy_trie_inplace_clear | ( | tommy_trie_inplace * | trie_inplace | ) |
Removes all elements.
The trie 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_trie_inplace_foreach() has freed the elements.
| trie_inplace | The trie. |
| void tommy_trie_inplace_swap | ( | tommy_trie_inplace * | first, |
| tommy_trie_inplace * | second | ||
| ) |
Exchanges the complete state of two initialized tries.
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. Passing the same trie twice has no effect. Both tries remain usable and require no deinitialization.
| first | The first trie. |
| second | The second trie. |
| TOMMY_API void tommy_trie_inplace_insert | ( | tommy_trie_inplace * | trie_inplace, |
| tommy_trie_inplace_node * | node, | ||
| void * | data, | ||
| tommy_key_t | key | ||
| ) |
Inserts an element in the trie.
| trie_inplace | 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_inplace_insert_unique | ( | tommy_trie_inplace * | trie_inplace, |
| tommy_trie_inplace_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 are left unchanged, and no memory is allocated. Otherwise, the candidate is inserted using the normal insertion policy and data is returned.
| trie_inplace | 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_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_remove | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace_bucket() and tommy_trie_inplace_remove_existing() separately.
| trie_inplace | The trie. |
| key | Key of the element to find and remove. |
| tommy_trie_inplace_node * tommy_trie_inplace_bucket | ( | tommy_trie_inplace * | trie_inplace, |
| 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_trie_inplace_node::next pointer until 0.
| trie_inplace | The trie. |
| key | Key of the element to find. |
| tommy_trie_inplace_node * tommy_trie_inplace_find | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Key of the element to find. |
| void * tommy_trie_inplace_search | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Key of the element to find. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_find_less | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_find_less_equal | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_find_greater_equal | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_find_greater | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_search_less | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_search_less_equal | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_search_greater_equal | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_search_greater | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace | The trie. |
| key | Search key, which must fit within TOMMY_TRIE_INPLACE_BIT bits. |
| TOMMY_API void * tommy_trie_inplace_remove_existing | ( | tommy_trie_inplace * | trie_inplace, |
| tommy_trie_inplace_node * | node | ||
| ) |
Removes an element from the trie.
You must already have the address of the node to remove.
| trie_inplace | The trie. |
| node | The node to remove. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_head | ( | tommy_trie_inplace * | trie_inplace | ) |
Gets the head (smallest key) node in the trie.
If multiple elements have the smallest key, the first in insertion order is returned.
| trie_inplace | The trie. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_tail | ( | tommy_trie_inplace * | trie_inplace | ) |
Gets the tail (greatest key) node in the trie.
If multiple elements have the greatest key, the last in insertion order is returned.
| trie_inplace | The trie. |
| void * tommy_trie_inplace_remove_head | ( | tommy_trie_inplace * | trie_inplace | ) |
Removes and returns the head (smallest key) element.
If multiple elements have the smallest key, the first in insertion order is removed.
| trie_inplace | The trie. |
| void * tommy_trie_inplace_remove_tail | ( | tommy_trie_inplace * | trie_inplace | ) |
Removes and returns the tail (greatest key) element.
If multiple elements have the greatest key, the last in insertion order is removed.
| trie_inplace | The trie. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_next | ( | tommy_trie_inplace * | trie_inplace, |
| tommy_trie_inplace_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_inplace_iterator_validity for iterator validity across insertions and removals.
| trie_inplace | The trie. |
| node | Node currently contained in the trie. It must not be 0. |
| TOMMY_API tommy_trie_inplace_node * tommy_trie_inplace_prev | ( | tommy_trie_inplace * | trie_inplace, |
| tommy_trie_inplace_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_inplace_iterator_validity for iterator validity across insertions and removals.
| trie_inplace | The trie. |
| node | Node currently contained in the trie. It must not be 0. |
| TOMMY_API void tommy_trie_inplace_foreach | ( | tommy_trie_inplace * | trie_inplace, |
| 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 or the trie. 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_inplace_clear() or tommy_trie_inplace_init() before reusing it. The trie has no storage to release.
| trie_inplace | The trie. |
| func | Function to call with each element. |
| TOMMY_API void tommy_trie_inplace_foreach_arg | ( | tommy_trie_inplace * | trie_inplace, |
| 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_inplace_foreach(). The callback receives arg followed by the data field of each node.
| trie_inplace | The trie. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |
| tommy_size_t tommy_trie_inplace_count | ( | const tommy_trie_inplace * | trie_inplace | ) |
Gets the number of elements.
| trie_inplace | The trie. |
| tommy_bool_t tommy_trie_inplace_empty | ( | const tommy_trie_inplace * | trie_inplace | ) |
Checks whether the trie is empty.
| trie_inplace | The trie. |
| TOMMY_API tommy_size_t tommy_trie_inplace_memory_usage | ( | const tommy_trie_inplace * | trie_inplace | ) |
Gets the size of allocated memory.
It includes the size of the tommy_trie_inplace_node of the stored elements.
| trie_inplace | The trie. |