From f5eefc6765e683b83e2694f6387f3a54b11a5d24 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Tue, 15 Jul 2008 20:36:16 +0000 Subject: [PATCH] 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 --- Source/Collections/PairPriorityQueue.cs | 6 ++++++ Source/Collections/PriorityQueue.cs | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Source/Collections/PairPriorityQueue.cs b/Source/Collections/PairPriorityQueue.cs index c3f316b..1b05ffa 100644 --- a/Source/Collections/PairPriorityQueue.cs +++ b/Source/Collections/PairPriorityQueue.cs @@ -73,6 +73,12 @@ namespace Nuclex.Support.Collections { ); } + /// Returns the topmost item in the queue without dequeueing it + /// The topmost item in the queue + public PriorityItemPair Peek() { + return this.internalQueue.Peek(); + } + /// Takes the item with the highest priority off from the queue /// The item with the highest priority in the list public PriorityItemPair Dequeue() { diff --git a/Source/Collections/PriorityQueue.cs b/Source/Collections/PriorityQueue.cs index 934d12e..ca3706c 100644 --- a/Source/Collections/PriorityQueue.cs +++ b/Source/Collections/PriorityQueue.cs @@ -107,12 +107,23 @@ namespace Nuclex.Support.Collections { this.heap = new ItemType[this.capacity]; } + /// Returns the topmost item in the queue without dequeueing it + /// The topmost item in the queue + public ItemType Peek() { + if(this.count == 0) { + throw new InvalidOperationException("No items queued"); + } + + return this.heap[0]; + } + /// Takes the item with the highest priority off from the queue /// The item with the highest priority in the list /// When the queue is empty 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 { /// Array to copy the priority queue into /// Starting index for the destination array public void CopyTo(Array array, int index) { - Array.Copy(heap, 0, array, index, this.count); + Array.Copy(this.heap, 0, array, index, this.count); } ///