Class in collections::deque coil-stdlib

VecDeque

class VecDeque<T>
Import
use collections::deque::{VecDeque};
Source: src/collections/deque.hy

A double-ended queue: constant-time push and pop at both ends.

Backed by a ring buffer that doubles when full. Use it as a FIFO queue (push_back + pop_front) or a stack at either end.

Associated functions#

NameDescription
with_capacityEmpty deque with ring capacity rounded up to the next power of two (min 8).
newEmpty deque with default capacity 8.

Methods#

NameDescription
sizeReturns the number of values.
is_emptyReturns true when the deque has no values.
capacityReturns how many values fit before the ring buffer grows.
push_backAdds v at the back.
push_frontAdds v at the front.
peek_frontReturns the front value without removing it, or None when the deque is empty.
peek_backReturns the back value without removing it, or None when the deque is empty.
pop_frontRemoves and returns the front value, or None when the deque is empty.
pop_backRemoves and returns the back value, or None when the deque is empty.
clearRemoves every value, keeping the capacity.
to_vecReturns the values front to back as a new Vec.

Invariants#

Checked according to --contracts (see Contracts).

invariant self.len >= 0 && self.len <= self.cap
invariant self.head >= 0 && self.head < self.cap
invariant self.buf.len() == self.cap

Examples#

use collections::deque::{VecDeque};
use tests::assert;
let q: VecDeque<string> = VecDeque::new();
q.push_back("first");
q.push_back("second");
q.push_front("zeroth");
assert!((q.pop_front() ?? "") == "zeroth");
assert!((q.pop_back() ?? "") == "second");
assert!(q.size() == 1);

See also#

↑ ↓ navigate ↵ open