Selaa lähdekoodia

Add Dictionary.merge()

(cherry picked from commit a0915e6dee4e54563a98ca6adb8968dbdcea1af1)
kobewi 3 vuotta sitten
vanhempi
commit
460fd7c03b
4 muutettua tiedostoa jossa 18 lisäystä ja 0 poistoa
  1. 8 0
      core/dictionary.cpp
  2. 1 0
      core/dictionary.h
  3. 2 0
      core/variant_call.cpp
  4. 7 0
      doc/classes/Dictionary.xml

+ 8 - 0
core/dictionary.cpp

@@ -197,6 +197,14 @@ void Dictionary::clear() {
 	_p->variant_map.clear();
 	_p->variant_map.clear();
 }
 }
 
 
+void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
+	for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::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 {
 void Dictionary::_unref() const {
 	ERR_FAIL_COND(!_p);
 	ERR_FAIL_COND(!_p);
 	if (_p->refcount.unref()) {
 	if (_p->refcount.unref()) {

+ 1 - 0
core/dictionary.h

@@ -62,6 +62,7 @@ public:
 	int size() const;
 	int size() const;
 	bool empty() const;
 	bool empty() const;
 	void clear();
 	void clear();
+	void merge(const Dictionary &p_dictionary, bool p_overwrite = false);
 
 
 	bool has(const Variant &p_key) const;
 	bool has(const Variant &p_key) const;
 	bool has_all(const Array &p_keys) const;
 	bool has_all(const Array &p_keys) const;

+ 2 - 0
core/variant_call.cpp

@@ -549,6 +549,7 @@ struct _VariantCall {
 	VCALL_LOCALMEM0R(Dictionary, size);
 	VCALL_LOCALMEM0R(Dictionary, size);
 	VCALL_LOCALMEM0R(Dictionary, empty);
 	VCALL_LOCALMEM0R(Dictionary, empty);
 	VCALL_LOCALMEM0(Dictionary, clear);
 	VCALL_LOCALMEM0(Dictionary, clear);
+	VCALL_LOCALMEM2(Dictionary, merge);
 	VCALL_LOCALMEM1R(Dictionary, has);
 	VCALL_LOCALMEM1R(Dictionary, has);
 	VCALL_LOCALMEM1R(Dictionary, has_all);
 	VCALL_LOCALMEM1R(Dictionary, has_all);
 	VCALL_LOCALMEM1R(Dictionary, erase);
 	VCALL_LOCALMEM1R(Dictionary, erase);
@@ -1919,6 +1920,7 @@ void register_variant_methods() {
 	ADDFUNC0R(DICTIONARY, INT, Dictionary, size, varray());
 	ADDFUNC0R(DICTIONARY, INT, Dictionary, size, varray());
 	ADDFUNC0R(DICTIONARY, BOOL, Dictionary, empty, varray());
 	ADDFUNC0R(DICTIONARY, BOOL, Dictionary, empty, varray());
 	ADDFUNC0NC(DICTIONARY, NIL, Dictionary, clear, 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, NIL, "key", varray());
 	ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has_all, ARRAY, "keys", varray());
 	ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has_all, ARRAY, "keys", varray());
 	ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray());
 	ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray());

+ 7 - 0
doc/classes/Dictionary.xml

@@ -164,6 +164,13 @@
 				Returns the list of keys in the [Dictionary].
 				Returns the list of keys in the [Dictionary].
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="merge">
+			<argument index="0" name="dictionary" type="Dictionary" />
+			<argument index="1" name="overwrite" type="bool" default="false" />
+			<description>
+				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].
+			</description>
+		</method>
 		<method name="size">
 		<method name="size">
 			<return type="int" />
 			<return type="int" />
 			<description>
 			<description>