Data Structures | Typedefs | Functions
tommylist.h File Reference

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...
 

Detailed Description

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.

tommy_list_init(&list); // initializes the list
Data structure node.
Definition: tommytypes.h:242
void tommy_list_init(tommy_list *list)
Initializes the list.
Definition: tommylist.h:190

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.

struct element {
int value;
// other fields
tommy_node node;
};
struct element* elem = malloc(sizeof(struct element)); // creates the element
elem->value = ...; // initializes the element
tommy_list_insert_tail(&list, &elem->node, elem); // inserts the element
void tommy_list_insert_tail(tommy_list *list, tommy_node *node, void *data)
Inserts an element at the tail of a list.
Definition: tommylist.h:331

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.

while (i) {
struct element* elem = i->data; // gets the element pointer
printf("%d\n", elem->value); // process the element
i = i->next; // go to the next element
}
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:247
void * data
Pointer to the element containing the node.
Definition: tommytypes.h:260
tommy_node * tommy_list_head(tommy_list *list)
Gets the head of the list.
Definition: tommylist.h:215

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.

while (i) {
struct element* elem = i->data;
printf("%d\n", elem->value);
i = tommy_list_prev(&list, i);
}
tommy_node * tommy_list_prev(tommy_list *list, tommy_node *node)
Gets the previous node in the list.
Definition: tommylist.h:246
tommy_node * tommy_list_tail(tommy_list *list)
Gets the tail of the list.
Definition: tommylist.h:226

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.

while (i) {
tommy_node* next = i->next; // obtain the next node before removal
if (condition) {
free(i->data); // safe to deallocate
}
i = next;
}
void * tommy_list_remove_existing(tommy_list *list, tommy_node *node)
Removes an element from the list.
Definition: tommylist.h:406

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.

tommy_list second;
tommy_list_init(&second);
// transfers node and all following nodes to second
tommy_list_split(&list, node, &second);
// appends the transferred nodes back to list
tommy_list_concat(&list, &second);
void tommy_list_split(tommy_list *list, tommy_node *node, tommy_list *second)
Splits a list before the specified node.
Definition: tommylist.h:518
void tommy_list_concat(tommy_list *first, tommy_list *second)
Concatenates two lists.
Definition: tommylist.h:571

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.

// deallocates all the elements iterating the list
tommy_list_foreach(&list, free);
tommy_list_clear(&list); // resets the list before reuse
void tommy_list_clear(tommy_list *list)
Removes all elements.
Definition: tommylist.h:204
void tommy_list_foreach(tommy_list *list, tommy_foreach_func *func)
Calls the specified function for each element in the list.
Definition: tommylist.h:770

Typedef Documentation

◆ tommy_list

Doubly linked list type.

◆ 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.

Function Documentation

◆ tommy_builder_init()

tommy_node * tommy_builder_init ( tommy_builder *  builder)

Initializes an empty builder in O(1).

Parameters
builderBuilder to initialize in its final location.
Returns
Initial tail, pointing to the builder's sentinel.

◆ tommy_builder_concat()

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.

Parameters
tailCurrent tail, initially returned by tommy_builder_init().
headHead of the non-empty source chain.
lastTail 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.
Returns
Updated tail to pass to the next append or tommy_list_concat_builder().

◆ tommy_list_init()

void tommy_list_init ( tommy_list *  list)

Initializes the list.

The list is completely in-place, so it doesn't need to be deinitialized.

Parameters
listThe list.

◆ tommy_list_clear()

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.

Parameters
listThe list.
Note
This operation is O(1).

◆ tommy_list_head()

tommy_node * tommy_list_head ( tommy_list *  list)

Gets the head of the list.

Parameters
listThe list.
Returns
The head node, or 0 if the list is empty.
Note
This operation is O(1).

◆ tommy_list_tail()

tommy_node * tommy_list_tail ( tommy_list *  list)

Gets the tail of the list.

Parameters
listThe list.
Returns
The tail node, or 0 if the list is empty.
Note
This operation is O(1).

◆ tommy_list_prev()

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.

Parameters
listThe list.
nodeThe current node. The node must be in the list.
Returns
The previous node, or 0 if the current node is the head.
Note
This operation is O(1).

◆ tommy_list_insert_head()

void tommy_list_insert_head ( tommy_list *  list,
tommy_node *  node,
void *  data 
)

Inserts an element at the head of a list.

Parameters
listThe list.
nodePointer to the node embedded into the element to insert.
dataPointer to the element to insert.

◆ tommy_list_insert_tail()

void tommy_list_insert_tail ( tommy_list *  list,
tommy_node *  node,
void *  data 
)

Inserts an element at the tail of a list.

Parameters
listThe list.
nodePointer to the node embedded into the element to insert.
dataPointer to the element to insert.

◆ tommy_list_insert_before()

