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:
@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user