dictionary.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "dictionary.h"
  31. #include "core/ordered_hash_map.h"
  32. #include "core/safe_refcount.h"
  33. #include "core/variant.h"
  34. struct DictionaryPrivate {
  35. SafeRefCount refcount;
  36. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> variant_map;
  37. };
  38. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  39. if (_p->variant_map.empty()) {
  40. return;
  41. }
  42. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  43. p_keys->push_back(E.key());
  44. }
  45. }
  46. Variant Dictionary::get_key_at_index(int p_index) const {
  47. int index = 0;
  48. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  49. if (index == p_index) {
  50. return E.key();
  51. }
  52. index++;
  53. }
  54. return Variant();
  55. }
  56. Variant Dictionary::get_value_at_index(int p_index) const {
  57. int index = 0;
  58. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  59. if (index == p_index) {
  60. return E.value();
  61. }
  62. index++;
  63. }
  64. return Variant();
  65. }
  66. Variant &Dictionary::operator[](const Variant &p_key) {
  67. return _p->variant_map[p_key];
  68. }
  69. const Variant &Dictionary::operator[](const Variant &p_key) const {
  70. return _p->variant_map[p_key];
  71. }
  72. const Variant *Dictionary::getptr(const Variant &p_key) const {
  73. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  74. if (!E) {
  75. return nullptr;
  76. }
  77. return &E.get();
  78. }
  79. Variant *Dictionary::getptr(const Variant &p_key) {
  80. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(p_key);
  81. if (!E) {
  82. return nullptr;
  83. }
  84. return &E.get();
  85. }
  86. Variant Dictionary::get_valid(const Variant &p_key) const {
  87. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  88. if (!E) {
  89. return Variant();
  90. }
  91. return E.get();
  92. }
  93. Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
  94. const Variant *result = getptr(p_key);
  95. if (!result) {
  96. return p_default;
  97. }
  98. return *result;
  99. }
  100. int Dictionary::size() const {
  101. return _p->variant_map.size();
  102. }
  103. bool Dictionary::empty() const {
  104. return !_p->variant_map.size();
  105. }
  106. bool Dictionary::has(const Variant &p_key) const {
  107. return _p->variant_map.has(p_key);
  108. }
  109. bool Dictionary::has_all(const Array &p_keys) const {
  110. for (int i = 0; i < p_keys.size(); i++) {
  111. if (!has(p_keys[i])) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. bool Dictionary::erase(const Variant &p_key) {
  118. return _p->variant_map.erase(p_key);
  119. }
  120. bool Dictionary::deep_equal(const Dictionary &p_dictionary, int p_recursion_count) const {
  121. // Cheap checks
  122. ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, 0, "Max recursion reached");
  123. if (_p == p_dictionary._p) {
  124. return true;
  125. }
  126. if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
  127. return false;
  128. }
  129. // Heavy O(n) check
  130. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element this_E = _p->variant_map.front();
  131. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element other_E = p_dictionary._p->variant_map.front();
  132. p_recursion_count++;
  133. while (this_E && other_E) {
  134. if (
  135. !this_E.key().deep_equal(other_E.key(), p_recursion_count) ||
  136. !this_E.value().deep_equal(other_E.value(), p_recursion_count)) {
  137. return false;
  138. }
  139. this_E = this_E.next();
  140. other_E = other_E.next();
  141. }
  142. return !this_E && !other_E;
  143. }
  144. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  145. return _p == p_dictionary._p;
  146. }
  147. bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
  148. return _p != p_dictionary._p;
  149. }
  150. void Dictionary::_ref(const Dictionary &p_from) const {
  151. //make a copy first (thread safe)
  152. if (!p_from._p->refcount.ref()) {
  153. return; // couldn't copy
  154. }
  155. //if this is the same, unreference the other one
  156. if (p_from._p == _p) {
  157. _p->refcount.unref();
  158. return;
  159. }
  160. if (_p) {
  161. _unref();
  162. }
  163. _p = p_from._p;
  164. }
  165. void Dictionary::clear() {
  166. _p->variant_map.clear();
  167. }
  168. void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  169. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = p_dictionary._p->variant_map.front(); E; E = E.next()) {
  170. if (p_overwrite || !has(E.key())) {
  171. this->operator[](E.key()) = E.value();
  172. }
  173. }
  174. }
  175. void Dictionary::_unref() const {
  176. ERR_FAIL_COND(!_p);
  177. if (_p->refcount.unref()) {
  178. memdelete(_p);
  179. }
  180. _p = nullptr;
  181. }
  182. uint32_t Dictionary::hash() const {
  183. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  184. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  185. h = hash_djb2_one_32(E.key().hash(), h);
  186. h = hash_djb2_one_32(E.value().hash(), h);
  187. }
  188. return h;
  189. }
  190. Array Dictionary::keys() const {
  191. Array varr;
  192. if (_p->variant_map.empty()) {
  193. return varr;
  194. }
  195. varr.resize(size());
  196. int i = 0;
  197. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  198. varr[i] = E.key();
  199. i++;
  200. }
  201. return varr;
  202. }
  203. Array Dictionary::values() const {
  204. Array varr;
  205. if (_p->variant_map.empty()) {
  206. return varr;
  207. }
  208. varr.resize(size());
  209. int i = 0;
  210. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  211. varr[i] = E.get();
  212. i++;
  213. }
  214. return varr;
  215. }
  216. const Variant *Dictionary::next(const Variant *p_key) const {
  217. if (p_key == nullptr) {
  218. // caller wants to get the first element
  219. if (_p->variant_map.front()) {
  220. return &_p->variant_map.front().key();
  221. }
  222. return nullptr;
  223. }
  224. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
  225. if (E && E.next()) {
  226. return &E.next().key();
  227. }
  228. return nullptr;
  229. }
  230. Dictionary Dictionary::duplicate(bool p_deep) const {
  231. Dictionary n;
  232. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  233. n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
  234. }
  235. return n;
  236. }
  237. void Dictionary::operator=(const Dictionary &p_dictionary) {
  238. _ref(p_dictionary);
  239. }
  240. const void *Dictionary::id() const {
  241. return _p;
  242. }
  243. Dictionary::Dictionary(const Dictionary &p_from) {
  244. _p = nullptr;
  245. _ref(p_from);
  246. }
  247. Dictionary::Dictionary() {
  248. _p = memnew(DictionaryPrivate);
  249. _p->refcount.init();
  250. }
  251. Dictionary::~Dictionary() {
  252. _unref();
  253. }