Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / libraries / inc / utlist.h
blobef833289f9f22b530e961b01c1d72776c384c8a8
1 /*
2 Copyright (c) 2007-2009, Troy D. Hanson
3 All rights reserved.
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.
24 #ifndef UTLIST_H
25 #define UTLIST_H
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 -------------------------
41 * struct item {
42 * int id;
43 * struct item *prev, *next;
44 * }
46 * struct item *list = NULL:
48 * int main() {
49 * struct item *item;
50 * ... allocate and populate item ...
51 * DL_APPEND(list, item);
52 * }
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) \
65 do { \
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; \
68 if (list) { \
69 _ls_insize = 1; \
70 _ls_looping = 1; \
71 while (_ls_looping) { \
72 _ls_p = list; \
73 _ls_oldhead = list; \
74 list = NULL; \
75 _ls_tail = NULL; \
76 _ls_nmerges = 0; \
77 while (_ls_p) { \
78 _ls_nmerges++; \
79 _ls_q = _ls_p; \
80 _ls_psize = 0; \
81 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
82 _ls_psize++; \
83 _ls_q = _ls_q->next; \
84 if (!_ls_q) { break; } \
85 } \
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--; \
90 } \
91 else if (_ls_qsize == 0 || !_ls_q) { \
92 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
93 } \
94 else if (cmp(_ls_p, _ls_q) <= 0) { \
95 _ls_e = _ls_p; _ls_p = _ls_p->next; _ls_psize--; \
96 } \
97 else { \
98 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
99 } \
100 if (_ls_tail) { \
101 _ls_tail->next = _ls_e; \
103 else { \
104 list = _ls_e; \
106 _ls_tail = _ls_e; \
108 _ls_p = _ls_q; \
110 _ls_tail->next = NULL; \
111 if (_ls_nmerges <= 1) { \
112 _ls_looping = 0; \
114 _ls_insize *= 2; \
118 while (0)
120 #define DL_SORT(list, cmp) \
121 do { \
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; \
124 if (list) { \
125 _ls_insize = 1; \
126 _ls_looping = 1; \
127 while (_ls_looping) { \
128 _ls_p = list; \
129 _ls_oldhead = list; \
130 list = NULL; \
131 _ls_tail = NULL; \
132 _ls_nmerges = 0; \
133 while (_ls_p) { \
134 _ls_nmerges++; \
135 _ls_q = _ls_p; \
136 _ls_psize = 0; \
137 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
138 _ls_psize++; \
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--; \
153 else { \
154 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
156 if (_ls_tail) { \
157 _ls_tail->next = _ls_e; \
159 else { \
160 list = _ls_e; \
162 _ls_e->prev = _ls_tail; \
163 _ls_tail = _ls_e; \
165 _ls_p = _ls_q; \
167 list->prev = _ls_tail; \
168 _ls_tail->next = NULL; \
169 if (_ls_nmerges <= 1) { \
170 _ls_looping = 0; \
172 _ls_insize *= 2; \
176 while (0)
178 #define CDL_SORT(list, cmp) \
179 do { \
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; \
182 if (list) { \
183 _ls_insize = 1; \
184 _ls_looping = 1; \
185 while (_ls_looping) { \
186 _ls_p = list; \
187 _ls_oldhead = list; \
188 list = NULL; \
189 _ls_tail = NULL; \
190 _ls_nmerges = 0; \
191 while (_ls_p) { \
192 _ls_nmerges++; \
193 _ls_q = _ls_p; \
194 _ls_psize = 0; \
195 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
196 _ls_psize++; \
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; } \
214 else { \
215 _ls_e = _ls_q; _ls_q = _ls_q->next; _ls_qsize--; \
216 if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
218 if (_ls_tail) { \
219 _ls_tail->next = _ls_e; \
221 else { \
222 list = _ls_e; \
224 _ls_e->prev = _ls_tail; \
225 _ls_tail = _ls_e; \
227 _ls_p = _ls_q; \
229 list->prev = _ls_tail; \
230 _ls_tail->next = list; \
231 if (_ls_nmerges <= 1) { \
232 _ls_looping = 0; \
234 _ls_insize *= 2; \
238 while (0)
240 /******************************************************************************
241 * singly linked list macros (non-circular) *
242 *****************************************************************************/
243 #define LL_PREPEND(head, add) \
244 do { \
245 (add)->next = head; \
246 head = add; \
248 while (0)
250 #define LL_APPEND(head, add) \
251 do { \
252 __typeof__(head)_tmp; \
253 (add)->next = NULL; \
254 if (head) { \
255 _tmp = head; \
256 while (_tmp->next) { _tmp = _tmp->next; } \
257 _tmp->next = (add); \
259 else { \
260 (head) = (add); \
263 while (0)
265 #define LL_DELETE(head, del) \
266 do { \
267 __typeof__(head)_tmp; \
268 if ((head) == (del)) { \
269 (head) = (head)->next; \
271 else { \
272 _tmp = head; \
273 while (_tmp->next && (_tmp->next != (del))) { \
274 _tmp = _tmp->next; \
276 if (_tmp->next) { \
277 _tmp->next = ((del)->next); \
281 while (0)
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) \
290 do { \
291 (add)->next = head; \
292 if (head) { \
293 (add)->prev = (head)->prev; \
294 (head)->prev = (add); \
296 else { \
297 (add)->prev = (add); \
299 (head) = (add); \
301 while (0)
303 #define DL_APPEND(head, add) \
304 do { \
305 if (head) { \
306 (add)->prev = (head)->prev; \
307 (head)->prev->next = (add); \
308 (head)->prev = (add); \
309 (add)->next = NULL; \
311 else { \
312 (head) = (add); \
313 (head)->prev = (head); \
314 (head)->next = NULL; \
317 while (0);
319 #define DL_DELETE(head, del) \
320 do { \
321 if ((del)->prev == (del)) { \
322 (head) = NULL; \
324 else if ((del) == (head)) { \
325 (del)->next->prev = (del)->prev; \
326 (head) = (del)->next; \
328 else { \
329 (del)->prev->next = (del)->next; \
330 if ((del)->next) { \
331 (del)->next->prev = (del)->prev; \
333 else { \
334 (head)->prev = (del)->prev; \
338 while (0);
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) \
348 do { \
349 if (head) { \
350 (add)->prev = (head)->prev; \
351 (add)->next = (head); \
352 (head)->prev = (add); \
353 (add)->prev->next = (add); \
355 else { \
356 (add)->prev = (add); \
357 (add)->next = (add); \
359 (head) = (add); \
361 while (0)
363 #define CDL_DELETE(head, del) \
364 do { \
365 if (((head) == (del)) && ((head)->next == (head))) { \
366 (head) = 0L; \
368 else { \
369 (del)->next->prev = (del)->prev; \
370 (del)->prev->next = (del)->next; \
371 if ((del) == (head)) { (head) = (del)->next; } \
374 while (0);
376 #define CDL_FOREACH(head, el) \
377 for (el = head; el; el = (el->next == head ? 0L : el->next))
380 #endif /* UTLIST_H */