// Try to avoid unnecessary slow memory reallocations by creating your queue with an ample capacity private void ExpandCapacity() { // Double our capacity capacity *= 2; // Create a new queue var newQueue = new ItemPriority <TItem, TPriority> [capacity]; // Copy the contents of the original queue to the new one Array.Copy(queue, newQueue, queue.Length); // Copy the new queue over the original one queue = newQueue; }
public void Enqueue(TItem item, TPriority priority) { if (++count > capacity) { ExpandCapacity(); } int newItemIndex = count - 1; queue[newItemIndex] = new ItemPriority <TItem, TPriority> { Item = item, Priority = priority }; ReorderItem(newItemIndex, -1); }
public PriorityQueue(int capacity, ITypeMath <TPriority> priorityMath = null) { if (capacity <= 0) { throw new ArgumentException("Capacity must be greater than zero"); } this.capacity = capacity; queue = new ItemPriority <TItem, TPriority> [capacity]; if (priorityMath != null) { this.priorityMath = priorityMath; } else { this.priorityMath = TypeMath <TPriority> .GetMath(); } }