dictionary.cpp 10 KB

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