void tommy_list_insert_before ( tommy_list *  list,
tommy_node *  reference,
tommy_node *  node,
void *  data 
)

Inserts an element before the specified reference node.

Parameters
listThe list.
referenceThe reference node. The new node will be inserted before this node. The reference node must be in the list.
nodePointer to the node embedded into the element to insert.
dataPointer to the element to insert.

◆ tommy_list_insert_after()

void tommy_list_insert_after ( tommy_list *  list,
tommy_node *  reference,
tommy_node *  node,
void *  data 
)

Inserts an element after the specified reference node.

Parameters
listThe list.
referenceThe reference node. The new node will be inserted after this node. The reference node must be in the list.
nodePointer to the node embedded into the element to insert.
dataPointer to the element to insert.

◆ tommy_list_remove_existing()

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.

Parameters
listThe list.
nodeThe node to remove.
Returns
The removed element.
Note
The node content is left unchanged, including the tommy_node::next and tommy_node::prev fields that still contain pointers in the list.

◆ tommy_list_remove_head()

void * tommy_list_remove_head ( tommy_list *  list)

Removes the element at the head of the list.

Parameters
listThe list.
Returns
The removed element, or 0 if the list is empty.

◆ tommy_list_remove_tail()

void * tommy_list_remove_tail ( tommy_list *  list)

Removes the element at the tail of the list.

Parameters
listThe list.
Returns
The removed element, or 0 if the list is empty.

◆ tommy_list_move_head()

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.

Parameters
listThe list.
nodeThe node to move. The node must be in the list.

◆ tommy_list_move_tail()

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.

Parameters
listThe list.
nodeThe node to move. The node must be in the list.

◆ tommy_list_split()

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.

Parameters
listThe list to split.
nodeThe 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.
secondOutput 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.
Note
This operation is O(1).

◆ tommy_list_swap()

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.

Parameters
firstThe first list.
secondThe second list.
Note
This operation is O(1).

◆ tommy_list_concat()

void tommy_list_concat ( tommy_list *  first,
tommy_list *  second 
)

Concatenates two lists.

The second list is concatenated to the first list.

Parameters
firstThe first list.
secondThe second list. After this call the list content is undefined, and you should not use it anymore.
Note
This operation is O(1).

◆ tommy_list_concat_builder()

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.

Parameters
listDestination list, possibly empty. It must not share nodes with the builder.
builderInitialized builder, possibly empty. Its contents are consumed; reinitialize it before reuse.
tailCurrent tail returned by the builder helpers, ignored when empty.
Note
This operation is O(1). An empty builder has no effect.

◆ tommy_list_prepend()

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.

Parameters
firstThe first list.
secondThe second list. After this call the list content is undefined, and you should not use it anymore.
Note
This operation is O(1).

◆ tommy_list_splice_before()

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.

Parameters
listThe destination list.
referenceThe node before which to insert. It must not be 0 and must be in the destination list.
secondThe 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.
Note
This operation is O(1). An empty source list has no effect.

◆ tommy_list_splice_after()

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.

Parameters
listThe destination list.
referenceThe node after which to insert. It must not be 0 and must be in the destination list.
secondThe 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.
Note
This operation is O(1). An empty source list has no effect.

◆ tommy_list_merge()

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.

Parameters
firstThe destination list, receiving the sorted result.
secondThe 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.
cmpCompare 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.
Note
This operation is O(n+m), or O(1) when one list is empty or the ranges can be concatenated directly. It uses O(1) auxiliary space.

◆ tommy_list_reverse()

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.

Parameters
listThe list.
Note
This operation is O(n) and uses O(1) auxiliary space.

◆ tommy_list_sort()

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.

Parameters
listThe list.
cmpCompare 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.
Note
This operation is O(n log n) in the worst case.

◆ tommy_list_empty()

tommy_bool_t tommy_list_empty ( const tommy_list *  list)

Checks whether the list is empty.

Parameters
listThe list.
Returns
Nonzero if the list is empty, 0 otherwise.
Note
This operation is O(1).

◆ tommy_list_count()

tommy_size_t tommy_list_count ( const tommy_list *  list)

Gets the number of elements.

Parameters
listThe list.
Returns
The number of elements in the list.
Note
This operation is O(n).

◆ tommy_list_foreach()

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.

// initializes the list
...
// creates an element
struct element* elem = malloc(sizeof(struct element));
...
// insert it in the list
tommy_list_insert_tail(&list, &elem->node, elem);
...
// deallocates all the elements iterating the list
tommy_list_foreach(&list, free);
tommy_list_clear(&list); // resets the list before reuse

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.

Parameters
listThe list.
funcFunction to call with each element.

◆ tommy_list_foreach_arg()

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.

Parameters
listThe list.
funcFunction to call with each element.
argArgument to pass to the function.