2
0

dictionary.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  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. #include "dictionary.h"
  31. #include "core/templates/ordered_hash_map.h"
  32. #include "core/templates/safe_refcount.h"
  33. #include "core/variant/variant.h"
  34. // required in this order by VariantInternal, do not remove this comment.
  35. #include "core/object/class_db.h"
  36. #include "core/object/object.h"
  37. #include "core/variant/type_info.h"
  38. #include "core/variant/variant_internal.h"
  39. struct DictionaryPrivate {
  40. SafeRefCount refcount;
  41. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> variant_map;
  42. };
  43. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  44. if (_p->variant_map.is_empty()) {
  45. return;
  46. }
  47. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  48. p_keys->push_back(E.key());
  49. }
  50. }
  51. Variant Dictionary::get_key_at_index(int p_index) const {
  52. int index = 0;
  53. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  54. if (index == p_index) {
  55. return E.key();
  56. }
  57. index++;
  58. }
  59. return Variant();
  60. }
  61. Variant Dictionary::get_value_at_index(int p_index) const {
  62. int index = 0;
  63. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  64. if (index == p_index) {
  65. return E.value();
  66. }
  67. index++;
  68. }
  69. return Variant();
  70. }
  71. Variant &Dictionary::operator[](const Variant &p_key) {
  72. if (p_key.get_type() == Variant::STRING_NAME) {
  73. const StringName *sn = VariantInternal::get_string_name(&p_key);
  74. return _p->variant_map[sn->operator String()];
  75. } else {
  76. return _p->variant_map[p_key];
  77. }
  78. }
  79. const Variant &Dictionary::operator[](const Variant &p_key) const {
  80. if (p_key.get_type() == Variant::STRING_NAME) {
  81. const StringName *sn = VariantInternal::get_string_name(&p_key);
  82. return _p->variant_map[sn->operator String()];
  83. } else {
  84. return _p->variant_map[p_key];
  85. }
  86. }
  87. const Variant *Dictionary::getptr(const Variant &p_key) const {
  88. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E;
  89. if (p_key.get_type() == Variant::STRING_NAME) {
  90. const StringName *sn = VariantInternal::get_string_name(&p_key);
  91. E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  92. } else {
  93. E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  94. }
  95. if (!E) {
  96. return nullptr;
  97. }
  98. return &E.get();
  99. }
  100. Variant *Dictionary::getptr(const Variant &p_key) {
  101. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E;
  102. if (p_key.get_type() == Variant::STRING_NAME) {
  103. const StringName *sn = VariantInternal::get_string_name(&p_key);
  104. E = ((OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  105. } else {
  106. E = ((OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  107. }
  108. if (!E) {
  109. return nullptr;
  110. }
  111. return &E.get();
  112. }
  113. Variant Dictionary::get_valid(const Variant &p_key) const {
  114. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E;
  115. if (p_key.get_type() == Variant::STRING_NAME) {
  116. const StringName *sn = VariantInternal::get_string_name(&p_key);
  117. E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  118. } else {
  119. E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  120. }
  121. if (!E) {
  122. return Variant();
  123. }
  124. return E.get();
  125. }
  126. Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
  127. const Variant *result = getptr(p_key);
  128. if (!result) {
  129. return p_default;
  130. }
  131. return *result;
  132. }
  133. int Dictionary::size() const {
  134. return _p->variant_map.size();
  135. }
  136. bool Dictionary::is_empty() const {
  137. return !_p->variant_map.size();
  138. }
  139. bool Dictionary::has(const Variant &p_key) const {
  140. if (p_key.get_type() == Variant::STRING_NAME) {
  141. const StringName *sn = VariantInternal::get_string_name(&p_key);
  142. return _p->variant_map.has(sn->operator String());
  143. } else {
  144. return _p->variant_map.has(p_key);
  145. }
  146. }
  147. bool Dictionary::has_all(const Array &p_keys) const {
  148. for (int i = 0; i < p_keys.size(); i++) {
  149. if (!has(p_keys[i])) {
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. bool Dictionary::erase(const Variant &p_key) {
  156. if (p_key.get_type() == Variant::STRING_NAME) {
  157. const StringName *sn = VariantInternal::get_string_name(&p_key);
  158. return _p->variant_map.erase(sn->operator String());
  159. } else {
  160. return _p->variant_map.erase(p_key);
  161. }
  162. }
  163. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  164. return recursive_equal(p_dictionary, 0);
  165. }
  166. bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
  167. return !recursive_equal(p_dictionary, 0);
  168. }
  169. bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
  170. // Cheap checks
  171. if (_p == p_dictionary._p) {
  172. return true;
  173. }
  174. if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
  175. return false;
  176. }
  177. // Heavy O(n) check
  178. if (recursion_count > MAX_RECURSION) {
  179. ERR_PRINT("Max recursion reached");
  180. return true;
  181. }
  182. recursion_count++;
  183. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement this_E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->front(); this_E; this_E = this_E.next()) {
  184. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement other_E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&p_dictionary._p->variant_map)->find(this_E.key());
  185. if (!other_E || !this_E.value().hash_compare(other_E.value(), recursion_count)) {
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. void Dictionary::_ref(const Dictionary &p_from) const {
  192. //make a copy first (thread safe)
  193. if (!p_from._p->refcount.ref()) {
  194. return; // couldn't copy
  195. }
  196. //if this is the same, unreference the other one
  197. if (p_from._p == _p) {
  198. _p->refcount.unref();
  199. return;
  200. }
  201. if (_p) {
  202. _unref();
  203. }
  204. _p = p_from._p;
  205. }
  206. void Dictionary::clear() {
  207. _p->variant_map.clear();
  208. }
  209. void Dictionary::_unref() const {
  210. ERR_FAIL_COND(!_p);
  211. if (_p->refcount.unref()) {
  212. memdelete(_p);
  213. }
  214. _p = nullptr;
  215. }
  216. uint32_t Dictionary::hash() const {
  217. return recursive_hash(0);
  218. }
  219. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  220. if (recursion_count > MAX_RECURSION) {
  221. ERR_PRINT("Max recursion reached");
  222. return 0;
  223. }
  224. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  225. recursion_count++;
  226. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  227. h = hash_djb2_one_32(E.key().recursive_hash(recursion_count), h);
  228. h = hash_djb2_one_32(E.value().recursive_hash(recursion_count), h);
  229. }
  230. return h;
  231. }
  232. Array Dictionary::keys() const {
  233. Array varr;
  234. if (_p->variant_map.is_empty()) {
  235. return varr;
  236. }
  237. varr.resize(size());
  238. int i = 0;
  239. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  240. varr[i] = E.key();
  241. i++;
  242. }
  243. return varr;
  244. }
  245. Array Dictionary::values() const {
  246. Array varr;
  247. if (_p->variant_map.is_empty()) {
  248. return varr;
  249. }
  250. varr.resize(size());
  251. int i = 0;
  252. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  253. varr[i] = E.get();
  254. i++;
  255. }
  256. return varr;
  257. }
  258. const Variant *Dictionary::next(const Variant *p_key) const {
  259. if (p_key == nullptr) {
  260. // caller wants to get the first element
  261. if (_p->variant_map.front()) {
  262. return &_p->variant_map.front().key();
  263. }
  264. return nullptr;
  265. }
  266. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
  267. if (E && E.next()) {
  268. return &E.next().key();
  269. }
  270. return nullptr;
  271. }
  272. Dictionary Dictionary::duplicate(bool p_deep) const {
  273. return recursive_duplicate(p_deep, 0);
  274. }
  275. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  276. Dictionary n;
  277. if (recursion_count > MAX_RECURSION) {
  278. ERR_PRINT("Max recursion reached");
  279. return n;
  280. }
  281. if (p_deep) {
  282. recursion_count++;
  283. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  284. n[E.key().recursive_duplicate(true, recursion_count)] = E.value().recursive_duplicate(true, recursion_count);
  285. }
  286. } else {
  287. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  288. n[E.key()] = E.value();
  289. }
  290. }
  291. return n;
  292. }
  293. void Dictionary::operator=(const Dictionary &p_dictionary) {
  294. _ref(p_dictionary);
  295. }
  296. const void *Dictionary::id() const {
  297. return _p->variant_map.id();
  298. }
  299. Dictionary::Dictionary(const Dictionary &p_from) {
  300. _p = nullptr;
  301. _ref(p_from);
  302. }
  303. Dictionary::Dictionary() {
  304. _p = memnew(DictionaryPrivate);
  305. _p->refcount.init();
  306. }
  307. Dictionary::~Dictionary() {
  308. _unref();
  309. }