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:
aaronp64 2025-04-18 22:10:26 -04:00
parent 1b37dacc18
commit 6b2674fe18
10 changed files with 309 additions and 190 deletions

View file

@ -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) {