NAME
LIST, LIST_ENTRY,
LIST_INIT, LIST_FIRST,
LIST_LAST, LIST_PREV,
LIST_NEXT, LIST_EMPTY,
LIST_LINKED,
LIST_INSERT_HEAD,
LIST_INSERT_TAIL,
LIST_INSERT_AFTER,
LIST_INSERT_BEFORE,
LIST_REMOVE, LIST_CONCAT,
LIST_FOREACH,
LIST_FOREACH_SAFE,
LIST_FOREACH_REVERSE,
LIST_FOREACH_REVERSE_SAFE —
doubly linked list structure
SYNOPSIS
#include
<libks/list.h>
LIST(list_type,
element_type);
LIST_ENTRY(list_type,
element_type);
struct element_type *
LIST_FIRST(struct
list_type *list);
struct element_type *
LIST_LAST(struct
list_type *list);
struct element_type *
LIST_PREV(struct
element_type *element);
struct element_type *
LIST_NEXT(struct
element_type *element);
int
LIST_EMPTY(struct
list_type *list);
int
LIST_LINKED(struct
element_type *element);
void
LIST_INSERT_HEAD(struct
list_type *list, struct
element_type *element);
void
LIST_INSERT_TAIL(struct
list_type *list, struct
element_type *element);
void
LIST_INSERT_AFTER(struct list_type
*list, struct element_type *after,
struct element_type *element);
void
LIST_INSERT_BEFORE(struct element_type
*before, struct element_type *element);
void
LIST_REMOVE(struct
list_type *list, struct
element_type *element);
void
LIST_CONCAT(struct
list_type *list1, struct
element_type *list2);
LIST_FOREACH(struct
element_type *element,
struct list_type
*list);
LIST_FOREACH_SAFE(struct
element_type *element, struct list_type *list,
struct element_type *tmp);
LIST_FOREACH_REVERSE(struct
element_type *element, struct list_type
*list);
LIST_FOREACH_REVERSE_SAFE(struct
element_type *element, struct list_type *list,
struct element_type *tmp);
DESCRIPTION
The list structure stores elements in a doubly linked list, allowing constant time insertion and deletion.
The
LIST() macro
declares a new list structure named list_type capable
of holding elements of the given element_type.
The
LIST_ENTRY()
macro declares a structure field making the enclosing structure of type
element_type capable of being part of
list_type.
The
LIST_FIRST()
macro returns the first element in list.
The
LIST_LAST()
macro returns the last element in list.
The
LIST_PREV()
macro returns the previous element relative to
element.
The
LIST_NEXT()
macro returns the next element relative to
element.
The
LIST_EMPTY()
returns non-zero if list is empty.
The
LIST_LINKED()
returns non-zero if element is part of a list.
The
LIST_INSERT_HEAD()
macro inserts element at the start of
list.
The
LIST_INSERT_TAIL()
macro inserts element at the end of
list.
The
LIST_INSERT_AFTER()
macro inserts element after
after.
The
LIST_INSERT_BEFORE()
macro inserts element before
before.
The
LIST_REMOVE()
macro removes element from
list.
The
LIST_CONCAT()
macro moves all elements from list2 to
list1 effectively making list2
empty.
The
LIST_FOREACH()
iterates over all elements in list starting from the
first element.
The
LIST_FOREACH_SAFE()
behaves like LIST_FOREACH() but allows the element
currently being iterated over to be removed from
list.
The
LIST_FOREACH_REVERSE()
iterates over all elements in list starting from the
last element.
The
LIST_FOREACH_REVERSE_SAFE()
behaves like LIST_FOREACH_REVERSE() but allows the
element currently being iterated over to be removed from
list.
EXAMPLES
The following example demonstrates how to create a token_list structure capable of storing struct token elements.
LIST(token_list, token);
struct token {
LIST_ENTRY(token_list, token);
};
int
main(void)
{
struct token_list list;
struct token tail = {0};
struct token *token;
LIST_INIT(&list);
LIST_INSERT_TAIL(&list, &tail);
LIST_FOREACH(token, &list)
continue;
return 0;
}
AUTHORS
Anton Lindqvist <anton@basename.se>