Browse Source

sortbox: improve performance

pull/276/head
Nikolas Rimikis 2 years ago
parent
commit
f9435b3f01
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 2
      packages/sort_box/.gitignore
  2. 66
      packages/sort_box/lib/sort_box.dart

2
packages/sort_box/.gitignore vendored

@ -28,3 +28,5 @@ migrate_working_dir/
.dart_tool/ .dart_tool/
.packages .packages
build/ build/
coverage/

66
packages/sort_box/lib/sort_box.dart

@ -1,68 +1,44 @@
// ignore_for_file: public_member_api_docs // ignore_for_file: public_member_api_docs
typedef ComparableGetter<T> = Comparable Function(T);
class SortBox<T extends Enum, R> { class SortBox<T extends Enum, R> {
SortBox( SortBox(
this._properties, this._properties,
this._secondaryBoxes, this._secondaryBoxes,
); );
final Map<T, Comparable Function(R)> _properties; final Map<T, ComparableGetter<R>> _properties;
final Map<T, Box<T>> _secondaryBoxes; final Map<T, Box<T>> _secondaryBoxes;
List<R> sort( List<R> sort(final List<R> input, final Box<T> box) {
final List<R> input,
final Box<T> box, {
final bool preventSecondarySort = false,
}) {
if (input.length <= 1) { if (input.length <= 1) {
return input; return input;
} }
final counters = <int, List<R>>{};
final comparableGetter = _properties[box.property]!; final comparableGetter = _properties[box.property]!;
for (final item1 in input) {
var counter = 0;
for (final item2 in input) {
if (item2 != item1) {
final comparable1 = comparableGetter(item1);
final comparable2 = comparableGetter(item2);
counter += box.order == SortBoxOrder.descending
? comparable1.compareTo(comparable2)
: comparable2.compareTo(comparable1);
}
}
if (counters[counter] == null) {
counters[counter] = [];
}
counters[counter]!.add(item1);
}
final secondaryBox = _secondaryBoxes[box.property]; final secondaryBox = _secondaryBoxes[box.property];
final comparableGetter2 = _properties[secondaryBox?.property];
final counterValues = counters.keys.toList()..sort((final a, final b) => b.compareTo(a)); return input
if (counterValues.length == 1 && counterValues.first == 0) { ..sort(
if (secondaryBox != null && !preventSecondarySort) { (final item1, final item2) {
return sort( final first = _compare(item1, item2, box.order, comparableGetter);
counters[0]!,
secondaryBox,
preventSecondarySort: true,
);
}
return counters[0]!; if (first == 0 && secondaryBox != null) {
} return _compare(item1, item2, secondaryBox.order, comparableGetter2!);
}
final output = <R>[]; return first;
for (final counterValue in counterValues) { },
var values = counters[counterValue]!; );
if (secondaryBox != null) { }
values = sort(values, secondaryBox);
} int _compare(final R item1, final R item2, final SortBoxOrder order, final ComparableGetter<R> getter) {
output.addAll(values); final comparable1 = getter(item1);
} final comparable2 = getter(item2);
return output; return order == SortBoxOrder.ascending ? comparable1.compareTo(comparable2) : comparable2.compareTo(comparable1);
} }
} }

Loading…
Cancel
Save