Debian Codegraph @

bcftools/utlist.h

raw symbols SCIP
1 /*
2 Copyright (c) 2007-2014, Troy D. Hanson http://troydhanson.github.com/uthash/
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
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.
22 */
23
24 #ifndef UTLIST_H
25 #define UTLIST_H
26
27 #define UTLIST_VERSION 1.9.9
28
29 #include <assert.h>
30
31 /*
32 * This file contains macros to manipulate singly and doubly-linked lists.
33 *
34 * 1. LL_ macros: singly-linked lists.
35 * 2. DL_ macros: doubly-linked lists.
36 * 3. CDL_ macros: circular doubly-linked lists.
37 *
38 * To use singly-linked lists, your structure must have a "next" pointer.
39 * To use doubly-linked lists, your structure must "prev" and "next" pointers.
40 * Either way, the pointer to the head of the list must be initialized to NULL.
41 *
42 * ----------------.EXAMPLE -------------------------
43 * struct item {
44 * int id;
45 * struct item *prev, *next;
46 * }
47 *
48 * struct item *list = NULL:
49 *
50 * int main() {
51 * struct item *item;
52 * ... allocate and populate item ...
53 * DL_APPEND(list, item);
54 * }
55 * --------------------------------------------------
56 *
57 * For doubly-linked lists, the append and delete macros are O(1)
58 * For singly-linked lists, append and delete are O(n) but prepend is O(1)
59 * The sort macro is O(n log(n)) for all types of single/double/circular lists.
60 */
61
62 /* These macros use decltype or the earlier __typeof GNU extension.
63 As decltype is only available in newer compilers (VS2010 or gcc 4.3+
64 when compiling c++ code), this code uses whatever method is needed
65 or, for VS2008 where neither is available, uses casting workarounds. */
66 #ifdef _MSC_VER /* MS compiler */
67 #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */
68 #define LDECLTYPE(x) decltype(x)
69 #else /* VS2008 or older (or VS2010 in C mode) */
70 #define NO_DECLTYPE
71 #define LDECLTYPE(x) char*
72 #endif
73 #elif defined(__ICCARM__)
74 #define NO_DECLTYPE
75 #define LDECLTYPE(x) char*
76 #else /* GNU, Sun and other compilers */
77 #define LDECLTYPE(x) __typeof(x)
78 #endif
79
80 /* for VS2008 we use some workarounds to get around the lack of decltype,
81 * namely, we always reassign our tmp variable to the list head if we need
82 * to dereference its prev/next pointers, and save/restore the real head.*/
83 #ifdef NO_DECLTYPE
84 #define _SV(elt,list) _tmp = (char*)(list); {char **_alias = (char**)&(list); *_alias = (elt); }
85 #define _NEXT(elt,list,next) ((char*)((list)->next))
86 #define _NEXTASGN(elt,list,to,next) { char **_alias = (char**)&((list)->next); *_alias=(char*)(to); }
87 /* #define _PREV(elt,list,prev) ((char*)((list)->prev)) */
88 #define _PREVASGN(elt,list,to,prev) { char **_alias = (char**)&((list)->prev); *_alias=(char*)(to); }
89 #define _RS(list) { char **_alias = (char**)&(list); *_alias=_tmp; }
90 #define _CASTASGN(a,b) { char **_alias = (char**)&(a); *_alias=(char*)(b); }
91 #else
92 #define _SV(elt,list)
93 #define _NEXT(elt,list,next) ((elt)->next)
94 #define _NEXTASGN(elt,list,to,next) ((elt)->next)=(to)
95 /* #define _PREV(elt,list,prev) ((elt)->prev) */
96 #define _PREVASGN(elt,list,to,prev) ((elt)->prev)=(to)
97 #define _RS(list)
98 #define _CASTASGN(a,b) (a)=(b)
99 #endif
100
101 /******************************************************************************
102 * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort *
103 * Unwieldy variable names used here to avoid shadowing passed-in variables. *
104 *****************************************************************************/
105 #define LL_SORT(list, cmp) \
106 LL_SORT2(list, cmp, next)
107
108 #define LL_SORT2(list, cmp, next) \
109 do { \
110 LDECLTYPE(list) _ls_p; \
111 LDECLTYPE(list) _ls_q; \
112 LDECLTYPE(list) _ls_e; \
113 LDECLTYPE(list) _ls_tail; \
114 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
115 if (list) { \
116 _ls_insize = 1; \
117 _ls_looping = 1; \
118 while (_ls_looping) { \
119 _CASTASGN(_ls_p,list); \
120 list = NULL; \
121 _ls_tail = NULL; \
122 _ls_nmerges = 0; \
123 while (_ls_p) { \
124 _ls_nmerges++; \
125 _ls_q = _ls_p; \
126 _ls_psize = 0; \
127 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
128 _ls_psize++; \
129 _SV(_ls_q,list); _ls_q = _NEXT(_ls_q,list,next); _RS(list); \
130 if (!_ls_q) break; \
131 } \
132 _ls_qsize = _ls_insize; \
133 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
134 if (_ls_psize == 0) { \
135 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
136 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
137 } else if (_ls_qsize == 0 || !_ls_q) { \
138 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
139 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
140 } else if (cmp(_ls_p,_ls_q) <= 0) { \
141 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
142 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
143 } else { \
144 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
145 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
146 } \
147 if (_ls_tail) { \
148 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list); \
149 } else { \
150 _CASTASGN(list,_ls_e); \
151 } \
152 _ls_tail = _ls_e; \
153 } \
154 _ls_p = _ls_q; \
155 } \
156 if (_ls_tail) { \
157 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,NULL,next); _RS(list); \
158 } \
159 if (_ls_nmerges <= 1) { \
160 _ls_looping=0; \
161 } \
162 _ls_insize *= 2; \
163 } \
164 } \
165 } while (0)
166
167
168 #define DL_SORT(list, cmp) \
169 DL_SORT2(list, cmp, prev, next)
170
171 #define DL_SORT2(list, cmp, prev, next) \
172 do { \
173 LDECLTYPE(list) _ls_p; \
174 LDECLTYPE(list) _ls_q; \
175 LDECLTYPE(list) _ls_e; \
176 LDECLTYPE(list) _ls_tail; \
177 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
178 if (list) { \
179 _ls_insize = 1; \
180 _ls_looping = 1; \
181 while (_ls_looping) { \
182 _CASTASGN(_ls_p,list); \
183 list = NULL; \
184 _ls_tail = NULL; \
185 _ls_nmerges = 0; \
186 while (_ls_p) { \
187 _ls_nmerges++; \
188 _ls_q = _ls_p; \
189 _ls_psize = 0; \
190 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
191 _ls_psize++; \
192 _SV(_ls_q,list); _ls_q = _NEXT(_ls_q,list,next); _RS(list); \
193 if (!_ls_q) break; \
194 } \
195 _ls_qsize = _ls_insize; \
196 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
197 if (_ls_psize == 0) { \
198 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
199 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
200 } else if (_ls_qsize == 0 || !_ls_q) { \
201 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
202 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
203 } else if (cmp(_ls_p,_ls_q) <= 0) { \
204 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
205 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
206 } else { \
207 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
208 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
209 } \
210 if (_ls_tail) { \
211 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list); \
212 } else { \
213 _CASTASGN(list,_ls_e); \
214 } \
215 _SV(_ls_e,list); _PREVASGN(_ls_e,list,_ls_tail,prev); _RS(list); \
216 _ls_tail = _ls_e; \
217 } \
218 _ls_p = _ls_q; \
219 } \
220 _CASTASGN(list->prev, _ls_tail); \
221 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,NULL,next); _RS(list); \
222 if (_ls_nmerges <= 1) { \
223 _ls_looping=0; \
224 } \
225 _ls_insize *= 2; \
226 } \
227 } \
228 } while (0)
229
230
231 #define DL_HEAD(list) (list)
232 #define DL_TAIL(list) ((list) ? (list)->prev : NULL)
233
234 #define CDL_SORT(list, cmp) \
235 CDL_SORT2(list, cmp, prev, next)
236
237 #define CDL_SORT2(list, cmp, prev, next) \
238 do { \
239 LDECLTYPE(list) _ls_p; \
240 LDECLTYPE(list) _ls_q; \
241 LDECLTYPE(list) _ls_e; \
242 LDECLTYPE(list) _ls_tail; \
243 LDECLTYPE(list) _ls_oldhead; \
244 LDECLTYPE(list) _tmp; \
245 int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
246 if (list) { \
247 _ls_insize = 1; \
248 _ls_looping = 1; \
249 while (_ls_looping) { \
250 _CASTASGN(_ls_p,list); \
251 _CASTASGN(_ls_oldhead,list); \
252 list = NULL; \
253 _ls_tail = NULL; \
254 _ls_nmerges = 0; \
255 while (_ls_p) { \
256 _ls_nmerges++; \
257 _ls_q = _ls_p; \
258 _ls_psize = 0; \
259 for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
260 _ls_psize++; \
261 _SV(_ls_q,list); \
262 if (_NEXT(_ls_q,list,next) == _ls_oldhead) { \
263 _ls_q = NULL; \
264 } else { \
265 _ls_q = _NEXT(_ls_q,list,next); \
266 } \
267 _RS(list); \
268 if (!_ls_q) break; \
269 } \
270 _ls_qsize = _ls_insize; \
271 while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
272 if (_ls_psize == 0) { \
273 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
274 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
275 if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
276 } else if (_ls_qsize == 0 || !_ls_q) { \
277 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
278 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
279 if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
280 } else if (cmp(_ls_p,_ls_q) <= 0) { \
281 _ls_e = _ls_p; _SV(_ls_p,list); _ls_p = \
282 _NEXT(_ls_p,list,next); _RS(list); _ls_psize--; \
283 if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
284 } else { \
285 _ls_e = _ls_q; _SV(_ls_q,list); _ls_q = \
286 _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--; \
287 if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
288 } \
289 if (_ls_tail) { \
290 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list); \
291 } else { \
292 _CASTASGN(list,_ls_e); \
293 } \
294 _SV(_ls_e,list); _PREVASGN(_ls_e,list,_ls_tail,prev); _RS(list); \
295 _ls_tail = _ls_e; \
296 } \
297 _ls_p = _ls_q; \
298 } \
299 _CASTASGN(list->prev,_ls_tail); \
300 _CASTASGN(_tmp,list); \
301 _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_tmp,next); _RS(list); \
302 if (_ls_nmerges <= 1) { \
303 _ls_looping=0; \
304 } \
305 _ls_insize *= 2; \
306 } \
307 } \
308 } while (0)
309
310 /******************************************************************************
311 * singly linked list macros (non-circular) *
312 *****************************************************************************/
313 #define LL_PREPEND(head,add) \
314 LL_PREPEND2(head,add,next)
315
316 #define LL_PREPEND2(head,add,next) \
317 do { \
318 (add)->next = head; \
319 head = add; \
320 } while (0)
321
322 #define LL_CONCAT(head1,head2) \
323 LL_CONCAT2(head1,head2,next)
324
325 #define LL_CONCAT2(head1,head2,next) \
326 do { \
327 LDECLTYPE(head1) _tmp; \
328 if (head1) { \
329 _tmp = head1; \
330 while (_tmp->next) { _tmp = _tmp->next; } \
331 _tmp->next=(head2); \
332 } else { \
333 (head1)=(head2); \
334 } \
335 } while (0)
336
337 #define LL_APPEND(head,add) \
338 LL_APPEND2(head,add,next)
339
340 #define LL_APPEND2(head,add,next) \
341 do { \
342 LDECLTYPE(head) _tmp; \
343 (add)->next=NULL; \
344 if (head) { \
345 _tmp = head; \
346 while (_tmp->next) { _tmp = _tmp->next; } \
347 _tmp->next=(add); \
348 } else { \
349 (head)=(add); \
350 } \
351 } while (0)
352
353 #define LL_DELETE(head,del) \
354 LL_DELETE2(head,del,next)
355
356 #define LL_DELETE2(head,del,next) \
357 do { \
358 LDECLTYPE(head) _tmp; \
359 if ((head) == (del)) { \
360 (head)=(head)->next; \
361 } else { \
362 _tmp = head; \
363 while (_tmp->next && (_tmp->next != (del))) { \
364 _tmp = _tmp->next; \
365 } \
366 if (_tmp->next) { \
367 _tmp->next = ((del)->next); \
368 } \
369 } \
370 } while (0)
371
372 /* Here are VS2008 replacements for LL_APPEND and LL_DELETE */
373 #define LL_APPEND_VS2008(head,add) \
374 LL_APPEND2_VS2008(head,add,next)
375
376 #define LL_APPEND2_VS2008(head,add,next) \
377 do { \
378 if (head) { \
379 (add)->next = head; /* use add->next as a temp variable */ \
380 while ((add)->next->next) { (add)->next = (add)->next->next; } \
381 (add)->next->next=(add); \
382 } else { \
383 (head)=(add); \
384 } \
385 (add)->next=NULL; \
386 } while (0)
387
388 #define LL_DELETE_VS2008(head,del) \
389 LL_DELETE2_VS2008(head,del,next)
390
391 #define LL_DELETE2_VS2008(head,del,next) \
392 do { \
393 if ((head) == (del)) { \
394 (head)=(head)->next; \
395 } else { \
396 char *_tmp = (char*)(head); \
397 while ((head)->next && ((head)->next != (del))) { \
398 head = (head)->next; \
399 } \
400 if ((head)->next) { \
401 (head)->next = ((del)->next); \
402 } \
403 { \
404 char **_head_alias = (char**)&(head); \
405 *_head_alias = _tmp; \
406 } \
407 } \
408 } while (0)
409 #ifdef NO_DECLTYPE
410 #undef LL_APPEND
411 #define LL_APPEND LL_APPEND_VS2008
412 #undef LL_DELETE
413 #define LL_DELETE LL_DELETE_VS2008
414 #undef LL_DELETE2
415 #define LL_DELETE2 LL_DELETE2_VS2008
416 #undef LL_APPEND2
417 #define LL_APPEND2 LL_APPEND2_VS2008
418 #undef LL_CONCAT /* no LL_CONCAT_VS2008 */
419 #undef DL_CONCAT /* no DL_CONCAT_VS2008 */
420 #endif
421 /* end VS2008 replacements */
422
423 #define LL_COUNT(head,el,counter) \
424 LL_COUNT2(head,el,counter,next) \
425
426 #define LL_COUNT2(head,el,counter,next) \
427 { \
428 counter = 0; \
429 LL_FOREACH2(head,el,next){ ++counter; } \
430 }
431
432 #define LL_FOREACH(head,el) \
433 LL_FOREACH2(head,el,next)
434
435 #define LL_FOREACH2(head,el,next) \
436 for(el=head;el;el=(el)->next)
437
438 #define LL_FOREACH_SAFE(head,el,tmp) \
439 LL_FOREACH_SAFE2(head,el,tmp,next)
440
441 #define LL_FOREACH_SAFE2(head,el,tmp,next) \
442 for((el)=(head);(el) && (tmp = (el)->next, 1); (el) = tmp)
443
444 #define LL_SEARCH_SCALAR(head,out,field,val) \
445 LL_SEARCH_SCALAR2(head,out,field,val,next)
446
447 #define LL_SEARCH_SCALAR2(head,out,field,val,next) \
448 do { \
449 LL_FOREACH2(head,out,next) { \
450 if ((out)->field == (val)) break; \
451 } \
452 } while(0)
453
454 #define LL_SEARCH(head,out,elt,cmp) \
455 LL_SEARCH2(head,out,elt,cmp,next)
456
457 #define LL_SEARCH2(head,out,elt,cmp,next) \
458 do { \
459 LL_FOREACH2(head,out,next) { \
460 if ((cmp(out,elt))==0) break; \
461 } \
462 } while(0)
463
464 #define LL_REPLACE_ELEM(head, el, add) \
465 do { \
466 LDECLTYPE(head) _tmp; \
467 assert(head != NULL); \
468 assert(el != NULL); \
469 assert(add != NULL); \
470 (add)->next = (el)->next; \
471 if ((head) == (el)) { \
472 (head) = (add); \
473 } else { \
474 _tmp = head; \
475 while (_tmp->next && (_tmp->next != (el))) { \
476 _tmp = _tmp->next; \
477 } \
478 if (_tmp->next) { \
479 _tmp->next = (add); \
480 } \
481 } \
482 } while (0)
483
484 #define LL_PREPEND_ELEM(head, el, add) \
485 do { \
486 LDECLTYPE(head) _tmp; \
487 assert(head != NULL); \
488 assert(el != NULL); \
489 assert(add != NULL); \
490 (add)->next = (el); \
491 if ((head) == (el)) { \
492 (head) = (add); \
493 } else { \
494 _tmp = head; \
495 while (_tmp->next && (_tmp->next != (el))) { \
496 _tmp = _tmp->next; \
497 } \
498 if (_tmp->next) { \
499 _tmp->next = (add); \
500 } \
501 } \
502 } while (0) \
503
504
505 /******************************************************************************
506 * doubly linked list macros (non-circular) *
507 *****************************************************************************/
508 #define DL_PREPEND(head,add) \
509 DL_PREPEND2(head,add,prev,next)
510
511 #define DL_PREPEND2(head,add,prev,next) \
512 do { \
513 (add)->next = head; \
514 if (head) { \
515 (add)->prev = (head)->prev; \
516 (head)->prev = (add); \
517 } else { \
518 (add)->prev = (add); \
519 } \
520 (head) = (add); \
521 } while (0)
522
523 #define DL_APPEND(head,add) \
524 DL_APPEND2(head,add,prev,next)
525
526 #define DL_APPEND2(head,add,prev,next) \
527 do { \
528 if (head) { \
529 (add)->prev = (head)->prev; \
530 (head)->prev->next = (add); \
531 (head)->prev = (add); \
532 (add)->next = NULL; \
533 } else { \
534 (head)=(add); \
535 (head)->prev = (head); \
536 (head)->next = NULL; \
537 } \
538 } while (0)
539
540 #define DL_CONCAT(head1,head2) \
541 DL_CONCAT2(head1,head2,prev,next)
542
543 #define DL_CONCAT2(head1,head2,prev,next) \
544 do { \
545 LDECLTYPE(head1) _tmp; \
546 if (head2) { \
547 if (head1) { \
548 _tmp = (head2)->prev; \
549 (head2)->prev = (head1)->prev; \
550 (head1)->prev->next = (head2); \
551 (head1)->prev = _tmp; \
552 } else { \
553 (head1)=(head2); \
554 } \
555 } \
556 } while (0)
557
558 #define DL_DELETE(head,del) \
559 DL_DELETE2(head,del,prev,next)
560
561 #define DL_DELETE2(head,del,prev,next) \
562 do { \
563 assert((del)->prev != NULL); \
564 if ((del)->prev == (del)) { \
565 (head)=NULL; \
566 } else if ((del)==(head)) { \
567 (del)->next->prev = (del)->prev; \
568 (head) = (del)->next; \
569 } else { \
570 (del)->prev->next = (del)->next; \
571 if ((del)->next) { \
572 (del)->next->prev = (del)->prev; \
573 } else { \
574 (head)->prev = (del)->prev; \
575 } \
576 } \
577 } while (0)
578
579 #define DL_COUNT(head,el,counter) \
580 DL_COUNT2(head,el,counter,next) \
581
582 #define DL_COUNT2(head,el,counter,next) \
583 { \
584 counter = 0; \
585 DL_FOREACH2(head,el,next){ ++counter; } \
586 }
587
588 #define DL_FOREACH(head,el) \
589 DL_FOREACH2(head,el,next)
590
591 #define DL_FOREACH2(head,el,next) \
592 for(el=head;el;el=(el)->next)
593
594 /* this version is safe for deleting the elements during iteration */
595 #define DL_FOREACH_SAFE(head,el,tmp) \
596 DL_FOREACH_SAFE2(head,el,tmp,next)
597
598 #define DL_FOREACH_SAFE2(head,el,tmp,next) \
599 for((el)=(head);(el) && (tmp = (el)->next, 1); (el) = tmp)
600
601 /* these are identical to their singly-linked list counterparts */
602 #define DL_SEARCH_SCALAR LL_SEARCH_SCALAR
603 #define DL_SEARCH LL_SEARCH
604 #define DL_SEARCH_SCALAR2 LL_SEARCH_SCALAR2
605 #define DL_SEARCH2 LL_SEARCH2
606
607 #define DL_REPLACE_ELEM(head, el, add) \
608 do { \
609 assert(head != NULL); \
610 assert(el != NULL); \
611 assert(add != NULL); \
612 if ((head) == (el)) { \
613 (head) = (add); \
614 (add)->next = (el)->next; \
615 if ((el)->next == NULL) { \
616 (add)->prev = (add); \
617 } else { \
618 (add)->prev = (el)->prev; \
619 (add)->next->prev = (add); \
620 } \
621 } else { \
622 (add)->next = (el)->next; \
623 (add)->prev = (el)->prev; \
624 (add)->prev->next = (add); \
625 if ((el)->next == NULL) { \
626 (head)->prev = (add); \
627 } else { \
628 (add)->next->prev = (add); \
629 } \
630 } \
631 } while (0)
632
633 #define DL_PREPEND_ELEM(head, el, add) \
634 do { \
635 assert(head != NULL); \
636 assert(el != NULL); \
637 assert(add != NULL); \
638 (add)->next = (el); \
639 (add)->prev = (el)->prev; \
640 (el)->prev = (add); \
641 if ((head) == (el)) { \
642 (head) = (add); \
643 } else { \
644 (add)->prev->next = (add); \
645 } \
646 } while (0) \
647
648
649 /******************************************************************************
650 * circular doubly linked list macros *
651 *****************************************************************************/
652 #define CDL_PREPEND(head,add) \
653 CDL_PREPEND2(head,add,prev,next)
654
655 #define CDL_PREPEND2(head,add,prev,next) \
656 do { \
657 if (head) { \
658 (add)->prev = (head)->prev; \
659 (add)->next = (head); \
660 (head)->prev = (add); \
661 (add)->prev->next = (add); \
662 } else { \
663 (add)->prev = (add); \
664 (add)->next = (add); \
665 } \
666 (head)=(add); \
667 } while (0)
668
669 #define CDL_DELETE(head,del) \
670 CDL_DELETE2(head,del,prev,next)
671
672 #define CDL_DELETE2(head,del,prev,next) \
673 do { \
674 if ( ((head)==(del)) && ((head)->next == (head))) { \
675 (head) = 0L; \
676 } else { \
677 (del)->next->prev = (del)->prev; \
678 (del)->prev->next = (del)->next; \
679 if ((del) == (head)) (head)=(del)->next; \
680 } \
681 } while (0)
682
683 #define CDL_COUNT(head,el,counter) \
684 CDL_COUNT2(head,el,counter,next) \
685
686 #define CDL_COUNT2(head, el, counter,next) \
687 { \
688 counter = 0; \
689 CDL_FOREACH2(head,el,next){ ++counter; } \
690 }
691
692 #define CDL_FOREACH(head,el) \
693 CDL_FOREACH2(head,el,next)
694
695 #define CDL_FOREACH2(head,el,next) \
696 for(el=head;el;el=((el)->next==head ? 0L : (el)->next))
697
698 #define CDL_FOREACH_SAFE(head,el,tmp1,tmp2) \
699 CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next)
700
701 #define CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next) \
702 for((el)=(head), ((tmp1)=(head)?((head)->prev):NULL); \
703 (el) && ((tmp2)=(el)->next, 1); \
704 ((el) = (((el)==(tmp1)) ? 0L : (tmp2))))
705
706 #define CDL_SEARCH_SCALAR(head,out,field,val) \
707 CDL_SEARCH_SCALAR2(head,out,field,val,next)
708
709 #define CDL_SEARCH_SCALAR2(head,out,field,val,next) \
710 do { \
711 CDL_FOREACH2(head,out,next) { \
712 if ((out)->field == (val)) break; \
713 } \
714 } while(0)
715
716 #define CDL_SEARCH(head,out,elt,cmp) \
717 CDL_SEARCH2(head,out,elt,cmp,next)
718
719 #define CDL_SEARCH2(head,out,elt,cmp,next) \
720 do { \
721 CDL_FOREACH2(head,out,next) { \
722 if ((cmp(out,elt))==0) break; \
723 } \
724 } while(0)
725
726 #define CDL_REPLACE_ELEM(head, el, add) \
727 do { \
728 assert(head != NULL); \
729 assert(el != NULL); \
730 assert(add != NULL); \
731 if ((el)->next == (el)) { \
732 (add)->next = (add); \
733 (add)->prev = (add); \
734 (head) = (add); \
735 } else { \
736 (add)->next = (el)->next; \
737 (add)->prev = (el)->prev; \
738 (add)->next->prev = (add); \
739 (add)->prev->next = (add); \
740 if ((head) == (el)) { \
741 (head) = (add); \
742 } \
743 } \
744 } while (0)
745
746 #define CDL_PREPEND_ELEM(head, el, add) \
747 do { \
748 assert(head != NULL); \
749 assert(el != NULL); \
750 assert(add != NULL); \
751 (add)->next = (el); \
752 (add)->prev = (el)->prev; \
753 (el)->prev = (add); \
754 (add)->prev->next = (add); \
755 if ((head) == (el)) { \
756 (head) = (add); \
757 } \
758 } while (0) \
759
760 #endif /* UTLIST_H */
761