PriorityHeap
public struct PriorityHeap<T>: PriorityQueue
An implementation of Priority Queues as a simple heap structure.
-
The comparision function.
Should return true if lhs has higher priority than rhs.
Declaration
Swift
public let compare: (T, T) -> Bool
-
Initialize an empty heap with a given comparison function.
Declaration
Swift
public init(compare: (T, T) -> Bool)
Parameters
compare
A comparison function over pairs of elements.
-
Initialize a heap with a given array of items and a comparison function.
Declaration
Swift
public init(items: [T], compare: (T, T) -> Bool)
Parameters
items
An array of elements to create the queue from.
compare
A comparison function over pairs of elements.
-
View the highest-priority element in the queue, without extracting.
Declaration
Swift
public func peek() -> T?
Return Value
The highest-priority element, or nil if queue is empty.
-
Insert a new element into the queue.
Declaration
Swift
public mutating func insert(item: T)
Parameters
item
the element to insert.
-
Extract the highest-priority element from the queue.
This removes the element from the queue.
Declaration
Swift
public mutating func extract() -> T?
Return Value
The highest-priority element, or nil if the queue is empty.