4 using System.Collections.Generic;
6 namespace SiliconStudio.Core.Collections
12 public class PriorityQueue<T>
17 private List<T> items =
new List<T>();
26 this.comparer = comparer;
31 this.comparer = Comparer<T>.Default;
44 var index = items.IndexOf(item);
49 var maxCount = items.Count - 1;
50 items[index] = items[maxCount];
51 items.RemoveAt(maxCount);
54 var parentIndex = index;
57 var childIndex = parentIndex * 2;
60 if (childIndex >= maxCount)
64 if (childIndex + 1 < maxCount && comparer.Compare(items[childIndex + 1], items[childIndex]) < 0)
68 if (comparer.Compare(items[parentIndex], items[childIndex]) <= 0)
72 T tmp = items[childIndex];
73 items[childIndex] = items[parentIndex];
74 items[parentIndex] = tmp;
77 parentIndex = childIndex;
88 var childIndex = items.Count;
92 while (childIndex != 0)
94 var parentIndex = childIndex / 2;
97 if (comparer.Compare(items[childIndex], items[parentIndex]) >= 0)
101 T tmp = items[childIndex];
102 items[childIndex] = items[parentIndex];
103 items[parentIndex] = tmp;
106 childIndex = parentIndex;
113 get {
return items.Count; }
119 get {
return items.Count == 0; }
139 var maxCount = items.Count - 1;
140 items[0] = items[maxCount];
141 items.RemoveAt(maxCount);
148 var childIndex = parentIndex * 2;
149 if (childIndex >= maxCount)
153 if (childIndex + 1 < maxCount && comparer.Compare(items[childIndex + 1], items[childIndex]) < 0)
157 if (comparer.Compare(items[parentIndex], items[childIndex]) <= 0)
161 T tmp = items[childIndex];
162 items[childIndex] = items[parentIndex];
163 items[parentIndex] = tmp;
166 parentIndex = childIndex;
T Peek()
Returns the object at the beginning of the PriorityQueue{T}, without removing it. ...
T Dequeue()
Removes and returns the object at the beginning of the PriorityQueue{T}.
void Enqueue(T item)
Adds an object to the PriorityQueue{T} and sorts underlying container.
PriorityQueue(IComparer< T > comparer)