dictionary.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. int Dictionary::size() const {
  135. return _p->variant_map.size();
  136. }
  137. bool Dictionary::is_empty() const {
  138. return !_p->variant_map.size();
  139. }
  140. bool Dictionary::has(const Variant &p_key) const {
  141. return _p->variant_map.has(p_key);
  142. }
  143. bool Dictionary::has_all(const Array &p_keys) const {
  144. for (int i = 0; i < p_keys.size(); i++) {
  145. if (!has(p_keys[i])) {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. Variant Dictionary::find_key(const Variant &p_value) const {
  152. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  153. if (E.value == p_value) {
  154. return E.key;
  155. }
  156. }
  157. return Variant();
  158. }
  159. bool Dictionary::erase(const Variant &p_key) {
  160. ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
  161. return _p->variant_map.erase(p_key);
  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 (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
  184. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator other_E(p_dictionary._p->variant_map.find(this_E.key));
  185. if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count, false)) {
  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. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  208. _p->variant_map.clear();
  209. }
  210. void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  211. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  212. if (p_overwrite || !has(E.key)) {
  213. this->operator[](E.key) = E.value;
  214. }
  215. }
  216. }
  217. void Dictionary::_unref() const {
  218. ERR_FAIL_NULL(_p);
  219. if (_p->refcount.unref()) {
  220. if (_p->read_only) {
  221. memdelete(_p->read_only);
  222. }
  223. memdelete(_p);
  224. }
  225. _p = nullptr;
  226. }
  227. uint32_t Dictionary::hash() const {
  228. return recursive_hash(0);
  229. }
  230. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  231. if (recursion_count > MAX_RECURSION) {
  232. ERR_PRINT("Max recursion reached");
  233. return 0;
  234. }
  235. uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
  236. recursion_count++;
  237. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  238. h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
  239. h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
  240. }
  241. return hash_fmix32(h);
  242. }
  243. Array Dictionary::keys() const {
  244. Array varr;
  245. if (_p->variant_map.is_empty()) {
  246. return varr;
  247. }
  248. varr.resize(size());
  249. int i = 0;
  250. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  251. varr[i] = E.key;
  252. i++;
  253. }
  254. return varr;
  255. }
  256. Array Dictionary::values() const {
  257. Array varr;
  258. if (_p->variant_map.is_empty()) {
  259. return varr;
  260. }
  261. varr.resize(size());
  262. int i = 0;
  263. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  264. varr[i] = E.value;
  265. i++;
  266. }
  267. return varr;
  268. }
  269. const Variant *Dictionary::next(const Variant *p_key) const {
  270. if (p_key == nullptr) {
  271. // caller wants to get the first element
  272. if (_p->variant_map.begin()) {
  273. return &_p->variant_map.begin()->key;
  274. }
  275. return nullptr;
  276. }
  277. HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(*p_key);
  278. if (!E) {
  279. return nullptr;
  280. }
  281. ++E;
  282. if (E) {
  283. return &E->key;
  284. }
  285. return nullptr;
  286. }
  287. Dictionary Dictionary::duplicate(bool p_deep) const {
  288. return recursive_duplicate(p_deep, 0);
  289. }
  290. void Dictionary::make_read_only() {
  291. if (_p->read_only == nullptr) {
  292. _p->read_only = memnew(Variant);
  293. }
  294. }
  295. bool Dictionary::is_read_only() const {
  296. return _p->read_only != nullptr;
  297. }
  298. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  299. Dictionary n;
  300. if (recursion_count > MAX_RECURSION) {
  301. ERR_PRINT("Max recursion reached");
  302. return n;
  303. }
  304. if (p_deep) {
  305. recursion_count++;
  306. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  307. n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
  308. }
  309. } else {
  310. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  311. n[E.key] = E.value;
  312. }
  313. }
  314. return n;
  315. }
  316. void Dictionary::operator=(const Dictionary &p_dictionary) {
  317. if (this == &p_dictionary) {
  318. return;
  319. }
  320. _ref(p_dictionary);
  321. }
  322. const void *Dictionary::id() const {
  323. return _p;
  324. }
  325. Dictionary::Dictionary(const Dictionary &p_from) {
  326. _p = nullptr;
  327. _ref(p_from);
  328. }
  329. Dictionary::Dictionary() {
  330. _p = memnew(DictionaryPrivate);
  331. _p->refcount.init();
  332. }
  333. Dictionary::~Dictionary() {
  334. _unref();
  335. }