[Heap] removeAll(where:): Fix exception safety

This commit is contained in:
Karoy Lorentey committed 2025-05-15 21:39:55 -07:00
1 parent 333760bd9c
commit dbc2866bb5
1 file changed
+7 -5
+7 -5
View File
@@ -280,13 +280,15 @@ extension Heap {
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()
defer {
if _storage.count > 1 {
_update { handle in
handle.heapify()
}
}
_checkInvariants()
}
try _storage.removeAll(where: shouldBeRemoved)
}
/// Replaces the maximum value in the heap with the given replacement,