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.
37 /* Initializes 'list' as an empty list. */
39 list_init(struct list *list)
41 list->next = list->prev = list;
44 /* Inserts 'elem' just before 'before'. */
46 list_insert(struct list *before, struct list *elem)
48 elem->prev = before->prev;
50 before->prev->next = elem;
54 /* Removes elements 'first' though 'last' (exclusive) from their current list,
55 then inserts them just before 'before'. */
57 list_splice(struct list *before, struct list *first, struct list *last)
63 /* Cleanly remove 'first'...'last' from its current list. */
64 first->prev->next = last->next;
65 last->next->prev = first->prev;
67 /* Splice 'first'...'last' into new list. */
68 first->prev = before->prev;
70 before->prev->next = first;
74 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
77 list_push_front(struct list *list, struct list *elem)
79 list_insert(list->next, elem);
82 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
85 list_push_back(struct list *list, struct list *elem)
87 list_insert(list, elem);
90 /* Puts 'elem' in the position currently occupied by 'position'.
91 * Afterward, 'position' is not part of a list. */
93 list_replace(struct list *element, const struct list *position)
95 element->next = position->next;
96 element->next->prev = element;
97 element->prev = position->prev;
98 element->prev->next = element;
101 /* Removes 'elem' from its list and returns the element that followed it.
102 Undefined behavior if 'elem' is not in a list. */
104 list_remove(struct list *elem)
106 elem->prev->next = elem->next;
107 elem->next->prev = elem->prev;
111 /* Removes the front element from 'list' and returns it. Undefined behavior if
112 'list' is empty before removal. */
114 list_pop_front(struct list *list)
116 struct list *front = list->next;
121 /* Removes the back element from 'list' and returns it.
122 Undefined behavior if 'list' is empty before removal. */
124 list_pop_back(struct list *list)
126 struct list *back = list->prev;
131 /* Returns the front element in 'list'.
132 Undefined behavior if 'list' is empty. */
134 list_front(struct list *list)
136 assert(!list_is_empty(list));
140 /* Returns the back element in 'list'.
141 Undefined behavior if 'list' is empty. */
143 list_back(struct list *list)
145 assert(!list_is_empty(list));
149 /* Returns the number of elements in 'list'.
150 Runs in O(n) in the number of elements. */
152 list_size(const struct list *list)
154 const struct list *e;
157 for (e = list->next; e != list; e = e->next)
162 /* Returns true if 'list' is empty, false otherwise. */
164 list_is_empty(const struct list *list)
166 return list->next == list;