Added Peek() method to the priority queue classes (it's a method and not a property because it can potentially fail with an exception and a property might lead someone to not think about this possibility)

git-svn-id: file:///srv/devel/repo-conversion/nusu@79 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-07-15 20:36:16 +00:00
parent 7bff8c5d52
commit f5eefc6765
2 changed files with 19 additions and 2 deletions

View File

@ -73,6 +73,12 @@ namespace Nuclex.Support.Collections {
);
}
/// <summary>Returns the topmost item in the queue without dequeueing it</summary>
/// <returns>The topmost item in the queue</returns>
public PriorityItemPair<PriorityType, ItemType> Peek() {
return this.internalQueue.Peek();
}
/// <summary>Takes the item with the highest priority off from the queue</summary>
/// <returns>The item with the highest priority in the list</returns>
public PriorityItemPair<PriorityType, ItemType> Dequeue() {

View File

@ -107,12 +107,23 @@ namespace Nuclex.Support.Collections {
this.heap = new ItemType[this.capacity];
}
/// <summary>Returns the topmost item in the queue without dequeueing it</summary>
/// <returns>The topmost item in the queue</returns>
public ItemType Peek() {
if(this.count == 0) {
throw new InvalidOperationException("No items queued");
}
return this.heap[0];
}
/// <summary>Takes the item with the highest priority off from the queue</summary>
/// <returns>The item with the highest priority in the list</returns>
/// <exception cref="InvalidOperationException">When the queue is empty</exception>
public ItemType Dequeue() {
if(count == 0)
if(this.count == 0) {
throw new InvalidOperationException("No items available to dequeue");
}
ItemType result = this.heap[0];
--this.count;
@ -150,7 +161,7 @@ namespace Nuclex.Support.Collections {
/// <param name="array">Array to copy the priority queue into</param>
/// <param name="index">Starting index for the destination array</param>
public void CopyTo(Array array, int index) {
Array.Copy(heap, 0, array, index, this.count);
Array.Copy(this.heap, 0, array, index, this.count);
}
/// <summary>