A dynamically resizable double-ended queue/ring-buffer.

Collection Info

View Source
Collection
core
Path
container/queue
Entries
34

Source Files

Constants

1

Types

1

Queue #

Source
Queue :: Queue

`Queue` is a dynamically resizable double-ended queue/ring-buffer. Being double-ended means that either end may be pushed onto or popped from across the same block of memory, in any order, thus providing both stack and queue-like behaviors in the same data structure.

Procedures

30

append_elem #

Source
append_elem :: proc(q: ^$Q/Queue($T), elem: $T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push an element to the back of the queue. If there is no more space left and allocation fails to get more, this will return false with an `Allocator_Error`. Example: import "base:runtime" import "core:container/queue" // This demonstrates typical queue behavior (First-In First-Out). main :: proc() { q: queue.Queue(int) queue.init(&q) queue.push_back(&q, 1) queue.push_back(&q, 2) queue.push_back(&q, 3) // q.data is now [1, 2, 3, ...] assert(queue.pop_front(&q) == 1) assert(queue.pop_front(&q) == 2) assert(queue.pop_front(&q) == 3) }

append_elems #

Source
append_elems :: proc(q: ^$Q/Queue($T), elems: ..$T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push many elements at once to the back of the queue. If there is not enough space left and allocation fails to get more, this will return false with an `Allocator_Error`.

back #

Source
back :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> $$deferred_return {…}

Get the element at the back of the queue. This will raise a bounds checking error if the queue is empty.

back_ptr #

Source
back_ptr :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> $$deferred_return {…}

Get a pointer to the element at the back of the queue. This will raise a bounds checking error if the queue is empty.

clear #

Source
clear :: proc(q: ^$Q/Queue($T)) {…}

Reset the queue's length and offset to zero, letting it write new elements over old memory, in effect clearing the accessible contents.

consume_back #

Source
consume_back :: proc(q: ^$Q/Queue($T), n: int, loc := #caller_location) {…}

Consume `n` elements from the back of the queue. This will raise a bounds checking error if the queue does not have enough elements.

consume_front #

Source
consume_front :: proc(q: ^$Q/Queue($T), n: int, loc := #caller_location) {…}

Consume `n` elements from the back of the queue. This will raise a bounds checking error if the queue does not have enough elements.

dequeue #

Source
dequeue :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> (elem: $$deferred_return) {…}

Pop an element from the front of the queue This will raise a bounds checking error if the queue is empty.

destroy #

Source
destroy :: proc(q: ^$Q/Queue($T)) {…}

Delete memory that has been dynamically allocated from a `Queue` that was setup with `init`. Note that this procedure should not be used on queues setup with `init_from_slice` or `init_with_contents`, as neither of those procedures keep track of the allocator state of the underlying `backing` slice.

enqueue #

Source
enqueue :: proc(q: ^$Q/Queue($T), elem: $T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push an element to the back of the queue. If there is no more space left and allocation fails to get more, this will return false with an `Allocator_Error`. Example: import "base:runtime" import "core:container/queue" // This demonstrates typical queue behavior (First-In First-Out). main :: proc() { q: queue.Queue(int) queue.init(&q) queue.push_back(&q, 1) queue.push_back(&q, 2) queue.push_back(&q, 3) // q.data is now [1, 2, 3, ...] assert(queue.pop_front(&q) == 1) assert(queue.pop_front(&q) == 2) assert(queue.pop_front(&q) == 3) }

front #

Source
front :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> $$deferred_return {…}

Get the element at the front of the queue. This will raise a bounds checking error if the queue is empty.

front_ptr #

Source
front_ptr :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> $$deferred_return {…}

Get a pointer to the element at the front of the queue. This will raise a bounds checking error if the queue is empty.

get #

Source
get :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> $$deferred_return {…}

Get the element at index `i`. This will raise a bounds checking error if `i` is an invalid index.

get_ptr #

Source
get_ptr :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> $$deferred_return {…}

Get a pointer to the element at index `i`. This will raise a bounds checking error if `i` is an invalid index.

init #

Source
init :: proc(q: ^$Q/Queue($T), capacity: int = DEFAULT_CAPACITY, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {…}

Initialize a `Queue` with a starting `capacity` and an `allocator`.

init_from_slice #

Source
init_from_slice :: proc(q: ^$Q/Queue($T), backing: []$T) -> bool {…}

Initialize a `Queue` from a fixed `backing` slice into which modifications are made directly. The contents of the `backing` will be overwritten as items are pushed onto the `Queue`. Any previous contents will not be available through the API but are not explicitly zeroed either. Note that procedures which need space to work (`push_back`, ...) will fail if the backing slice runs out of space.

init_with_contents #

Source
init_with_contents :: proc(q: ^$Q/Queue($T), backing: []$T) -> bool {…}

Initialize a `Queue` from a fixed `backing` slice into which modifications are made directly. The contents of the queue will start out with all of the elements in `backing`, effectively creating a full queue from the slice. As such, no procedures will be able to add more elements to the queue until some are taken off.

pop_back #

Source
pop_back :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> (elem: $$deferred_return) {…}

Pop an element from the back of the queue. This will raise a bounds checking error if the queue is empty. Example: import "base:runtime" import "core:container/queue" // This demonstrates stack behavior (First-In Last-Out) at the far end of the data array. main :: proc() { q: queue.Queue(int) queue.init(&q) queue.push_front(&q, 1) queue.push_front(&q, 2) queue.push_front(&q, 3) // q.data is now [..., 3, 2, 1] log.infof("%#v", q) assert(queue.pop_front(&q) == 3) assert(queue.pop_front(&q) == 2) assert(queue.pop_front(&q) == 1) }

pop_back_safe #

Source
pop_back_safe :: proc(q: ^$Q/Queue($T)) -> (elem: $$deferred_return, ok: bool) {…}

Pop an element from the back of the queue if one exists and return true. Otherwise, return a nil element and false.

pop_front #

Source
pop_front :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> (elem: $$deferred_return) {…}

Pop an element from the front of the queue This will raise a bounds checking error if the queue is empty.

pop_front_safe #

Source
pop_front_safe :: proc(q: ^$Q/Queue($T)) -> (elem: $$deferred_return, ok: bool) {…}

Pop an element from the front of the queue if one exists and return true. Otherwise, return a nil element and false.

push_back #

Source
push_back :: proc(q: ^$Q/Queue($T), elem: $T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push an element to the back of the queue. If there is no more space left and allocation fails to get more, this will return false with an `Allocator_Error`. Example: import "base:runtime" import "core:container/queue" // This demonstrates typical queue behavior (First-In First-Out). main :: proc() { q: queue.Queue(int) queue.init(&q) queue.push_back(&q, 1) queue.push_back(&q, 2) queue.push_back(&q, 3) // q.data is now [1, 2, 3, ...] assert(queue.pop_front(&q) == 1) assert(queue.pop_front(&q) == 2) assert(queue.pop_front(&q) == 3) }

push_back_elems #

Source
push_back_elems :: proc(q: ^$Q/Queue($T), elems: ..$T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push many elements at once to the back of the queue. If there is not enough space left and allocation fails to get more, this will return false with an `Allocator_Error`.

push_front #

Source
push_front :: proc(q: ^$Q/Queue($T), elem: $T, loc := #caller_location) -> (ok: bool, err: Allocator_Error) {…}

Push an element to the front of the queue. If there is no more space left and allocation fails to get more, this will return false with an `Allocator_Error`. Example: import "base:runtime" import "core:container/queue" // This demonstrates stack behavior (First-In Last-Out). main :: proc() { q: queue.Queue(int) queue.init(&q) queue.push_back(&q, 1) queue.push_back(&q, 2) queue.push_back(&q, 3) // q.data is now [1, 2, 3, ...] assert(queue.pop_back(&q) == 3) assert(queue.pop_back(&q) == 2) assert(queue.pop_back(&q) == 1) }

reserve #

Source
reserve :: proc(q: ^$Q/Queue($T), capacity: int, loc := #caller_location) -> Allocator_Error {…}

Reserve enough space in the queue for at least the specified capacity. This may return an error if allocation failed.

set #

Source
set :: proc(q: ^$Q/Queue($T), #any_int i: int, val: $T, loc := #caller_location) {…}

Set the element at index `i` to `val`. This will raise a bounds checking error if `i` is an invalid index.

shrink #

Source
shrink :: proc(q: ^$Q/Queue($T), temp_allocator := context.temp_allocator, loc := #caller_location) {…}

Shrink a queue's dynamically allocated array. This has no effect if the queue was initialized with a backing slice.

space #

Source
space :: proc(q: $Q/Queue($T)) -> int {…}

Return the remaining space in the queue. This will be `cap() - len()`.

Procedure Groups

2