/src/rt/uthash/utlist.h

http://github.com/jruderman/rust · C Header · 280 lines · 188 code · 20 blank · 72 comment · 80 complexity · 46e17b096ad08131da73e6a2258645de MD5 · raw file

  1. /*
  2. Copyright (c) 2007-2009, Troy D. Hanson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. #ifndef UTLIST_H
  21. #define UTLIST_H
  22. #define UTLIST_VERSION 1.0
  23. /* C++ requires extra stringent casting */
  24. #if defined __cplusplus
  25. #define LTYPEOF(x) (typeof(x))
  26. #else
  27. #define LTYPEOF(x)
  28. #endif
  29. /*
  30. * This file contains macros to manipulate singly and doubly-linked lists.
  31. *
  32. * 1. LL_ macros: singly-linked lists.
  33. * 2. DL_ macros: doubly-linked lists.
  34. * 3. CDL_ macros: circular doubly-linked lists.
  35. *
  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.
  39. *
  40. * ----------------.EXAMPLE -------------------------
  41. * struct item {
  42. * int id;
  43. * struct item *prev, *next;
  44. * }
  45. *
  46. * struct item *list = NULL:
  47. *
  48. * int main() {
  49. * struct item *item;
  50. * ... allocate and populate item ...
  51. * DL_APPEND(list, item);
  52. * }
  53. * --------------------------------------------------
  54. *
  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.
  58. */
  59. /******************************************************************************
  60. * The SORT macros *
  61. *****************************************************************************/
  62. #define LL_SORT(l,cmp) \
  63. LISTSORT(l,0,0,FIELD_OFFSET(l,next),cmp)
  64. #define DL_SORT(l,cmp) \
  65. LISTSORT(l,0,FIELD_OFFSET(l,prev),FIELD_OFFSET(l,next),cmp)
  66. #define CDL_SORT(l,cmp) \
  67. LISTSORT(l,1,FIELD_OFFSET(l,prev),FIELD_OFFSET(l,next),cmp)
  68. /* The macros can't assume or cast to the caller's list element type. So we use
  69. * a couple tricks when we need to deal with those element's prev/next pointers.
  70. * Basically we use char pointer arithmetic to get those field offsets. */
  71. #define FIELD_OFFSET(ptr,field) ((char*)&((ptr)->field) - (char*)(ptr))
  72. #define LNEXT(e,no) (*(char**)(((char*)e) + no))
  73. #define LPREV(e,po) (*(char**)(((char*)e) + po))
  74. /******************************************************************************
  75. * The LISTSORT macro is an adaptation of Simon Tatham's O(n log(n)) mergesort*
  76. * Unwieldy variable names used here to avoid shadowing passed-in variables. *
  77. *****************************************************************************/
  78. #define LISTSORT(list, is_circular, po, no, cmp) \
  79. do { \
  80. void *_ls_p, *_ls_q, *_ls_e, *_ls_tail, *_ls_oldhead; \
  81. int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
  82. int _ls_is_double = (po==0) ? 0 : 1; \
  83. if (list) { \
  84. _ls_insize = 1; \
  85. _ls_looping = 1; \
  86. while (_ls_looping) { \
  87. _ls_p = list; \
  88. _ls_oldhead = list; \
  89. list = NULL; \
  90. _ls_tail = NULL; \
  91. _ls_nmerges = 0; \
  92. while (_ls_p) { \
  93. _ls_nmerges++; \
  94. _ls_q = _ls_p; \
  95. _ls_psize = 0; \
  96. for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
  97. _ls_psize++; \
  98. if (is_circular) { \
  99. _ls_q = ((LNEXT(_ls_q,no) == _ls_oldhead) ? NULL : LNEXT(_ls_q,no)); \
  100. } else { \
  101. _ls_q = LNEXT(_ls_q,no); \
  102. } \
  103. if (!_ls_q) break; \
  104. } \
  105. _ls_qsize = _ls_insize; \
  106. while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
  107. if (_ls_psize == 0) { \
  108. _ls_e = _ls_q; _ls_q = LNEXT(_ls_q,no); _ls_qsize--; \
  109. if (is_circular && _ls_q == _ls_oldhead) { _ls_q = NULL; } \
  110. } else if (_ls_qsize == 0 || !_ls_q) { \
  111. _ls_e = _ls_p; _ls_p = LNEXT(_ls_p,no); _ls_psize--; \
  112. if (is_circular && (_ls_p == _ls_oldhead)) { _ls_p = NULL; } \
  113. } else if (cmp(LTYPEOF(list)_ls_p,LTYPEOF(list)_ls_q) <= 0) { \
  114. _ls_e = _ls_p; _ls_p = LNEXT(_ls_p,no); _ls_psize--; \
  115. if (is_circular && (_ls_p == _ls_oldhead)) { _ls_p = NULL; } \
  116. } else { \
  117. _ls_e = _ls_q; _ls_q = LNEXT(_ls_q,no); _ls_qsize--; \
  118. if (is_circular && (_ls_q == _ls_oldhead)) { _ls_q = NULL; } \
  119. } \
  120. if (_ls_tail) { \
  121. LNEXT(_ls_tail,no) = (char*)_ls_e; \
  122. } else { \
  123. list = LTYPEOF(list)_ls_e; \
  124. } \
  125. if (_ls_is_double) { \
  126. LPREV(_ls_e,po) = (char*)_ls_tail; \
  127. } \
  128. _ls_tail = _ls_e; \
  129. } \
  130. _ls_p = _ls_q; \
  131. } \
  132. if (is_circular) { \
  133. LNEXT(_ls_tail,no) = (char*)list; \
  134. if (_ls_is_double) { \
  135. LPREV(list,po) = (char*)_ls_tail; \
  136. } \
  137. } else { \
  138. LNEXT(_ls_tail,no) = NULL; \
  139. } \
  140. if (_ls_nmerges <= 1) { \
  141. _ls_looping=0; \
  142. } \
  143. _ls_insize *= 2; \
  144. } \
  145. } \
  146. } while (0)
  147. /******************************************************************************
  148. * singly linked list macros (non-circular) *
  149. *****************************************************************************/
  150. #define LL_PREPEND(head,add) \
  151. do { \
  152. (add)->next = head; \
  153. head = add; \
  154. } while (0)
  155. #define LL_APPEND(head,add) \
  156. do { \
  157. (add)->next=NULL; \
  158. if (head) { \
  159. char *_lla_el = (char*)(head); \
  160. unsigned _lla_no = FIELD_OFFSET(head,next); \
  161. while (LNEXT(_lla_el,_lla_no)) { _lla_el = LNEXT(_lla_el,_lla_no); } \
  162. LNEXT(_lla_el,_lla_no)=(char*)(add); \
  163. } else { \
  164. (head)=(add); \
  165. } \
  166. } while (0)
  167. #define LL_DELETE(head,del) \
  168. do { \
  169. if ((head) == (del)) { \
  170. (head)=(head)->next; \
  171. } else { \
  172. char *_lld_el = (char*)(head); \
  173. unsigned _lld_no = FIELD_OFFSET(head,next); \
  174. while (LNEXT(_lld_el,_lld_no) && (LNEXT(_lld_el,_lld_no) != (char*)(del))) { \
  175. _lld_el = LNEXT(_lld_el,_lld_no); \
  176. } \
  177. if (LNEXT(_lld_el,_lld_no)) { \
  178. LNEXT(_lld_el,_lld_no) = (char*)((del)->next); \
  179. } \
  180. } \
  181. } while (0)
  182. #define LL_FOREACH(head,el) \
  183. for(el=head;el;el=el->next)
  184. /******************************************************************************
  185. * doubly linked list macros (non-circular) *
  186. *****************************************************************************/
  187. #define DL_PREPEND(head,add) \
  188. do { \
  189. (add)->next = head; \
  190. if (head) { \
  191. (add)->prev = (head)->prev; \
  192. (head)->prev = (add); \
  193. } else { \
  194. (add)->prev = (add); \
  195. } \
  196. (head) = (add); \
  197. } while (0)
  198. #define DL_APPEND(head,add) \
  199. do { \
  200. if (head) { \
  201. (add)->prev = (head)->prev; \
  202. (head)->prev->next = (add); \
  203. (head)->prev = (add); \
  204. (add)->next = NULL; \
  205. } else { \
  206. (head)=(add); \
  207. (head)->prev = (head); \
  208. (head)->next = NULL; \
  209. } \
  210. } while (0);
  211. #define DL_DELETE(head,del) \
  212. do { \
  213. if ((del)->prev == (del)) { \
  214. (head)=NULL; \
  215. } else if ((del)==(head)) { \
  216. (del)->next->prev = (del)->prev; \
  217. (head) = (del)->next; \
  218. } else { \
  219. (del)->prev->next = (del)->next; \
  220. if ((del)->next) { \
  221. (del)->next->prev = (del)->prev; \
  222. } else { \
  223. (head)->prev = (del)->prev; \
  224. } \
  225. } \
  226. } while (0);
  227. #define DL_FOREACH(head,el) \
  228. for(el=head;el;el=el->next)
  229. /******************************************************************************
  230. * circular doubly linked list macros *
  231. *****************************************************************************/
  232. #define CDL_PREPEND(head,add) \
  233. do { \
  234. if (head) { \
  235. (add)->prev = (head)->prev; \
  236. (add)->next = (head); \
  237. (head)->prev = (add); \
  238. (add)->prev->next = (add); \
  239. } else { \
  240. (add)->prev = (add); \
  241. (add)->next = (add); \
  242. } \
  243. (head)=(add); \
  244. } while (0)
  245. #define CDL_DELETE(head,del) \
  246. do { \
  247. if ( ((head)==(del)) && ((head)->next == (head))) { \
  248. (head) = 0L; \
  249. } else { \
  250. (del)->next->prev = (del)->prev; \
  251. (del)->prev->next = (del)->next; \
  252. if ((del) == (head)) (head)=(del)->next; \
  253. } \
  254. } while (0);
  255. #define CDL_FOREACH(head,el) \
  256. for(el=head;el;el= (el->next==head ? 0L : el->next))
  257. #endif /* UTLIST_H */