From 92777666a94985232dd645e3333bc5a605af3cfa Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Fri, 23 Sep 2022 22:03:13 -0400 Subject: [PATCH] changed peek to include an offset --- priorityqueue.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/priorityqueue.go b/priorityqueue.go index a2330a8..bfc28ec 100644 --- a/priorityqueue.go +++ b/priorityqueue.go @@ -58,16 +58,17 @@ func (q *PriorityQueue[T]) Len() int { return len(q.items) } -func (q *PriorityQueue[T]) Peek() (T, bool) { +// offset should be between 0 and Len() - 1 +func (q *PriorityQueue[T]) Peek(offset int) (T, bool) { q.lock.RLock() defer q.lock.RUnlock() - if len(q.items) == 0 { - var def T - return def, false + if offset < 0 || offset >= len(q.items) { + var t T + return t, false } - return q.items[0].value, true + return q.items[offset].value, true } func (q *PriorityQueue[T]) Clear() {