IndexedList.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file IndexedList.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. * A data structure similar to a list, but with efficient index-based access.
  32. */
  33. #ifndef BADVPN_INDEXEDLIST_H
  34. #define BADVPN_INDEXEDLIST_H
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <misc/offset.h>
  38. #include <misc/debug.h>
  39. #include <structure/CAvl.h>
  40. typedef struct IndexedList_s IndexedList;
  41. typedef struct IndexedListNode_s IndexedListNode;
  42. typedef IndexedListNode *IndexedList__tree_link;
  43. #include "IndexedList_tree.h"
  44. #include <structure/CAvl_decl.h>
  45. struct IndexedList_s {
  46. IndexedList__Tree tree;
  47. };
  48. struct IndexedListNode_s {
  49. IndexedListNode *tree_child[2];
  50. IndexedListNode *tree_parent;
  51. int8_t tree_balance;
  52. uint64_t tree_count;
  53. };
  54. /**
  55. * Initializes the indexed list.
  56. *
  57. * @param o uninitialized list object to initialize
  58. */
  59. static void IndexedList_Init (IndexedList *o);
  60. /**
  61. * Inserts a node into the indexed list.
  62. *
  63. * @param o indexed list to insert into
  64. * @param node uninitialized node to insert
  65. * @param index index to insert at (starting with zero). Any existing elements
  66. * at or after this index will be shifted forward, i.e. their
  67. * indices will be incremented by one. Must be <=count.
  68. */
  69. static void IndexedList_InsertAt (IndexedList *o, IndexedListNode *node, uint64_t index);
  70. /**
  71. * Removes a nove from the indexed list.
  72. *
  73. * @param o indexed list to remove from
  74. * @param node node in the list to remove
  75. */
  76. static void IndexedList_Remove (IndexedList *o, IndexedListNode *node);
  77. /**
  78. * Returns the number of nodes in the indexed list.
  79. *
  80. * @param o indexed list
  81. * @return number of nodes
  82. */
  83. static uint64_t IndexedList_Count (IndexedList *o);
  84. /**
  85. * Returns the index of a node in the indexed list.
  86. *
  87. * @param o indexed list
  88. * @param node node in the list to get index of
  89. * @return index of the node
  90. */
  91. static uint64_t IndexedList_IndexOf (IndexedList *o, IndexedListNode *node);
  92. /**
  93. * Returns the node at the specified index in the indexed list.
  94. *
  95. * @param o indexed list
  96. * @param index index of the node to return. Must be < count.
  97. * @return node at the specified index
  98. */
  99. static IndexedListNode * IndexedList_GetAt (IndexedList *o, uint64_t index);
  100. /**
  101. * Returns the first node, or NULL if the list is empty.
  102. *
  103. * @param o indexed list
  104. * @return first node, or NULL
  105. */
  106. static IndexedListNode * IndexedList_GetFirst (IndexedList *o);
  107. /**
  108. * Returns the last node, or NULL if the list is empty.
  109. *
  110. * @param o indexed list
  111. * @return last node, or NULL
  112. */
  113. static IndexedListNode * IndexedList_GetLast (IndexedList *o);
  114. /**
  115. * Returns the next node of a given node, or NULL this is the last node.
  116. *
  117. * @param o indexed list
  118. * @param node existing node
  119. * @return next node, or NULL
  120. */
  121. static IndexedListNode * IndexedList_GetNext (IndexedList *o, IndexedListNode *node);
  122. /**
  123. * Returns the previous node of a given node, or NULL this is the first node.
  124. *
  125. * @param o indexed list
  126. * @param node existing node
  127. * @return previous node, or NULL
  128. */
  129. static IndexedListNode * IndexedList_GetPrev (IndexedList *o, IndexedListNode *node);
  130. #include "IndexedList_tree.h"
  131. #include <structure/CAvl_impl.h>
  132. static IndexedListNode * IndexedList__deref (IndexedList__TreeRef ref)
  133. {
  134. return ref.link;
  135. }
  136. static void IndexedList_Init (IndexedList *o)
  137. {
  138. IndexedList__Tree_Init(&o->tree);
  139. }
  140. static void IndexedList_InsertAt (IndexedList *o, IndexedListNode *node, uint64_t index)
  141. {
  142. ASSERT(index <= IndexedList__Tree_Count(&o->tree, 0))
  143. ASSERT(IndexedList__Tree_Count(&o->tree, 0) < UINT64_MAX - 1)
  144. uint64_t orig_count = IndexedList__Tree_Count(&o->tree, 0);
  145. B_USE(orig_count)
  146. IndexedList__Tree_InsertAt(&o->tree, 0, IndexedList__TreeDeref(0, node), index);
  147. ASSERT(IndexedList__Tree_IndexOf(&o->tree, 0, IndexedList__TreeDeref(0, node)) == index)
  148. ASSERT(IndexedList__Tree_Count(&o->tree, 0) == orig_count + 1)
  149. }
  150. static void IndexedList_Remove (IndexedList *o, IndexedListNode *node)
  151. {
  152. IndexedList__Tree_Remove(&o->tree, 0, IndexedList__TreeDeref(0, node));
  153. }
  154. static uint64_t IndexedList_Count (IndexedList *o)
  155. {
  156. return IndexedList__Tree_Count(&o->tree, 0);
  157. }
  158. static uint64_t IndexedList_IndexOf (IndexedList *o, IndexedListNode *node)
  159. {
  160. return IndexedList__Tree_IndexOf(&o->tree, 0, IndexedList__TreeDeref(0, node));
  161. }
  162. static IndexedListNode * IndexedList_GetAt (IndexedList *o, uint64_t index)
  163. {
  164. ASSERT(index < IndexedList__Tree_Count(&o->tree, 0))
  165. IndexedList__TreeRef ref = IndexedList__Tree_GetAt(&o->tree, 0, index);
  166. ASSERT(!IndexedList__TreeIsNullRef(ref))
  167. return ref.ptr;
  168. }
  169. static IndexedListNode * IndexedList_GetFirst (IndexedList *o)
  170. {
  171. return IndexedList__deref(IndexedList__Tree_GetFirst(&o->tree, 0));
  172. }
  173. static IndexedListNode * IndexedList_GetLast (IndexedList *o)
  174. {
  175. return IndexedList__deref(IndexedList__Tree_GetLast(&o->tree, 0));
  176. }
  177. static IndexedListNode * IndexedList_GetNext (IndexedList *o, IndexedListNode *node)
  178. {
  179. ASSERT(node)
  180. return IndexedList__deref(IndexedList__Tree_GetNext(&o->tree, 0, IndexedList__TreeDeref(0, node)));
  181. }
  182. static IndexedListNode * IndexedList_GetPrev (IndexedList *o, IndexedListNode *node)
  183. {
  184. ASSERT(node)
  185. return IndexedList__deref(IndexedList__Tree_GetPrev(&o->tree, 0, IndexedList__TreeDeref(0, node)));
  186. }
  187. #endif