ordered_hash_map.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* ordered_hash_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ORDERED_HASH_MAP_H
  31. #define ORDERED_HASH_MAP_H
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/list.h"
  34. #include "core/templates/pair.h"
  35. /**
  36. * A hash map which allows to iterate elements in insertion order.
  37. * Insertion, lookup, deletion have O(1) complexity.
  38. * The API aims to be consistent with Map rather than HashMap, because the
  39. * former is more frequently used and is more coherent with the rest of the
  40. * codebase.
  41. * Deletion during iteration is safe and will preserve the order.
  42. */
  43. template <class K, class V, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<K>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8>
  44. class OrderedHashMap {
  45. typedef List<Pair<const K *, V>> InternalList;
  46. typedef HashMap<K, typename InternalList::Element *, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP> InternalMap;
  47. InternalList list;
  48. InternalMap map;
  49. public:
  50. class Element {
  51. friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>;
  52. typename InternalList::Element *list_element = nullptr;
  53. typename InternalList::Element *prev_element = nullptr;
  54. typename InternalList::Element *next_element = nullptr;
  55. Element(typename InternalList::Element *p_element) {
  56. list_element = p_element;
  57. if (list_element) {
  58. next_element = list_element->next();
  59. prev_element = list_element->prev();
  60. }
  61. }
  62. public:
  63. _FORCE_INLINE_ Element() {}
  64. Element next() const {
  65. return Element(next_element);
  66. }
  67. Element prev() const {
  68. return Element(prev_element);
  69. }
  70. Element(const Element &other) :
  71. list_element(other.list_element),
  72. prev_element(other.prev_element),
  73. next_element(other.next_element) {
  74. }
  75. Element &operator=(const Element &other) {
  76. list_element = other.list_element;
  77. next_element = other.next_element;
  78. prev_element = other.prev_element;
  79. return *this;
  80. }
  81. _FORCE_INLINE_ bool operator==(const Element &p_other) const {
  82. return this->list_element == p_other.list_element;
  83. }
  84. _FORCE_INLINE_ bool operator!=(const Element &p_other) const {
  85. return this->list_element != p_other.list_element;
  86. }
  87. operator bool() const {
  88. return (list_element != nullptr);
  89. }
  90. const K &key() const {
  91. CRASH_COND(!list_element);
  92. return *(list_element->get().first);
  93. }
  94. V &value() {
  95. CRASH_COND(!list_element);
  96. return list_element->get().second;
  97. }
  98. const V &value() const {
  99. CRASH_COND(!list_element);
  100. return list_element->get().second;
  101. }
  102. V &get() {
  103. CRASH_COND(!list_element);
  104. return list_element->get().second;
  105. }
  106. const V &get() const {
  107. CRASH_COND(!list_element);
  108. return list_element->get().second;
  109. }
  110. };
  111. class ConstElement {
  112. friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>;
  113. const typename InternalList::Element *list_element = nullptr;
  114. ConstElement(const typename InternalList::Element *p_element) :
  115. list_element(p_element) {
  116. }
  117. public:
  118. _FORCE_INLINE_ ConstElement() {}
  119. ConstElement(const ConstElement &other) :
  120. list_element(other.list_element) {
  121. }
  122. ConstElement &operator=(const ConstElement &other) {
  123. list_element = other.list_element;
  124. return *this;
  125. }
  126. ConstElement next() const {
  127. return ConstElement(list_element ? list_element->next() : nullptr);
  128. }
  129. ConstElement prev() const {
  130. return ConstElement(list_element ? list_element->prev() : nullptr);
  131. }
  132. _FORCE_INLINE_ bool operator==(const ConstElement &p_other) const {
  133. return this->list_element == p_other.list_element;
  134. }
  135. _FORCE_INLINE_ bool operator!=(const ConstElement &p_other) const {
  136. return this->list_element != p_other.list_element;
  137. }
  138. operator bool() const {
  139. return (list_element != nullptr);
  140. }
  141. const K &key() const {
  142. CRASH_COND(!list_element);
  143. return *(list_element->get().first);
  144. }
  145. const V &value() const {
  146. CRASH_COND(!list_element);
  147. return list_element->get().second;
  148. }
  149. const V &get() const {
  150. CRASH_COND(!list_element);
  151. return list_element->get().second;
  152. }
  153. };
  154. ConstElement find(const K &p_key) const {
  155. typename InternalList::Element *const *list_element = map.getptr(p_key);
  156. if (list_element) {
  157. return ConstElement(*list_element);
  158. }
  159. return ConstElement(nullptr);
  160. }
  161. Element find(const K &p_key) {
  162. typename InternalList::Element **list_element = map.getptr(p_key);
  163. if (list_element) {
  164. return Element(*list_element);
  165. }
  166. return Element(nullptr);
  167. }
  168. Element insert(const K &p_key, const V &p_value) {
  169. typename InternalList::Element **list_element = map.getptr(p_key);
  170. if (list_element) {
  171. (*list_element)->get().second = p_value;
  172. return Element(*list_element);
  173. }
  174. typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(nullptr, p_value));
  175. typename InternalMap::Element *e = map.set(p_key, new_element);
  176. new_element->get().first = &e->key();
  177. return Element(new_element);
  178. }
  179. void erase(Element &p_element) {
  180. map.erase(p_element.key());
  181. list.erase(p_element.list_element);
  182. p_element.list_element = nullptr;
  183. }
  184. bool erase(const K &p_key) {
  185. typename InternalList::Element **list_element = map.getptr(p_key);
  186. if (list_element) {
  187. list.erase(*list_element);
  188. map.erase(p_key);
  189. return true;
  190. }
  191. return false;
  192. }
  193. inline bool has(const K &p_key) const {
  194. return map.has(p_key);
  195. }
  196. const V &operator[](const K &p_key) const {
  197. ConstElement e = find(p_key);
  198. CRASH_COND(!e);
  199. return e.value();
  200. }
  201. V &operator[](const K &p_key) {
  202. Element e = find(p_key);
  203. if (!e) {
  204. // consistent with Map behaviour
  205. e = insert(p_key, V());
  206. }
  207. return e.value();
  208. }
  209. inline Element front() {
  210. return Element(list.front());
  211. }
  212. inline Element back() {
  213. return Element(list.back());
  214. }
  215. inline ConstElement front() const {
  216. return ConstElement(list.front());
  217. }
  218. inline ConstElement back() const {
  219. return ConstElement(list.back());
  220. }
  221. inline bool is_empty() const { return list.is_empty(); }
  222. inline int size() const { return list.size(); }
  223. const void *id() const {
  224. return list.id();
  225. }
  226. void clear() {
  227. map.clear();
  228. list.clear();
  229. }
  230. private:
  231. void _copy_from(const OrderedHashMap &p_map) {
  232. for (ConstElement E = p_map.front(); E; E = E.next()) {
  233. insert(E.key(), E.value());
  234. }
  235. }
  236. public:
  237. void operator=(const OrderedHashMap &p_map) {
  238. _copy_from(p_map);
  239. }
  240. OrderedHashMap(const OrderedHashMap &p_map) {
  241. _copy_from(p_map);
  242. }
  243. _FORCE_INLINE_ OrderedHashMap() {}
  244. };
  245. #endif // ORDERED_HASH_MAP_H