예제 #1
0
파일: PriorityQueue.cs 프로젝트: x2v0/gep
        // Remove a node at a particular position in the queue.
        private PriorityQueueItem <TValue> RemoveAt(Int32 index)
        {
            // remove an item from the heap
            PriorityQueueItem <TValue> o   = items[index];
            PriorityQueueItem <TValue> tmp = items[numItems - 1];

            items[--numItems] = default(PriorityQueueItem <TValue>);
            if (numItems > 0)
            {
                int i = index;
                int j = i + 1;
                while (i < Count / 2)
                {
                    if ((j < Count - 1) && (items[j].Priority > items[j + 1].Priority))
                    {
                        j++;
                    }
                    if (items[j].Priority >= tmp.Priority)
                    {
                        break;
                    }
                    items[i] = items[j];
                    i        = j;
                    j       *= 2;
                }
                items[i] = tmp;
            }
            return(o);
        }
예제 #2
0
파일: PriorityQueue.cs 프로젝트: x2v0/gep
 /// <summary>
 /// Copies the queue elements to a new array.
 /// </summary>
 /// <returns>A new array containing elements copied from the Queue.</returns>
 public PriorityQueueItem <TValue>[] ToArray()
 {
     PriorityQueueItem <TValue>[] newItems =
         new PriorityQueueItem <TValue> [numItems];
     Array.Copy(items, newItems, numItems);
     return(newItems);
 }
예제 #3
0
파일: PriorityQueue.cs 프로젝트: x2v0/gep
        /// <summary>
        /// Adds an object to the queue, in order by priority.
        /// </summary>
        /// <param name="value">The object to be added.</param>
        /// <param name="priority">Priority of the object to be added.</param>
        public void Enqueue(TValue value, int priority)
        {
            if (numItems == capacity)
            {
                // need to increase capacity
                // grow by 50 percent
                SetCapacity((3 * Capacity) / 2);
            }

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

            ++numItems;

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