From fdbef1c07428cd6840eb34eaa624ce735b404657 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 5 Sep 2022 15:58:22 +0200 Subject: [PATCH] Add Dictionary.find_key() --- core/dictionary.cpp | 9 +++++++++ core/dictionary.h | 1 + core/variant_call.cpp | 2 ++ doc/classes/Dictionary.xml | 10 +++++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/dictionary.cpp b/core/dictionary.cpp index 158d127de4f..675558515a6 100644 --- a/core/dictionary.cpp +++ b/core/dictionary.cpp @@ -136,6 +136,15 @@ bool Dictionary::has_all(const Array &p_keys) const { return true; } +Variant Dictionary::find_key(const Variant &p_value) const { + for (OrderedHashMap::Element E = _p->variant_map.front(); E; E = E.next()) { + if (E.value() == p_value) { + return E.key(); + } + } + return Variant(); +} + bool Dictionary::erase(const Variant &p_key) { return _p->variant_map.erase(p_key); } diff --git a/core/dictionary.h b/core/dictionary.h index d25b81d1c24..a42cf8fa4c8 100644 --- a/core/dictionary.h +++ b/core/dictionary.h @@ -66,6 +66,7 @@ public: bool has(const Variant &p_key) const; bool has_all(const Array &p_keys) const; + Variant find_key(const Variant &p_value) const; bool erase(const Variant &p_key); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index e948bd986ed..e57e6bde059 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -552,6 +552,7 @@ struct _VariantCall { VCALL_LOCALMEM2(Dictionary, merge); VCALL_LOCALMEM1R(Dictionary, has); VCALL_LOCALMEM1R(Dictionary, has_all); + VCALL_LOCALMEM1R(Dictionary, find_key); VCALL_LOCALMEM1R(Dictionary, erase); VCALL_LOCALMEM0R(Dictionary, hash); VCALL_LOCALMEM0R(Dictionary, keys); @@ -1937,6 +1938,7 @@ void register_variant_methods() { 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()); + ADDFUNC1R(DICTIONARY, NIL, Dictionary, find_key, NIL, "value", varray()); ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray()); ADDFUNC0R(DICTIONARY, INT, Dictionary, hash, varray()); ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, keys, varray()); diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index 7633750c6f5..6bc8f9b9f22 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -4,7 +4,7 @@ Dictionary type. - Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements, even though this may not be reflected when printing the dictionary. In other programming languages, this data structure is sometimes referred to as a hash map or associative array. + Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements. In other programming languages, this data structure is sometimes referred to as a hash map or associative array. You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs in curly braces [code]{}[/code]. Erasing elements while iterating over them [b]is not supported[/b] and will result in undefined behavior. [b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate]. @@ -115,6 +115,14 @@ [b]Note:[/b] Don't erase elements while iterating over the dictionary. You can iterate over the [method keys] array instead. + + + + + Returns the first key whose associated value is equal to [code]value[/code], or [code]null[/code] if no such value is found. + [b]Node:[/b] [code]null[/code] is also a valid key. I you have it in your [Dictionary], the [method find_key] method can give misleading results. + +