Doubly linked list for collisions into hashtables. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_builder_struct |
| Temporary list builder with an embedded sentinel. More... | |
Typedefs | |
| typedef tommy_node * | tommy_list |
| Doubly linked list type. More... | |
| typedef struct tommy_builder_struct | tommy_builder |
| Temporary list builder with an embedded sentinel. More... | |
Functions | |
| tommy_node * | tommy_builder_init (tommy_builder *builder) |
| Initializes an empty builder in O(1). More... | |
| tommy_node * | tommy_builder_concat (tommy_node *tail, tommy_node *head, tommy_node *last) |
| Appends a non-empty chain to a builder in O(1), preserving element order. More... | |
| void | tommy_list_init (tommy_list *list) |
| Initializes the list. More... | |
| void | tommy_list_clear (tommy_list *list) |
| Removes all elements. More... | |
| tommy_node * | tommy_list_head (tommy_list *list) |
| Gets the head of the list. More... | |
| tommy_node * | tommy_list_tail (tommy_list *list) |
| Gets the tail of the list. More... | |
| tommy_node * | tommy_list_prev (tommy_list *list, tommy_node *node) |
| Gets the previous node in the list. More... | |
| void | tommy_list_insert_head (tommy_list *list, tommy_node *node, void *data) |
| Inserts an element at the head of a list. More... | |
| void | tommy_list_insert_tail (tommy_list *list, tommy_node *node, void *data) |
| Inserts an element at the tail of a list. More... | |
| void | tommy_list_insert_before (tommy_list *list, tommy_node *reference, tommy_node *node, void *data) |
| Inserts an element before the specified reference node. More... | |
| void | tommy_list_insert_after (tommy_list *list, tommy_node *reference, tommy_node *node, void *data) |
| Inserts an element after the specified reference node. More... | |
| void * | tommy_list_remove_existing (tommy_list *list, tommy_node *node) |
| Removes an element from the list. More... | |
| void * | tommy_list_remove_head (tommy_list *list) |
| Removes the element at the head of the list. More... | |
| void * | tommy_list_remove_tail (tommy_list *list) |
| Removes the element at the tail of the list. More... | |
| void | tommy_list_move_head (tommy_list *list, tommy_node *node) |
| Moves an existing node to the head of the list. More... | |
| void | tommy_list_move_tail (tommy_list *list, tommy_node *node) |
| Moves an existing node to the tail of the list. More... | |
| void | tommy_list_split (tommy_list *list, tommy_node *node, tommy_list *second) |
| Splits a list before the specified node. More... | |
| void | tommy_list_swap (tommy_list *first, tommy_list *second) |
| Exchanges the contents of two lists. More... | |
| void | tommy_list_concat (tommy_list *first, tommy_list *second) |
| Concatenates two lists. More... | |
| void | tommy_list_concat_builder (tommy_list *list, const tommy_builder *builder, tommy_node *tail) |
| Appends a builder's chain to a list, preserving element order and restoring list links. More... | |
| void | tommy_list_prepend (tommy_list *first, tommy_list *second) |
| Prepends a list to another list. More... | |
| TOMMY_API void | tommy_list_splice_before (tommy_list *list, tommy_node *reference, tommy_list *second) |
| Transfers all elements of a list before the specified reference node. More... | |
| TOMMY_API void | tommy_list_splice_after (tommy_list *list, tommy_node *reference, tommy_list *second) |
| Transfers all elements of a list after the specified reference node. More... | |
| TOMMY_API void | tommy_list_merge (tommy_list *first, tommy_list *second, tommy_compare_func *cmp) |
| Merges two sorted lists. More... | |
| TOMMY_API void | tommy_list_reverse (tommy_list *list) |
| Reverses the order of the elements in a list. More... | |
| TOMMY_API void | tommy_list_sort (tommy_list *list, tommy_compare_func *cmp) |
| Sorts a list. More... | |
| tommy_bool_t | tommy_list_empty (const tommy_list *list) |
| Checks whether the list is empty. More... | |
| tommy_size_t | tommy_list_count (const tommy_list *list) |
| Gets the number of elements. More... | |
| void | tommy_list_foreach (tommy_list *list, tommy_foreach_func *func) |
| Calls the specified function for each element in the list. More... | |
| void | tommy_list_foreach_arg (tommy_list *list, tommy_foreach_arg_func *func, void *arg) |
| Calls the specified function with an argument for each element in the list. More... | |
Doubly linked list for collisions into hashtables.
This list is a doubly linked list mainly targeted for handling collisions into a hashtable, but usable also as a generic list.
The main feature of this list is to require only one pointer to represent the list, compared to a classic implementation requiring both head and tail pointers. This reduces the memory usage in hashtables.
Another feature is to support the insertion at the end of the list. This allows storing collisions in a stable order, meaning that equal elements preserve their insertion order.
To initialize the list, you have to call tommy_list_init(), or to simply assign to it 0, as an empty list is represented by the 0 value.
To insert elements in the list you have to call tommy_list_insert_tail() or tommy_list_insert_head() for each element. In the insertion call you have to specify the address of the node and the address of the element. The address of the element is used to initialize the tommy_node::data field of the node.
To iterate over all the elements in the list you have to call tommy_list_head() to get the head of the list and follow the tommy_node::next pointer until 0.
To iterate backward, start at tommy_list_tail() and use tommy_list_prev(). Do not follow tommy_node::prev directly past the head, as it points to the tail.
Iterators are pointers to nodes contained in the list. Inserting elements or removing other nodes between iterator calls does not invalidate pointers to nodes that remain in the list. 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.
Concurrent traversal and modification require external synchronization.
To reorder existing elements, use tommy_list_move_head(), tommy_list_move_tail() or tommy_list_reverse(). To split a list or transfer elements between lists, use tommy_list_split(), tommy_list_prepend(), tommy_list_splice_before() and tommy_list_splice_after(). These operations preserve the element pointers and do not allocate memory.
Two lists sorted with the same comparison function can be merged with tommy_list_merge(). The merge is stable.
To destroy the list you have to remove all the elements, as the list is completely in-place and it doesn't allocate memory. This can be done with the tommy_list_foreach() function.
| typedef tommy_node* tommy_list |
Doubly linked list type.
| typedef struct tommy_builder_struct tommy_builder |
Temporary list builder with an embedded sentinel.
Initialize with tommy_builder_init() and finish with tommy_list_concat_builder(). Keep the builder at the same address until finalization, as node links refer to its sentinel.
| tommy_node * tommy_builder_init | ( | tommy_builder * | builder | ) |
Initializes an empty builder in O(1).
| builder | Builder to initialize in its final location. |
| tommy_node * tommy_builder_concat | ( | tommy_node * | tail, |
| tommy_node * | head, | ||
| tommy_node * | last | ||
| ) |
Appends a non-empty chain to a builder in O(1), preserving element order.
| tail | Current tail, initially returned by tommy_builder_init(). |
| head | Head of the non-empty source chain. |
| last | Tail of the source chain. For a single node, pass head again. The chains must not share nodes. Inner links must be valid; head's prev and last's next may be undefined. |
| void tommy_list_init | ( | tommy_list * | list | ) |
Initializes the list.
The list is completely in-place, so it doesn't need to be deinitialized.
| list | The list. |
| void tommy_list_clear | ( | tommy_list * | list | ) |
Removes all elements.
The list 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_list_foreach() has freed the elements.
| list | The list. |
| tommy_node * tommy_list_head | ( | tommy_list * | list | ) |
Gets the head of the list.
| list | The list. |
| tommy_node * tommy_list_tail | ( | tommy_list * | list | ) |
Gets the tail of the list.
| list | The list. |
| tommy_node * tommy_list_prev | ( | tommy_list * | list, |
| tommy_node * | node | ||
| ) |
Gets the previous node in the list.
Unlike tommy_node::prev, this function returns 0 at the head of the list, allowing backward iteration starting from tommy_list_tail(). See tommy_list_iterator_validity for iterator validity across insertions and removals.
| list | The list. |
| node | The current node. The node must be in the list. |
| void tommy_list_insert_head | ( | tommy_list * | list, |
| tommy_node * | node, | ||
| void * | data | ||
| ) |
Inserts an element at the head of a list.
| list | The list. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| void tommy_list_insert_tail | ( | tommy_list * | list, |
| tommy_node * | node, | ||
| void * | data | ||
| ) |
Inserts an element at the tail of a list.
| list | The list. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| void tommy_list_insert_before | ( | tommy_list * | list, |
| tommy_node * | reference, | ||
| tommy_node * | node, | ||
| void * | data | ||
| ) |
Inserts an element before the specified reference node.
| list | The list. |
| reference | The reference node. The new node will be inserted before this node. The reference node must be in the list. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| void tommy_list_insert_after | ( | tommy_list * | list, |
| tommy_node * | reference, | ||
| tommy_node * | node, | ||
| void * | data | ||
| ) |
Inserts an element after the specified reference node.
| list | The list. |
| reference | The reference node. The new node will be inserted after this node. The reference node must be in the list. |
| node | Pointer to the node embedded into the element to insert. |
| data | Pointer to the element to insert. |
| void * tommy_list_remove_existing | ( | tommy_list * | list, |
| tommy_node * | node | ||
| ) |
Removes an element from the list.
You must already have the address of the node to remove.
| list | The list. |
| node | The node to remove. |
| void * tommy_list_remove_head | ( | tommy_list * | list | ) |
Removes the element at the head of the list.
| list | The list. |
| void * tommy_list_remove_tail | ( | tommy_list * | list | ) |
Removes the element at the tail of the list.
| list | The list. |
| void tommy_list_move_head | ( | tommy_list * | list, |
| tommy_node * | node | ||
| ) |
Moves an existing node to the head of the list.
If the node is already at the head, nothing is done. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The list. |
| node | The node to move. The node must be in the list. |
| void tommy_list_move_tail | ( | tommy_list * | list, |
| tommy_node * | node | ||
| ) |
Moves an existing node to the tail of the list.
If the node is already at the tail, nothing is done. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The list. |
| node | The node to move. The node must be in the list. |
| void tommy_list_split | ( | tommy_list * | list, |
| tommy_node * | node, | ||
| tommy_list * | second | ||
| ) |
Splits a list before the specified node.
The list retains the nodes preceding the specified node. The second list receives the specified node and all following nodes. The order of the elements is preserved. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The list to split. |
| node | The first node to transfer. The node must be in the list, or 0 to leave list unchanged and set second to 0. Specifying the head transfers the whole list. |
| second | Output list receiving node and all following nodes. It must be distinct from list and need not be initialized. Its previous value is overwritten. Any previously referenced nodes are left unchanged and are not deallocated. |
| void tommy_list_swap | ( | tommy_list * | first, |
| tommy_list * | second | ||
| ) |
Exchanges the contents of two lists.
The lists must not share nodes. Nodes and elements are not accessed or modified. The tommy_node::data and tommy_node::index fields are left unchanged. Passing the same list twice has no effect.
| first | The first list. |
| second | The second list. |
| void tommy_list_concat | ( | tommy_list * | first, |
| tommy_list * | second | ||
| ) |
Concatenates two lists.
The second list is concatenated to the first list.
| first | The first list. |
| second | The second list. After this call the list content is undefined, and you should not use it anymore. |
| void tommy_list_concat_builder | ( | tommy_list * | list, |
| const tommy_builder * | builder, | ||
| tommy_node * | tail | ||
| ) |
Appends a builder's chain to a list, preserving element order and restoring list links.
| list | Destination list, possibly empty. It must not share nodes with the builder. |
| builder | Initialized builder, possibly empty. Its contents are consumed; reinitialize it before reuse. |
| tail | Current tail returned by the builder helpers, ignored when empty. |
| void tommy_list_prepend | ( | tommy_list * | first, |
| tommy_list * | second | ||
| ) |
Prepends a list to another list.
The second list is prepended to the first list.
| first | The first list. |
| second | The second list. After this call the list content is undefined, and you should not use it anymore. |
| TOMMY_API void tommy_list_splice_before | ( | tommy_list * | list, |
| tommy_node * | reference, | ||
| tommy_list * | second | ||
| ) |
Transfers all elements of a list before the specified reference node.
The order of the transferred elements is preserved. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The destination list. |
| reference | The node before which to insert. It must not be 0 and must be in the destination list. |
| second | The source list. After this call the list content is undefined, and you should not use it anymore. The lists must be distinct and must not share nodes. |
| TOMMY_API void tommy_list_splice_after | ( | tommy_list * | list, |
| tommy_node * | reference, | ||
| tommy_list * | second | ||
| ) |
Transfers all elements of a list after the specified reference node.
The order of the transferred elements is preserved. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The destination list. |
| reference | The node after which to insert. It must not be 0 and must be in the destination list. |
| second | The source list. After this call the list content is undefined, and you should not use it anymore. The lists must be distinct and must not share nodes. |
| TOMMY_API void tommy_list_merge | ( | tommy_list * | first, |
| tommy_list * | second, | ||
| tommy_compare_func * | cmp | ||
| ) |
Merges two sorted lists.
Both lists must be sorted using the same comparison function. The merge is stable: equal elements from the first list precede those from the second list, and the relative order within each list is preserved. The tommy_node::data and tommy_node::index fields are left unchanged.
| first | The destination list, receiving the sorted result. |
| second | The source list. After this call the list content is undefined, and you should not use it anymore. The lists must be distinct and must not share nodes. |
| cmp | Compare function called with two elements. The function should return <0 if the first element is less than the second, ==0 if equal, and >0 if greater. It must not modify the lists. |
| TOMMY_API void tommy_list_reverse | ( | tommy_list * | list | ) |
Reverses the order of the elements in a list.
Empty lists and lists containing one element are left unchanged. The tommy_node::data and tommy_node::index fields are left unchanged.
| list | The list. |
| TOMMY_API void tommy_list_sort | ( | tommy_list * | list, |
| tommy_compare_func * | cmp | ||
| ) |
Sorts a list.
It's a stable merge sort with O(n log n) worst complexity. It's faster on degenerate cases like partially ordered lists.
| list | The list. |
| cmp | Compare function called with two elements. The function should return <0 if the first element is less than the second, ==0 if equal, and >0 if greater. |
| tommy_bool_t tommy_list_empty | ( | const tommy_list * | list | ) |
Checks whether the list is empty.
| list | The list. |
| tommy_size_t tommy_list_count | ( | const tommy_list * | list | ) |
Gets the number of elements.
| list | The list. |
| void tommy_list_foreach | ( | tommy_list * | list, |
| tommy_foreach_func * | func | ||
| ) |
Calls the specified function for each element in the list.
You cannot add or remove elements from the inside of the callback, but can use it to deallocate them.
If the callback frees the elements, the list retains dangling node pointers. Call tommy_list_clear() or tommy_list_init() before reusing it. The list has no storage to release.
| list | The list. |
| func | Function to call with each element. |
| void tommy_list_foreach_arg | ( | tommy_list * | list, |
| tommy_foreach_arg_func * | func, | ||
| void * | arg | ||
| ) |
Calls the specified function with an argument for each element in the list.
The iteration order and callback rules are the same as tommy_list_foreach(). The callback may deallocate the current element. Adding or removing elements from inside the callback is not allowed.
| list | The list. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |