ordered_hash_map.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/hash_map.h"
  33. #include "core/list.h"
  34. #include "core/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;
  53. typename InternalList::Element *prev_element;
  54. typename InternalList::Element *next_element;
  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. list_element(NULL),
  65. prev_element(NULL),
  66. next_element(NULL) {
  67. }
  68. Element next() const {
  69. return Element(next_element);
  70. }
  71. Element prev() const {
  72. return Element(prev_element);
  73. }
  74. Element(const Element &other) :
  75. list_element(other.list_element),
  76. prev_element(other.prev_element),
  77. next_element(other.next_element) {
  78. }
  79. Element &operator=(const Element &other) {
  80. list_element = other.list_element;
  81. next_element = other.next_element;
  82. prev_element = other.prev_element;
  83. return *this;
  84. }
  85. _FORCE_INLINE_ bool operator==(const Element &p_other) const {
  86. return this->list_element == p_other.list_element;
  87. }
  88. _FORCE_INLINE_ bool operator!=(const Element &p_other) const {
  89. return this->list_element != p_other.list_element;
  90. }
  91. operator bool() const {
  92. return (list_element != NULL);
  93. }
  94. const K &key() const {
  95. CRASH_COND(!list_element);
  96. return *(list_element->get().first);
  97. };
  98. V &value() {
  99. CRASH_COND(!list_element);
  100. return list_element->get().second;
  101. };
  102. const V &value() const {
  103. CRASH_COND(!list_element);
  104. return list_element->get().second;
  105. };
  106. V &get() {
  107. CRASH_COND(!list_element);
  108. return list_element->get().second;
  109. };
  110. const V &get() const {
  111. CRASH_COND(!list_element);
  112. return list_element->get().second;
  113. };
  114. };
  115. class ConstElement {
  116. friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>;
  117. const typename InternalList::Element *list_element;
  118. ConstElement(const typename InternalList::Element *p_element) :
  119. list_element(p_element) {
  120. }
  121. public:
  122. _FORCE_INLINE_ ConstElement() :
  123. list_element(NULL) {
  124. }
  125. ConstElement(const ConstElement &other) :
  126. list_element(other.list_element) {
  127. }
  128. ConstElement &operator=(const ConstElement &other) {
  129. list_element = other.list_element;
  130. return *this;
  131. }
  132. ConstElement next() const {
  133. return ConstElement(list_element ? list_element->next() : NULL);
  134. }
  135. ConstElement prev() const {
  136. return ConstElement(list_element ? list_element->prev() : NULL);
  137. }
  138. _FORCE_INLINE_ bool operator==(const ConstElement &p_other) const {
  139. return this->list_element == p_other.list_element;
  140. }
  141. _FORCE_INLINE_ bool operator!=(const ConstElement &p_other) const {
  142. return this->list_element != p_other.list_element;
  143. }
  144. operator bool() const {
  145. return (list_element != NULL);
  146. }
  147. const K &key() const {
  148. CRASH_COND(!list_element);
  149. return *(list_element->get().first);
  150. };
  151. const V &value() const {
  152. CRASH_COND(!list_element);
  153. return list_element->get().second;
  154. };
  155. const V &get() const {
  156. CRASH_COND(!list_element);
  157. return list_element->get().second;
  158. };
  159. };
  160. ConstElement find(const K &p_key) const {
  161. typename InternalList::Element *const *list_element = map.getptr(p_key);
  162. if (list_element) {
  163. return ConstElement(*list_element);
  164. }
  165. return ConstElement(NULL);
  166. }
  167. Element find(const K &p_key) {
  168. typename InternalList::Element **list_element = map.getptr(p_key);
  169. if (list_element) {
  170. return Element(*list_element);
  171. }
  172. return Element(NULL);
  173. }
  174. Element insert(const K &p_key, const V &p_value) {
  175. typename InternalList::Element **list_element = map.getptr(p_key);
  176. if (list_element) {
  177. (*list_element)->get().second = p_value;
  178. return Element(*list_element);
  179. }
  180. typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(NULL, p_value));
  181. typename InternalMap::Element *e = map.set(p_key, new_element);
  182. new_element->get().first = &e->key();
  183. return Element(new_element);
  184. }
  185. void erase(Element &p_element) {
  186. map.erase(p_element.key());
  187. list.erase(p_element.list_element);
  188. p_element.list_element = NULL;
  189. }
  190. bool erase(const K &p_key) {
  191. typename InternalList::Element **list_element = map.getptr(p_key);
  192. if (list_element) {
  193. list.erase(*list_element);
  194. map.erase(p_key);
  195. return true;
  196. }
  197. return false;
  198. }
  199. inline bool has(const K &p_key) const {
  200. return map.has(p_key);
  201. }
  202. const V &operator[](const K &p_key) const {
  203. ConstElement e = find(p_key);
  204. CRASH_COND(!e);
  205. return e.value();
  206. }
  207. V &operator[](const K &p_key) {
  208. Element e = find(p_key);
  209. if (!e) {
  210. // consistent with Map behaviour
  211. e = insert(p_key, V());
  212. }
  213. return e.value();
  214. }
  215. inline Element front() {
  216. return Element(list.front());
  217. }
  218. inline Element back() {
  219. return Element(list.back());
  220. }
  221. inline ConstElement front() const {
  222. return ConstElement(list.front());
  223. }
  224. inline ConstElement back() const {
  225. return ConstElement(list.back());
  226. }
  227. inline bool empty() const { return list.empty(); }
  228. inline int size() const { return list.size(); }
  229. void clear() {
  230. map.clear();
  231. list.clear();
  232. }
  233. private:
  234. void _copy_from(const OrderedHashMap &p_map) {
  235. for (ConstElement E = p_map.front(); E; E = E.next()) {
  236. insert(E.key(), E.value());
  237. }
  238. }
  239. public:
  240. void operator=(const OrderedHashMap &p_map) {
  241. _copy_from(p_map);
  242. }
  243. OrderedHashMap(const OrderedHashMap &p_map) {
  244. _copy_from(p_map);
  245. }
  246. _FORCE_INLINE_ OrderedHashMap() {
  247. }
  248. };
  249. #endif // ORDERED_HASH_MAP_H