dictionary.cpp 7.4 KB

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