Add Heap.removeAll(where:)

This commit is contained in:
Dakshin Devanand authored and Karoy Lorentey committed 2025-05-16 00:25:42 -07:00
1 parent 60746986d8
commit 027b92153b
2 files changed
+58

No files matched your search

+20
View File
@@ -268,6 +268,26 @@ extension Heap {
_checkInvariants()
return removed
}
/// Removes all the elements that satisfy the given predicate.
///
/// - Parameter shouldBeRemoved: A closure that takes an element of the
/// heap as its argument and returns a Boolean value indicating
/// whether the element should be removed from the heap.
///
/// - Complexity: O(*n*), where *n* is the number of items in the heap.
@inlinable
public mutating func removeAll(
where shouldBeRemoved: (Element) throws -> Bool
) rethrows {
defer { _checkInvariants() }
try _storage.removeAll(where: shouldBeRemoved)
if _storage.count > 1 {
_update { handle in
handle.heapify()
}
}
}
/// Replaces the maximum value in the heap with the given replacement,
/// then updates heap contents to reflect the change.