From 83c002c197301bffdd2140966e98c89c4529b1b6 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Mon, 29 Aug 2022 01:53:40 -0700 Subject: [PATCH 1/2] add peeks --- core/container/queue/queue.odin | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/container/queue/queue.odin b/core/container/queue/queue.odin index a42d0e5a4..9674b40be 100644 --- a/core/container/queue/queue.odin +++ b/core/container/queue/queue.odin @@ -99,6 +99,16 @@ get_ptr :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> ^ return &q.data[idx] } +peek_front :: proc(q: ^$Q/Queue($T)) -> ^T { + idx := q.offset%builtin.len(q.data) + return &q.data[idx] +} + +peek_back :: proc(q: ^$Q/Queue($T)) -> ^T { + idx := (uint(q.len - 1)+q.offset)%builtin.len(q.data) + return &q.data[idx] +} + // Push an element to the back of the queue push_back :: proc(q: ^$Q/Queue($T), elem: T) -> bool { if space(q^) == 0 { From 7a6fc3a93bf1be42de21a1af7af6e9e435438832 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Mon, 29 Aug 2022 02:03:12 -0700 Subject: [PATCH 2/2] Add bounds check for peeks --- core/container/queue/queue.odin | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/container/queue/queue.odin b/core/container/queue/queue.odin index 9674b40be..b3a8ad43f 100644 --- a/core/container/queue/queue.odin +++ b/core/container/queue/queue.odin @@ -99,12 +99,14 @@ get_ptr :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> ^ return &q.data[idx] } -peek_front :: proc(q: ^$Q/Queue($T)) -> ^T { +peek_front :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> ^T { + runtime.bounds_check_error_loc(loc, 0, builtin.len(q.data)) idx := q.offset%builtin.len(q.data) return &q.data[idx] } -peek_back :: proc(q: ^$Q/Queue($T)) -> ^T { +peek_back :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> ^T { + runtime.bounds_check_error_loc(loc, int(q.len - 1), builtin.len(q.data)) idx := (uint(q.len - 1)+q.offset)%builtin.len(q.data) return &q.data[idx] }