示例#1
0
        /// <summary>
        /// Removes the specified item from the PriorityAndItems object, and removes that object from the tree and the item to priority map, if the specified item is the last in the object.
        /// </summary>
        /// <param name="priorityAndItems">The object to remove the item from.</param>
        /// <param name="item">The item to remove.</param>
        protected void Remove(PriorityAndItems <T> priorityAndItems, T item)
        {
            Boolean removeFromMap = true;

            // Remove the item from the tree
            if (priorityAndItems.Items.Count == 1 && priorityAndItems.Items[item] == 1)
            {
                tree.Remove(priorityAndItems);
            }
            else
            {
                if (priorityAndItems.Items[item] == 1)
                {
                    priorityAndItems.Items.Remove(item);
                }
                else
                {
                    priorityAndItems.Items[item]--;
                    removeFromMap = false;
                }
            }
            // Remove the item from the item to priority map
            if (removeFromMap == true)
            {
                itemToPriorityMap[item].Remove(priorityAndItems.Priority);
                if (itemToPriorityMap[item].Count == 0)
                {
                    itemToPriorityMap.Remove(item);
                }
            }
            count--;
        }
示例#2
0
        /// <summary>
        /// Returns the total number of items in the specified PriorityAndItems object.
        /// </summary>
        /// <param name="priorityAndItems">The PriorityAndItems object to retrieve the total for.</param>
        /// <returns>The total number of items in the PriorityAndItems object.</returns>
        protected Int32 GetTotalItemCount(PriorityAndItems <T> priorityAndItems)
        {
            Int32 returnCount = 0;

            foreach (Int32 currentItemCount in priorityAndItems.Items.Values)
            {
                returnCount += currentItemCount;
            }

            return(returnCount);
        }
示例#3
0
        /// <summary>
        /// Removes and returns the item with the lowest priority.
        /// </summary>
        /// <returns>The item with the lowest priority.</returns>
        /// <exception cref="System.InvalidOperationException">The queue is empty.</exception>
        public T DequeueMin()
        {
            ThrowExceptionIfQueueIsEmpty();

            PriorityAndItems <T>    minPriorityAndItems = tree.Min;
            KeyValuePair <T, Int32> firstItemAndCount   = GetFirstKeyValuePairFromDictionary(minPriorityAndItems.Items);

            Dequeue(firstItemAndCount.Key, minPriorityAndItems.Priority);

            return(firstItemAndCount.Key);
        }
示例#4
0
 /// <summary>
 /// Stores an item in the queue with minimum priority (i.e. lower than all existing items).
 /// </summary>
 /// <param name="item">The item to enqueue.</param>
 public void EnqueueAsMin(T item)
 {
     if (count == 0)
     {
         Enqueue(item, 0.0);
     }
     else
     {
         PriorityAndItems <T> minNode = tree.Min;
         Enqueue(item, priorityIncrementer.Decrement(minNode.Priority));
     }
 }
示例#5
0
 /// <summary>
 /// Stores an item in the queue with maximum priority (i.e. higher than all existing items).
 /// </summary>
 /// <param name="item">The item to enqueue.</param>
 public void EnqueueAsMax(T item)
 {
     if (count == 0)
     {
         Enqueue(item, 0.0);
     }
     else
     {
         PriorityAndItems <T> maxNode = tree.Max;
         Enqueue(item, priorityIncrementer.Increment(maxNode.Priority));
     }
 }
示例#6
0
        /// <summary>
        /// Stores an item in the queue with the specified priority.
        /// </summary>
        /// <param name="item">The item to enqueue.</param>
        /// <param name="priority">The priority of the item.</param>
        /// <exception cref="System.ArgumentException">The parameter 'priority' cannot be NaN.</exception>
        /// <exception cref="System.InvalidOperationException">The queue cannot hold more than Int32.MaxValue items.</exception>
        public void Enqueue(T item, Double priority)
        {
            if (Double.IsNaN(priority))
            {
                throw new ArgumentException($"Parameter '{nameof(priority)}' cannot be '{nameof(Double.NaN)}'.", nameof(priority));
            }
            if (count == Int32.MaxValue)
            {
                throw new InvalidOperationException($"The queue cannot hold greater than {Int32.MaxValue} items.");
            }

            // Add the item to the tree
            var newNode = new PriorityAndItems <T>(priority);
            WeightBalancedTreeNode <PriorityAndItems <T> > priorityNode = tree.TraverseDownToNodeHoldingItem(newNode);

            if (priorityNode == null)
            {
                newNode.Items.Add(item, 1);
                tree.Add(newNode);
            }
            else
            {
                if (priorityNode.Item.Items.ContainsKey(item))
                {
                    priorityNode.Item.Items[item]++;
                }
                else
                {
                    priorityNode.Item.Items.Add(item, 1);
                }
            }
            // Add the item to the item to priority map
            if (itemToPriorityMap.ContainsKey(item))
            {
                if (itemToPriorityMap[item].Contains(priority) == false)
                {
                    itemToPriorityMap[item].Add(priority);
                }
            }
            else
            {
                itemToPriorityMap.Add(item, new HashSet <Double>()
                {
                    priority
                });
            }
            count++;
        }
示例#7
0
 #pragma warning disable 1591
 public Int32 CompareTo(PriorityAndItems <T> other)
 {
     return(this.priority.CompareTo(other.priority));
 }