PriorityQueue
public struct PriorityQueue<T>
In a priority queue each element is associated with a priority
,
elements are dequeued in highest-priority-first order (the
elements with the highest priority are dequeued first).
The enqueue
and dequeue
operations run in O(log(n)) time.
Conforms to Sequence
, CustomStringConvertible
.
-
Constructs an empty priority queue using a closure to determine the order of a provided pair of elements. The closure that you supply for
sortedBy
should return a boolean value to indicate whether one element should be before (true
) or after (false
) another element using strict weak ordering. See http://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderingsDeclaration
Swift
public init(sortedBy sortFunction: @escaping (T,T) -> Bool)
Parameters
sortedBy
Strict weak ordering function checking if the first element has higher priority.
-
Constructs a priority queue from a sequence, such as an array, using a given closure to determine the order of a provided pair of elements. The closure that you supply for
sortedBy
should return a boolean value to indicate whether one element should be before (true
) or after (false
) another element using strict weak ordering. See http://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderingsDeclaration
Swift
public init<S: Sequence>(_ elements: S, sortedBy sortFunction: @escaping (T,T) -> Bool) where S.Iterator.Element == T
Parameters
sortedBy
Strict weak ordering function for checking if the first element has higher priority.
-
Number of elements stored in the priority queue.
Declaration
Swift
public var count: Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
The highest priority element in the queue, or
nil
if the queue is empty.Declaration
Swift
public var first: T?
-
Inserts an element into the priority queue.
Declaration
Swift
public mutating func enqueue(_ element: T)
-
Retrieves and removes the highest priority element of the queue.
Declaration
Swift
public mutating func dequeue() -> T
Return Value
The highest priority element, or
nil
if the queue is empty. -
Removes all the elements from the priority queue, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepingCapacity keep: Bool = false)
-
Provides for-in loop functionality. Generates elements in no particular order.
Declaration
Swift
public func makeIterator() -> AnyIterator<T>
Return Value
A generator over the elements.
-
A string containing a suitable textual representation of the priority queue.
Declaration
Swift
public var description: String