IndexedList.h 6.8 KB

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