Skip to content

Commit 974448b

Browse files
committed
Final backing sets
1 parent 43d1801 commit 974448b

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

lib/comparing_ordered_set.dart

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class ComparingOrderedSet<E> extends OrderedSet<E>
1616
// If the default implementation of `Set` changes from `LinkedHashSet` to
1717
// something else that isn't ordered we'll have to change this to explicitly
1818
// be `LinkedHashSet` (or some other data structure that preserves order).
19-
late SplayTreeSet<Set<E>> _backingSet;
20-
late int _length;
19+
late final SplayTreeSet<Set<E>> _backingSet = SplayTreeSet<LinkedHashSet<E>>(
20+
_outerComparator,
21+
);
22+
final int Function(E e1, E e2) _comparator;
23+
int _length = 0;
2124

2225
bool _validReverseCache = true;
2326
Iterable<E> _reverseCache = const Iterable.empty();
@@ -45,22 +48,7 @@ class ComparingOrderedSet<E> extends OrderedSet<E>
4548
ComparingOrderedSet({
4649
int Function(E e1, E e2)? compare,
4750
this.strictMode = true,
48-
}) {
49-
final comparator = compare ?? _defaultCompare<E>();
50-
_backingSet = SplayTreeSet<LinkedHashSet<E>>((Set<E> l1, Set<E> l2) {
51-
if (l1.isEmpty) {
52-
if (l2.isEmpty) {
53-
return 0;
54-
}
55-
return -1;
56-
}
57-
if (l2.isEmpty) {
58-
return 1;
59-
}
60-
return comparator(l1.first, l2.first);
61-
});
62-
_length = 0;
63-
}
51+
}) : _comparator = compare ?? _defaultCompare<E>();
6452

6553
@override
6654
int get length => _length;
@@ -143,4 +131,17 @@ class ComparingOrderedSet<E> extends OrderedSet<E>
143131
_length = 0;
144132
onClear();
145133
}
134+
135+
int _outerComparator(Set<E> l1, Set<E> l2) {
136+
if (l1.isEmpty) {
137+
if (l2.isEmpty) {
138+
return 0;
139+
}
140+
return -1;
141+
}
142+
if (l2.isEmpty) {
143+
return 1;
144+
}
145+
return _comparator(l1.first, l2.first);
146+
}
146147
}

lib/mapping_ordered_set.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ import 'package:ordered_set/queryable_ordered_set_impl.dart';
1414
class MappingOrderedSet<K extends Comparable<K>, E> extends OrderedSet<E>
1515
with QueryableOrderedSetImpl<E> {
1616
final K Function(E a) _mappingFunction;
17-
late SplayTreeMap<K, Set<E>> _backingSet;
18-
late int _length;
17+
final SplayTreeMap<K, Set<E>> _backingSet;
18+
int _length = 0;
1919

2020
bool _validReverseCache = true;
2121
Iterable<E> _reverseCache = const Iterable.empty();
2222

2323
@override
2424
final bool strictMode;
2525

26-
MappingOrderedSet(this._mappingFunction, {this.strictMode = true}) {
27-
_backingSet = SplayTreeMap((K k1, K k2) {
28-
return k1.compareTo(k2);
29-
});
30-
_length = 0;
31-
}
26+
MappingOrderedSet(
27+
this._mappingFunction, {
28+
this.strictMode = true,
29+
}) : _backingSet = SplayTreeMap((K k1, K k2) => k1.compareTo(k2));
3230

3331
@override
3432
int get length => _length;

0 commit comments

Comments
 (0)