IndexedList.h 6.6 KB

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