dictionary.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**************************************************************************/
  2. /* dictionary.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/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. Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
  42. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
  43. };
  44. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  45. if (_p->variant_map.is_empty()) {
  46. return;
  47. }
  48. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  49. p_keys->push_back(E.key);
  50. }
  51. }
  52. Variant Dictionary::get_key_at_index(int p_index) const {
  53. int index = 0;
  54. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  55. if (index == p_index) {
  56. return E.key;
  57. }
  58. index++;
  59. }
  60. return Variant();
  61. }
  62. Variant Dictionary::get_value_at_index(int p_index) const {
  63. int index = 0;
  64. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  65. if (index == p_index) {
  66. return E.value;
  67. }
  68. index++;
  69. }
  70. return Variant();
  71. }
  72. Variant &Dictionary::operator[](const Variant &p_key) {
  73. if (unlikely(_p->read_only)) {
  74. if (p_key.get_type() == Variant::STRING_NAME) {
  75. const StringName *sn = VariantInternal::get_string_name(&p_key);
  76. const String &key = sn->operator String();
  77. if (likely(_p->variant_map.has(key))) {
  78. *_p->read_only = _p->variant_map[key];
  79. } else {
  80. *_p->read_only = Variant();
  81. }
  82. } else if (likely(_p->variant_map.has(p_key))) {
  83. *_p->read_only = _p->variant_map[p_key];
  84. } else {
  85. *_p->read_only = Variant();
  86. }
  87. return *_p->read_only;
  88. } else {
  89. if (p_key.get_type() == Variant::STRING_NAME) {
  90. const StringName *sn = VariantInternal::get_string_name(&p_key);
  91. return _p->variant_map[sn->operator String()];
  92. } else {
  93. return _p->variant_map[p_key];
  94. }
  95. }
  96. }
  97. const Variant &Dictionary::operator[](const Variant &p_key) const {
  98. // Will not insert key, so no conversion is necessary.
  99. return _p->variant_map[p_key];
  100. }
  101. const Variant *Dictionary::getptr(const Variant &p_key) const {
  102. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(p_key));
  103. if (!E) {
  104. return nullptr;
  105. }
  106. return &E->value;
  107. }
  108. Variant *Dictionary::getptr(const Variant &p_key) {
  109. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(p_key));
  110. if (!E) {
  111. return nullptr;
  112. }
  113. if (unlikely(_p->read_only != nullptr)) {
  114. *_p->read_only = E->value;
  115. return _p->read_only;
  116. } else {
  117. return &E->value;
  118. }
  119. }
  120. Variant Dictionary::get_valid(const Variant &p_key) const {
  121. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(p_key));
  122. if (!E) {
  123. return Variant();
  124. }
  125. return E->value;
  126. }
  127. Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
  128. const Variant *result = getptr(p_key);
  129. if (!result) {
  130. return p_default;
  131. }
  132. return *result;
  133. }
  134. Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
  135. const Variant *result = getptr(p_key);
  136. if (!result) {
  137. operator[](p_key) = p_default;
  138. return p_default;
  139. }
  140. return *result;
  141. }
  142. int Dictionary::size() const {
  143. return _p->variant_map.size();
  144. }
  145. bool Dictionary::is_empty() const {
  146. return !_p->variant_map.size();
  147. }
  148. bool Dictionary::has(const Variant &p_key) const {
  149. return _p->variant_map.has(p_key);
  150. }
  151. bool Dictionary::has_all(const Array &p_keys) const {
  152. for (int i = 0; i < p_keys.size(); i++) {
  153. if (!has(p_keys[i])) {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. Variant Dictionary::find_key(const Variant &p_value) const {
  160. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  161. if (E.value == p_value) {
  162. return E.key;
  163. }
  164. }
  165. return Variant();
  166. }
  167. bool Dictionary::erase(const Variant &p_key) {
  168. ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
  169. return _p->variant_map.erase(p_key);
  170. }
  171. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  172. return recursive_equal(p_dictionary, 0);
  173. }
  174. bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
  175. return !recursive_equal(p_dictionary, 0);
  176. }
  177. bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
  178. // Cheap checks
  179. if (_p == p_dictionary._p) {
  180. return true;
  181. }
  182. if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
  183. return false;
  184. }
  185. // Heavy O(n) check
  186. if (recursion_count > MAX_RECURSION) {
  187. ERR_PRINT("Max recursion reached");
  188. return true;
  189. }
  190. recursion_count++;
  191. for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
  192. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key));
  193. if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) {
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. void Dictionary::_ref(const Dictionary &p_from) const {
  200. //make a copy first (thread safe)
  201. if (!p_from._p->refcount.ref()) {
  202. return; // couldn't copy
  203. }
  204. //if this is the same, unreference the other one
  205. if (p_from._p == _p) {
  206. _p->refcount.unref();
  207. return;
  208. }
  209. if (_p) {
  210. _unref();
  211. }
  212. _p = p_from._p;
  213. }
  214. void Dictionary::clear() {
  215. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  216. _p->variant_map.clear();
  217. }
  218. void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  219. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  220. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  221. if (p_overwrite || !has(E.key)) {
  222. operator[](E.key) = E.value;
  223. }
  224. }
  225. }
  226. Dictionary Dictionary::merged(const Dictionary &p_dictionary, bool p_overwrite) const {
  227. Dictionary ret = duplicate();
  228. ret.merge(p_dictionary, p_overwrite);
  229. return ret;
  230. }
  231. void Dictionary::_unref() const {
  232. ERR_FAIL_NULL(_p);
  233. if (_p->refcount.unref()) {
  234. if (_p->read_only) {
  235. memdelete(_p->read_only);
  236. }
  237. memdelete(_p);
  238. }
  239. _p = nullptr;
  240. }
  241. uint32_t Dictionary::hash() const {
  242. return recursive_hash(0);
  243. }
  244. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  245. if (recursion_count > MAX_RECURSION) {
  246. ERR_PRINT("Max recursion reached");
  247. return 0;
  248. }
  249. uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
  250. recursion_count++;
  251. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  252. h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
  253. h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
  254. }
  255. return hash_fmix32(h);
  256. }
  257. Array Dictionary::keys() const {
  258. Array varr;
  259. if (_p->variant_map.is_empty()) {
  260. return varr;
  261. }
  262. varr.resize(size());
  263. int i = 0;
  264. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  265. varr[i] = E.key;
  266. i++;
  267. }
  268. return varr;
  269. }
  270. Array Dictionary::values() const {
  271. Array varr;
  272. if (_p->variant_map.is_empty()) {
  273. return varr;
  274. }
  275. varr.resize(size());
  276. int i = 0;
  277. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  278. varr[i] = E.value;
  279. i++;
  280. }
  281. return varr;
  282. }
  283. const Variant *Dictionary::next(const Variant *p_key) const {
  284. if (p_key == nullptr) {
  285. // caller wants to get the first element
  286. if (_p->variant_map.begin()) {
  287. return &_p->variant_map.begin()->key;
  288. }
  289. return nullptr;
  290. }
  291. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(*p_key);
  292. if (!E) {
  293. return nullptr;
  294. }
  295. ++E;
  296. if (E) {
  297. return &E->key;
  298. }
  299. return nullptr;
  300. }
  301. Dictionary Dictionary::duplicate(bool p_deep) const {
  302. return recursive_duplicate(p_deep, 0);
  303. }
  304. void Dictionary::make_read_only() {
  305. if (_p->read_only == nullptr) {
  306. _p->read_only = memnew(Variant);
  307. }
  308. }
  309. bool Dictionary::is_read_only() const {
  310. return _p->read_only != nullptr;
  311. }
  312. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  313. Dictionary n;
  314. if (recursion_count > MAX_RECURSION) {
  315. ERR_PRINT("Max recursion reached");
  316. return n;
  317. }
  318. if (p_deep) {
  319. recursion_count++;
  320. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  321. n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
  322. }
  323. } else {
  324. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  325. n[E.key] = E.value;
  326. }
  327. }
  328. return n;
  329. }
  330. void Dictionary::operator=(const Dictionary &p_dictionary) {
  331. if (this == &p_dictionary) {
  332. return;
  333. }
  334. _ref(p_dictionary);
  335. }
  336. const void *Dictionary::id() const {
  337. return _p;
  338. }
  339. Dictionary::Dictionary(const Dictionary &p_from) {
  340. _p = nullptr;
  341. _ref(p_from);
  342. }
  343. Dictionary::Dictionary() {
  344. _p = memnew(DictionaryPrivate);
  345. _p->refcount.init();
  346. }
  347. Dictionary::~Dictionary() {
  348. _unref();
  349. }