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
16 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included
26 * in all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
32 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #ifndef _SIMPLE_LIST_H
38 #define _SIMPLE_LIST_H
41 struct simple_node
*next
;
42 struct simple_node
*prev
;
46 * Remove an element from list.
48 * \param elem element to remove.
50 #define remove_from_list(elem) \
52 (elem)->next->prev = (elem)->prev; \
53 (elem)->prev->next = (elem)->next; \
57 * Insert an element to the list head.
60 * \param elem element to insert.
62 #define insert_at_head(list, elem) \
64 (elem)->prev = list; \
65 (elem)->next = (list)->next; \
66 (list)->next->prev = elem; \
67 (list)->next = elem; \
71 * Insert an element to the list tail.
74 * \param elem element to insert.
76 #define insert_at_tail(list, elem) \
78 (elem)->next = list; \
79 (elem)->prev = (list)->prev; \
80 (list)->prev->next = elem; \
81 (list)->prev = elem; \
85 * Move an element to the list head.
88 * \param elem element to move.
90 #define move_to_head(list, elem) \
92 remove_from_list(elem); \
93 insert_at_head(list, elem); \
97 * Move an element to the list tail.
100 * \param elem element to move.
102 #define move_to_tail(list, elem) \
104 remove_from_list(elem); \
105 insert_at_tail(list, elem); \
109 * Make a empty list empty.
111 * \param sentinal list (sentinal element).
113 #define make_empty_list(sentinal) \
115 (sentinal)->next = sentinal; \
116 (sentinal)->prev = sentinal; \
120 * Get list first element.
124 * \return pointer to first element.
126 #define first_elem(list) ((list)->next)
129 * Get list last element.
133 * \return pointer to last element.
135 #define last_elem(list) ((list)->prev)
140 * \param elem element.
142 * \return pointer to next element.
144 #define next_elem(elem) ((elem)->next)
147 * Get previous element.
149 * \param elem element.
151 * \return pointer to previous element.
153 #define prev_elem(elem) ((elem)->prev)
156 * Test whether element is at end of the list.
159 * \param elem element.
161 * \return non-zero if element is at end of list, or zero otherwise.
163 #define at_end(list, elem) ((elem) == (list))
166 * Test if a list is empty.
170 * \return non-zero if list empty, or zero otherwise.
172 #define is_empty_list(list) ((list)->next == (list))
175 * Walk through the elements of a list.
177 * \param ptr pointer to the current element.
180 * \note It should be followed by a { } block or a single statement, as in a \c
183 #define foreach(ptr, list) \
184 for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next )
187 * Walk through the elements of a list.
189 * Same as #foreach but lets you unlink the current value during a list
190 * traversal. Useful for freeing a list, element by element.
192 * \param ptr pointer to the current element.
193 * \param t temporary pointer.
196 * \note It should be followed by a { } block or a single statement, as in a \c
199 #define foreach_s(ptr, t, list) \
200 for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next)