/// <summary> /// Searches for the index where the given item should be place in the queue and, /// subsequently, inserts the item at the specified index. /// </summary> /// <remarks> /// This method will be called when the specified item equals /// another item (can be more than one) within the queue. /// David Venegoni, Jan 02 2014. /// </remarks> /// <param name="item"> The item to insert into the queue. </param> /// <param name="currentIndex"> The index in which to start at. </param> /// <param name="priorityConvention"> The priority convention to use when finding the index. </param> private void FindIndexAndInsertItem(T item, Int32 currentIndex, PriorityConvention priorityConvention) { Int32 currentPosition = currentIndex; Int32 condition = priorityConvention == PriorityConvention.HighestPriorityInFront ? 1 : -1; Boolean isLastElement = false; while (item.CompareTo(items[currentPosition]) != condition) { ++currentPosition; if (currentPosition < items.Count) // Make sure the index does not go out of range { continue; } isLastElement = true; break; } if (isLastElement) { items.Add(item); } else { items.Insert(currentPosition, item); } }
/// <summary> Initializes a new instance of the PriorityQueue class. </summary> /// <remarks> David Venegoni, Jan 02 2014. </remarks> /// <exception cref="ArgumentException"> Thrown when the convention is specified /// as PriorityConvention.None. </exception> /// <param name="convention"> /// (Optional) the convention to use when sorting and inserting items (this cannot be changed /// after the priority queue is created). /// </param> public PriorityQueue(PriorityConvention convention) { if (convention == PriorityConvention.None) { throw new ArgumentException("No valid ordering convention was specified", "convention"); } OrderingConvention = convention; items = new List <T>(); }
public PriorityQueue() { OrderingConvention = PriorityConvention.HighestPriorityInFront; items = new List <T>(); }