namespace System.Collections.Generic
{
///
/// Priority Queue data structure. The implementation is based on an array-based implementation of Binary Heap.
/// Exposes some of the functionality of the Binary Heap as a queue.
///
/// Provided type.
[Serializable]
public sealed class PriorityQueue : IQueue where T : IComparable
{
#region Fields
private readonly BinaryHeap binaryHeap;
#endregion
#region Properties
///
/// Returns the number of elements stored into the queue.
///
public int Count
{
get
{
return binaryHeap.Count;
}
}
#endregion
#region Constructors
///
/// Constructor for priority queue data structure.
///
public PriorityQueue()
{
binaryHeap = new BinaryHeap();
}
#endregion
#region Public Methods
///
/// Add provided value to the queue.
///
/// Value to be added to the queue.
public void Enqueue(T value)
{
binaryHeap.Add(value);
}
///
/// Pops the queue and removes the highest priority value from the queue.
///
/// Highest priority value from the queue
public T Dequeue()
{
if (Count > 0)
{
return binaryHeap.Remove();
}
else
{
throw new IndexOutOfRangeException("Queue is empty!");
}
}
///
/// Looks up the highest priority value from the queue. Doesn't alter the queue in any way.
///
/// Highest priority value from the queue.
public T Peek()
{
return binaryHeap.Min;
}
///
/// Clears the queue contents, removing any value stored into the queue.
///
public void Clear()
{
binaryHeap.Clear();
}
///
/// Checks if queue contains provided item.
///
/// Item to be checked.
/// True if queue contains the provided item. False otherwise.
public bool Contains(T item)
{
return binaryHeap.Contains(item);
}
///
/// Returns an enumerator that iterates over the queue.
///
/// Enumerator that iterates over the queue.
public IEnumerator GetEnumerator()
{
return binaryHeap.GetEnumerator();
}
#endregion
#region Private Methods
///
/// Necesarry for the implementatio of IQueue.
///
///
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
}