VecDeque
class VecDeque<T> Import
Source: use collections::deque::{VecDeque}; 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#
| Name | Description |
|---|---|
with_capacity | Empty deque with ring capacity rounded up to the next power of two (min 8). |
new | Empty deque with default capacity 8. |
Methods#
| Name | Description |
|---|---|
size | Returns the number of values. |
is_empty | Returns true when the deque has no values. |
capacity | Returns how many values fit before the ring buffer grows. |
push_back | Adds v at the back. |
push_front | Adds v at the front. |
peek_front | Returns the front value without removing it, or None when the deque is empty. |
peek_back | Returns the back value without removing it, or None when the deque is empty. |
pop_front | Removes and returns the front value, or None when the deque is empty. |
pop_back | Removes and returns the back value, or None when the deque is empty. |
clear | Removes every value, keeping the capacity. |
to_vec | Returns 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);