]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/lists.c
2 * BIRD Library -- Linked Lists
4 * (c) 1998 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 * The BIRD library provides a set of functions for operating on linked
13 * lists. The lists are internally represented as standard doubly linked
14 * lists with synthetic head and tail which makes all the basic operations
15 * run in constant time and contain no extra end-of-list checks. Each list
16 * is described by a &list structure, nodes can have any format as long
17 * as they start with a &node structure. If you want your nodes to belong
18 * to multiple lists at once, you can embed multiple &node structures in them
19 * and use the SKIP_BACK() macro to calculate a pointer to the start of the
20 * structure from a &node pointer, but beware of obscurity.
22 * There also exist safe linked lists (&slist, &snode and all functions
23 * being prefixed with |s_|) which support asynchronous walking very
24 * similar to that used in the &fib structure.
27 #define _BIRD_LISTS_C_
29 #include "nest/bird.h"
30 #include "lib/lists.h"
33 * add_tail - append a node to a list
37 * add_tail() takes a node @n and appends it at the end of the list @l.
40 add_tail(list
*l
, node
*n
)
44 n
->next
= (node
*) &l
->null
;
51 * add_head - prepend a node to a list
55 * add_head() takes a node @n and prepends it at the start of the list @l.
58 add_head(list
*l
, node
*n
)
63 n
->prev
= (node
*) &l
->head
;
69 * insert_node - insert a node to a list
71 * @after: a node of a list
73 * Inserts a node @n to a linked list after an already inserted
77 insert_node(node
*n
, node
*after
)
79 node
*z
= after
->next
;
88 * rem_node - remove a node from a list
89 * @n: node to be removed
91 * Removes a node @n from the list it's linked in.
104 * rem2_node - remove a node from a list, with cleanup
105 * @n: node to be removed
107 * Removes a node @n from the list it's linked in and resets its pointers to NULL.
108 * Useful if you want to distinguish between linked and unlinked nodes.
123 * replace_node - replace a node in a list with another one
124 * @old: node to be removed
125 * @new: node to be inserted
127 * Replaces node @old in the list it's linked in with node @new. Node
128 * @old may be a copy of the original node, which is not accessed
129 * through the list. The function could be called with @old == @new,
130 * which just fixes neighbors' pointers in the case that the node
134 replace_node(node
*old
, node
*new)
136 old
->next
->prev
= new;
137 old
->prev
->next
= new;
139 new->prev
= old
->prev
;
140 new->next
= old
->next
;
144 * init_list - create an empty list
147 * init_list() takes a &list structure and initializes its
148 * fields, so that it represents an empty list.
153 l
->head
= (node
*) &l
->null
;
155 l
->tail
= (node
*) &l
->head
;
159 * add_tail_list - concatenate two lists
160 * @to: destination list
163 * This function appends all elements of the list @l to
164 * the list @to in constant time.
167 add_tail_list(list
*to
, list
*l
)
175 q
->next
= (node
*) &to
->null
;