dictionary.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "safe_refcount.h"
  32. #include "variant.h"
  33. struct _DictionaryVariantHash {
  34. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  35. };
  36. struct DictionaryPrivate {
  37. struct Data {
  38. Variant variant;
  39. int order;
  40. };
  41. SafeRefCount refcount;
  42. HashMap<Variant, Data, _DictionaryVariantHash> variant_map;
  43. int counter;
  44. };
  45. struct DictionaryPrivateSort {
  46. bool operator()(const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *A, const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *B) const {
  47. return A->data.order < B->data.order;
  48. }
  49. };
  50. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  51. if (_p->variant_map.empty())
  52. return;
  53. int count = _p->variant_map.size();
  54. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  55. _p->variant_map.get_key_value_ptr_array(pairs);
  56. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  57. sort.sort(pairs, count);
  58. for (int i = 0; i < count; i++) {
  59. p_keys->push_back(pairs[i]->key);
  60. }
  61. }
  62. Variant &Dictionary::operator[](const Variant &p_key) {
  63. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  64. if (!v) {
  65. DictionaryPrivate::Data d;
  66. d.order = _p->counter++;
  67. _p->variant_map[p_key] = d;
  68. v = _p->variant_map.getptr(p_key);
  69. }
  70. return v->variant;
  71. }
  72. const Variant &Dictionary::operator[](const Variant &p_key) const {
  73. return _p->variant_map[p_key].variant;
  74. }
  75. const Variant *Dictionary::getptr(const Variant &p_key) const {
  76. const DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  77. if (!v)
  78. return NULL;
  79. else
  80. return &v->variant;
  81. }
  82. Variant *Dictionary::getptr(const Variant &p_key) {
  83. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  84. if (!v)
  85. return NULL;
  86. else
  87. return &v->variant;
  88. }
  89. Variant Dictionary::get_valid(const Variant &p_key) const {
  90. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  91. if (!v)
  92. return Variant();
  93. else
  94. return v->variant;
  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. void Dictionary::erase(const Variant &p_key) {
  114. _p->variant_map.erase(p_key);
  115. }
  116. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  117. return _p == p_dictionary._p;
  118. }
  119. void Dictionary::_ref(const Dictionary &p_from) const {
  120. //make a copy first (thread safe)
  121. if (!p_from._p->refcount.ref())
  122. return; // couldn't copy
  123. //if this is the same, unreference the other one
  124. if (p_from._p == _p) {
  125. _p->refcount.unref();
  126. return;
  127. }
  128. if (_p)
  129. _unref();
  130. _p = p_from._p;
  131. }
  132. void Dictionary::clear() {
  133. _p->variant_map.clear();
  134. _p->counter = 0;
  135. }
  136. void Dictionary::_unref() const {
  137. ERR_FAIL_COND(!_p);
  138. if (_p->refcount.unref()) {
  139. memdelete(_p);
  140. }
  141. _p = NULL;
  142. }
  143. uint32_t Dictionary::hash() const {
  144. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  145. List<Variant> keys;
  146. get_key_list(&keys);
  147. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  148. h = hash_djb2_one_32(E->get().hash(), h);
  149. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  150. }
  151. return h;
  152. }
  153. Array Dictionary::keys() const {
  154. #if 0
  155. Array karr;
  156. karr.resize(size());
  157. const Variant *K = NULL;
  158. int idx = 0;
  159. while ((K = next(K))) {
  160. karr[idx++] = (*K);
  161. }
  162. return karr;
  163. #else
  164. Array varr;
  165. varr.resize(size());
  166. if (_p->variant_map.empty())
  167. return varr;
  168. int count = _p->variant_map.size();
  169. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  170. _p->variant_map.get_key_value_ptr_array(pairs);
  171. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  172. sort.sort(pairs, count);
  173. for (int i = 0; i < count; i++) {
  174. varr[i] = pairs[i]->key;
  175. }
  176. return varr;
  177. #endif
  178. }
  179. Array Dictionary::values() const {
  180. Array varr;
  181. varr.resize(size());
  182. if (_p->variant_map.empty())
  183. return varr;
  184. int count = _p->variant_map.size();
  185. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  186. _p->variant_map.get_key_value_ptr_array(pairs);
  187. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  188. sort.sort(pairs, count);
  189. for (int i = 0; i < count; i++) {
  190. varr[i] = pairs[i]->data.variant;
  191. }
  192. return varr;
  193. }
  194. const Variant *Dictionary::next(const Variant *p_key) const {
  195. return _p->variant_map.next(p_key);
  196. }
  197. Dictionary Dictionary::copy() const {
  198. Dictionary n;
  199. List<Variant> keys;
  200. get_key_list(&keys);
  201. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  202. n[E->get()] = operator[](E->get());
  203. }
  204. return n;
  205. }
  206. void Dictionary::operator=(const Dictionary &p_dictionary) {
  207. _ref(p_dictionary);
  208. }
  209. Dictionary::Dictionary(const Dictionary &p_from) {
  210. _p = NULL;
  211. _ref(p_from);
  212. }
  213. Dictionary::Dictionary() {
  214. _p = memnew(DictionaryPrivate);
  215. _p->refcount.init();
  216. _p->counter = 0;
  217. }
  218. Dictionary::~Dictionary() {
  219. _unref();
  220. }