mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[PersistentSet] Add removeAll(where:)
This commit is contained in:
1 parent
80195a8374
commit
a7411c59bc
2 files changed
+39
No files matched your search
@@ -28,4 +28,28 @@ extension PersistentSet {
|
||||
guard let result = result else { return self }
|
||||
return PersistentSet(_new: result.finalize(.top))
|
||||
}
|
||||
|
||||
/// Removes all the elements that satisfy the given predicate.
|
||||
///
|
||||
/// Use this method to remove every element in the set that meets
|
||||
/// particular criteria.
|
||||
/// This example removes all the odd values from an
|
||||
/// set of numbers:
|
||||
///
|
||||
/// var numbers: PersistentSet = [5, 6, 7, 8, 9, 10, 11]
|
||||
/// numbers.removeAll(where: { $0 % 2 != 0 })
|
||||
/// // numbers == [6, 8, 10], in some order
|
||||
///
|
||||
/// - Parameter shouldBeRemoved: A closure that takes an element of the
|
||||
/// set as its argument and returns a Boolean value indicating
|
||||
/// whether the element should be removed from the collection.
|
||||
///
|
||||
/// - Complexity: O(`count`)
|
||||
@inlinable
|
||||
public mutating func removeAll(
|
||||
where shouldBeRemoved: (Element) throws -> Bool
|
||||
) rethrows {
|
||||
// FIXME: Implement in-place reductions
|
||||
self = try filter { try !shouldBeRemoved($0) }
|
||||
}
|
||||
}
|
||||
@@ -509,6 +509,21 @@ class PersistentSetTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_removeAll_where_exhaustive() {
|
||||
withEvery("isShared", in: [false, true]) { isShared in
|
||||
withEverySubset("a", of: testItems) { a in
|
||||
withEverySubset("b", of: a) { b in
|
||||
var x = PersistentSet(a)
|
||||
let v = Set(b)
|
||||
withHiddenCopies(if: isShared, of: &x) { x in
|
||||
x.removeAll { !v.contains($0) }
|
||||
expectEqualSets(x, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_union_exhaustive() {
|
||||
withEverySubset("a", of: testItems) { a in
|
||||
let x = PersistentSet(a)
|
||||
|
||||
Reference in new issue
Block a user