2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Initializes 'list' as an empty list. */
22 list_init(struct list *list)
24 list->next = list->prev = list;
27 /* Inserts 'elem' just before 'before'. */
29 list_insert(struct list *before, struct list *elem)
31 elem->prev = before->prev;
33 before->prev->next = elem;
37 /* Removes elements 'first' though 'last' (exclusive) from their current list,
38 then inserts them just before 'before'. */
40 list_splice(struct list *before, struct list *first, struct list *last)
46 /* Cleanly remove 'first'...'last' from its current list. */
47 first->prev->next = last->next;
48 last->next->prev = first->prev;
50 /* Splice 'first'...'last' into new list. */
51 first->prev = before->prev;
53 before->prev->next = first;
57 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
60 list_push_front(struct list *list, struct list *elem)
62 list_insert(list->next, elem);
65 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
68 list_push_back(struct list *list, struct list *elem)
70 list_insert(list, elem);
73 /* Puts 'elem' in the position currently occupied by 'position'.
74 * Afterward, 'position' is not part of a list. */
76 list_replace(struct list *element, const struct list *position)
78 element->next = position->next;
79 element->next->prev = element;
80 element->prev = position->prev;
81 element->prev->next = element;
84 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
85 * around in memory (e.g. as a consequence of realloc()). */
87 list_moved(struct list *list)
89 list->prev->next = list->next->prev = list;
92 /* Removes 'elem' from its list and returns the element that followed it.
93 Undefined behavior if 'elem' is not in a list. */
95 list_remove(struct list *elem)
97 elem->prev->next = elem->next;
98 elem->next->prev = elem->prev;
102 /* Removes the front element from 'list' and returns it. Undefined behavior if
103 'list' is empty before removal. */
105 list_pop_front(struct list *list)
107 struct list *front = list->next;
112 /* Removes the back element from 'list' and returns it.
113 Undefined behavior if 'list' is empty before removal. */
115 list_pop_back(struct list *list)
117 struct list *back = list->prev;
122 /* Returns the front element in 'list'.
123 Undefined behavior if 'list' is empty. */
125 list_front(struct list *list)
127 assert(!list_is_empty(list));
131 /* Returns the back element in 'list'.
132 Undefined behavior if 'list' is empty. */
134 list_back(struct list *list)
136 assert(!list_is_empty(list));
140 /* Returns the number of elements in 'list'.
141 Runs in O(n) in the number of elements. */
143 list_size(const struct list *list)
145 const struct list *e;
148 for (e = list->next; e != list; e = e->next)
153 /* Returns true if 'list' is empty, false otherwise. */
155 list_is_empty(const struct list *list)
157 return list->next == list;