core/container/queue
container_queue
Types
1Constants
1Procedures
31_grow
_grow :: proc(q: ^Q, min_capacity: uint, loc = #caller_location) -> (runtime.Allocator_Error)SourceInternal growing procedure
append_elem
append_elem :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Sourceappend_elems
append_elems :: proc(q: ^Q, elems, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Sourceback
back :: proc(q: ^Q, loc = #caller_location) -> (T)SourceGet the element at the back of the queue.
This will raise a bounds checking error if the queue is empty.
back_ptr
back_ptr :: proc(q: ^Q, loc = #caller_location) -> (^T)SourceGet a pointer to the element at the back of the queue.
This will raise a bounds checking error if the queue is empty.
cap
cap :: proc(q: Q) -> (int)SourceReturn the capacity of the queue.
clear
clear :: proc(q: ^Q)SourceReset the queue's length and offset to zero, letting it write new elements over old memory, in effect clearing the accessible contents.
consume_back
consume_back :: proc(q: ^Q, n: int, loc = #caller_location)SourceConsume 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
consume_front :: proc(q: ^Q, n: int, loc = #caller_location)SourceConsume n elements from the front of the queue.
This will raise a bounds checking error if the queue does not have enough elements.
dequeue
dequeue :: proc(q: ^Q, loc = #caller_location) -> (elem: T)Sourcedestroy
destroy :: proc(q: ^Q)SourceDelete 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
enqueue :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Sourcefront
front :: proc(q: ^Q, loc = #caller_location) -> (T)SourceGet the element at the front of the queue.
This will raise a bounds checking error if the queue is empty.
front_ptr
front_ptr :: proc(q: ^Q, loc = #caller_location) -> (^T)SourceGet a pointer to the element at the front of the queue.
This will raise a bounds checking error if the queue is empty.
get
get :: proc(q: ^Q, i: int, loc = #caller_location) -> (T)SourceGet the element at index i.
This will raise a bounds checking error if i is an invalid index.
get_ptr
get_ptr :: proc(q: ^Q, i: int, loc = #caller_location) -> (^T)SourceGet a pointer to the element at index i.
This will raise a bounds checking error if i is an invalid index.
init
init :: proc(q: ^Q, capacity = DEFAULT_CAPACITY, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (runtime.Allocator_Error)SourceInitialize a Queue with a starting capacity and an allocator.
init_from_slice
init_from_slice :: proc(q: ^Q, backing: []T) -> (bool)SourceInitialize 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
init_with_contents :: proc(q: ^Q, backing: []T) -> (bool)SourceInitialize 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.
len
len :: proc(q: Q) -> (int)SourceReturn the length of the queue.
pop_back
pop_back :: proc(q: ^Q, loc = #caller_location) -> (elem: T)SourcePop 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
pop_back_safe :: proc(q: ^Q) -> (elem: T, ok: bool)SourcePop an element from the back of the queue if one exists and return true. Otherwise, return a nil element and false.
pop_front
pop_front :: proc(q: ^Q, loc = #caller_location) -> (elem: T)SourcePop an element from the front of the queue
This will raise a bounds checking error if the queue is empty.
pop_front_safe
pop_front_safe :: proc(q: ^Q) -> (elem: T, ok: bool)SourcePop an element from the front of the queue if one exists and return true. Otherwise, return a nil element and false.
push_back
push_back :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)SourcePush 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
push_back_elems :: proc(q: ^Q, elems, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)SourcePush 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
push_front :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)SourcePush 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
reserve :: proc(q: ^Q, capacity: int, loc = #caller_location) -> (runtime.Allocator_Error)SourceReserve enough space in the queue for at least the specified capacity.
This may return an error if allocation failed.
set
set :: proc(q: ^Q, i: int, val: T, loc = #caller_location)SourceSet the element at index i to val.
This will raise a bounds checking error if i is an invalid index.
shrink
shrink :: proc(q: ^Q, temp_allocator = context.temp_allocator, loc = #caller_location)SourceShrink a queue's dynamically allocated array.
This has no effect if the queue was initialized with a backing slice.
space
space :: proc(q: Q) -> (int)SourceReturn the remaining space in the queue.
Procedure Groups
2append
append :: proc{push_back, push_back_elems}Sourcepush
push :: proc{push_back, push_back_elems}Source