dictionary.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  148. h = hash_djb2_one_32(E.key().hash(), h);
  149. h = hash_djb2_one_32(E.value().hash(), h);
  150. }
  151. return h;
  152. }
  153. Array Dictionary::keys() const {
  154. Array varr;
  155. if (_p->variant_map.empty())
  156. return varr;
  157. varr.resize(size());
  158. int i = 0;
  159. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  160. varr[i] = E.key();
  161. i++;
  162. }
  163. return varr;
  164. }
  165. Array Dictionary::values() const {
  166. Array varr;
  167. if (_p->variant_map.empty())
  168. return varr;
  169. varr.resize(size());
  170. int i = 0;
  171. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  172. varr[i] = E.get();
  173. i++;
  174. }
  175. return varr;
  176. }
  177. const Variant *Dictionary::next(const Variant *p_key) const {
  178. if (p_key == NULL) {
  179. // caller wants to get the first element
  180. if (_p->variant_map.front())
  181. return &_p->variant_map.front().key();
  182. return NULL;
  183. }
  184. OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.find(*p_key);
  185. if (E && E.next())
  186. return &E.next().key();
  187. return NULL;
  188. }
  189. Dictionary Dictionary::duplicate(bool p_deep) const {
  190. Dictionary n;
  191. for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
  192. n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
  193. }
  194. return n;
  195. }
  196. void Dictionary::operator=(const Dictionary &p_dictionary) {
  197. _ref(p_dictionary);
  198. }
  199. const void *Dictionary::id() const {
  200. return _p->variant_map.id();
  201. }
  202. Dictionary::Dictionary(const Dictionary &p_from) {
  203. _p = NULL;
  204. _ref(p_from);
  205. }
  206. Dictionary::Dictionary() {
  207. _p = memnew(DictionaryPrivate);
  208. _p->refcount.init();
  209. }
  210. Dictionary::~Dictionary() {
  211. _unref();
  212. }