dictionary.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /* 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 "io/json.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. struct Data {
  39. Variant variant;
  40. int order;
  41. };
  42. SafeRefCount refcount;
  43. HashMap<Variant, Data, _DictionaryVariantHash> variant_map;
  44. int counter;
  45. bool shared;
  46. };
  47. struct DictionaryPrivateSort {
  48. bool operator()(const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *A, const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *B) const {
  49. return A->data.order < B->data.order;
  50. }
  51. };
  52. void Dictionary::get_key_list(List<Variant> *p_keys) const {
  53. if (_p->variant_map.empty())
  54. return;
  55. int count = _p->variant_map.size();
  56. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  57. _p->variant_map.get_key_value_ptr_array(pairs);
  58. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  59. sort.sort(pairs, count);
  60. for (int i = 0; i < count; i++) {
  61. p_keys->push_back(pairs[i]->key);
  62. }
  63. }
  64. void Dictionary::_copy_on_write() const {
  65. //make a copy of what we have
  66. if (_p->shared)
  67. return;
  68. DictionaryPrivate *p = memnew(DictionaryPrivate);
  69. p->shared = _p->shared;
  70. p->variant_map = _p->variant_map;
  71. p->refcount.init();
  72. _unref();
  73. _p = p;
  74. }
  75. Variant &Dictionary::operator[](const Variant &p_key) {
  76. _copy_on_write();
  77. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  78. if (!v) {
  79. DictionaryPrivate::Data d;
  80. d.order = _p->counter++;
  81. _p->variant_map[p_key] = d;
  82. v = _p->variant_map.getptr(p_key);
  83. }
  84. return v->variant;
  85. }
  86. const Variant &Dictionary::operator[](const Variant &p_key) const {
  87. return _p->variant_map[p_key].variant;
  88. }
  89. const Variant *Dictionary::getptr(const Variant &p_key) const {
  90. const DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  91. if (!v)
  92. return NULL;
  93. else
  94. return &v->variant;
  95. }
  96. Variant *Dictionary::getptr(const Variant &p_key) {
  97. _copy_on_write();
  98. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  99. if (!v)
  100. return NULL;
  101. else
  102. return &v->variant;
  103. }
  104. Variant Dictionary::get_valid(const Variant &p_key) const {
  105. DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key);
  106. if (!v)
  107. return Variant();
  108. else
  109. return v->variant;
  110. }
  111. int Dictionary::size() const {
  112. return _p->variant_map.size();
  113. }
  114. bool Dictionary::empty() const {
  115. return !_p->variant_map.size();
  116. }
  117. bool Dictionary::has(const Variant &p_key) const {
  118. return _p->variant_map.has(p_key);
  119. }
  120. bool Dictionary::has_all(const Array &p_keys) const {
  121. for (int i = 0; i < p_keys.size(); i++) {
  122. if (!has(p_keys[i])) {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. void Dictionary::erase(const Variant &p_key) {
  129. _copy_on_write();
  130. _p->variant_map.erase(p_key);
  131. }
  132. bool Dictionary::operator==(const Dictionary &p_dictionary) const {
  133. return _p == p_dictionary._p;
  134. }
  135. void Dictionary::_ref(const Dictionary &p_from) const {
  136. //make a copy first (thread safe)
  137. if (!p_from._p->refcount.ref())
  138. return; // couldn't copy
  139. //if this is the same, unreference the other one
  140. if (p_from._p == _p) {
  141. _p->refcount.unref();
  142. return;
  143. }
  144. if (_p)
  145. _unref();
  146. _p = p_from._p;
  147. }
  148. void Dictionary::clear() {
  149. _copy_on_write();
  150. _p->variant_map.clear();
  151. _p->counter = 0;
  152. }
  153. bool Dictionary::is_shared() const {
  154. return _p->shared;
  155. }
  156. void Dictionary::_unref() const {
  157. ERR_FAIL_COND(!_p);
  158. if (_p->refcount.unref()) {
  159. memdelete(_p);
  160. }
  161. _p = NULL;
  162. }
  163. uint32_t Dictionary::hash() const {
  164. uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);
  165. List<Variant> keys;
  166. get_key_list(&keys);
  167. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  168. h = hash_djb2_one_32(E->get().hash(), h);
  169. h = hash_djb2_one_32(operator[](E->get()).hash(), h);
  170. }
  171. return h;
  172. }
  173. Array Dictionary::keys() const {
  174. Array karr;
  175. karr.resize(size());
  176. const Variant *K = NULL;
  177. int idx = 0;
  178. while ((K = next(K))) {
  179. karr[idx++] = (*K);
  180. }
  181. return karr;
  182. }
  183. Array Dictionary::values() const {
  184. Array varr;
  185. varr.resize(size());
  186. if (_p->variant_map.empty())
  187. return varr;
  188. int count = _p->variant_map.size();
  189. const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **pairs = (const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair **)alloca(count * sizeof(HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *));
  190. _p->variant_map.get_key_value_ptr_array(pairs);
  191. SortArray<const HashMap<Variant, DictionaryPrivate::Data, _DictionaryVariantHash>::Pair *, DictionaryPrivateSort> sort;
  192. sort.sort(pairs, count);
  193. for (int i = 0; i < count; i++) {
  194. varr[i] = pairs[i]->data.variant;
  195. }
  196. return varr;
  197. }
  198. const Variant *Dictionary::next(const Variant *p_key) const {
  199. return _p->variant_map.next(p_key);
  200. }
  201. Error Dictionary::parse_json(const String &p_json) {
  202. String errstr;
  203. int errline = 0;
  204. if (p_json != "") {
  205. Error err = JSON::parse(p_json, *this, errstr, errline);
  206. if (err != OK) {
  207. ERR_EXPLAIN("Error parsing JSON: " + errstr + " at line: " + itos(errline));
  208. ERR_FAIL_COND_V(err != OK, err);
  209. }
  210. }
  211. return OK;
  212. }
  213. String Dictionary::to_json() const {
  214. return JSON::print(*this);
  215. }
  216. void Dictionary::operator=(const Dictionary &p_dictionary) {
  217. _ref(p_dictionary);
  218. }
  219. Dictionary::Dictionary(const Dictionary &p_from) {
  220. _p = NULL;
  221. _ref(p_from);
  222. }
  223. Dictionary::Dictionary(bool p_shared) {
  224. _p = memnew(DictionaryPrivate);
  225. _p->refcount.init();
  226. _p->counter = 0;
  227. _p->shared = p_shared;
  228. }
  229. Dictionary::~Dictionary() {
  230. _unref();
  231. }