3 * Simple macros for type-safe, intrusive lists.
5 * Intended to work with a list sentinal which is created as an empty
6 * list. Insert & delete are O(1).
9 * (C) 1997, Keith Whitwell
13 * Mesa 3-D graphics library
15 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
37 #ifndef _SIMPLE_LIST_H
38 #define _SIMPLE_LIST_H
45 struct simple_node
*next
;
46 struct simple_node
*prev
;
50 * Remove an element from list.
52 * \param elem element to remove.
54 #define remove_from_list(elem) \
56 (elem)->next->prev = (elem)->prev; \
57 (elem)->prev->next = (elem)->next; \
58 make_empty_list(elem); \
62 * Insert an element to the list head.
65 * \param elem element to insert.
67 #define insert_at_head(list, elem) \
69 (elem)->prev = list; \
70 (elem)->next = (list)->next; \
71 (list)->next->prev = elem; \
72 (list)->next = elem; \
76 * Insert an element to the list tail.
79 * \param elem element to insert.
81 #define insert_at_tail(list, elem) \
83 (elem)->next = list; \
84 (elem)->prev = (list)->prev; \
85 (list)->prev->next = elem; \
86 (list)->prev = elem; \
90 * Move an element to the list head.
93 * \param elem element to move.
95 #define move_to_head(list, elem) \
97 remove_from_list(elem); \
98 insert_at_head(list, elem); \
102 * Move an element to the list tail.
105 * \param elem element to move.
107 #define move_to_tail(list, elem) \
109 remove_from_list(elem); \
110 insert_at_tail(list, elem); \
114 * Make a empty list empty.
116 * \param sentinal list (sentinal element).
118 #define make_empty_list(sentinal) \
120 (sentinal)->next = sentinal; \
121 (sentinal)->prev = sentinal; \
125 * Get list first element.
129 * \return pointer to first element.
131 #define first_elem(list) ((list)->next)
134 * Get list last element.
138 * \return pointer to last element.
140 #define last_elem(list) ((list)->prev)
145 * \param elem element.
147 * \return pointer to next element.
149 #define next_elem(elem) ((elem)->next)
152 * Get previous element.
154 * \param elem element.
156 * \return pointer to previous element.
158 #define prev_elem(elem) ((elem)->prev)
161 * Test whether element is at end of the list.
164 * \param elem element.
166 * \return non-zero if element is at end of list, or zero otherwise.
168 #define at_end(list, elem) ((elem) == (list))
171 * Test if a list is empty.
175 * \return non-zero if list empty, or zero otherwise.
177 #define is_empty_list(list) ((list)->next == (list))
180 * Walk through the elements of a list.
182 * \param ptr pointer to the current element.
185 * \note It should be followed by a { } block or a single statement, as in a \c
188 #define foreach(ptr, list) \
189 for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next )
192 * Walk through the elements of a list.
194 * Same as #foreach but lets you unlink the current value during a list
195 * traversal. Useful for freeing a list, element by element.
197 * \param ptr pointer to the current element.
198 * \param t temporary pointer.
201 * \note It should be followed by a { } block or a single statement, as in a \c
204 #define foreach_s(ptr, t, list) \
205 for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next)