Priority Queue
An implementation of a min-Priority Queue using a heap. Has O(1) .Contains()!
See https://github.com/BlueRaja/High-Speed-Priority-Queue-for-C-Sharp/wiki/Getting-Started for more information
The values in the queue. Must extend the FastPriorityQueueNode class
Instantiate a new Priority Queue
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)
Returns the number of nodes in the queue.
O(1)
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)
Removes every node from the queue.
O(n) (So, don't do this often!)
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)
Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken arbitrarily.
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)
Returns true if 'higher' has higher priority than 'lower', false otherwise.
Note that calling HasHigherPriority(node, node) (ie. both arguments the same node) will return false
Returns true if 'higher' has higher priority than 'lower', false otherwise.
Note that calling HasHigherOrEqualPriority(node, node) (ie. both arguments the same node) will return true
Removes the head of the queue and returns it.
If queue is empty, result is undefined
O(log n)
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)
Returns the head of the queue, without removing it (use Dequeue() for that).
If the queue is empty, behavior is undefined.
O(1)
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)
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)
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
If the node is currently in the queue or belongs to another queue, the result is undefined
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.
The Priority to insert this node at. Must be set BEFORE adding a node to the queue (ideally just once, in the node's constructor).
Should not be manually edited once the node has been enqueued - use queue.UpdatePriority() instead
Represents the current position in the queue
A copy of StablePriorityQueue which also has generic priority-type
The values in the queue. Must extend the GenericPriorityQueueNode class
The priority-type. Must extend IComparable<TPriority>
Instantiate a new Priority Queue
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)
Instantiate a new Priority Queue
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)
The comparer used to compare TPriority values.
Instantiate a new Priority Queue
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)
The comparison function to use to compare TPriority values
Returns the number of nodes in the queue.
O(1)
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)
Removes every node from the queue.
O(n) (So, don't do this often!)
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)
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)
Returns true if 'higher' has higher priority than 'lower', false otherwise.
Note that calling HasHigherPriority(node, node) (ie. both arguments the same node) will return false
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)
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)
Returns the head of the queue, without removing it (use Dequeue() for that).
If the queue is empty, behavior is undefined.
O(1)
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)
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)
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
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.
The Priority to insert this node at. Must be set BEFORE adding a node to the queue (ideally just once, in the node's constructor).
Should not be manually edited once the node has been enqueued - use queue.UpdatePriority() instead
Represents the current position in the queue
Represents the order the node was inserted in
A helper-interface only needed to make writing unit tests a bit easier (hence the 'internal' access modifier)
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
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.
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
The IPriorityQueue interface. This is mainly here for purists, and in case I decide to add more implementations later.
For speed purposes, it is actually recommended that you *don't* access the priority queue through this interface, since the JIT can
(theoretically?) optimize method calls from concrete-types slightly better.
Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken by first-in-first-out.
See implementation for how duplicates are handled.
Removes the head of the queue (node with minimum priority; ties are broken by order of insertion), and returns it.
Removes every node from the queue.
Returns whether the given node is in the queue.
Removes a node from the queue. The node does not need to be the head of the queue.
Call this method to change the priority of a node.
Returns the head of the queue, without removing it (use Dequeue() for that).
Returns the number of nodes in the queue.
A simplified priority queue implementation. Is stable, auto-resizes, and thread-safe, at the cost of being slightly slower than
FastPriorityQueue
Methods tagged as O(1) or O(log n) are assuming there are no duplicates. Duplicates may increase the algorithmic complexity.
The type to enqueue
The priority-type to use for nodes. Must extend IComparable<TPriority>
Instantiate a new Priority Queue
Instantiate a new Priority Queue
The comparer used to compare TPriority values. Defaults to Comparer<TPriority>.default
Instantiate a new Priority Queue
The comparison function to use to compare TPriority values
Instantiate a new Priority Queue
The equality comparison function to use to compare TItem values
Instantiate a new Priority Queue
The comparer used to compare TPriority values. Defaults to Comparer<TPriority>.default
The equality comparison function to use to compare TItem values
Instantiate a new Priority Queue
The comparison function to use to compare TPriority values
The equality comparison function to use to compare TItem values
Given an item of type T, returns the existing SimpleNode in the queue
Adds an item to the Node-cache to allow for many methods to be O(1) or O(log n)
Removes an item to the Node-cache to allow for many methods to be O(1) or O(log n) (assuming no duplicates)
Returns the number of nodes in the queue.
O(1)
Returns the head of the queue, without removing it (use Dequeue() for that).
Throws an exception when the queue is empty.
O(1)
Removes every node from the queue.
O(n)
Returns whether the given item is in the queue.
O(1)
Removes the head of the queue (node with minimum priority; ties are broken by order of insertion), and returns it.
If queue is empty, throws an exception
O(log n)
Enqueue the item with the given priority, without calling lock(_queue) or AddToNodeCache(node)
Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken by first-in-first-out.
This queue automatically resizes itself, so there's no concern of the queue becoming 'full'.
Duplicates and null-values are allowed.
O(log n)
Enqueue a node to the priority queue if it doesn't already exist. Lower values are placed in front. Ties are broken by first-in-first-out.
This queue automatically resizes itself, so there's no concern of the queue becoming 'full'. Null values are allowed.
Returns true if the node was successfully enqueued; false if it already exists.
O(log n)
Removes an item from the queue. The item does not need to be the head of the queue.
If the item is not in the queue, an exception is thrown. If unsure, check Contains() first.
If multiple copies of the item are enqueued, only the first one is removed.
O(log n)
Call this method to change the priority of an item.
Calling this method on a item not in the queue will throw an exception.
If the item is enqueued multiple times, only the first one will be updated.
(If your requirements are complex enough that you need to enqueue the same item multiple times and be able
to update all of them, please wrap your items in a wrapper class so they can be distinguished).
O(log n)
Returns the priority of the given item.
Calling this method on a item not in the queue will throw an exception.
If the item is enqueued multiple times, only the priority of the first will be returned.
(If your requirements are complex enough that you need to enqueue the same item multiple times and be able
to query all their priorities, please wrap your items in a wrapper class so they can be distinguished).
O(1)
Get the head of the queue, without removing it (use TryDequeue() for that).
Useful for multi-threading, where the queue may become empty between calls to Contains() and First
Returns true if successful, false otherwise
O(1)
Removes the head of the queue (node with minimum priority; ties are broken by order of insertion), and sets it to first.
Useful for multi-threading, where the queue may become empty between calls to Contains() and Dequeue()
Returns true if successful; false if queue was empty
O(log n)
Attempts to remove an item from the queue. The item does not need to be the head of the queue.
Useful for multi-threading, where the queue may become empty between calls to Contains() and Remove()
Returns true if the item was successfully removed, false if it wasn't in the queue.
If multiple copies of the item are enqueued, only the first one is removed.
O(log n)
Call this method to change the priority of an item.
Useful for multi-threading, where the queue may become empty between calls to Contains() and UpdatePriority()
If the item is enqueued multiple times, only the first one will be updated.
(If your requirements are complex enough that you need to enqueue the same item multiple times and be able
to update all of them, please wrap your items in a wrapper class so they can be distinguished).
Returns true if the item priority was updated, false otherwise.
O(log n)
Attempt to get the priority of the given item.
Useful for multi-threading, where the queue may become empty between calls to Contains() and GetPriority()
If the item is enqueued multiple times, only the priority of the first will be returned.
(If your requirements are complex enough that you need to enqueue the same item multiple times and be able
to query all their priorities, please wrap your items in a wrapper class so they can be distinguished).
Returns true if the item was found in the queue, false otherwise
O(1)
A simplified priority queue implementation. Is stable, auto-resizes, and thread-safe, at the cost of being slightly slower than
FastPriorityQueue
This class is kept here for backwards compatibility. It's recommended you use SimplePriorityQueue<TItem, TPriority>
The type to enqueue
Instantiate a new Priority Queue
Instantiate a new Priority Queue
The comparer used to compare priority values. Defaults to Comparer<float>.default
Instantiate a new Priority Queue
The comparison function to use to compare priority values
A copy of FastPriorityQueue which is also stable - that is, when two nodes are enqueued with the same priority, they
are always dequeued in the same order.
See https://github.com/BlueRaja/High-Speed-Priority-Queue-for-C-Sharp/wiki/Getting-Started for more information
The values in the queue. Must extend the StablePriorityQueueNode class
Instantiate a new Priority Queue
The max nodes ever allowed to be enqueued (going over this will cause undefined behavior)
Returns the number of nodes in the queue.
O(1)
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)
Removes every node from the queue.
O(n) (So, don't do this often!)
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)
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)
Returns true if 'higher' has higher priority than 'lower', false otherwise.
Note that calling HasHigherPriority(node, node) (ie. both arguments the same node) will return false
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)
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)
Returns the head of the queue, without removing it (use Dequeue() for that).
If the queue is empty, behavior is undefined.
O(1)
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)
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)
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
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.
Represents the order the node was inserted in