LinkedList4.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @file LinkedList4.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Doubly linked list without a central object to represent the list.
  32. * Like {@link LinkedList3}, but without iterators.
  33. */
  34. #ifndef BADVPN_STRUCTURE_LINKEDLIST4_H
  35. #define BADVPN_STRUCTURE_LINKEDLIST4_H
  36. #include <stddef.h>
  37. #include <stdint.h>
  38. #include <misc/debug.h>
  39. /**
  40. * Linked list node.
  41. */
  42. typedef struct _LinkedList4Node {
  43. struct _LinkedList4Node *p;
  44. struct _LinkedList4Node *n;
  45. } LinkedList4Node;
  46. /**
  47. * Initializes a list node to form a new list consisting of a
  48. * single node.
  49. *
  50. * @param node list node structure to initialize. The node must remain
  51. * available until it is freed with {@link LinkedList4Node_Free},
  52. * or the list is no longer required.
  53. */
  54. static void LinkedList4Node_InitLonely (LinkedList4Node *node);
  55. /**
  56. * Initializes a list node to go after an existing node.
  57. *
  58. * @param node list node structure to initialize. The node must remain
  59. * available until it is freed with {@link LinkedList4Node_Free},
  60. * or the list is no longer required.
  61. * @param ref existing list node
  62. */
  63. static void LinkedList4Node_InitAfter (LinkedList4Node *node, LinkedList4Node *ref);
  64. /**
  65. * Initializes a list node to go before an existing node.
  66. *
  67. * @param node list node structure to initialize. The node must remain
  68. * available until it is freed with {@link LinkedList4Node_Free},
  69. * or the list is no longer required.
  70. * @param ref existing list node
  71. */
  72. static void LinkedList4Node_InitBefore (LinkedList4Node *node, LinkedList4Node *ref);
  73. /**
  74. * Frees a list node, removing it a list (if there were other nodes
  75. * in the list).
  76. *
  77. * @param node list node to free
  78. */
  79. static void LinkedList4Node_Free (LinkedList4Node *node);
  80. /**
  81. * Determines if a list node is a single node in a list.
  82. *
  83. * @param node list node
  84. * @return 1 if the node ia a single node, 0 if not
  85. */
  86. static int LinkedList4Node_IsLonely (LinkedList4Node *node);
  87. /**
  88. * Returnes the node preceding this node (if there is one),
  89. * the node following this node (if there is one), or NULL,
  90. * respectively.
  91. *
  92. * @param node list node
  93. * @return neighbour node or NULL if none
  94. */
  95. static LinkedList4Node * LinkedList4Node_PrevOrNext (LinkedList4Node *node);
  96. /**
  97. * Returnes the node following this node (if there is one),
  98. * the node preceding this node (if there is one), or NULL,
  99. * respectively.
  100. *
  101. * @param node list node
  102. * @return neighbour node or NULL if none
  103. */
  104. static LinkedList4Node * LinkedList4Node_NextOrPrev (LinkedList4Node *node);
  105. /**
  106. * Returns the node preceding this node, or NULL if there is none.
  107. *
  108. * @param node list node
  109. * @return left neighbour, or NULL if none
  110. */
  111. static LinkedList4Node * LinkedList4Node_Prev (LinkedList4Node *node);
  112. /**
  113. * Returns the node following this node, or NULL if there is none.
  114. *
  115. * @param node list node
  116. * @return right neighbour, or NULL if none
  117. */
  118. static LinkedList4Node * LinkedList4Node_Next (LinkedList4Node *node);
  119. /**
  120. * Returns the first node in the list which this node is part of.
  121. * It is found by iterating the list from this node to the beginning.
  122. *
  123. * @param node list node
  124. * @return first node in the list
  125. */
  126. static LinkedList4Node * LinkedList4Node_First (LinkedList4Node *node);
  127. /**
  128. * Returns the last node in the list which this node is part of.
  129. * It is found by iterating the list from this node to the end.
  130. *
  131. * @param node list node
  132. * @return last node in the list
  133. */
  134. static LinkedList4Node * LinkedList4Node_Last (LinkedList4Node *node);
  135. void LinkedList4Node_InitLonely (LinkedList4Node *node)
  136. {
  137. node->p = NULL;
  138. node->n = NULL;
  139. }
  140. void LinkedList4Node_InitAfter (LinkedList4Node *node, LinkedList4Node *ref)
  141. {
  142. ASSERT(ref)
  143. node->p = ref;
  144. node->n = ref->n;
  145. ref->n = node;
  146. if (node->n) {
  147. node->n->p = node;
  148. }
  149. }
  150. void LinkedList4Node_InitBefore (LinkedList4Node *node, LinkedList4Node *ref)
  151. {
  152. ASSERT(ref)
  153. node->n = ref;
  154. node->p = ref->p;
  155. ref->p = node;
  156. if (node->p) {
  157. node->p->n = node;
  158. }
  159. }
  160. void LinkedList4Node_Free (LinkedList4Node *node)
  161. {
  162. if (node->p) {
  163. node->p->n = node->n;
  164. }
  165. if (node->n) {
  166. node->n->p = node->p;
  167. }
  168. }
  169. int LinkedList4Node_IsLonely (LinkedList4Node *node)
  170. {
  171. return (!node->p && !node->n);
  172. }
  173. LinkedList4Node * LinkedList4Node_PrevOrNext (LinkedList4Node *node)
  174. {
  175. if (node->p) {
  176. return node->p;
  177. }
  178. if (node->n) {
  179. return node->n;
  180. }
  181. return NULL;
  182. }
  183. LinkedList4Node * LinkedList4Node_NextOrPrev (LinkedList4Node *node)
  184. {
  185. if (node->n) {
  186. return node->n;
  187. }
  188. if (node->p) {
  189. return node->p;
  190. }
  191. return NULL;
  192. }
  193. LinkedList4Node * LinkedList4Node_Prev (LinkedList4Node *node)
  194. {
  195. return node->p;
  196. }
  197. LinkedList4Node * LinkedList4Node_Next (LinkedList4Node *node)
  198. {
  199. return node->n;
  200. }
  201. LinkedList4Node * LinkedList4Node_First (LinkedList4Node *node)
  202. {
  203. while (node->p) {
  204. node = node->p;
  205. }
  206. return node;
  207. }
  208. LinkedList4Node * LinkedList4Node_Last (LinkedList4Node *node)
  209. {
  210. while (node->n) {
  211. node = node->n;
  212. }
  213. return node;
  214. }
  215. #endif