Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Heap] Final(?) Heap API adjustments for 1.1 #354

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Sources/HeapModule/Heap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ extension Heap {
Array(_storage)
}

/// Creates an empty heap with preallocated space for at least the
/// specified number of elements.
///
/// Use this initializer to avoid intermediate reallocations of a heap's
/// storage when you know in advance how many elements you'll insert into it
/// after creation.
///
/// - Parameter minimumCapacity: The minimum number of elements that the newly
/// created heap should be able to store without reallocating its storage.
///
/// - Complexity: O(1) allocations
@inlinable
public init(minimumCapacity: Int) {
self.init()
self.reserveCapacity(minimumCapacity)
}

/// Reserves enough space to store the specified number of elements.
///
/// If you are adding a known number of elements to a heap, use this method
/// to avoid multiple reallocations. This method ensures that the heap has
/// unique, mutable, contiguous storage, with space allocated for at least
/// the requested number of elements.
///
/// For performance reasons, the size of the newly allocated storage might be
/// greater than the requested capacity.
///
/// - Parameter minimumCapacity: The minimum number of elements that the
/// resulting heap should be able to store without reallocating its storage.
///
/// - Complexity: O(`count`)
@inlinable
public mutating func reserveCapacity(_ minimumCapacity: Int) {
_storage.reserveCapacity(minimumCapacity)
}

/// Inserts the given element into the heap.
///
/// - Complexity: O(log(`count`)) element comparisons
Expand Down Expand Up @@ -192,6 +228,7 @@ extension Heap {
///
/// - Complexity: O(log(`count`)) element comparisons
@inlinable
@discardableResult
public mutating func removeMin() -> Element {
return popMin()!
}
Expand All @@ -202,6 +239,7 @@ extension Heap {
///
/// - Complexity: O(log(`count`)) element comparisons
@inlinable
@discardableResult
public mutating func removeMax() -> Element {
return popMax()!
}
Expand Down