Skip to content

Latest commit

 

History

History
251 lines (158 loc) · 5.94 KB

priority_queue.genericpriorityqueue-2.md

File metadata and controls

251 lines (158 loc) · 5.94 KB

GenericPriorityQueue<TItem, TPriority>

Namespace: Priority_Queue

A copy of StablePriorityQueue which also has generic priority-type

public sealed class GenericPriorityQueue<TItem, TPriority> : IFixedSizePriorityQueue`2, IPriorityQueue`2, , System.Collections.IEnumerable

Type Parameters

TItem
The values in the queue. Must extend the GenericPriorityQueueNode class

TPriority
The priority-type. Must extend IComparable

Inheritance ObjectGenericPriorityQueue<TItem, TPriority>
Implements IFixedSizePriorityQueue<TItem, TPriority>, IPriorityQueue<TItem, TPriority>, IEnumerable<TItem>, IEnumerable

Properties

Count

Returns the number of nodes in the queue. O(1)

public int Count { get; }

Property Value

Int32

MaxSize

Returns the maximum number of items that can be enqueued at once in this queue. Once you hit this number (ie. once Count == MaxSize), attempting to enqueue another item will cause undefined behavior. O(1)

public int MaxSize { get; }

Property Value

Int32

First

Returns the head of the queue, without removing it (use Dequeue() for that). If the queue is empty, behavior is undefined. O(1)

public TItem First { get; }

Property Value

TItem

Constructors

GenericPriorityQueue(Int32)

Instantiate a new Priority Queue

public GenericPriorityQueue(int maxNodes)

Parameters

maxNodes Int32
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)

GenericPriorityQueue(Int32, IComparer<TPriority>)

Instantiate a new Priority Queue

public GenericPriorityQueue(int maxNodes, IComparer<TPriority> comparer)

Parameters

maxNodes Int32
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)

comparer IComparer<TPriority>
The comparer used to compare TPriority values.

GenericPriorityQueue(Int32, Comparison<TPriority>)

Instantiate a new Priority Queue

public GenericPriorityQueue(int maxNodes, Comparison<TPriority> comparer)

Parameters

maxNodes Int32
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)

comparer Comparison<TPriority>
The comparison function to use to compare TPriority values

Methods

Clear()

Removes every node from the queue. O(n) (So, don't do this often!)

public void Clear()

Contains(TItem)

Returns (in O(1)!) whether the given node is in the queue. If node is or has been previously added to another queue, the result is undefined unless oldQueue.ResetNode(node) has been called O(1)

public bool Contains(TItem node)

Parameters

node TItem

Returns

Boolean

Enqueue(TItem, TPriority)

Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken by first-in-first-out. If the queue is full, the result is undefined. If the node is already enqueued, the result is undefined. If node is or has been previously added to another queue, the result is undefined unless oldQueue.ResetNode(node) has been called O(log n)

public void Enqueue(TItem node, TPriority priority)

Parameters

node TItem

priority TPriority

Dequeue()

Removes the head of the queue (node with minimum priority; ties are broken by order of insertion), and returns it. If queue is empty, result is undefined O(log n)

public TItem Dequeue()

Returns

TItem

Resize(Int32)

Resize the queue so it can accept more nodes. All currently enqueued nodes are remain. Attempting to decrease the queue size to a size too small to hold the existing nodes results in undefined behavior O(n)

public void Resize(int maxNodes)

Parameters

maxNodes Int32

UpdatePriority(TItem, TPriority)

This method must be called on a node every time its priority changes while it is in the queue. Forgetting to call this method will result in a corrupted queue! Calling this method on a node not in the queue results in undefined behavior O(log n)

public void UpdatePriority(TItem node, TPriority priority)

Parameters

node TItem

priority TPriority

Remove(TItem)

Removes a node from the queue. The node does not need to be the head of the queue. If the node is not in the queue, the result is undefined. If unsure, check Contains() first O(log n)

public void Remove(TItem node)

Parameters

node TItem

ResetNode(TItem)

By default, nodes that have been previously added to one queue cannot be added to another queue. If you need to do this, please call originalQueue.ResetNode(node) before attempting to add it in the new queue

public void ResetNode(TItem node)

Parameters

node TItem

GetEnumerator()

public IEnumerator<TItem> GetEnumerator()

Returns

IEnumerator<TItem>

IsValidQueue()

Should not be called in production code. Checks to make sure the queue is still in a valid state. Used for testing/debugging the queue.

public bool IsValidQueue()

Returns

Boolean