Skip to content

Commit

Permalink
Merge pull request godotengine#22 from ramatakinc/backport/dictionary…
Browse files Browse the repository at this point in the history
…_find_key

backported Dictionary::find_key from 3.x
  • Loading branch information
hpvb authored Sep 5, 2023
2 parents 821ab12 + fc4c477 commit c979c1c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions core/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Dictionary {

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);

Expand Down
2 changes: 2 additions & 0 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1930,6 +1931,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());
Expand Down
8 changes: 8 additions & 0 deletions doc/classes/Dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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]Note:[/b] [code]null[/code] is also a valid key. If 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" />
Expand Down

0 comments on commit c979c1c

Please sign in to comment.