dictionary.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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::erase_checked(const Variant &p_key) {
  90. return _p->variant_map.erase(p_key);
  91. }
  92. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  93. return _p == p_dictionary._p;
  94. }
  95. void Dictionary::_ref(const Dictionary &p_from) const {
  96. //make a copy first (thread safe)
  97. if (!p_from._p->refcount.ref())
  98. return; // couldn't copy
  99. //if this is the same, unreference the other one
  100. if (p_from._p == _p) {
  101. _p->refcount.unref();
  102. return;
  103. }
  104. if (_p)
  105. _unref();
  106. _p = p_from._p;
  107. }
  108. void Dictionary::clear() {
  109. _p->variant_map.clear();
  110. }
  111. void Dictionary::_unref() const {
  112. ERR_FAIL_COND(!_p);
  113. if (_p->refcount.unref()) {
  114. memdelete(_p);
  115. }
  116. _p = NULL;
  117. }
  118. uint32_t Dictionary::hash() const {
  119. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  120. List<Variant> keys;
  121. get_key_list(&keys);
  122. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  123. h = hash_djb2_one_32(E->get().hash(), h);
  124. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  125. }
  126. return h;
  127. }
  128. Array Dictionary::keys() const {
  129. Array varr;
  130. varr.resize(size());
  131. if (_p->variant_map.empty())
  132. return varr;
  133. int i = 0;
  134. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  135. varr[i] = E.key();
  136. i++;
  137. }
  138. return varr;
  139. }
  140. Array Dictionary::values() const {
  141. Array varr;
  142. varr.resize(size());
  143. if (_p->variant_map.empty())
  144. return varr;
  145. int i = 0;
  146. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  147. varr[i] = E.get();
  148. i++;
  149. }
  150. return varr;
  151. }
  152. const Variant *Dictionary::next(const Variant *p_key) const {
  153. if (p_key == NULL) {
  154. // caller wants to get the first element
  155. if (_p->variant_map.front())
  156. return &_p->variant_map.front().key();
  157. return NULL;
  158. }
  159. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
  160. if (E && E.next())
  161. return &E.next().key();
  162. return NULL;
  163. }
  164. Dictionary Dictionary::duplicate() const {
  165. Dictionary n;
  166. List<Variant> keys;
  167. get_key_list(&keys);
  168. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  169. n[E->get()] = operator[](E->get());
  170. }
  171. return n;
  172. }
  173. void Dictionary::operator=(const Dictionary &p_dictionary) {
  174. _ref(p_dictionary);
  175. }
  176. Dictionary::Dictionary(const Dictionary &p_from) {
  177. _p = NULL;
  178. _ref(p_from);
  179. }
  180. Dictionary::Dictionary() {
  181. _p = memnew(DictionaryPrivate);
  182. _p->refcount.init();
  183. }
  184. Dictionary::~Dictionary() {
  185. _unref();
  186. }