Merge pull request #65366 from KoBeWi/3333333_33333_33_3_3333333333
[3.x] Add Dictionary.find_key()
This commit is contained in:
commit
d8e590195a
4 changed files with 21 additions and 1 deletions
|
@ -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<Variant, Variant, VariantHasher, VariantComparator>::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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
Dictionary type.
|
||||
</brief_description>
|
||||
<description>
|
||||
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.
|
||||
</description>
|
||||
</method>
|
||||
<method name="find_key">
|
||||
<return type="Variant" />
|
||||
<argument index="0" name="value" type="Variant" />
|
||||
<description>
|
||||
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.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get">
|
||||
<return type="Variant" />
|
||||
<argument index="0" name="key" type="Variant" />
|
||||
|
|
Loading…
Reference in a new issue