Пример #1
0
        /// <summary>
        /// Put an element into this priority queue. The element is inserted into the index found by BinarySearchUtil.SearchProximity. (Ave. O(log(n))).
        /// </summary>
        /// <param name="priority"></param>
        /// <param name="item"></param>
        public void Put(P priority, T item)
        {
            PriorityQueueItem <P, T> newItem = new PriorityQueueItem <P, T>(priority, item);
            int idx = BinarySearchUtil.SearchProximity(newItem, ItemQueue);

            if (ItemQueue.Count == 0)
            {
                ItemQueue.Add(newItem);
            }
            else
            {
                if (idx == 0 && newItem.CompareTo(ItemQueue[0]) < 0)
                {
                    ItemQueue.Insert(0, newItem);
                }
                else if (idx + 1 < ItemQueue.Count)
                {
                    ItemQueue.Insert(idx + 1, newItem);
                }
                else
                {
                    ItemQueue.Add(newItem);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Override fuction from <c>IComparable</c>.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }
            PriorityQueueItem <P, T> otherObj = obj as PriorityQueueItem <P, T>;

            return(Priority.CompareTo(otherObj.Priority));
        }
Пример #3
0
 /// <summary>
 /// Pop the first element in this priority queue.
 /// </summary>
 /// <returns></returns>
 public PriorityQueueItem <P, T> Pop()
 {
     if (ItemQueue.Count > 0)
     {
         PriorityQueueItem <P, T> popItem = ItemQueue[0];
         ItemQueue.RemoveAt(0);
         return(popItem);
     }
     else
     {
         return(null);
     }
 }
Пример #4
0
        /// <summary>
        /// Remove the first element that has its item reference equals to the param. (Worst O(n)).
        /// </summary>
        /// <param name="item"></param>
        public void Remove(T item)
        {
            int idx = -1;

            for (int i = 0; i < ItemQueue.Count; i++)
            {
                PriorityQueueItem <P, T> queueItem = ItemQueue[i];
                if (Object.ReferenceEquals(queueItem.Item, item))
                {
                    idx = i;
                    break;
                }
            }

            if (idx != -1)
            {
                ItemQueue.RemoveAt(idx);
            }
        }