diff --git a/Sources/PriorityQueueModule/Heap.swift b/Sources/PriorityQueueModule/Heap.swift index 1f6f3d83..16b10897 100644 --- a/Sources/PriorityQueueModule/Heap.swift +++ b/Sources/PriorityQueueModule/Heap.swift @@ -28,6 +28,42 @@ public struct Heap { @usableFromInline internal var _storage: ContiguousArray + /// Creates a heap from the given sequence, whether said sequence conforms to + /// an implicit data structure for a min-max heap or not. + /// + /// - Warning: This initializer is meant only as a shared implementation for + /// the public initializers. Those initializers need to either ensure the + /// input is already valid, condition the input to be valid afterwards, or + /// flag an error if the input is invalid. No other code should call this + /// initializer. + /// + /// - Precondition: `s` is finite. + /// + /// - Complexity: O(*n*), where *n* is the length of `s`. + @inlinable @inline(__always) + internal init(unchecked s: S) where S.Element == Element { + _storage = .init(s) + } + + /// Creates a heap from the given sequence, assuming said sequence is already + /// an implicit data structure for a binary min-max heap, failing otherwise. + /// + /// - Precondition: `storage` is finite. + /// + /// - 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`. + /// + /// - Complexity: O(*n*), where *n* is the length of `storage`. + @inlinable + public init?(raw storage: S) where S.Element == Element { + self.init(unchecked: storage) + if _someHeapViolation() != nil { + return nil + } + } + /// Creates an empty heap. @inlinable public init() { diff --git a/Tests/PriorityQueueTests/HeapTests.swift b/Tests/PriorityQueueTests/HeapTests.swift index ca326858..72d4df3a 100644 --- a/Tests/PriorityQueueTests/HeapTests.swift +++ b/Tests/PriorityQueueTests/HeapTests.swift @@ -393,6 +393,33 @@ final class HeapTests: XCTestCase { XCTAssertEqual(heap.count, 20) } + func test_initializer_withChecking() { + XCTAssertNotNil(Heap(raw: EmptyCollection())) + XCTAssertNotNil(Heap(raw: CollectionOfOne("a"))) + XCTAssertNotNil(Heap(raw: repeatElement(2.0, count: 15))) + + // As these test non-identical multi-element sources, they may need to be + // updated whenever the internal storage format changes. + + let twoElements = Heap(raw: [1, 2]) + XCTAssertEqual(twoElements?.unordered, [1, 2]) + XCTAssertNil(Heap(raw: [2, 1])) + + let threeElements = Heap(raw: 1...3) + XCTAssertEqual(threeElements?.unordered, [1, 2, 3]) + + let otherThreeElements = Heap(raw: [1, 3, 2]) + XCTAssertEqual(otherThreeElements?.unordered, [1, 3, 2]) + XCTAssertNil(Heap(raw: [2, 1, 3])) + XCTAssertNil(Heap(raw: [2, 3, 1])) + XCTAssertNil(Heap(raw: [3, 1, 2])) + XCTAssertNil(Heap(raw: [3, 2, 1])) + + // Errors past the first two layers. + XCTAssertNil(Heap(raw: [1, 2, 3, 0, 2, 3, 2])) // 0 >= 1 is false + XCTAssertNil(Heap(raw: [1, 2, 3, 1, 4, 3, 1])) // 4 <= 2 is false + } + func test_initializer_fromArrayLiteral() { var heap: Heap = [1, 3, 5, 7, 9] XCTAssertEqual(heap.count, 5)