Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Array/Dictionary::operator== to do real key/value comparison #35816

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/templates/ordered_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ class OrderedHashMap {
(*list_element)->get().second = p_value;
return Element(*list_element);
}
typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(nullptr, p_value));
// Incorrectly set the first value of the pair with a value that will
// be invalid as soon as we leave this function...
typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(&p_key, p_value));
// ...this is needed here in case the hashmap recursively reference itself...
typename InternalMap::Element *e = map.set(p_key, new_element);
// ...now we can set the right value !
new_element->get().first = &e->key();

return Element(new_element);
Expand Down
3 changes: 3 additions & 0 deletions core/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
template <size_t... Is>
struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};

// Limit the depth of recursive algorithms when dealing with Array/Dictionary
#define MAX_RECURSION 100

#ifdef DEBUG_ENABLED
#define DEBUG_METHODS_ENABLED
#endif
Expand Down
66 changes: 60 additions & 6 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,38 @@ void Array::clear() {
}

bool Array::operator==(const Array &p_array) const {
return _p == p_array._p;
return recursive_equal(p_array, 0);
}

bool Array::operator!=(const Array &p_array) const {
return !operator==(p_array);
return !recursive_equal(p_array, 0);
}

bool Array::recursive_equal(const Array &p_array, int recursion_count) const {
// Cheap checks
if (_p == p_array._p) {
return true;
}
const Vector<Variant> &a1 = _p->array;
const Vector<Variant> &a2 = p_array._p->array;
const int size = a1.size();
if (size != a2.size()) {
return false;
}

// Heavy O(n) check
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return true;
}
recursion_count++;
for (int i = 0; i < size; i++) {
if (!a1[i].hash_compare(a2[i], recursion_count)) {
return false;
}
}

return true;
}

bool Array::operator<(const Array &p_array) const {
Expand Down Expand Up @@ -132,10 +159,20 @@ bool Array::operator>=(const Array &p_array) const {
}

uint32_t Array::hash() const {
uint32_t h = hash_djb2_one_32(0);
return recursive_hash(0);
}

uint32_t Array::recursive_hash(int recursion_count) const {
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return 0;
}

uint32_t h = hash_djb2_one_32(Variant::ARRAY);

recursion_count++;
for (int i = 0; i < _p->array.size(); i++) {
h = hash_djb2_one_32(_p->array[i].hash(), h);
h = hash_djb2_one_32(_p->array[i].recursive_hash(recursion_count), h);
}
return h;
}
Expand Down Expand Up @@ -300,12 +337,29 @@ const Variant &Array::get(int p_idx) const {
}

Array Array::duplicate(bool p_deep) const {
return recursive_duplicate(p_deep, 0);
}

Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
Array new_arr;

if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return new_arr;
}

int element_count = size();
new_arr.resize(element_count);
new_arr._p->typed = _p->typed;
for (int i = 0; i < element_count; i++) {
new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
if (p_deep) {
recursion_count++;
for (int i = 0; i < element_count; i++) {
new_arr[i] = get(i).recursive_duplicate(true, recursion_count);
}
} else {
for (int i = 0; i < element_count; i++) {
new_arr[i] = get(i);
}
}

return new_arr;
Expand Down
3 changes: 3 additions & 0 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ class Array {

bool operator==(const Array &p_array) const;
bool operator!=(const Array &p_array) const;
bool recursive_equal(const Array &p_array, int recursion_count) const;

uint32_t hash() const;
uint32_t recursive_hash(int recursion_count) const;
void operator=(const Array &p_array);

void push_back(const Variant &p_value);
Expand Down Expand Up @@ -100,6 +102,7 @@ class Array {
Variant pop_at(int p_pos);

Array duplicate(bool p_deep = false) const;
Array recursive_duplicate(bool p_deep, int recursion_count) const;

Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const;
Array filter(const Callable &p_callable) const;
Expand Down
62 changes: 56 additions & 6 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,35 @@ bool Dictionary::erase(const Variant &p_key) {
}

bool Dictionary::operator==(const Dictionary &p_dictionary) const {
return _p == p_dictionary._p;
return recursive_equal(p_dictionary, 0);
}

bool Dictionary::operator!=(const Dictionary &p_dictionary) const {
return _p != p_dictionary._p;
return !recursive_equal(p_dictionary, 0);
}

bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_count) const {
// Cheap checks
if (_p == p_dictionary._p) {
return true;
}
if (_p->variant_map.size() != p_dictionary._p->variant_map.size()) {
return false;
}

// Heavy O(n) check
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return true;
}
recursion_count++;
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement this_E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&_p->variant_map)->front(); this_E; this_E = this_E.next()) {
OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::ConstElement other_E = ((const OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator> *)&p_dictionary._p->variant_map)->find(this_E.key());
if (!other_E || !this_E.value().hash_compare(other_E.value(), recursion_count)) {
return false;
}
}
return true;
}

void Dictionary::_ref(const Dictionary &p_from) const {
Expand Down Expand Up @@ -225,11 +249,21 @@ void Dictionary::_unref() const {
}

uint32_t Dictionary::hash() const {
return recursive_hash(0);
}

uint32_t Dictionary::recursive_hash(int recursion_count) const {
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return 0;
}

uint32_t h = hash_djb2_one_32(Variant::DICTIONARY);

recursion_count++;
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
h = hash_djb2_one_32(E.key().hash(), h);
h = hash_djb2_one_32(E.value().hash(), h);
h = hash_djb2_one_32(E.key().recursive_hash(recursion_count), h);
h = hash_djb2_one_32(E.value().recursive_hash(recursion_count), h);
}

return h;
Expand Down Expand Up @@ -286,10 +320,26 @@ const Variant *Dictionary::next(const Variant *p_key) const {
}

Dictionary Dictionary::duplicate(bool p_deep) const {
return recursive_duplicate(p_deep, 0);
}

Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
Dictionary n;

for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
return n;
}

if (p_deep) {
recursion_count++;
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
n[E.key().recursive_duplicate(true, recursion_count)] = E.value().recursive_duplicate(true, recursion_count);
}
} else {
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
n[E.key()] = E.value();
}
}

return n;
Expand Down
3 changes: 3 additions & 0 deletions core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ class Dictionary {

bool operator==(const Dictionary &p_dictionary) const;
bool operator!=(const Dictionary &p_dictionary) const;
bool recursive_equal(const Dictionary &p_dictionary, int recursion_count) const;

uint32_t hash() const;
uint32_t recursive_hash(int recursion_count) const;
void operator=(const Dictionary &p_dictionary);

const Variant *next(const Variant *p_key = nullptr) const;
Expand All @@ -80,6 +82,7 @@ class Dictionary {
Array values() const;

Dictionary duplicate(bool p_deep = false) const;
Dictionary recursive_duplicate(bool p_deep, int recursion_count) const;

const void *id() const;

Expand Down
Loading