| 1 |
/** |
| 2 |
* \file simple_list.h |
| 3 |
* Simple macros for type-safe, intrusive lists. |
| 4 |
* |
| 5 |
* Intended to work with a list sentinal which is created as an empty |
| 6 |
* list. Insert & delete are O(1). |
| 7 |
* |
| 8 |
* \author |
| 9 |
* (C) 1997, Keith Whitwell |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* Mesa 3-D graphics library |
| 14 |
* Version: 3.5 |
| 15 |
* |
| 16 |
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved. |
| 17 |
* |
| 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: |
| 24 |
* |
| 25 |
* The above copyright notice and this permission notice shall be included |
| 26 |
* in all copies or substantial portions of the Software. |
| 27 |
* |
| 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. |
| 34 |
*/ |
| 35 |
|
| 36 |
|
| 37 |
#ifndef _SIMPLE_LIST_H |
| 38 |
#define _SIMPLE_LIST_H |
| 39 |
|
| 40 |
/** |
| 41 |
* Remove an element from list. |
| 42 |
* |
| 43 |
* \param elem element to remove. |
| 44 |
*/ |
| 45 |
#define remove_from_list(elem) \ |
| 46 |
do { \ |
| 47 |
(elem)->next->prev = (elem)->prev; \ |
| 48 |
(elem)->prev->next = (elem)->next; \ |
| 49 |
} while (0) |
| 50 |
|
| 51 |
/** |
| 52 |
* Insert an element to the list head. |
| 53 |
* |
| 54 |
* \param list list. |
| 55 |
* \param elem element to insert. |
| 56 |
*/ |
| 57 |
#define insert_at_head(list, elem) \ |
| 58 |
do { \ |
| 59 |
(elem)->prev = list; \ |
| 60 |
(elem)->next = (list)->next; \ |
| 61 |
(list)->next->prev = elem; \ |
| 62 |
(list)->next = elem; \ |
| 63 |
} while(0) |
| 64 |
|
| 65 |
/** |
| 66 |
* Insert an element to the list tail. |
| 67 |
* |
| 68 |
* \param list list. |
| 69 |
* \param elem element to insert. |
| 70 |
*/ |
| 71 |
#define insert_at_tail(list, elem) \ |
| 72 |
do { \ |
| 73 |
(elem)->next = list; \ |
| 74 |
(elem)->prev = (list)->prev; \ |
| 75 |
(list)->prev->next = elem; \ |
| 76 |
(list)->prev = elem; \ |
| 77 |
} while(0) |
| 78 |
|
| 79 |
/** |
| 80 |
* Move an element to the list head. |
| 81 |
* |
| 82 |
* \param list list. |
| 83 |
* \param elem element to move. |
| 84 |
*/ |
| 85 |
#define move_to_head(list, elem) \ |
| 86 |
do { \ |
| 87 |
remove_from_list(elem); \ |
| 88 |
insert_at_head(list, elem); \ |
| 89 |
} while (0) |
| 90 |
|
| 91 |
/** |
| 92 |
* Move an element to the list tail. |
| 93 |
* |
| 94 |
* \param list list. |
| 95 |
* \param elem element to move. |
| 96 |
*/ |
| 97 |
#define move_to_tail(list, elem) \ |
| 98 |
do { \ |
| 99 |
remove_from_list(elem); \ |
| 100 |
insert_at_tail(list, elem); \ |
| 101 |
} while (0) |
| 102 |
|
| 103 |
/** |
| 104 |
* Make a empty list empty. |
| 105 |
* |
| 106 |
* \param sentinal list (sentinal element). |
| 107 |
*/ |
| 108 |
#define make_empty_list(sentinal) \ |
| 109 |
do { \ |
| 110 |
(sentinal)->next = sentinal; \ |
| 111 |
(sentinal)->prev = sentinal; \ |
| 112 |
} while (0) |
| 113 |
|
| 114 |
/** |
| 115 |
* Get list first element. |
| 116 |
* |
| 117 |
* \param list list. |
| 118 |
* |
| 119 |
* \return pointer to first element. |
| 120 |
*/ |
| 121 |
#define first_elem(list) ((list)->next) |
| 122 |
|
| 123 |
/** |
| 124 |
* Get list last element. |
| 125 |
* |
| 126 |
* \param list list. |
| 127 |
* |
| 128 |
* \return pointer to last element. |
| 129 |
*/ |
| 130 |
#define last_elem(list) ((list)->prev) |
| 131 |
|
| 132 |
/** |
| 133 |
* Get next element. |
| 134 |
* |
| 135 |
* \param elem element. |
| 136 |
* |
| 137 |
* \return pointer to next element. |
| 138 |
*/ |
| 139 |
#define next_elem(elem) ((elem)->next) |
| 140 |
|
| 141 |
/** |
| 142 |
* Get previous element. |
| 143 |
* |
| 144 |
* \param elem element. |
| 145 |
* |
| 146 |
* \return pointer to previous element. |
| 147 |
*/ |
| 148 |
#define prev_elem(elem) ((elem)->prev) |
| 149 |
|
| 150 |
/** |
| 151 |
* Test whether element is at end of the list. |
| 152 |
* |
| 153 |
* \param list list. |
| 154 |
* \param elem element. |
| 155 |
* |
| 156 |
* \return non-zero if element is at end of list, or zero otherwise. |
| 157 |
*/ |
| 158 |
#define at_end(list, elem) ((elem) == (list)) |
| 159 |
|
| 160 |
/** |
| 161 |
* Test if a list is empty. |
| 162 |
* |
| 163 |
* \param list list. |
| 164 |
* |
| 165 |
* \return non-zero if list empty, or zero otherwise. |
| 166 |
*/ |
| 167 |
#define is_empty_list(list) ((list)->next == (list)) |
| 168 |
|
| 169 |
/** |
| 170 |
* Walk through the elements of a list. |
| 171 |
* |
| 172 |
* \param ptr pointer to the current element. |
| 173 |
* \param list list. |
| 174 |
* |
| 175 |
* \note It should be followed by a { } block or a single statement, as in a \c |
| 176 |
* for loop. |
| 177 |
*/ |
| 178 |
#define foreach(ptr, list) \ |
| 179 |
for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next ) |
| 180 |
|
| 181 |
/** |
| 182 |
* Walk through the elements of a list. |
| 183 |
* |
| 184 |
* Same as #foreach but lets you unlink the current value during a list |
| 185 |
* traversal. Useful for freeing a list, element by element. |
| 186 |
* |
| 187 |
* \param ptr pointer to the current element. |
| 188 |
* \param t temporary pointer. |
| 189 |
* \param list list. |
| 190 |
* |
| 191 |
* \note It should be followed by a { } block or a single statement, as in a \c |
| 192 |
* for loop. |
| 193 |
*/ |
| 194 |
#define foreach_s(ptr, t, list) \ |
| 195 |
for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next) |
| 196 |
|
| 197 |
#endif |