package core:container/queue

⌘K
Ctrl+K
or
/

    Types

    Queue ¶

    Queue :: struct($T: typeid) {
    	… // See source for fields
    }
     

    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.

    Related Procedures With Parameters

    Constants

    DEFAULT_CAPACITY ¶

    DEFAULT_CAPACITY: int : 16

    Variables

    This section is empty.

    Procedures

    append_elem ¶

    append_elem :: push_back
     

    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 ¶

    append_elems :: push_back_elems
     

    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 ¶

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

    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/Queue($T), loc := #caller_location) -> ^$T {…}
     

    Get 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/Queue($T)) -> int {…}
     

    Return the capacity of the queue.

    clear ¶

    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 ¶

    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 ¶

    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 ¶

    dequeue :: pop_front
     

    Pop an element from the front of the queue

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

    destroy ¶

    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 ¶

    enqueue :: push_back
     

    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 ¶

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

    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/Queue($T), loc := #caller_location) -> ^$T {…}
     

    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/Queue($T), #any_int i: int, loc := #caller_location) -> $T {…}
     

    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/Queue($T), #any_int i: int, loc := #caller_location) -> ^$T {…}
     

    Get 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/Queue($T), capacity: int = DEFAULT_CAPACITY, allocator := context.allocator) -> runtime.Allocator_Error {…}
     

    Initialize a Queue with a starting capacity and an allocator.

    init_from_slice ¶

    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 ¶

    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.

    len ¶

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

    Return the length of the queue.

    peek_back ¶

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

    peek_front ¶

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

    pop_back ¶

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

    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/Queue($T)) -> (elem: $T, 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 ¶

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

    Pop 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/Queue($T)) -> (elem: $T, 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 ¶

    push_back :: proc(q: ^$Q/Queue($T), elem: $T) -> (ok: bool, err: runtime.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 ¶

    push_back_elems :: proc(q: ^$Q/Queue($T), .. elems: ..$T) -> (ok: bool, err: runtime.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 ¶

    push_front :: proc(q: ^$Q/Queue($T), elem: $T) -> (ok: bool, err: runtime.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 ¶

    reserve :: proc(q: ^$Q/Queue($T), capacity: int) -> runtime.Allocator_Error {…}
     

    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/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 ¶

    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 ¶

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

    Return the remaining space in the queue.

    This will be cap() - len().

    Procedure Groups

    Source Files

    Generation Information

    Generated with odin version dev-2025-06 (vendor "odin") Windows_amd64 @ 2025-06-16 21:13:22.611885700 +0000 UTC