IndexedList.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "IndexedList_tree.h"
  100. #include <structure/CAvl_impl.h>
  101. static void IndexedList_Init (IndexedList *o)
  102. {
  103. IndexedList__Tree_Init(&o->tree);
  104. }
  105. static void IndexedList_InsertAt (IndexedList *o, IndexedListNode *node, uint64_t index)
  106. {
  107. ASSERT(index <= IndexedList__Tree_Count(&o->tree, 0))
  108. ASSERT(IndexedList__Tree_Count(&o->tree, 0) < UINT64_MAX - 1)
  109. uint64_t orig_count = IndexedList__Tree_Count(&o->tree, 0);
  110. IndexedList__Tree_InsertAt(&o->tree, 0, IndexedList__Tree_Deref(0, node), index);
  111. ASSERT(IndexedList__Tree_IndexOf(&o->tree, 0, IndexedList__Tree_Deref(0, node)) == index)
  112. ASSERT(IndexedList__Tree_Count(&o->tree, 0) == orig_count + 1)
  113. }
  114. static void IndexedList_Remove (IndexedList *o, IndexedListNode *node)
  115. {
  116. IndexedList__Tree_Remove(&o->tree, 0, IndexedList__Tree_Deref(0, node));
  117. }
  118. static uint64_t IndexedList_Count (IndexedList *o)
  119. {
  120. return IndexedList__Tree_Count(&o->tree, 0);
  121. }
  122. static uint64_t IndexedList_IndexOf (IndexedList *o, IndexedListNode *node)
  123. {
  124. return IndexedList__Tree_IndexOf(&o->tree, 0, IndexedList__Tree_Deref(0, node));
  125. }
  126. static IndexedListNode * IndexedList_GetAt (IndexedList *o, uint64_t index)
  127. {
  128. ASSERT(index < IndexedList__Tree_Count(&o->tree, 0))
  129. IndexedList__TreeNode ref = IndexedList__Tree_GetAt(&o->tree, 0, index);
  130. ASSERT(ref.link != IndexedList__TreeNullLink)
  131. return ref.ptr;
  132. }
  133. #endif