From 460fd7c03b6d1506cca0d8623d9995c19b9197e1 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 4 Apr 2022 18:46:55 +0200 Subject: [PATCH] Add Dictionary.merge() (cherry picked from commit a0915e6dee4e54563a98ca6adb8968dbdcea1af1) --- core/dictionary.cpp | 8 ++++++++ core/dictionary.h | 1 + core/variant_call.cpp | 2 ++ doc/classes/Dictionary.xml | 7 +++++++ 4 files changed, 18 insertions(+) diff --git a/core/dictionary.cpp b/core/dictionary.cpp index 1402980b0ea..158d127de4f 100644 --- a/core/dictionary.cpp +++ b/core/dictionary.cpp @@ -197,6 +197,14 @@ void Dictionary::clear() { _p->variant_map.clear(); } +void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) { + for (OrderedHashMap::Element E = p_dictionary._p->variant_map.front(); E; E = E.next()) { + if (p_overwrite || !has(E.key())) { + this->operator[](E.key()) = E.value(); + } + } +} + void Dictionary::_unref() const { ERR_FAIL_COND(!_p); if (_p->refcount.unref()) { diff --git a/core/dictionary.h b/core/dictionary.h index c1c6541ac78..d25b81d1c24 100644 --- a/core/dictionary.h +++ b/core/dictionary.h @@ -62,6 +62,7 @@ public: int size() const; bool empty() const; void clear(); + void merge(const Dictionary &p_dictionary, bool p_overwrite = false); bool has(const Variant &p_key) const; bool has_all(const Array &p_keys) const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 3d53d9db752..7caa27cc68e 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -549,6 +549,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Dictionary, size); VCALL_LOCALMEM0R(Dictionary, empty); VCALL_LOCALMEM0(Dictionary, clear); + VCALL_LOCALMEM2(Dictionary, merge); VCALL_LOCALMEM1R(Dictionary, has); VCALL_LOCALMEM1R(Dictionary, has_all); VCALL_LOCALMEM1R(Dictionary, erase); @@ -1919,6 +1920,7 @@ void register_variant_methods() { ADDFUNC0R(DICTIONARY, INT, Dictionary, size, varray()); ADDFUNC0R(DICTIONARY, BOOL, Dictionary, empty, varray()); ADDFUNC0NC(DICTIONARY, NIL, Dictionary, clear, varray()); + ADDFUNC2NC(DICTIONARY, NIL, Dictionary, merge, DICTIONARY, "dictionary", BOOL, "overwrite", varray(false)); ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has, NIL, "key", varray()); ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has_all, ARRAY, "keys", varray()); ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray()); diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index 673bcaec76a..c7250871847 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -164,6 +164,13 @@ Returns the list of keys in the [Dictionary]. + + + + + Adds elements from [code]dictionary[/code] to this [Dictionary]. By default, duplicate keys will not be copied over, unless [code]overwrite[/code] is [code]true[/code]. + +