diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2022-08-29 12:02:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-29 12:02:17 +0100 |
| commit | d39f1c461e7fc2ee72398c8ff3f9262da5fb4ed9 (patch) | |
| tree | d5e75cedc3700d252fd68c5b8448888e460c1c1a | |
| parent | fc47b5dee04378e151fcf87bf7299f23b27e88ec (diff) | |
| parent | 7a6fc3a93bf1be42de21a1af7af6e9e435438832 (diff) | |
Merge pull request #1988 from colrdavidson/queue_peek
Add peek_front and peek_back
| -rw-r--r-- | core/container/queue/queue.odin | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/core/container/queue/queue.odin b/core/container/queue/queue.odin index a42d0e5a4..b3a8ad43f 100644 --- a/core/container/queue/queue.odin +++ b/core/container/queue/queue.odin @@ -99,6 +99,18 @@ 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), 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), 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] +} + // Push an element to the back of the queue push_back :: proc(q: ^$Q/Queue($T), elem: T) -> bool { if space(q^) == 0 { |