tkDList.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * tkDList.h --
  3. *
  4. * A list is headed by pointers to first and last element. The elements
  5. * are doubly linked so that an arbitrary element can be removed without
  6. * a need to traverse the list. New elements can be added to the list
  7. * before or after an existing element or at the head/tail of the list.
  8. * A list may be traversed in the forward or backward direction.
  9. *
  10. * Copyright (c) 2018 by Gregor Cramer.
  11. *
  12. * See the file "license.terms" for information on usage and redistribution of
  13. * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. */
  15. /*
  16. * Note that this file will not be included in header files, it is the purpose
  17. * of this file to be included in source files only. Thus we are not using the
  18. * prefix "Tk_" here for functions, because all the functions have private scope.
  19. */
  20. /*
  21. * -------------------------------------------------------------------------------
  22. * Use the double linked list in the following way:
  23. * -------------------------------------------------------------------------------
  24. * typedef struct MyListEntry { TK_DLIST_LINKS(MyListEntry); int value; } MyListEntry;
  25. * TK_DLIST_DEFINE(MyList, MyListEntry);
  26. * MyList listHdr = TK_DLIST_LIST_INITIALIZER; // or MyList_Init(&listHdr)
  27. * MyListEntry *p;
  28. * int i = 0;
  29. * MyList_Append(&listHdr, ckalloc(sizeof(MyListEntry)));
  30. * MyList_Append(&listHdr, ckalloc(sizeof(MyListEntry)));
  31. * TK_DLIST_FOREACH(p, &listHdr) { p->value = i++; }
  32. * // ...
  33. * MyList_RemoveHead(&listHdr);
  34. * MyList_RemoveHead(&listHdr);
  35. * MyList_Clear(MyListEntry, &listHdr); // invokes ckfree() for each element
  36. * -------------------------------------------------------------------------------
  37. * IMPORTANT NOTE: TK_DLIST_LINKS must be used at start of struct!
  38. */
  39. #ifndef TK_DLIST_DEFINED
  40. #define TK_DLIST_DEFINED
  41. #include "tkInt.h"
  42. /*
  43. * List definitions.
  44. */
  45. #define TK_DLIST_LINKS(ElemType) \
  46. struct { \
  47. struct ElemType *prev; /* previous element */ \
  48. struct ElemType *next; /* next element */ \
  49. } _dl_
  50. #define TK_DLIST_LIST_INITIALIZER { NULL, NULL }
  51. #define TK_DLIST_ELEM_INITIALIZER { NULL, NULL }
  52. /*************************************************************************/
  53. /*
  54. * DList_Init: Initialize list header. This can also be used to clear the
  55. * list.
  56. *
  57. * See also: DList_Clear()
  58. */
  59. /*************************************************************************/
  60. /*
  61. * DList_ElemInit: Initialize a list element.
  62. */
  63. /*************************************************************************/
  64. /*
  65. * DList_InsertAfter: Insert 'elem' after 'listElem'. 'elem' must not
  66. * be linked, but 'listElem' must be linked.
  67. */
  68. /*************************************************************************/
  69. /*
  70. * DList_InsertBefore: Insert 'elem' before 'listElem'. 'elem' must not
  71. * be linked, but 'listElem' must be linked.
  72. */
  73. /*************************************************************************/
  74. /*
  75. * DList_Prepend: Insert 'elem' at start of list. This element must not
  76. * be linked.
  77. *
  78. * See also: DList_Append()
  79. */
  80. /*************************************************************************/
  81. /*
  82. * DList_Append: Append 'elem' to end of list. This element must not
  83. * be linked.
  84. *
  85. * See also: DList_Prepend()
  86. */
  87. /*************************************************************************/
  88. /*
  89. * DList_Move: Append all list items of 'src' to end of 'dst'.
  90. */
  91. /*************************************************************************/
  92. /*
  93. * DList_Remove: Remove 'elem' from list. This element must be linked.
  94. *
  95. * See also: DList_Free()
  96. */
  97. /*************************************************************************/
  98. /*
  99. * DList_Free: Remove 'elem' from list and free it. This element must
  100. * be linked.
  101. *
  102. * See also: DList_Remove()
  103. */
  104. /*************************************************************************/
  105. /*
  106. * DList_RemoveHead: Remove first element from list. The list must
  107. * not be empty.
  108. *
  109. * See also: DList_FreeHead()
  110. */
  111. /*************************************************************************/
  112. /*
  113. * DList_RemoveTail: Remove last element from list. The list must
  114. * not be empty.
  115. *
  116. * See also: DList_FreeTail()
  117. */
  118. /*************************************************************************/
  119. /*
  120. * DList_FreeHead: Remove first element from list and free it.
  121. * The list must not be empty.
  122. *
  123. * See also: DList_RemoveHead()
  124. */
  125. /*************************************************************************/
  126. /*
  127. * DList_FreeTail: Remove last element from list and free it.
  128. * The list must not be empty.
  129. *
  130. * See also: DList_RemoveTail()
  131. */
  132. /*************************************************************************/
  133. /*
  134. * DList_SwapElems: Swap positions of given elements 'lhs' and 'rhs'.
  135. * Both elements must be linked, and must belong to same list.
  136. */
  137. /*************************************************************************/
  138. /*
  139. * DList_Clear: Clear whole list and free all elements.
  140. *
  141. * See also: DList_Init
  142. */
  143. /*************************************************************************/
  144. /*
  145. * DList_Traverse: Iterate over all elements in list from first to last.
  146. * Call given function func(head, elem) for each element. The function has
  147. * to return the next element in list to traverse, normally this is
  148. * DList_Next(elem).
  149. *
  150. * See also: TK_DLIST_FOREACH, TK_DLIST_FOREACH_REVERSE
  151. */
  152. /*************************************************************************/
  153. /*
  154. * DList_First: Return pointer of first element in list, maybe it's NULL.
  155. */
  156. /*************************************************************************/
  157. /*
  158. * DList_Last: Return pointer of last element in list, maybe it's NULL.
  159. */
  160. /*************************************************************************/
  161. /*
  162. * DList_Next: Return pointer of next element after 'elem', maybe it's NULL.
  163. *
  164. * See also: DList_Prev()
  165. */
  166. /*************************************************************************/
  167. /*
  168. * DList_Prev: Return pointer of previous element before 'elem', maybe it's
  169. * NULL.
  170. *
  171. * See also: DList_Next()
  172. */
  173. /*************************************************************************/
  174. /*
  175. * DList_IsEmpty: Test whether given list is empty.
  176. */
  177. /*************************************************************************/
  178. /*
  179. * DList_IsLinked: Test whether given element is linked.
  180. */
  181. /*************************************************************************/
  182. /*
  183. * DList_IsFirst: Test whether given element is first element in list.
  184. * Note that 'elem' must be linked.
  185. *
  186. * See also: DList_IsLast(), DList_IsLinked()
  187. */
  188. /*************************************************************************/
  189. /*
  190. * DList_IsLast: Test whether given element is last element in list.
  191. * Note that 'elem' must be linked.
  192. *
  193. * See also: DList_IsFirst(), DList_IsLinked()
  194. */
  195. /*************************************************************************/
  196. /*
  197. * DList_Size: Count number of elements in given list.
  198. */
  199. /*************************************************************************/
  200. /*
  201. * TK_DLIST_FOREACH: Iterate over all elements in list from first to last.
  202. * 'var' is the name of the variable which points to current element.
  203. *
  204. * See also: TK_DLIST_FOREACH_REVERSE, DList_Traverse()
  205. */
  206. /*************************************************************************/
  207. /*
  208. * TK_DLIST_FOREACH_REVERSE: Iterate over all elements in list from last
  209. * to first (backwards). 'var' is the name of the variable which points to
  210. * current element.
  211. */
  212. /*************************************************************************/
  213. #if defined(__GNUC__) || defined(__clang__)
  214. # define __TK_DLIST_UNUSED __attribute__((unused))
  215. #else
  216. # define __TK_DLIST_UNUSED
  217. #endif
  218. #define TK_DLIST_DEFINE(LT, ElemType) /* LT = type of head/list */ \
  219. /* ------------------------------------------------------------------------- */ \
  220. typedef struct LT { \
  221. struct ElemType *first; /* first element */ \
  222. struct ElemType *last; /* last element */ \
  223. } LT; \
  224. \
  225. typedef struct ElemType *(LT##_Func)(LT *head, struct ElemType *elem); \
  226. \
  227. __TK_DLIST_UNUSED \
  228. static void \
  229. LT##_Init(LT *head) \
  230. { \
  231. assert(head); \
  232. head->first = NULL; \
  233. head->last = NULL; \
  234. } \
  235. \
  236. __TK_DLIST_UNUSED \
  237. static void \
  238. LT##_ElemInit(struct ElemType *elem) \
  239. { \
  240. assert(elem); \
  241. elem->_dl_.next = NULL; \
  242. elem->_dl_.prev = NULL; \
  243. } \
  244. \
  245. __TK_DLIST_UNUSED \
  246. static int \
  247. LT##_IsEmpty(LT *head) \
  248. { \
  249. assert(head); \
  250. return head->first == NULL; \
  251. } \
  252. \
  253. __TK_DLIST_UNUSED \
  254. static int \
  255. LT##_IsLinked(struct ElemType *elem) \
  256. { \
  257. assert(elem); \
  258. return elem->_dl_.next && elem->_dl_.prev; \
  259. } \
  260. \
  261. __TK_DLIST_UNUSED \
  262. static int \
  263. LT##_IsFirst(struct ElemType *elem) \
  264. { \
  265. assert(LT##_IsLinked(elem)); \
  266. return elem->_dl_.prev->_dl_.prev == elem; \
  267. } \
  268. \
  269. __TK_DLIST_UNUSED \
  270. static int \
  271. LT##_IsLast(struct ElemType *elem) \
  272. { \
  273. assert(LT##_IsLinked(elem)); \
  274. return elem->_dl_.next->_dl_.next == elem; \
  275. } \
  276. \
  277. __TK_DLIST_UNUSED \
  278. static struct ElemType * \
  279. LT##_First(LT *head) \
  280. { \
  281. assert(head); \
  282. return head->first; \
  283. } \
  284. \
  285. __TK_DLIST_UNUSED \
  286. static struct ElemType * \
  287. LT##_Last(LT *head) \
  288. { \
  289. assert(head); \
  290. return head->last; \
  291. } \
  292. \
  293. __TK_DLIST_UNUSED \
  294. static struct ElemType * \
  295. LT##_Next(struct ElemType *elem) \
  296. { \
  297. assert(elem); \
  298. return LT##_IsLast(elem) ? NULL : elem->_dl_.next; \
  299. } \
  300. \
  301. __TK_DLIST_UNUSED \
  302. static struct ElemType * \
  303. LT##_Prev(struct ElemType *elem) \
  304. { \
  305. assert(elem); \
  306. return LT##_IsFirst(elem) ? NULL : elem->_dl_.prev; \
  307. } \
  308. \
  309. __TK_DLIST_UNUSED \
  310. static unsigned \
  311. LT##_Size(const LT *head) \
  312. { \
  313. const struct ElemType *elem; \
  314. unsigned size = 0; \
  315. assert(head); \
  316. if ((elem = head->first)) { \
  317. for ( ; elem != (void *) head; elem = elem->_dl_.next) { \
  318. ++size; \
  319. } \
  320. } \
  321. return size; \
  322. } \
  323. \
  324. __TK_DLIST_UNUSED \
  325. static void \
  326. LT##_InsertAfter(struct ElemType *listElem, struct ElemType *elem) \
  327. { \
  328. assert(listElem); \
  329. assert(elem); \
  330. elem->_dl_.next = listElem->_dl_.next; \
  331. elem->_dl_.prev = listElem; \
  332. listElem->_dl_.next->_dl_.prev = elem; \
  333. listElem->_dl_.next = elem; \
  334. } \
  335. \
  336. __TK_DLIST_UNUSED \
  337. static void \
  338. LT##_InsertBefore(struct ElemType *listElem, struct ElemType *elem) \
  339. { \
  340. assert(listElem); \
  341. assert(elem); \
  342. elem->_dl_.next = listElem; \
  343. elem->_dl_.prev = listElem->_dl_.prev;; \
  344. listElem->_dl_.prev->_dl_.next = elem; \
  345. listElem->_dl_.prev = elem; \
  346. } \
  347. \
  348. __TK_DLIST_UNUSED \
  349. static void \
  350. LT##_Prepend(LT *head, struct ElemType *elem) \
  351. { \
  352. assert(head); \
  353. assert(elem); \
  354. elem->_dl_.prev = (void *) head; \
  355. if (!head->first) { \
  356. elem->_dl_.next = (void *) head; \
  357. head->last = elem; \
  358. } else { \
  359. elem->_dl_.next = head->first; \
  360. head->first->_dl_.prev = elem; \
  361. } \
  362. head->first = elem; \
  363. } \
  364. \
  365. __TK_DLIST_UNUSED \
  366. static void \
  367. LT##_Append(LT *head, struct ElemType *elem) \
  368. { \
  369. assert(head); \
  370. assert(elem); \
  371. elem->_dl_.next = (void *) head; \
  372. if (!head->first) { \
  373. elem->_dl_.prev = (void *) head; \
  374. head->first = elem; \
  375. } else { \
  376. elem->_dl_.prev = head->last; \
  377. head->last->_dl_.next = elem; \
  378. } \
  379. head->last = elem; \
  380. } \
  381. \
  382. __TK_DLIST_UNUSED \
  383. static void \
  384. LT##_Move(LT *dst, LT *src) \
  385. { \
  386. assert(dst); \
  387. assert(src); \
  388. if (src->first) { \
  389. if (dst->first) { \
  390. dst->last->_dl_.next = src->first; \
  391. src->first->_dl_.prev = dst->last; \
  392. dst->last = src->last; \
  393. } else { \
  394. *dst = *src; \
  395. dst->first->_dl_.prev = (void *) dst; \
  396. } \
  397. dst->last->_dl_.next = (void *) dst; \
  398. LT##_Init(src); \
  399. } \
  400. } \
  401. \
  402. __TK_DLIST_UNUSED \
  403. static void \
  404. LT##_Remove(struct ElemType *elem) \
  405. { \
  406. int isFirst, isLast; \
  407. assert(LT##_IsLinked(elem)); \
  408. isFirst = LT##_IsFirst(elem); \
  409. isLast = LT##_IsLast(elem); \
  410. if (isFirst && isLast) { \
  411. ((LT *) elem->_dl_.prev)->first = NULL; \
  412. ((LT *) elem->_dl_.next)->last = NULL; \
  413. } else { \
  414. if (isFirst) { \
  415. ((LT *) elem->_dl_.prev)->first = elem->_dl_.next; \
  416. } else { \
  417. elem->_dl_.prev->_dl_.next = elem->_dl_.next; \
  418. } \
  419. if (isLast) { \
  420. ((LT *) elem->_dl_.next)->last = elem->_dl_.prev; \
  421. } else { \
  422. elem->_dl_.next->_dl_.prev = elem->_dl_.prev; \
  423. } \
  424. } \
  425. LT##_ElemInit(elem); \
  426. } \
  427. \
  428. __TK_DLIST_UNUSED \
  429. static void \
  430. LT##_Free(struct ElemType *elem) \
  431. { \
  432. LT##_Remove(elem); \
  433. ckfree((void *) elem); \
  434. } \
  435. \
  436. __TK_DLIST_UNUSED \
  437. static void \
  438. LT##_RemoveHead(LT *head) \
  439. { \
  440. assert(!LT##_IsEmpty(head)); \
  441. LT##_Remove(head->first); \
  442. } \
  443. \
  444. __TK_DLIST_UNUSED \
  445. static void \
  446. LT##_RemoveTail(LT *head) \
  447. { \
  448. assert(!LT##_IsEmpty(head)); \
  449. LT##_Remove(head->last); \
  450. } \
  451. \
  452. __TK_DLIST_UNUSED \
  453. static void \
  454. LT##_FreeHead(LT *head) \
  455. { \
  456. assert(!LT##_IsEmpty(head)); \
  457. LT##_Free(head->first); \
  458. } \
  459. \
  460. __TK_DLIST_UNUSED \
  461. static void \
  462. LT##_FreeTail(LT *head) \
  463. { \
  464. assert(!LT##_IsEmpty(head)); \
  465. LT##_Free(head->last); \
  466. } \
  467. \
  468. __TK_DLIST_UNUSED \
  469. static void \
  470. LT##_SwapElems(struct ElemType *lhs, struct ElemType *rhs) \
  471. { \
  472. assert(lhs); \
  473. assert(rhs); \
  474. if (lhs != rhs) { \
  475. struct ElemType tmp; \
  476. if (LT##_IsFirst(lhs)) { \
  477. ((LT *) lhs->_dl_.prev)->first = rhs; \
  478. } else if (LT##_IsFirst(rhs)) { \
  479. ((LT *) rhs->_dl_.prev)->first = lhs; \
  480. } \
  481. if (LT##_IsLast(lhs)) { \
  482. ((LT *) lhs->_dl_.next)->last = rhs; \
  483. } else if (LT##_IsLast(rhs)) { \
  484. ((LT *) rhs->_dl_.next)->last = lhs; \
  485. } \
  486. tmp._dl_ = lhs->_dl_; \
  487. lhs->_dl_ = rhs->_dl_; \
  488. rhs->_dl_ = tmp._dl_; \
  489. } \
  490. } \
  491. \
  492. __TK_DLIST_UNUSED \
  493. static void \
  494. LT##_Clear(LT *head) \
  495. { \
  496. struct ElemType *p; \
  497. struct ElemType *next; \
  498. assert(head); \
  499. for (p = head->first; p; p = next) { \
  500. next = LT##_Next(p); \
  501. ckfree((void *) p); \
  502. } \
  503. LT##_Init(head); \
  504. } \
  505. \
  506. __TK_DLIST_UNUSED \
  507. static void \
  508. LT##_Traverse(LT *head, LT##_Func func) \
  509. { \
  510. struct ElemType *next; \
  511. struct ElemType *p; \
  512. assert(head); \
  513. for (p = head->first; p; p = next) { \
  514. next = func(head, p); \
  515. } \
  516. } \
  517. /* ------------------------------------------------------------------------- */
  518. #define TK_DLIST_FOREACH(var, head) \
  519. assert(head); \
  520. for (var = head->first ? head->first : (void *) head; var != (void *) head; var = var->_dl_.next)
  521. #define TK_DLIST_FOREACH_REVERSE(var, head) \
  522. assert(head); \
  523. for (var = head->last ? head->last : (void *) head; var != (void *) head; var = var->_dl_.prev)
  524. #endif /* TK_DLIST_DEFINED */
  525. /*
  526. * Local Variables:
  527. * mode: c
  528. * c-basic-offset: 4
  529. * fill-column: 105
  530. * End:
  531. * vi:set ts=8 sw=4:
  532. */