Class PriorityQueue<T>
PriorityQueue is a collection that allows you to retrieve the item with the lowest priority in constant time and be able update priority of an item with log complexity. Items are unique within the collection but priorities can have duplicate values.
Inheritance
Inherited Members
Namespace: Elements
Assembly: Hypar.Elements.dll
Syntax
public class PriorityQueue<T>
where T : IComparable<T>
Type Parameters
Name | Description |
---|---|
T | Type of the item. Must support hash and comparing. |
Constructors
PriorityQueue()
Creates an empty collection.
Declaration
public PriorityQueue()
PriorityQueue(IEnumerable<T>)
Creates collection from input list. First item in the list is set to priority 0, others with double.MaxValue.
Declaration
public PriorityQueue(IEnumerable<T> uniqueCollection)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | uniqueCollection | List of ids. |
Methods
AddOrUpdate(T, Double)
Adds a new item to the collection. If an item with id already exist in collection - it's priority will be updated.
Declaration
public void AddOrUpdate(T id, double priority)
Parameters
Type | Name | Description |
---|---|---|
T | id | Id of the item. |
System.Double | priority | New priority. |
Contains(T)
Checks if certain Id is in the queue.
Declaration
public bool Contains(T id)
Parameters
Type | Name | Description |
---|---|---|
T | id |
Returns
Type | Description |
---|---|
System.Boolean |
Empty()
Is collection empty.
Declaration
public bool Empty()
Returns
Type | Description |
---|---|
System.Boolean |
PopMin()
Returns the lowest priority item from collection. Throws an exception if called on an empty collection.
Declaration
public T PopMin()
Returns
Type | Description |
---|---|
T | Id of the item with lowest priority. |
UpdatePriority(T, Double)
Sets priority for the item with given id. Does nothing if item is not present in the queue.
Declaration
public void UpdatePriority(T id, double priority)
Parameters
Type | Name | Description |
---|---|---|
T | id | Id of the item. |
System.Double | priority | New priority. |