Skip to content

Commit

Permalink
Merge 3d383f0 into 6736e9a
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo committed Apr 5, 2020
2 parents 6736e9a + 3d383f0 commit 58dd674
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions lib/ordered_set.dart
Expand Up @@ -70,19 +70,21 @@ class OrderedSet<E> extends IterableMixin<E> implements Iterable<E> {
return true;
}

/// Remove all elements that match the [test] condition, returns the amount of element removed.
int removeWhere(bool Function(E element) test) {
final prevLength = _length;
/// Remove all elements that match the [test] condition, returns the removed elements
Iterable<E> removeWhere(bool Function(E element) test) {
List<E> _removed = [];
for (final es in _backingSet.toList()) {
final removed = es.where(test).length;
if (removed == es.length) {
final removed = es.where(test);
_removed.addAll(removed);
_length -= removed.length;

if (removed.length == es.length) {
_backingSet.remove(es);
} else {
es.removeWhere(test);
}
_length -= removed;
}
return prevLength - _length;
return _removed;
}

/// Remove a single element that is equal to [e].
Expand Down
8 changes: 4 additions & 4 deletions test/ordered_set_test.dart
Expand Up @@ -10,15 +10,15 @@ void main() {
OrderedSet<int> a = OrderedSet();
expect(a.addAll([7, 4, 3, 1, 2, 6, 5]), 7);
expect(a.length, 7);
expect(a.removeWhere((e) => e == 3), 1);
expect(a.removeWhere((e) => e == 3).length, 1);
expect(a.length, 6);
expect(a.toList().join(), '124567');
});

test('remove with property', () {
OrderedSet<int> a = OrderedSet();
expect(a.addAll([7, 4, 3, 1, 2, 6, 5]), 7);
expect(a.removeWhere((e) => e % 2 == 1), 4);
expect(a.removeWhere((e) => e % 2 == 1).length, 4);
expect(a.length, 3);
expect(a.toList().join(), '246');
});
Expand All @@ -33,7 +33,7 @@ void main() {
expect(a.add(ComparableObject(1, 'b3')), true);
expect(a.add(ComparableObject(2, 'a4')), true);
expect(a.add(ComparableObject(2, 'b4')), true);
expect(a.removeWhere((e) => e.name.startsWith('a')), 4);
expect(a.removeWhere((e) => e.name.startsWith('a')).length, 4);
expect(a.length, 4);
expect(a.toList().join(), 'b1b2b3b4');
});
Expand All @@ -48,7 +48,7 @@ void main() {
expect(a.add(ComparableObject(1, 'b3')), true);
expect(a.add(ComparableObject(2, 'a4')), true);
expect(a.add(ComparableObject(2, 'b4')), true);
expect(a.removeWhere((e) => true), 8);
expect(a.removeWhere((e) => true).length, 8);
expect(a.length, 0);
expect(a.toList().join(), '');
});
Expand Down

0 comments on commit 58dd674

Please sign in to comment.