示例#1
0
        // Remove a node at a particular position in the queue.
        private PriorityQueueItem <TValue, TPriority> RemoveAt(Int32 index)
        {
            // remove an item from the heap
            PriorityQueueItem <TValue, TPriority> o   = items[index];
            PriorityQueueItem <TValue, TPriority> tmp = items[numItems - 1];

            items[--numItems] = default(PriorityQueueItem <TValue, TPriority>);
            if (numItems > 0)
            {
                int i = index;
                int j = i + 1;
                while (i < Count / 2)
                {
                    if ((j < Count - 1) && (compareFunc(items[j].Priority, items[j + 1].Priority) < 0))
                    {
                        j++;
                    }
                    if (compareFunc(items[j].Priority, tmp.Priority) <= 0)
                    {
                        break;
                    }
                    items[i] = items[j];
                    i        = j;
                    j       *= 2;
                }
                items[i] = tmp;
            }
            return(o);
        }
示例#2
0
        public TValue Dequeue(out TPriority prio)
        {
            if (Count == 0)
            {
                throw new InvalidOperationException("The queue is empty");
            }
            PriorityQueueItem <TValue, TPriority> item = RemoveAt(0);

            prio = item.Priority;
            return(item.Value);
        }
示例#3
0
        public void Enqueue(TValue value, TPriority priority)
        {
            if (numItems == capacity)
            {
                // need to increase capacity
                // grow by 50 percent
                SetCapacity((3 * Capacity) / 2);
            }

            // Create the new item
            var newItem =
                new PriorityQueueItem <TValue, TPriority>(value, priority);
            int i = numItems;

            ++numItems;

            // and insert it into the heap.
            while ((i > 0) && (compareFunc(items[i / 2].Priority, newItem.Priority) < 0))
            {
                items[i] = items[i / 2];
                i       /= 2;
            }
            items[i] = newItem;
        }