Browse Source

Merge pull request #3078 from Kelimion/pq_peek

Add `peek` to priority queue.
Jeroen van Rijn 1 year ago
parent
commit
efb2b05040
1 changed files with 15 additions and 0 deletions
  1. 15 0
      core/container/priority_queue/priority_queue.odin

+ 15 - 0
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
+}