From 656e62d7242f9b58d152afecacc10de66b700e7f Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Mon, 8 Jan 2024 19:33:30 +0100 Subject: [PATCH] Add `peek` to priority queue. --- core/container/priority_queue/priority_queue.odin | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/container/priority_queue/priority_queue.odin b/core/container/priority_queue/priority_queue.odin index 0c5c4931d..0c43816e1 100644 --- a/core/container/priority_queue/priority_queue.odin +++ b/core/container/priority_queue/priority_queue.odin @@ -140,3 +140,18 @@ remove :: proc(pq: ^$Q/Priority_Queue($T), i: int) -> (value: T, ok: bool) { return } +peek_safe :: proc(pq: $Q/Priority_Queue($T), loc := #caller_location) -> (res: T, ok: bool) { + if builtin.len(pq.queue) > 0 { + return pq.queue[0], true + } + return +} + +peek :: proc(pq: $Q/Priority_Queue($T), loc := #caller_location) -> (res: T) { + assert(condition=builtin.len(pq.queue)>0, loc=loc) + + if builtin.len(pq.queue) > 0 { + return pq.queue[0] + } + return +} \ No newline at end of file