PriorityQueue
Represents a min priority queue.
Implements: ICollection<ValueTuple<T, T>>, IEnumerable<ValueTuple<T, T>>, IEnumerable, IQueue<ValueTuple<T, T>>
Constructors
public PriorityQueue(Func<TPriority, TPriority, TPriority> subtractAbs, IComparer<TPriority> comparer)
priorityQueue = PriorityQueue(subtractAbs, comparer)
Represents a min priority queue.
- subtractAbs
- The function that calculates the absolute difference between two priorities.
- comparer
- Custom comparer dictating the ordering of elements. Uses Default if the argument is .
public PriorityQueue(Func<TPriority, TPriority, TPriority> subtractAbs)
priorityQueue = PriorityQueue(subtractAbs)
Initializes a new instance of the PriorityQueue class.
Properties
public IComparer<TPriority> Comparer { get; }
value = priorityQueue.Comparer
Gets the priority comparer used by the PriorityQueue.
public int Count { get; }
value = priorityQueue.Count
Gets the number of elements contained in the PriorityQueue.
Methods
public ValueTuple<TPriority, TElement> Dequeue()
result = priorityQueue.Dequeue()
Removes and returns the minimal element from the PriorityQueue.
Returns: The minimal element of the PriorityQueue.
public TElement DequeueEnqueue(TPriority priority, TElement element)
result = priorityQueue.DequeueEnqueue(priority, element)
Removes the minimal element and then immediately adds the specified element with associated priority to the PriorityQueue,
- priority
- The priority with which to associate the new element.
- element
- The element to add to the PriorityQueue.
Returns: The minimal element removed before performing the enqueue operation.
public void Enqueue(TPriority priority, TElement element)
priorityQueue.Enqueue(priority, element)
Adds the specified element with associated priority to the PriorityQueue.
- priority
- The priority with which to associate the new element.
- element
- The element to add to the PriorityQueue.
public TElement EnqueueDequeue(TPriority priority, TElement element)
result = priorityQueue.EnqueueDequeue(priority, element)
Adds the specified element with associated priority to the PriorityQueue, and immediately removes the minimal element, returning the result.
- priority
- The priority with which to associate the new element.
- element
- The element to add to the PriorityQueue.
Returns: The minimal element removed after the enqueue operation.
public void EnqueueRange(IEnumerable<ValueTuple<TPriority, TElement>> items)
priorityQueue.EnqueueRange(items)
Enqueues a sequence of element/priority pairs to the PriorityQueue.
- items
- The pairs of elements and priorities to add to the queue.
public void EnqueueRange(TPriority priority, IEnumerable<TElement> elements)
priorityQueue.EnqueueRange(priority, elements)
Enqueues a sequence of elements pairs to the PriorityQueue, all associated with the specified priority.
- elements
- The elements to add to the queue.
- priority
- The priority to associate with the new elements.
public ValueTuple<TPriority, TElement> Peek()
result = priorityQueue.Peek()
Returns the minimal element from the PriorityQueue without removing it.
Returns: The minimal element of the PriorityQueue.
public bool TryDequeue(TElement element, TPriority priority)
result = priorityQueue.TryDequeue(element, priority)
Removes the minimal element from the PriorityQueue, and copies it to the parameter, and its associated priority to the parameter.
- element
- The removed element.
- priority
- The priority associated with the removed element.
Returns: if the element is successfully removed; if the PriorityQueue is empty.
public bool TryPeek(TElement element, TPriority priority)
result = priorityQueue.TryPeek(element, priority)
Returns a value that indicates whether there is a minimal element in the PriorityQueue, and if one is present, copies it to the parameter, and its associated priority to the parameter. The element is not removed from the PriorityQueue.
- element
- The minimal element in the queue.
- priority
- The priority associated with the minimal element.
Returns: if there is a minimal element; if the PriorityQueue is empty.
Fields
private int _version
value = priorityQueue._version
Version updated on mutation to help validate enumerators operate on a consistent state.