Esempio n. 1
0
        // 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;
        }
Esempio n. 2
0
        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);
        }