Skip to main content

GenericPriorityQueue<TItem, TPriority>

A class in Alta.Timing.Runtime, Alta.Timing.PriorityQueue.

Fixed size binary heap with any comparable priority. Ties go to whichever node was enqueued first, so equal priorities come out in order.

Constructors​

MemberWhat it is
GenericPriorityQueue(int maxNodes)Creates a GenericPriorityQueue from maxNodes and comparer.
GenericPriorityQueue(int maxNodes, IComparer<TPriority> comparer)Creates a GenericPriorityQueue from maxNodes and comparer.
GenericPriorityQueue(int maxNodes, Comparison<TPriority> comparer)Creates a GenericPriorityQueue from maxNodes and comparer.

Fields​

4 private and internal fields
MemberWhat it is
private readonly Comparison<TPriority> _comparerComparison used for priorities. Defaults to Comparer.Default.
private TItem[] _nodesHeap array. Slot 0 is unused.
private int _numNodesBacking field for Count.
private long _numNodesEverEnqueuedCounter for insertion order, used to break ties.

Properties​

MemberWhat it is
int Count { get; }Number of nodes in the queue.
TItem First { get; }The first node, without removing it.
int MaxSize { get; }How many nodes the queue can hold.

Methods​

MemberWhat it is
void Clear()Empties the queue without resizing it.
bool Contains(TItem node)True if the node is in this queue, checked through its stored index.
TItem Dequeue()Removes and returns the first node. Does not check for an empty queue.
void Enqueue(TItem node, TPriority priority)Adds a node with a priority and stamps its insertion order. No full or duplicate checks.
IEnumerator<TItem> GetEnumerator() (iterator)Enumerates nodes in heap order, not priority order.
bool IsValidQueue()Debug check that every parent comes before its children.
void Remove(TItem node)Removes a node from anywhere in the queue.
void Resize(int maxNodes)Changes the capacity, dropping nodes beyond the new size.
void UpdatePriority(TItem node, TPriority priority)Changes a queued node's priority and moves it to its new place.
5 private and internal methods
MemberWhat it is
private void CascadeDown(TItem node)Moves a node down the heap until both children come after it.
private void CascadeUp(TItem node)Moves a node up the heap past parents that come after it.
private IEnumerator System.Collections.IEnumerable.GetEnumerator()Enumerates nodes in heap order, not priority order.
private bool HasHigherPriority(TItem higher, TItem lower)True if the first node's priority compares lower, or equal and it was enqueued earlier.
private void OnNodeUpdated(TItem node)Moves a node up or down after its priority changed.

Used in​

Members of other types that take, return or hold a GenericPriorityQueue&lt;TItem, TPriority>: