1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
36 /* Initializes 'list' as an empty list. */
38 list_init(struct list *list)
40 list->next = list->prev = list;
43 /* Inserts 'elem' just before 'before'. */
45 list_insert(struct list *before, struct list *elem)
47 elem->prev = before->prev;
49 before->prev->next = elem;
53 /* Removes elements 'first' though 'last' (exclusive) from their current list,
54 then inserts them just before 'before'. */
56 list_splice(struct list *before, struct list *first, struct list *last)
62 /* Cleanly remove 'first'...'last' from its current list. */
63 first->prev->next = last->next;
64 last->next->prev = first->prev;
66 /* Splice 'first'...'last' into new list. */
67 first->prev = before->prev;
69 before->prev->next = first;
73 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
76 list_push_front(struct list *list, struct list *elem)
78 list_insert(list->next, elem);
81 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
84 list_push_back(struct list *list, struct list *elem)
86 list_insert(list, elem);
89 /* Puts 'elem' in the position currently occupied by 'position'.
90 * Afterward, 'position' is not part of a list. */
92 list_replace(struct list *element, const struct list *position)
94 element->next = position->next;
95 element->next->prev = element;
96 element->prev = position->prev;
97 element->prev->next = element;
100 /* Removes 'elem' from its list and returns the element that followed it.
101 Undefined behavior if 'elem' is not in a list. */
103 list_remove(struct list *elem)
105 elem->prev->next = elem->next;
106 elem->next->prev = elem->prev;
110 /* Removes the front element from 'list' and returns it. Undefined behavior if
111 'list' is empty before removal. */
113 list_pop_front(struct list *list)
115 struct list *front = list->next;
120 /* Removes the back element from 'list' and returns it.
121 Undefined behavior if 'list' is empty before removal. */
123 list_pop_back(struct list *list)
125 struct list *back = list->prev;
130 /* Returns the front element in 'list'.
131 Undefined behavior if 'list' is empty. */
133 list_front(struct list *list)
135 assert(!list_is_empty(list));
139 /* Returns the back element in 'list'.
140 Undefined behavior if 'list' is empty. */
142 list_back(struct list *list)
144 assert(!list_is_empty(list));
148 /* Returns the number of elements in 'list'.
149 Runs in O(n) in the number of elements. */
151 list_size(const struct list *list)
153 const struct list *e;
156 for (e = list->next; e != list; e = e->next)
161 /* Returns true if 'list' is empty, false otherwise. */
163 list_is_empty(const struct list *list)
165 return list->next == list;