dictionary.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/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, VariantComparator> 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. *_p->read_only = _p->variant_map[sn->operator String()];
  77. } else {
  78. *_p->read_only = _p->variant_map[p_key];
  79. }
  80. return *_p->read_only;
  81. } else {
  82. if (p_key.get_type() == Variant::STRING_NAME) {
  83. const StringName *sn = VariantInternal::get_string_name(&p_key);
  84. return _p->variant_map[sn->operator String()];
  85. } else {
  86. return _p->variant_map[p_key];
  87. }
  88. }
  89. }
  90. const Variant &Dictionary::operator[](const Variant &p_key) const {
  91. if (p_key.get_type() == Variant::STRING_NAME) {
  92. const StringName *sn = VariantInternal::get_string_name(&p_key);
  93. return _p->variant_map[sn->operator String()];
  94. } else {
  95. return _p->variant_map[p_key];
  96. }
  97. }
  98. const Variant *Dictionary::getptr(const Variant &p_key) const {
  99. HashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstIterator E;
  100. if (p_key.get_type() == Variant::STRING_NAME) {
  101. const StringName *sn = VariantInternal::get_string_name(&p_key);
  102. E = ((const HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  103. } else {
  104. E = ((const HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  105. }
  106. if (!E) {
  107. return nullptr;
  108. }
  109. return &E->value;
  110. }
  111. Variant *Dictionary::getptr(const Variant &p_key) {
  112. HashMap<Variant, Variant, VariantHasher, VariantComparator>::Iterator E;
  113. if (p_key.get_type() == Variant::STRING_NAME) {
  114. const StringName *sn = VariantInternal::get_string_name(&p_key);
  115. E = ((HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  116. } else {
  117. E = ((HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  118. }
  119. if (!E) {
  120. return nullptr;
  121. }
  122. if (unlikely(_p->read_only != nullptr)) {
  123. *_p->read_only = E->value;
  124. return _p->read_only;
  125. } else {
  126. return &E->value;
  127. }
  128. }
  129. Variant Dictionary::get_valid(const Variant &p_key) const {
  130. HashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstIterator E;
  131. if (p_key.get_type() == Variant::STRING_NAME) {
  132. const StringName *sn = VariantInternal::get_string_name(&p_key);
  133. E = ((const HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(sn->operator String());
  134. } else {
  135. E = ((const HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  136. }
  137. if (!E) {
  138. return Variant();
  139. }
  140. return E->value;
  141. }
  142. Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
  143. const Variant *result = getptr(p_key);
  144. if (!result) {
  145. return p_default;
  146. }
  147. return *result;
  148. }
  149. int Dictionary::size() const {
  150. return _p->variant_map.size();
  151. }
  152. bool Dictionary::is_empty() const {
  153. return !_p->variant_map.size();
  154. }
  155. bool Dictionary::has(const Variant &p_key) const {
  156. if (p_key.get_type() == Variant::STRING_NAME) {
  157. const StringName *sn = VariantInternal::get_string_name(&p_key);
  158. return _p->variant_map.has(sn->operator String());
  159. } else {
  160. return _p->variant_map.has(p_key);
  161. }
  162. }
  163. bool Dictionary::has_all(const Array &p_keys) const {
  164. for (int i = 0; i < p_keys.size(); i++) {
  165. if (!has(p_keys[i])) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. Variant Dictionary::find_key(const Variant &p_value) const {
  172. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  173. if (E.value == p_value) {
  174. return E.key;
  175. }
  176. }
  177. return Variant();
  178. }
  179. bool Dictionary::erase(const Variant &p_key) {
  180. ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
  181. if (p_key.get_type() == Variant::STRING_NAME) {
  182. const StringName *sn = VariantInternal::get_string_name(&p_key);
  183. return _p->variant_map.erase(sn->operator String());
  184. } else {
  185. return _p->variant_map.erase(p_key);
  186. }
  187. }
  188. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  189. return recursive_equal(p_dictionary, 0);
  190. }
  191. bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
  192. return !recursive_equal(p_dictionary, 0);
  193. }
  194. bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
  195. // Cheap checks
  196. if (_p == p_dictionary._p) {
  197. return true;
  198. }
  199. if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
  200. return false;
  201. }
  202. // Heavy O(n) check
  203. if (recursion_count > MAX_RECURSION) {
  204. ERR_PRINT("Max recursion reached");
  205. return true;
  206. }
  207. recursion_count++;
  208. for (const KeyValue<Variant, Variant> &this_E : _p->variant_map) {
  209. HashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstIterator other_E = ((const HashMap<Variant, Variant, VariantHasher, VariantComparator> *)&p_dictionary._p->variant_map)->find(this_E.key);
  210. if (!other_E || !this_E.value.hash_compare(other_E->value, recursion_count)) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. void Dictionary::_ref(const Dictionary &p_from) const {
  217. if (unlikely(p_from._p->read_only != nullptr)) {
  218. // If p_from is a read-only dictionary, just copy the contents to avoid further modification.
  219. if (_p) {
  220. _unref();
  221. }
  222. _p = memnew(DictionaryPrivate);
  223. _p->refcount.init();
  224. _p->variant_map = p_from._p->variant_map;
  225. return;
  226. }
  227. //make a copy first (thread safe)
  228. if (!p_from._p->refcount.ref()) {
  229. return; // couldn't copy
  230. }
  231. //if this is the same, unreference the other one
  232. if (p_from._p == _p) {
  233. _p->refcount.unref();
  234. return;
  235. }
  236. if (_p) {
  237. _unref();
  238. }
  239. _p = p_from._p;
  240. }
  241. void Dictionary::clear() {
  242. ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
  243. _p->variant_map.clear();
  244. }
  245. void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
  246. for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
  247. if (p_overwrite || !has(E.key)) {
  248. this->operator[](E.key) = E.value;
  249. }
  250. }
  251. }
  252. void Dictionary::_unref() const {
  253. ERR_FAIL_COND(!_p);
  254. if (_p->refcount.unref()) {
  255. if (_p->read_only) {
  256. memdelete(_p->read_only);
  257. }
  258. memdelete(_p);
  259. }
  260. _p = nullptr;
  261. }
  262. uint32_t Dictionary::hash() const {
  263. return recursive_hash(0);
  264. }
  265. uint32_t Dictionary::recursive_hash(int recursion_count) const {
  266. if (recursion_count > MAX_RECURSION) {
  267. ERR_PRINT("Max recursion reached");
  268. return 0;
  269. }
  270. uint32_t h = hash_murmur3_one_32(Variant::DICTIONARY);
  271. recursion_count++;
  272. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  273. h = hash_murmur3_one_32(E.key.recursive_hash(recursion_count), h);
  274. h = hash_murmur3_one_32(E.value.recursive_hash(recursion_count), h);
  275. }
  276. return hash_fmix32(h);
  277. }
  278. Array Dictionary::keys() const {
  279. Array varr;
  280. if (_p->variant_map.is_empty()) {
  281. return varr;
  282. }
  283. varr.resize(size());
  284. int i = 0;
  285. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  286. varr[i] = E.key;
  287. i++;
  288. }
  289. return varr;
  290. }
  291. Array Dictionary::values() const {
  292. Array varr;
  293. if (_p->variant_map.is_empty()) {
  294. return varr;
  295. }
  296. varr.resize(size());
  297. int i = 0;
  298. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  299. varr[i] = E.value;
  300. i++;
  301. }
  302. return varr;
  303. }
  304. const Variant *Dictionary::next(const Variant *p_key) const {
  305. if (p_key == nullptr) {
  306. // caller wants to get the first element
  307. if (_p->variant_map.begin()) {
  308. return &_p->variant_map.begin()->key;
  309. }
  310. return nullptr;
  311. }
  312. HashMap<Variant, Variant, VariantHasher, VariantComparator>::Iterator E = _p->variant_map.find(*p_key);
  313. if (!E) {
  314. return nullptr;
  315. }
  316. ++E;
  317. if (E) {
  318. return &E->key;
  319. }
  320. return nullptr;
  321. }
  322. Dictionary Dictionary::duplicate(bool p_deep) const {
  323. return recursive_duplicate(p_deep, 0);
  324. }
  325. void Dictionary::set_read_only(bool p_enable) {
  326. if (p_enable == bool(_p->read_only != nullptr)) {
  327. return;
  328. }
  329. if (p_enable) {
  330. _p->read_only = memnew(Variant);
  331. } else {
  332. memdelete(_p->read_only);
  333. _p->read_only = nullptr;
  334. }
  335. }
  336. bool Dictionary::is_read_only() const {
  337. return _p->read_only != nullptr;
  338. }
  339. Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
  340. Dictionary n;
  341. if (recursion_count > MAX_RECURSION) {
  342. ERR_PRINT("Max recursion reached");
  343. return n;
  344. }
  345. if (p_deep) {
  346. recursion_count++;
  347. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  348. n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
  349. }
  350. } else {
  351. for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
  352. n[E.key] = E.value;
  353. }
  354. }
  355. return n;
  356. }
  357. void Dictionary::operator=(const Dictionary &p_dictionary) {
  358. if (this == &p_dictionary) {
  359. return;
  360. }
  361. _ref(p_dictionary);
  362. }
  363. const void *Dictionary::id() const {
  364. return _p;
  365. }
  366. Dictionary::Dictionary(const Dictionary &p_from) {
  367. _p = nullptr;
  368. _ref(p_from);
  369. }
  370. Dictionary::Dictionary() {
  371. _p = memnew(DictionaryPrivate);
  372. _p->refcount.init();
  373. }
  374. Dictionary::~Dictionary() {
  375. _unref();
  376. }