mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
Change the clear-box initializer to no longer be fail-able
If the format of the incoming data does not match that of a heap, a precondition error is flagged instead.
This commit is contained in:
1 parent
8438c2c408
commit
dec05b96fe
2 files changed
+10
-12
No files matched your search
@@ -29,22 +29,20 @@ public struct Heap<Element: Comparable> {
|
||||
internal var _storage: ContiguousArray<Element>
|
||||
|
||||
/// 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?<S: Sequence>(_raw storage: S) where S.Element == Element {
|
||||
internal init<S: Sequence>(_raw storage: S) where S.Element == Element {
|
||||
_storage = .init(storage)
|
||||
if _someHeapViolation() != nil {
|
||||
return nil
|
||||
}
|
||||
_checkInvariants()
|
||||
}
|
||||
|
||||
/// Creates an empty heap.
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user