Skip to content
CryoCryo home
Stdlibcollections

deque

import std::collections::deque; · source

Types

Deque<T, A>

type struct Deque<T, A = GlobalAllocator> where A: Allocator {
    private buffer: RawBuffer<T, A>;
    private head:   i64;
    private length: i64;
}

A double-ended queue, growable at both ends. The elements live in one ring buffer: they start at the front slot and run length slots on, wrapping from the buffer's end to its start. Pushing and popping at either end is O(1), amortized over the doublings that grow the buffer, and indexing is O(1). A deque allocates nothing until the first push.

import std::collections::deque::{ Deque };

mut queue: Deque<i64> = Deque::new();
queue.push_back(2);
queue.push_front(1);
queue.push_back(3);
const first: Option<i64> = queue.pop_front();   // Some(1)
const middle: i64 = queue[0];                    // 2

front and back borrow the end elements and get_reference(index) any one, each answering None when there is none; queue[i] reads and writes element i from the front. as_slices hands the elements out front to back as the two runs they occupy — the run up to the buffer's end, and the run that wrapped to its start, empty when nothing wrapped. clear releases every element and keeps the buffer.

Constructors

static new() -> Deque<T, GlobalAllocator>;

An empty deque backed by GlobalAllocator. It allocates nothing until the first push.

static new_in(alloc: A) -> Deque<T, A>;

An empty deque that allocates through alloc.

static with_capacity(capacity: i64) -> Deque<T, GlobalAllocator>;

An empty deque with room for capacity elements before it grows. Panics on allocation failure.

Methods

length(&this) -> i64;

The number of elements in the deque.

is_empty(&this) -> boolean;

True when the deque has no elements.

capacity(&this) -> i64;

How many elements fit before the deque grows.

push_back(mut &this, value: T) -> void;

Add value after the back element. Panics on allocation failure.

push_front(mut &this, value: T) -> void;

Add value before the front element. Panics on allocation failure.

pop_front(mut &this) -> Option<T>;

Remove and return the front element, or None when empty.

pop_back(mut &this) -> Option<T>;

Remove and return the back element, or None when empty.

front(&this) -> Option<T*>;

The front element, or None when empty.

back(&this) -> Option<T*>;

The back element, or None when empty.

get_reference(&this, index: i64) -> Option<T*>;

The element index places from the front, or None past the end.

as_slices(&this) -> Pair<&Slice<T>, &Slice<T>>;

The elements front to back, as the run from the front element to the buffer's end and the run that wrapped round to its start. The second is empty when nothing wrapped.

clear(mut &this) -> void;

Release every element, keeping the buffer.

Trait implementations

implement<T, A> trait Drop for struct Deque<T, A> where A: Allocator
implement<T, A> trait Index<i64> for struct Deque<T, A> where A: Allocator
implement<T, A> trait IndexMutable<i64> for struct Deque<T, A> where A: Allocator
implement<T, A> trait Display for struct Deque<T, A> where T: Display, A: Allocator
std::fmt::display
implement<T, A> trait Debug for struct Deque<T, A> where T: Debug, A: Allocator
std::fmt::display