diff --git a/Sources/PriorityQueueModule/Heap.swift b/Sources/PriorityQueueModule/Heap.swift index e22338f8..fea6d0b4 100644 --- a/Sources/PriorityQueueModule/Heap.swift +++ b/Sources/PriorityQueueModule/Heap.swift @@ -29,22 +29,20 @@ public struct Heap { internal var _storage: ContiguousArray /// Creates a heap from the given sequence, assuming said sequence is already - /// an implicit data structure for a binary min-max heap, failing otherwise. + /// an implicit data structure for a binary min-max heap. /// - /// - Precondition: `storage` is finite. + /// - Precondition: `storage` is finite, and it already matches the implicit + /// data structure for a binary min-max heap. /// /// - Parameter storage: The elements of the heap. - /// - Postcondition: If the initializer doesn't fail: - /// `unordered.elementsEqual(s)`, where *s* is a sequence with the same - /// elements as pre-call `storage`. + /// - Postcondition: `unordered.elementsEqual(s)`, where *s* is a sequence + /// with the same elements as pre-call `storage`. /// /// - Complexity: O(*n*), where *n* is the length of `storage`. @_alwaysEmitIntoClient - internal init?(_raw storage: S) where S.Element == Element { + internal init(_raw storage: S) where S.Element == Element { _storage = .init(storage) - if _someHeapViolation() != nil { - return nil - } + _checkInvariants() } /// Creates an empty heap. diff --git a/Tests/PriorityQueueTests/HeapTests.swift b/Tests/PriorityQueueTests/HeapTests.swift index b44ca359..0e2aff90 100644 --- a/Tests/PriorityQueueTests/HeapTests.swift +++ b/Tests/PriorityQueueTests/HeapTests.swift @@ -326,14 +326,14 @@ final class HeapTests: XCTestCase { // Check the finer details. As these peek into the stored structure, they // may need to be updated whenever the internal format changes. - var heap2 = Heap(_raw: [1])! + var heap2 = Heap(_raw: [1]) XCTAssertEqual(heap2.max(), 1) XCTAssertEqual(Array(heap2.unordered), [1]) XCTAssertEqual(heap2.replaceMax(with: 2), 1) XCTAssertEqual(heap2.max(), 2) XCTAssertEqual(Array(heap2.unordered), [2]) - heap2 = Heap(_raw: [1, 2])! + heap2 = Heap(_raw: [1, 2]) XCTAssertEqual(heap2.max(), 2) XCTAssertEqual(Array(heap2.unordered), [1, 2]) XCTAssertEqual(heap2.replaceMax(with: 3), 2) @@ -343,7 +343,7 @@ final class HeapTests: XCTestCase { XCTAssertEqual(heap2.max(), 1) XCTAssertEqual(Array(heap2.unordered), [0, 1]) - heap2 = Heap(_raw: [5, 20, 31, 16, 8, 7, 18])! + heap2 = Heap(_raw: [5, 20, 31, 16, 8, 7, 18]) XCTAssertEqual(heap2.max(), 31) XCTAssertEqual(Array(heap2.unordered), [5, 20, 31, 16, 8, 7, 18]) XCTAssertEqual(heap2.replaceMax(with: 29), 31)