ordered_hash_map.h 8.5 KB

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