#include <libcork/ds.h>
This section defines a doubly-linked list data structure. The structure is “invasive”, since you must place an instance of the cork_dllist_item type into the type whose instances will be stored in the list. The list itself is represented by the cork_dllist type.
As an example, we could define the following types for storing groups, as well as the users within each group:
struct group {
const char *group_name;
struct cork_dllist members;
};
struct user {
const char *username;
const char *real_name;
struct cork_dllist_item list;
};
Note that both cork_dllist and cork_dllist_item are embedded directly into our domain-specific types. This means that every list operation defined in this section is guaranteed to succeed, since no memory operations will be involved. (The list and any items will have already been allocated before you try to call the list function.)
Like with any embedded struct, you can use the cork_container_of() macro to obtain a pointer to a struct user if you’re given a pointer to a cork_dllist_item.
A doubly-linked list. The list itself is represented by a sentinel element, representing the empty list.
An element of a doubly-linked list. This type will usually be embedded within the type whose instances will be stored in the list.
A pointer to the next (or previous) element in the list. If this element marks the end (or beginning) of the list, then next (or prev) will point to the list’s sentinel value.
Initializes a doubly-linked list. The list will initially be empty.
Returns the number of elements in list.
This operation runs in \(O(n)\) time.
Returns whether list is empty.
This operation runs in \(O(1)\) time.
Adds element to the end of list. You are responsible for allocating the list element yourself, most likely by allocating the struct that you’ve embedded cork_dllist_item into.
Note
This function assumes that the list element isn’t already a member of a different list. You’re responsible for calling cork_dllist_remove() if this isn’t the case. (If you don’t, the other list will become malformed.)
This operation runs in \(O(1)\) time.
Removes element from the list that it currently belongs to. (Note that you don’t have to pass in a pointer to that list.)
Note
You must not call this function on a list’s sentinel element.
This operation runs in \(O(1)\) time.
There are two strategies you can use to access all of the elements in a doubly-linked list: mapping and iterating. With mapping, you write a mapping function, which will be applied to each element in the list. (In this case, libcork controls the loop that steps through each element.)
Apply a function to each element in list. The function is allowed to remove the current element from the list; this will not affect our ability to iterate through the remainder of the list. The function will be given a pointer to the cork_dllist_item for each element; you can use cork_container_of() to obtain a pointer to the actual element type.
A function that can be applied to each element in a doubly-linked list.
For instance, you can manually calculate the number of elements in a list as follows (assuming you didn’t want to use the built-in cork_dllist_size() function, of course):
static void
count_elements(struct cork_dllist_item *element, void *ud)
{
size_t *count = ud;
(*count)++;
}
struct cork_dllist *list = /* from somewhere */;
size_t count = 0;
cork_dllist_map(list, count_elements, &count);
/* the number of elements is now in count */
The second strategy is to iterate through the elements yourself.
Returns the element at the beginning of list. If list is empty, then the _head variant will return NULL, while the _start variant will return the list’s sentinel element.
Returns the element at the end of list. If list is empty, then the _tail variant will return NULL, while the _end variant will return the list’s sentinel element.
Returns whether element marks the start (or end) of list.
With these functions, manually counting the list elements looks like:
struct cork_dllist *list = /* from somewhere */;
struct cork_dllist_item *curr;
size_t count = 0;
for (curr = cork_dllist_start(list); !cork_dllist_is_end(list, curr);
curr = curr->next) {
count++;
}
/* the number of elements is now in count */
You can also count the elements in reverse order:
struct cork_dllist *list = /* from somewhere */;
struct cork_dllist_item *curr;
size_t count = 0;
for (curr = cork_dllist_end(list); !cork_dllist_is_start(list, curr);
curr = curr->prev) {
count++;
}
/* the number of elements is now in count */