mirror of
https://github.com/godotengine/godot.git
synced 2025-12-07 22:00:10 +00:00
Reuse and optimize sorting logic for List, SelfList, and HashMap
Added SortList class, and updated List, SelfList, and HashMap sort methods to use it. Sorting is done with merge sort, with an initial check to optimize for already sorted lists, and sorted lists that were appended to.
This commit is contained in:
parent
1b37dacc18
commit
6b2674fe18
10 changed files with 309 additions and 190 deletions
|
|
@ -304,9 +304,21 @@ void Dictionary::clear() {
|
|||
_p->variant_map.clear();
|
||||
}
|
||||
|
||||
struct _DictionaryVariantSort {
|
||||
_FORCE_INLINE_ bool operator()(const KeyValue<Variant, Variant> &p_l, const KeyValue<Variant, Variant> &p_r) const {
|
||||
bool valid = false;
|
||||
Variant res;
|
||||
Variant::evaluate(Variant::OP_LESS, p_l.key, p_r.key, res, valid);
|
||||
if (!valid) {
|
||||
res = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
void Dictionary::sort() {
|
||||
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
|
||||
_p->variant_map.sort();
|
||||
_p->variant_map.sort_custom<_DictionaryVariantSort>();
|
||||
}
|
||||
|
||||
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue