4 using System.Collections.Generic;
6 namespace SiliconStudio.Core.Collections
24 public class PriorityNodeQueue<T>
27 private List<PriorityQueueNode<T>> items =
new List<PriorityQueueNode<T>>();
34 this.comparer = comparer;
39 this.comparer = Comparer<T>.Default;
54 int index = item.Index;
65 int nMax = items.Count - 1;
66 var itemMax = items[nMax];
67 itemMax.Index = index;
68 var itemToRemove = items[index];
69 itemToRemove.Index = -1;
70 items[index] = itemMax;
78 int c = p * 2;
if (c >= nMax)
break;
82 if (c + 1 < nMax && comparer.Compare(items[c + 1].Value, items[c].Value) < 0) c++;
85 if (comparer.Compare(items[p].Value, items[c].Value) <= 0)
115 if (item.
Index != -1)
116 throw new InvalidOperationException(
"Item belongs to another PriorityNodeQueue.");
134 if (comparer.Compare(items[n].Value, items[p].Value) >= 0)
152 get {
return items.Count; }
160 get {
return items.Count == 0; }
167 return items[0].Value;
181 int nMax = items.Count - 1;
182 var itemToRemove = items[0];
183 var itemMax = items[nMax];
185 var val = itemToRemove.Value;
186 itemToRemove.Index = -1;
188 items.RemoveAt(nMax);
195 int c = p * 2;
if (c >= nMax)
break;
199 if (c + 1 < nMax && comparer.Compare(items[c + 1].Value, items[c].Value) < 0) c++;
202 if (comparer.Compare(items[p].Value, items[c].Value) <= 0)
T Dequeue()
Removes and returns the first element from the queue (least element)
void Enqueue(PriorityQueueNode< T > item)
Add an element to the priority queue - O(log(n)) time operation.
void Clear()
Clear all the elements from the priority queue
Represents a node in a priority queue, to allow O(n) removal.
void Remove(PriorityQueueNode< T > item)
Removes the specified item.
PriorityNodeQueue(IComparer< T > comparer)
T Peek()
Allows you to look at the first element waiting in the queue, without removing it.
PriorityQueueNode< T > Enqueue(T item)
Add an element to the priority queue - O(log(n)) time operation.