Skip to content

Commit

Permalink
fix addAll with different comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 22, 2024
1 parent 917d1e9 commit 4196b6d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/src/filteredmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class _FilteredMap<K extends Comparable, V> extends _SortedMap<K, V>
@override
FilteredMap<K, V> clone() => _FilteredMap<K, V>._(
filter,
TreeSet(comparator: (a, b) => Comparable.compare(a.index, b.index))
..addAll(_sortedEntries),
TreeSet(comparator: _SortedMap._compare)..addAll(_sortedEntries),
TreeMap.from(_map));

@override
Expand Down
9 changes: 5 additions & 4 deletions lib/src/sortedmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@ class _SortedMap<K extends Comparable, V> extends MapBase<K, V>
final TreeSet<_MapEntryWithIndex<K, V>> _sortedEntries;
final TreeMap<K, V> _map;

static int _compare(_MapEntryWithIndex a, _MapEntryWithIndex b) =>
Comparable.compare(a.index, b.index);

_SortedMap._(this.ordering, TreeSet<_MapEntryWithIndex<K, V>>? sortedPairs,
TreeMap<K, V>? map)
: _sortedEntries = sortedPairs ??
TreeSet(comparator: (a, b) => Comparable.compare(a.index, b.index)),
: _sortedEntries = sortedPairs ?? TreeSet(comparator: _compare),
_map = map ?? TreeMap();

@override
Expand All @@ -179,8 +181,7 @@ class _SortedMap<K extends Comparable, V> extends MapBase<K, V>
@override
SortedMap<K, V> clone() => _SortedMap<K, V>._(
ordering,
TreeSet(comparator: (a, b) => Comparable.compare(a.index, b.index))
..addAll(_sortedEntries),
TreeSet(comparator: _compare)..addAll(_sortedEntries),
TreeMap<K, V>.from(_map));

@override
Expand Down
3 changes: 1 addition & 2 deletions lib/src/treemap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class TreeMap<K extends Comparable, V> extends MapBase<K, V> {

factory TreeMap.from(Map<K, V> other) => TreeMap()..addAll(other);

static int _compareKeys<K extends Comparable, V>(
MapEntry<K, V> a, MapEntry<K, V> b) =>
static int _compareKeys(MapEntry a, MapEntry b) =>
Comparable.compare(a.key, b.key);

final TreeSet<MapEntry<K, V?>> _tree = TreeSet(comparator: _compareKeys);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/treeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class AvlTreeSet<V> extends TreeSet<V> {

@override
bool addAll(Iterable<V> items) {
if (_root == null && items is AvlTreeSet<V>) {
if (_root == null &&
items is AvlTreeSet<V> &&
identical((items as dynamic).comparator, comparator)) {
_root = items._root;
return _root != null;
}
Expand Down

0 comments on commit 4196b6d

Please sign in to comment.