IndexedList.h 6.1 KB

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