dictionary.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "ordered_hash_map.h"
  32. #include "safe_refcount.h"
  33. #include "variant.h"
  34. struct DictionaryPrivate {
  35. SafeRefCount refcount;
  36. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> variant_map;
  37. };
  38. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  39. if (_p->variant_map.empty())
  40. return;
  41. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  42. p_keys->push_back(E.key());
  43. }
  44. }
  45. Variant &Dictionary::operator[](const Variant &p_key) {
  46. return _p->variant_map[p_key];
  47. }
  48. const Variant &Dictionary::operator[](const Variant &p_key) const {
  49. return _p->variant_map[p_key];
  50. }
  51. const Variant *Dictionary::getptr(const Variant &p_key) const {
  52. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  53. if (!E)
  54. return NULL;
  55. return &E.get();
  56. }
  57. Variant *Dictionary::getptr(const Variant &p_key) {
  58. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(p_key);
  59. if (!E)
  60. return NULL;
  61. return &E.get();
  62. }
  63. Variant Dictionary::get_valid(const Variant &p_key) const {
  64. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->find(p_key);
  65. if (!E)
  66. return Variant();
  67. return E.get();
  68. }
  69. int Dictionary::size() const {
  70. return _p->variant_map.size();
  71. }
  72. bool Dictionary::empty() const {
  73. return !_p->variant_map.size();
  74. }
  75. bool Dictionary::has(const Variant &p_key) const {
  76. return _p->variant_map.has(p_key);
  77. }
  78. bool Dictionary::has_all(const Array &p_keys) const {
  79. for (int i = 0; i < p_keys.size(); i++) {
  80. if (!has(p_keys[i])) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. void Dictionary::erase(const Variant &p_key) {
  87. _p->variant_map.erase(p_key);
  88. }
  89. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  90. return _p == p_dictionary._p;
  91. }
  92. void Dictionary::_ref(const Dictionary &p_from) const {
  93. //make a copy first (thread safe)
  94. if (!p_from._p->refcount.ref())
  95. return; // couldn't copy
  96. //if this is the same, unreference the other one
  97. if (p_from._p == _p) {
  98. _p->refcount.unref();
  99. return;
  100. }
  101. if (_p)
  102. _unref();
  103. _p = p_from._p;
  104. }
  105. void Dictionary::clear() {
  106. _p->variant_map.clear();
  107. }
  108. void Dictionary::_unref() const {
  109. ERR_FAIL_COND(!_p);
  110. if (_p->refcount.unref()) {
  111. memdelete(_p);
  112. }
  113. _p = NULL;
  114. }
  115. uint32_t Dictionary::hash() const {
  116. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  117. List<Variant> keys;
  118. get_key_list(&keys);
  119. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  120. h = hash_djb2_one_32(E->get().hash(), h);
  121. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  122. }
  123. return h;
  124. }
  125. Array Dictionary::keys() const {
  126. Array varr;
  127. varr.resize(size());
  128. if (_p->variant_map.empty())
  129. return varr;
  130. int i = 0;
  131. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  132. varr[i] = E.key();
  133. i++;
  134. }
  135. return varr;
  136. }
  137. Array Dictionary::values() const {
  138. Array varr;
  139. varr.resize(size());
  140. if (_p->variant_map.empty())
  141. return varr;
  142. int i = 0;
  143. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  144. varr[i] = E.get();
  145. i++;
  146. }
  147. return varr;
  148. }
  149. const Variant *Dictionary::next(const Variant *p_key) const {
  150. if (p_key == NULL) {
  151. // caller wants to get the first element
  152. if (_p->variant_map.front())
  153. return &_p->variant_map.front().key();
  154. return NULL;
  155. }
  156. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
  157. if (E && E.next())
  158. return &E.next().key();
  159. return NULL;
  160. }
  161. Dictionary Dictionary::duplicate(bool p_deep) const {
  162. Dictionary n;
  163. List<Variant> keys;
  164. get_key_list(&keys);
  165. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  166. n[E->get()] = p_deep ? operator[](E->get()).duplicate(p_deep) : operator[](E->get());
  167. }
  168. return n;
  169. }
  170. void Dictionary::operator=(const Dictionary &p_dictionary) {
  171. _ref(p_dictionary);
  172. }
  173. Dictionary::Dictionary(const Dictionary &p_from) {
  174. _p = NULL;
  175. _ref(p_from);
  176. }
  177. Dictionary::Dictionary() {
  178. _p = memnew(DictionaryPrivate);
  179. _p->refcount.init();
  180. }
  181. Dictionary::~Dictionary() {
  182. _unref();
  183. }