LinkedList3.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. * @file LinkedList3.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Doubly linked list that support multiple iterations and removing
  25. * aritrary elements during iteration, without a central object to
  26. * represent the list.
  27. */
  28. #ifndef BADVPN_STRUCTURE_LINKEDLIST3_H
  29. #define BADVPN_STRUCTURE_LINKEDLIST3_H
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #include <misc/debug.h>
  33. struct _LinkedList3Iterator;
  34. /**
  35. * Linked list node.
  36. */
  37. typedef struct _LinkedList3Node {
  38. struct _LinkedList3Node *p;
  39. struct _LinkedList3Node *n;
  40. struct _LinkedList3Iterator *it;
  41. } LinkedList3Node;
  42. /**
  43. * Linked list iterator.
  44. */
  45. typedef struct _LinkedList3Iterator {
  46. int8_t dir;
  47. struct _LinkedList3Node *e;
  48. struct _LinkedList3Iterator *pi;
  49. struct _LinkedList3Iterator *ni;
  50. } LinkedList3Iterator;
  51. /**
  52. * Initializes a list node to form a new list consisting of a
  53. * single node.
  54. *
  55. * @param node list node structure to initialize. The node must remain
  56. * available until it is freed with {@link LinkedList3Node_Free},
  57. * or the list is no longer required.
  58. */
  59. static void LinkedList3Node_InitLonely (LinkedList3Node *node);
  60. /**
  61. * Initializes a list node to go after an existing node.
  62. *
  63. * @param node list node structure to initialize. The node must remain
  64. * available until it is freed with {@link LinkedList3Node_Free},
  65. * or the list is no longer required.
  66. * @param ref existing list node
  67. */
  68. static void LinkedList3Node_InitAfter (LinkedList3Node *node, LinkedList3Node *ref);
  69. /**
  70. * Initializes a list node to go before an existing node.
  71. *
  72. * @param node list node structure to initialize. The node must remain
  73. * available until it is freed with {@link LinkedList3Node_Free},
  74. * or the list is no longer required.
  75. * @param ref existing list node
  76. */
  77. static void LinkedList3Node_InitBefore (LinkedList3Node *node, LinkedList3Node *ref);
  78. /**
  79. * Frees a list node, removing it a list (if there were other nodes
  80. * in the list).
  81. *
  82. * @param node list node to free
  83. */
  84. static void LinkedList3Node_Free (LinkedList3Node *node);
  85. /**
  86. * Determines if a list node is a single node in a list.
  87. *
  88. * @param node list node
  89. * @return 1 if the node ia a single node, 0 if not
  90. */
  91. static int LinkedList3Node_IsLonely (LinkedList3Node *node);
  92. /**
  93. * Returnes the node preceding this node (if there is one),
  94. * the node following this node (if there is one), or NULL,
  95. * respectively.
  96. *
  97. * @param node list node
  98. * @return neighbour node or NULL if none
  99. */
  100. static LinkedList3Node * LinkedList3Node_PrevOrNext (LinkedList3Node *node);
  101. /**
  102. * Returnes the node following this node (if there is one),
  103. * the node preceding this node (if there is one), or NULL,
  104. * respectively.
  105. *
  106. * @param node list node
  107. * @return neighbour node or NULL if none
  108. */
  109. static LinkedList3Node * LinkedList3Node_NextOrPrev (LinkedList3Node *node);
  110. /**
  111. * Returns the node preceding this node, or NULL if there is none.
  112. *
  113. * @param node list node
  114. * @return left neighbour, or NULL if none
  115. */
  116. static LinkedList3Node * LinkedList3Node_Prev (LinkedList3Node *node);
  117. /**
  118. * Returns the node following this node, or NULL if there is none.
  119. *
  120. * @param node list node
  121. * @return right neighbour, or NULL if none
  122. */
  123. static LinkedList3Node * LinkedList3Node_Next (LinkedList3Node *node);
  124. /**
  125. * Returns the first node in the list which this node is part of.
  126. * It is found by iterating the list from this node to the beginning.
  127. *
  128. * @param node list node
  129. * @return first node in the list
  130. */
  131. static LinkedList3Node * LinkedList3Node_First (LinkedList3Node *node);
  132. /**
  133. * Returns the last node in the list which this node is part of.
  134. * It is found by iterating the list from this node to the end.
  135. *
  136. * @param node list node
  137. * @return last node in the list
  138. */
  139. static LinkedList3Node * LinkedList3Node_Last (LinkedList3Node *node);
  140. /**
  141. * Initializes a linked list iterator.
  142. * The iterator structure must remain available until either of these occurs:
  143. * - the list is no longer needed, or
  144. * - the iterator is freed with {@link LinkedList3Iterator_Free}, or
  145. * - the iterator reaches the end of iteration.
  146. *
  147. * @param it uninitialized iterator to initialize
  148. * @param e initial position of the iterator. NULL for end of iteration.
  149. * @param dir direction of iteration. Must be 1 (forward) or -1 (backward).
  150. */
  151. static void LinkedList3Iterator_Init (LinkedList3Iterator *it, LinkedList3Node *e, int dir);
  152. /**
  153. * Frees a linked list iterator.
  154. *
  155. * @param it iterator to free
  156. */
  157. static void LinkedList3Iterator_Free (LinkedList3Iterator *it);
  158. /**
  159. * Moves the iterator one node forward or backward (depending on its direction), or,
  160. * if it's at the last or first node (depending on the direction), it reaches
  161. * the end of iteration, or, if it's at the end of iteration, it remains there.
  162. * Returns the the previous position.
  163. *
  164. * @param it the iterator
  165. * @return node on the position of iterator before it was (possibly) moved, or NULL
  166. * if it was at the end of iteration
  167. */
  168. static LinkedList3Node * LinkedList3Iterator_Next (LinkedList3Iterator *it);
  169. void LinkedList3Node_InitLonely (LinkedList3Node *node)
  170. {
  171. node->p = NULL;
  172. node->n = NULL;
  173. node->it = NULL;
  174. }
  175. void LinkedList3Node_InitAfter (LinkedList3Node *node, LinkedList3Node *ref)
  176. {
  177. ASSERT(ref)
  178. node->p = ref;
  179. node->n = ref->n;
  180. ref->n = node;
  181. if (node->n) {
  182. node->n->p = node;
  183. }
  184. node->it = NULL;
  185. }
  186. void LinkedList3Node_InitBefore (LinkedList3Node *node, LinkedList3Node *ref)
  187. {
  188. ASSERT(ref)
  189. node->n = ref;
  190. node->p = ref->p;
  191. ref->p = node;
  192. if (node->p) {
  193. node->p->n = node;
  194. }
  195. node->it = NULL;
  196. }
  197. void LinkedList3Node_Free (LinkedList3Node *node)
  198. {
  199. // jump iterators
  200. while (node->it) {
  201. LinkedList3Iterator_Next(node->it);
  202. }
  203. if (node->p) {
  204. node->p->n = node->n;
  205. }
  206. if (node->n) {
  207. node->n->p = node->p;
  208. }
  209. }
  210. int LinkedList3Node_IsLonely (LinkedList3Node *node)
  211. {
  212. return (!node->p && !node->n);
  213. }
  214. LinkedList3Node * LinkedList3Node_PrevOrNext (LinkedList3Node *node)
  215. {
  216. if (node->p) {
  217. return node->p;
  218. }
  219. if (node->n) {
  220. return node->n;
  221. }
  222. return NULL;
  223. }
  224. LinkedList3Node * LinkedList3Node_NextOrPrev (LinkedList3Node *node)
  225. {
  226. if (node->n) {
  227. return node->n;
  228. }
  229. if (node->p) {
  230. return node->p;
  231. }
  232. return NULL;
  233. }
  234. LinkedList3Node * LinkedList3Node_Prev (LinkedList3Node *node)
  235. {
  236. return node->p;
  237. }
  238. LinkedList3Node * LinkedList3Node_Next (LinkedList3Node *node)
  239. {
  240. return node->n;
  241. }
  242. LinkedList3Node * LinkedList3Node_First (LinkedList3Node *node)
  243. {
  244. while (node->p) {
  245. node = node->p;
  246. }
  247. return node;
  248. }
  249. LinkedList3Node * LinkedList3Node_Last (LinkedList3Node *node)
  250. {
  251. while (node->n) {
  252. node = node->n;
  253. }
  254. return node;
  255. }
  256. void LinkedList3Iterator_Init (LinkedList3Iterator *it, LinkedList3Node *e, int dir)
  257. {
  258. ASSERT(dir == -1 || dir == 1)
  259. it->dir = dir;
  260. it->e = e;
  261. if (e) {
  262. // link into node's iterator list
  263. it->pi = NULL;
  264. it->ni = e->it;
  265. if (e->it) {
  266. e->it->pi = it;
  267. }
  268. e->it = it;
  269. }
  270. }
  271. void LinkedList3Iterator_Free (LinkedList3Iterator *it)
  272. {
  273. if (it->e) {
  274. // remove from node's iterator list
  275. if (it->ni) {
  276. it->ni->pi = it->pi;
  277. }
  278. if (it->pi) {
  279. it->pi->ni = it->ni;
  280. } else {
  281. it->e->it = it->ni;
  282. }
  283. }
  284. }
  285. LinkedList3Node * LinkedList3Iterator_Next (LinkedList3Iterator *it)
  286. {
  287. // remember original entry
  288. LinkedList3Node *orig = it->e;
  289. // jump to next entry
  290. if (it->e) {
  291. // get next entry
  292. LinkedList3Node *next;
  293. switch (it->dir) {
  294. case 1:
  295. next = it->e->n;
  296. break;
  297. case -1:
  298. next = it->e->p;
  299. break;
  300. default:
  301. ASSERT(0);
  302. }
  303. // destroy interator
  304. LinkedList3Iterator_Free(it);
  305. // re-initialize at next entry
  306. LinkedList3Iterator_Init(it, next, it->dir);
  307. }
  308. // return original entry
  309. return orig;
  310. }
  311. #endif