dictionary.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 _DictionaryVariantHash {
  35. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  36. };
  37. struct DictionaryPrivate {
  38. SafeRefCount refcount;
  39. OrderedHashMap<Variant, Variant, _DictionaryVariantHash> variant_map;
  40. };
  41. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  42. if (_p->variant_map.empty())
  43. return;
  44. for (OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::Element E = _p->variant_map.front(); E; E = E.next()) {
  45. p_keys->push_back(E.key());
  46. }
  47. }
  48. Variant &Dictionary::operator[](const Variant &p_key) {
  49. return _p->variant_map[p_key];
  50. }
  51. const Variant &Dictionary::operator[](const Variant &p_key) const {
  52. return ((const OrderedHashMap<Variant, Variant, _DictionaryVariantHash> *)&_p->variant_map)->operator[](p_key);
  53. }
  54. const Variant *Dictionary::getptr(const Variant &p_key) const {
  55. OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::ConstElement E = ((const OrderedHashMap<Variant, Variant, _DictionaryVariantHash> *)&_p->variant_map)->find(p_key);
  56. if (!E)
  57. return NULL;
  58. return &E.get();
  59. }
  60. Variant *Dictionary::getptr(const Variant &p_key) {
  61. OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::Element E = _p->variant_map.find(p_key);
  62. if (!E)
  63. return NULL;
  64. return &E.get();
  65. }
  66. Variant Dictionary::get_valid(const Variant &p_key) const {
  67. OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::ConstElement E = ((const OrderedHashMap<Variant, Variant, _DictionaryVariantHash> *)&_p->variant_map)->find(p_key);
  68. if (!E)
  69. return Variant();
  70. return E.get();
  71. }
  72. int Dictionary::size() const {
  73. return _p->variant_map.size();
  74. }
  75. bool Dictionary::empty() const {
  76. return !_p->variant_map.size();
  77. }
  78. bool Dictionary::has(const Variant &p_key) const {
  79. return _p->variant_map.has(p_key);
  80. }
  81. bool Dictionary::has_all(const Array &p_keys) const {
  82. for (int i = 0; i < p_keys.size(); i++) {
  83. if (!has(p_keys[i])) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. void Dictionary::erase(const Variant &p_key) {
  90. _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, _DictionaryVariantHash>::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, _DictionaryVariantHash>::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. return &_p->variant_map.front().key();
  156. }
  157. OrderedHashMap<Variant, Variant, _DictionaryVariantHash>::Element E = _p->variant_map.find(*p_key);
  158. if (E && E.next())
  159. return &E.next().key();
  160. return NULL;
  161. }
  162. Dictionary Dictionary::copy() const {
  163. Dictionary n;
  164. List<Variant> keys;
  165. get_key_list(&keys);
  166. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  167. n[E->get()] = operator[](E->get());
  168. }
  169. return n;
  170. }
  171. void Dictionary::operator=(const Dictionary &p_dictionary) {
  172. _ref(p_dictionary);
  173. }
  174. Dictionary::Dictionary(const Dictionary &p_from) {
  175. _p = NULL;
  176. _ref(p_from);
  177. }
  178. Dictionary::Dictionary() {
  179. _p = memnew(DictionaryPrivate);
  180. _p->refcount.init();
  181. }
  182. Dictionary::~Dictionary() {
  183. _unref();
  184. }