示例#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
 /// <summary>
 /// Copies the queue elements to a new array.
 /// </summary>
 /// <returns>A new array containing elements copied from the Queue.</returns>
 public PriorityQueueItem <TValue, TPriority>[] ToArray()
 {
     PriorityQueueItem <TValue, TPriority>[] newItems =
         new PriorityQueueItem <TValue, TPriority> [numItems];
     Array.Copy(items, newItems, numItems);
     return(newItems);
 }
示例#3
0
        /// <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, TPriority priority)
        {
            if (numItems == capacity)
            {
                // need to increase capacity
                // grow by 50 percent
                //SetCapacity((3 * Capacity) / 2);
                SetCapacity((int)(Capacity * 1.5));
            }

            // Create the new item
            PriorityQueueItem <TValue, TPriority> 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;
        }