| 1 |
/* |
| 2 |
Copyright (c) 2007-2009, Troy D. Hanson |
| 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.7 |
| 28 |
|
| 29 |
/* From: http://uthash.sourceforge.net/utlist.html */ |
| 30 |
/* |
| 31 |
* This file contains macros to manipulate singly and doubly-linked lists. |
| 32 |
* |
| 33 |
* 1. LL_ macros: singly-linked lists. |
| 34 |
* 2. DL_ macros: doubly-linked lists. |
| 35 |
* 3. CDL_ macros: circular doubly-linked lists. |
| 36 |
* |
| 37 |
* To use singly-linked lists, your structure must have a "next" pointer. |
| 38 |
* To use doubly-linked lists, your structure must "prev" and "next" pointers. |
| 39 |
* Either way, the pointer to the head of the list must be initialized to NULL. |
| 40 |
* |
| 41 |
* ----------------.EXAMPLE ------------------------- |
| 42 |
* struct item { |
| 43 |
* int id; |
| 44 |
* struct item *prev, *next; |
| 45 |
* } |
| 46 |
* |
| 47 |
* struct item *list = NULL: |
| 48 |
* |
| 49 |
* int main() { |
| 50 |
* struct item *item; |
| 51 |
* ... allocate and populate item ... |
| 52 |
* DL_APPEND(list, item); |
| 53 |
* } |
| 54 |
* -------------------------------------------------- |
| 55 |
* |
| 56 |
* For doubly-linked lists, the append and delete macros are O(1) |
| 57 |
* For singly-linked lists, append and delete are O(n) but prepend is O(1) |
| 58 |
* The sort macro is O(n log(n)) for all types of single/double/circular lists. |
| 59 |
*/ |
| 60 |
|
| 61 |
|
| 62 |
/****************************************************************************** |
| 63 |
* doubly linked list macros (non-circular) * |
| 64 |
*****************************************************************************/ |
| 65 |
#define DL_PREPEND(head,add) \ |
| 66 |
do { \ |
| 67 |
(add)->next = head; \ |
| 68 |
if (head) { \ |
| 69 |
(add)->prev = (head)->prev; \ |
| 70 |
(head)->prev = (add); \ |
| 71 |
} else { \ |
| 72 |
(add)->prev = (add); \ |
| 73 |
} \ |
| 74 |
(head) = (add); \ |
| 75 |
} while (0) |
| 76 |
|
| 77 |
#define DL_APPEND(head,add) \ |
| 78 |
do { \ |
| 79 |
if (head) { \ |
| 80 |
(add)->prev = (head)->prev; \ |
| 81 |
(head)->prev->next = (add); \ |
| 82 |
(head)->prev = (add); \ |
| 83 |
(add)->next = NULL; \ |
| 84 |
} else { \ |
| 85 |
(head)=(add); \ |
| 86 |
(head)->prev = (head); \ |
| 87 |
(head)->next = NULL; \ |
| 88 |
} \ |
| 89 |
} while (0); |
| 90 |
|
| 91 |
#define DL_DELETE(head,del) \ |
| 92 |
do { \ |
| 93 |
if ((del)->prev == (del)) { \ |
| 94 |
(head)=NULL; \ |
| 95 |
} else if ((del)==(head)) { \ |
| 96 |
(del)->next->prev = (del)->prev; \ |
| 97 |
(head) = (del)->next; \ |
| 98 |
} else { \ |
| 99 |
(del)->prev->next = (del)->next; \ |
| 100 |
if ((del)->next) { \ |
| 101 |
(del)->next->prev = (del)->prev; \ |
| 102 |
} else { \ |
| 103 |
(head)->prev = (del)->prev; \ |
| 104 |
} \ |
| 105 |
} \ |
| 106 |
} while (0); |
| 107 |
|
| 108 |
|
| 109 |
#define DL_FOREACH(head,el) \ |
| 110 |
for(el=head;el;el=el->next) |
| 111 |
|
| 112 |
#define DL_FOREACH_SAFE(head,el,tmp) \ |
| 113 |
for(el=head,tmp=el->next;el;el=tmp,tmp=(el) ? (el->next) : NULL) |
| 114 |
|
| 115 |
#endif /* UTLIST_H */ |
| 116 |
|