2 Copyright (c) 2007-2009, Troy D. Hanson
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
13 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
15 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #define UTLIST_VERSION 1.8
30 * This file contains macros to manipulate singly and doubly-linked lists.
32 * 1. LL_ macros: singly-linked lists.
33 * 2. DL_ macros: doubly-linked lists.
34 * 3. CDL_ macros: circular doubly-linked lists.
36 * To use singly-linked lists, your structure must have a "next" pointer.
37 * To use doubly-linked lists, your structure must "prev" and "next" pointers.
38 * Either way, the pointer to the head of the list must be initialized to NULL.
40 * ----------------.EXAMPLE -------------------------
43 * struct item *prev, *next;
46 * struct item *list = NULL:
50 * ... allocate and populate item ...
51 * DL_APPEND(list, item);
53 * --------------------------------------------------
55 * For doubly-linked lists, the append and delete macros are O(1)
56 * For singly-linked lists, append and delete are O(n) but prepend is O(1)
57 * The sort macro is O(n log(n)) for all types of single/double/circular lists.
60 /******************************************************************************
61 * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort *
62 * Unwieldy variable names used here to avoid shadowing passed-in variables. *
63 *****************************************************************************/
64 #define LL_SORT(list, cmp) \
66 __typeof__(list)_ls_p, _ls_q, _ls_e, _ls_tail, _ls_oldhead; \
67 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
71 while (_ls_looping) { \
81 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
83 _ls_q = _ls_q->next; \
84 if (!_ls_q) { break; } \
86 _ls_qsize = _ls_insize; \
87 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
88 if (_ls_psize == 0) { \
89 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
91 else if (_ls_qsize == 0 || !_ls_q) { \
92 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
94 else if (cmp(_ls_p, _ls_q) <= 0) { \
95 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
98 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
101 _ls_tail->next = _ls_e; \
110 _ls_tail->next = NULL; \
111 if (_ls_nmerges <= 1) { \
120 #define DL_SORT(list, cmp) \
122 __typeof__(list)_ls_p, _ls_q, _ls_e, _ls_tail, _ls_oldhead; \
123 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
127 while (_ls_looping) { \
129 _ls_oldhead = list; \
137 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
139 _ls_q = _ls_q->next; \
140 if (!_ls_q) { break; } \
142 _ls_qsize = _ls_insize; \
143 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
144 if (_ls_psize == 0) { \
145 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
147 else if (_ls_qsize == 0 || !_ls_q) { \
148 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
150 else if (cmp(_ls_p, _ls_q) <= 0) { \
151 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
154 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
157 _ls_tail->next = _ls_e; \
162 _ls_e->prev = _ls_tail; \
167 list->prev = _ls_tail; \
168 _ls_tail->next = NULL; \
169 if (_ls_nmerges <= 1) { \
178 #define CDL_SORT(list, cmp) \
180 __typeof__(list)_ls_p, _ls_q, _ls_e, _ls_tail, _ls_oldhead; \
181 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
185 while (_ls_looping) { \
187 _ls_oldhead = list; \
195 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
197 _ls_q = ((_ls_q->next == _ls_oldhead) ? NULL : _ls_q->next); \
198 if (!_ls_q) { break; } \
200 _ls_qsize = _ls_insize; \
201 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
202 if (_ls_psize == 0) { \
203 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
204 if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
206 else if (_ls_qsize == 0 || !_ls_q) { \
207 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
208 if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
210 else if (cmp(_ls_p, _ls_q) <= 0) { \
211 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
212 if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
215 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
216 if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
219 _ls_tail->next = _ls_e; \
224 _ls_e->prev = _ls_tail; \
229 list->prev = _ls_tail; \
230 _ls_tail->next = list; \
231 if (_ls_nmerges <= 1) { \
240 /******************************************************************************
241 * singly linked list macros (non-circular) *
242 *****************************************************************************/
243 #define LL_PREPEND(head, add) \
245 (add)->next = head; \
250 #define LL_APPEND(head, add) \
252 __typeof__(head)_tmp; \
253 (add)->next = NULL; \
256 while (_tmp->next) { _tmp = _tmp->next; } \
257 _tmp->next = (add); \
265 #define LL_DELETE(head, del) \
267 __typeof__(head)_tmp; \
268 if ((head) == (del)) { \
269 (head) = (head)->next; \
273 while (_tmp->next && (_tmp->next != (del))) { \
277 _tmp->next = ((del)->next); \
283 #define LL_FOREACH(head, el) \
284 for (el = head; el; el = el->next)
286 /******************************************************************************
287 * doubly linked list macros (non-circular) *
288 *****************************************************************************/
289 #define DL_PREPEND(head, add) \
291 (add)->next = head; \
293 (add)->prev = (head)->prev; \
294 (head)->prev = (add); \
297 (add)->prev = (add); \
303 #define DL_APPEND(head, add) \
306 (add)->prev = (head)->prev; \
307 (head)->prev->next = (add); \
308 (head)->prev = (add); \
309 (add)->next = NULL; \
313 (head)->prev = (head); \
314 (head)->next = NULL; \
319 #define DL_DELETE(head, del) \
321 if ((del)->prev == (del)) { \
324 else if ((del) == (head)) { \
325 (del)->next->prev = (del)->prev; \
326 (head) = (del)->next; \
329 (del)->prev->next = (del)->next; \
331 (del)->next->prev = (del)->prev; \
334 (head)->prev = (del)->prev; \
341 #define DL_FOREACH(head, el) \
342 for (el = head; el; el = el->next)
344 /******************************************************************************
345 * circular doubly linked list macros *
346 *****************************************************************************/
347 #define CDL_PREPEND(head, add) \
350 (add)->prev = (head)->prev; \
351 (add)->next = (head); \
352 (head)->prev = (add); \
353 (add)->prev->next = (add); \
356 (add)->prev = (add); \
357 (add)->next = (add); \
363 #define CDL_DELETE(head, del) \
365 if (((head) == (del)) && ((head)->next == (head))) { \
369 (del)->next->prev = (del)->prev; \
370 (del)->prev->next = (del)->next; \
371 if ((del) == (head)) { (head) = (del)->next; } \
376 #define CDL_FOREACH(head, el) \
377 for (el = head; el; el = (el->next == head ? 0L : el->next))
380 #endif /* UTLIST_H */