core/container/queue

container_queue

Types

1

Queue

Queue :: struct {}Source

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.

Constants

1

Procedures

31

back

back :: proc(q: ^Q, loc = #caller_location) -> (T)Source

Get 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)Source

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

clear :: proc(q: ^Q)Source

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

consume_back :: proc(q: ^Q, n: int, loc = #caller_location)Source

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

consume_front :: proc(q: ^Q, n: int, loc = #caller_location)Source

Consume n elements from the front of the queue.

This will raise a bounds checking error if the queue does not have enough elements.

destroy

destroy :: proc(q: ^Q)Source

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.

front

front :: proc(q: ^Q, loc = #caller_location) -> (T)Source

Get 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)Source

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

get :: proc(q: ^Q, i: int, loc = #caller_location) -> (T)Source

Get 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)Source

Get a pointer to the element at index i.

This will raise a bounds checking error if i is an invalid index.

init_from_slice

init_from_slice :: proc(q: ^Q, backing: []T) -> (bool)Source

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

init_with_contents :: proc(q: ^Q, backing: []T) -> (bool)Source

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

pop_back :: proc(q: ^Q, loc = #caller_location) -> (elem: T)Source

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

pop_back_safe :: proc(q: ^Q) -> (elem: T, ok: bool)Source

Pop 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)Source

Pop an element from the front of the queue

This will raise a bounds checking error if the queue is empty.

push_back

push_back :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Source

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

push_back_elems :: proc(q: ^Q, elems, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Source

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

push_front :: proc(q: ^Q, elem: T, loc = #caller_location) -> (ok: bool, err: runtime.Allocator_Error)Source

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

reserve :: proc(q: ^Q, capacity: int, loc = #caller_location) -> (runtime.Allocator_Error)Source

Reserve 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)Source

Set 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)Source

Shrink a queue's dynamically allocated array.

This has no effect if the queue was initialized with a backing slice.

Procedure Groups

2

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.