A PriorityQueue takes objects to be pushed of any type that implements Comparable. It will pop the objects in the order that they would be sorted. A pop() or a push() can be accomplished in O(lg n) time. It can be specified whether the objects should be popped in ascending or descending order (Max Priority Queue or Min Priority Queue) at the time of initialization.
public struct PriorityQueue<T: Comparable>
Collection
, CustomDebugStringConvertible
, CustomStringConvertible
, IteratorProtocol
, Sequence
public typealias Element = T
public typealias Iterator = PriorityQueue
public typealias Index = Int
public init(ascending: Bool = false, startingValues: [T] = [])
Creates a new PriorityQueue with the given ordering.
public init(order: @escaping (T, T) -> Bool, startingValues: [T] = [])
- order: - order: A function that specifies whether its first argument should come after the second argument in the PriorityQueue.
- startingValues: - startingValues: An array of elements to initialize the PriorityQueue with.
var heap
let ordered: (T, T) -> Bool
How many elements the Priority Queue stores
var count: Int
true if and only if the Priority Queue is empty
var isEmpty: Bool
var startIndex: Int
var endIndex: Int
var description: String
var debugDescription: String
Add a new element onto the Priority Queue. O(lg n)
public mutating func push(_ element: T)
- element: - element: The element to be inserted into the Priority Queue.
Remove and return the element with the highest priority (or lowest if ascending). O(lg n)
public mutating func pop() -> T?
The element with the highest priority in the Priority Queue, or nil if the PriorityQueue is empty.
Removes the first occurence of a particular item. Finds it by value comparison using ==. O(n) Silently exits if no occurrence found.
public mutating func remove(_ item: T)
- item: - item: The item to remove the first occurrence of.
Removes all occurences of a particular item. Finds it by value comparison using ==. O(n) Silently exits if no occurrence found.
public mutating func removeAll(_ item: T)
- item: - item: The item to remove.
Get a look at the current highest priority item, without removing it. O(1)
public func peek() -> T?
The element with the highest priority in the PriorityQueue, or nil if the PriorityQueue is empty.
Eliminate all of the elements from the Priority Queue.
public mutating func clear()
private mutating func sink(_ index: Int)
private mutating func swim(_ index: Int)
mutating public func next() -> Element?
public func makeIterator() -> Iterator
public func index(after i: PriorityQueue.Index) -> PriorityQueue.Index