dictionary.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* dictionary.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "dictionary.h"
  30. #include "safe_refcount.h"
  31. #include "variant.h"
  32. struct _DictionaryVariantHash {
  33. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  34. };
  35. struct DictionaryPrivate {
  36. struct Data {
  37. Variant variant;
  38. int order;
  39. };
  40. SafeRefCount refcount;
  41. HashMap<Variant, Data, _DictionaryVariantHash> variant_map;
  42. int counter;
  43. };
  44. struct DictionaryPrivateSort {
  45. bool operator()(const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *A, const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *B) const {
  46. return A->data.order < B->data.order;
  47. }
  48. };
  49. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  50. if (_p->variant_map.empty())
  51. return;
  52. int count = _p->variant_map.size();
  53. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  54. _p->variant_map.get_key_value_ptr_array(pairs);
  55. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  56. sort.sort(pairs, count);
  57. for (int i = 0; i < count; i++) {
  58. p_keys->push_back(pairs[i]->key);
  59. }
  60. }
  61. Variant &Dictionary::operator[](const Variant &p_key) {
  62. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  63. if (!v) {
  64. DictionaryPrivate::Data d;
  65. d.order = _p->counter++;
  66. _p->variant_map[p_key] = d;
  67. v = _p->variant_map.getptr(p_key);
  68. }
  69. return v->variant;
  70. }
  71. const Variant &Dictionary::operator[](const Variant &p_key) const {
  72. return _p->variant_map[p_key].variant;
  73. }
  74. const Variant *Dictionary::getptr(const Variant &p_key) const {
  75. const DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  76. if (!v)
  77. return NULL;
  78. else
  79. return &v->variant;
  80. }
  81. Variant *Dictionary::getptr(const Variant &p_key) {
  82. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  83. if (!v)
  84. return NULL;
  85. else
  86. return &v->variant;
  87. }
  88. Variant Dictionary::get_valid(const Variant &p_key) const {
  89. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  90. if (!v)
  91. return Variant();
  92. else
  93. return v->variant;
  94. }
  95. int Dictionary::size() const {
  96. return _p->variant_map.size();
  97. }
  98. bool Dictionary::empty() const {
  99. return !_p->variant_map.size();
  100. }
  101. bool Dictionary::has(const Variant &p_key) const {
  102. return _p->variant_map.has(p_key);
  103. }
  104. bool Dictionary::has_all(const Array &p_keys) const {
  105. for (int i = 0; i < p_keys.size(); i++) {
  106. if (!has(p_keys[i])) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. void Dictionary::erase(const Variant &p_key) {
  113. _p->variant_map.erase(p_key);
  114. }
  115. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  116. return _p == p_dictionary._p;
  117. }
  118. void Dictionary::_ref(const Dictionary &p_from) const {
  119. //make a copy first (thread safe)
  120. if (!p_from._p->refcount.ref())
  121. return; // couldn't copy
  122. //if this is the same, unreference the other one
  123. if (p_from._p == _p) {
  124. _p->refcount.unref();
  125. return;
  126. }
  127. if (_p)
  128. _unref();
  129. _p = p_from._p;
  130. }
  131. void Dictionary::clear() {
  132. _p->variant_map.clear();
  133. _p->counter = 0;
  134. }
  135. void Dictionary::_unref() const {
  136. ERR_FAIL_COND(!_p);
  137. if (_p->refcount.unref()) {
  138. memdelete(_p);
  139. }
  140. _p = NULL;
  141. }
  142. uint32_t Dictionary::hash() const {
  143. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  144. List<Variant> keys;
  145. get_key_list(&keys);
  146. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  147. h = hash_djb2_one_32(E->get().hash(), h);
  148. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  149. }
  150. return h;
  151. }
  152. Array Dictionary::keys() const {
  153. Array karr;
  154. karr.resize(size());
  155. const Variant *K = NULL;
  156. int idx = 0;
  157. while ((K = next(K))) {
  158. karr[idx++] = (*K);
  159. }
  160. return karr;
  161. }
  162. Array Dictionary::values() const {
  163. Array varr;
  164. varr.resize(size());
  165. if (_p->variant_map.empty())
  166. return varr;
  167. int count = _p->variant_map.size();
  168. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  169. _p->variant_map.get_key_value_ptr_array(pairs);
  170. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  171. sort.sort(pairs, count);
  172. for (int i = 0; i < count; i++) {
  173. varr[i] = pairs[i]->data.variant;
  174. }
  175. return varr;
  176. }
  177. const Variant *Dictionary::next(const Variant *p_key) const {
  178. return _p->variant_map.next(p_key);
  179. }
  180. Dictionary Dictionary::copy() const {
  181. Dictionary n;
  182. List<Variant> keys;
  183. get_key_list(&keys);
  184. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  185. n[E->get()] = operator[](E->get());
  186. }
  187. return n;
  188. }
  189. void Dictionary::operator=(const Dictionary &p_dictionary) {
  190. _ref(p_dictionary);
  191. }
  192. Dictionary::Dictionary(const Dictionary &p_from) {
  193. _p = NULL;
  194. _ref(p_from);
  195. }
  196. Dictionary::Dictionary() {
  197. _p = memnew(DictionaryPrivate);
  198. _p->refcount.init();
  199. _p->counter = 0;
  200. }
  201. Dictionary::~Dictionary() {
  202. _unref();
  203. }