dictionary.cpp 7.5 KB

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