diff --git a/core/dictionary.cpp b/core/dictionary.cpp index 382129e4e7c..599224da14c 100644 --- a/core/dictionary.cpp +++ b/core/dictionary.cpp @@ -39,40 +39,14 @@ struct _DictionaryVariantHash { struct DictionaryPrivate { - struct Data { - Variant variant; - int order; - }; - SafeRefCount refcount; - HashMap variant_map; - int counter; + HashMap variant_map; bool shared; }; -struct DictionaryPrivateSort { - - bool operator()(const HashMap::Pair *A, const HashMap::Pair *B) const { - - return A->data.order < B->data.order; - } -}; - void Dictionary::get_key_list(List *p_keys) const { - if (_p->variant_map.empty()) - return; - - int count = _p->variant_map.size(); - const HashMap::Pair **pairs = (const HashMap::Pair **)alloca(count * sizeof(HashMap::Pair *)); - _p->variant_map.get_key_value_ptr_array(pairs); - - SortArray::Pair *, DictionaryPrivateSort> sort; - sort.sort(pairs, count); - - for (int i = 0; i < count; i++) { - p_keys->push_back(pairs[i]->key); - } + _p->variant_map.get_key_list(p_keys); } void Dictionary::_copy_on_write() const { @@ -93,47 +67,29 @@ Variant &Dictionary::operator[](const Variant &p_key) { _copy_on_write(); - DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key); - - if (!v) { - - DictionaryPrivate::Data d; - d.order = _p->counter++; - _p->variant_map[p_key] = d; - v = _p->variant_map.getptr(p_key); - } - return v->variant; + return _p->variant_map[p_key]; } const Variant &Dictionary::operator[](const Variant &p_key) const { - return _p->variant_map[p_key].variant; + return _p->variant_map[p_key]; } const Variant *Dictionary::getptr(const Variant &p_key) const { - const DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key); - if (!v) - return NULL; - else - return &v->variant; + return _p->variant_map.getptr(p_key); } Variant *Dictionary::getptr(const Variant &p_key) { _copy_on_write(); - DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key); - if (!v) - return NULL; - else - return &v->variant; + return _p->variant_map.getptr(p_key); } Variant Dictionary::get_valid(const Variant &p_key) const { - DictionaryPrivate::Data *v = _p->variant_map.getptr(p_key); + const Variant *v = getptr(p_key); if (!v) return Variant(); - else - return v->variant; + return *v; } int Dictionary::size() const { @@ -189,7 +145,6 @@ void Dictionary::clear() { _copy_on_write(); _p->variant_map.clear(); - _p->counter = 0; } bool Dictionary::is_shared() const { @@ -237,20 +192,11 @@ Array Dictionary::values() const { Array varr; varr.resize(size()); - if (_p->variant_map.empty()) - return varr; - - int count = _p->variant_map.size(); - const HashMap::Pair **pairs = (const HashMap::Pair **)alloca(count * sizeof(HashMap::Pair *)); - _p->variant_map.get_key_value_ptr_array(pairs); - - SortArray::Pair *, DictionaryPrivateSort> sort; - sort.sort(pairs, count); - - for (int i = 0; i < count; i++) { - varr[i] = pairs[i]->data.variant; + const Variant *key = NULL; + int i = 0; + while ((key = next(key))) { + varr[i++] = _p->variant_map[*key]; } - return varr; } @@ -293,7 +239,6 @@ Dictionary::Dictionary(bool p_shared) { _p = memnew(DictionaryPrivate); _p->refcount.init(); - _p->counter = 0; _p->shared = p_shared; } Dictionary::~Dictionary() { diff --git a/core/hash_map.h b/core/hash_map.h index 1096269787e..1bbe6c1877f 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -570,20 +570,6 @@ public: hash_table_power = 0; } - void get_key_value_ptr_array(const Pair **p_pairs) const { - if (!hash_table) - return; - for (int i = 0; i < (1 << hash_table_power); i++) { - - Entry *e = hash_table[i]; - while (e) { - *p_pairs = &e->pair; - p_pairs++; - e = e->next; - } - } - } - void get_key_list(List *p_keys) const { if (!hash_table) return;