Add heap initializer that takes pre-arranged data

This commit is contained in:
Daryle Walker committed 2021-10-10 00:33:54 -04:00
1 parent 35e4873c5c
commit c8e24c399c
2 files changed
+63

No files matched your search

+36
View File
@@ -28,6 +28,42 @@ public struct Heap<Element: Comparable> {
@usableFromInline
internal var _storage: ContiguousArray<Element>
/// 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<S: Sequence>(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?<S: Sequence>(raw storage: S) where S.Element == Element {
self.init(unchecked: storage)
if _someHeapViolation() != nil {
return nil
}
}
/// Creates an empty heap.
@inlinable
public init() {