utlist.h 55 KB

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