pub struct CircularQueue<T> { /* private fields */ }
Expand description
A circular buffer-like queue.
Implementations§
Source§impl<T> CircularQueue<T>
impl<T> CircularQueue<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty CircularQueue<T>
with the requested capacity.
§Examples
use circular_queue::CircularQueue;
let mut queue: CircularQueue<i32> = CircularQueue::with_capacity(5);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current number of elements in the queue.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(5);
queue.push(1);
queue.push(2);
queue.push(3);
assert_eq!(queue.len(), 3);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the queue contains no elements.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(5);
assert!(queue.is_empty());
queue.push(1);
assert!(!queue.is_empty());
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true
if the queue is full.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(5);
assert!(!queue.is_full());
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
assert!(queue.is_full());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the queue.
§Examples
use circular_queue::CircularQueue;
let queue: CircularQueue<i32> = CircularQueue::with_capacity(5);
assert_eq!(queue.capacity(), 5);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the queue.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(5);
queue.push(1);
queue.push(2);
queue.push(3);
queue.clear();
assert_eq!(queue.len(), 0);
Sourcepub fn push(&mut self, x: T) -> Popped<T>
pub fn push(&mut self, x: T) -> Popped<T>
Pushes a new element into the queue.
Once the capacity is reached, pushing new items will overwrite old ones.
In case an old value is overwritten, it will be returned.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
assert_eq!(queue.push(3), None);
assert_eq!(queue.push(4), Some(1));
assert_eq!(queue.len(), 3);
let mut iter = queue.iter();
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator over the queue’s contents.
The iterator goes from the most recently pushed items to the oldest ones.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
let mut iter = queue.iter();
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns a mutable iterator over the queue’s contents.
The iterator goes from the most recently pushed items to the oldest ones.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
let mut iter = queue.iter_mut();
assert_eq!(iter.next(), Some(&mut 4));
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 2));
Sourcepub fn asc_iter(&self) -> AscIter<'_, T>
pub fn asc_iter(&self) -> AscIter<'_, T>
Returns an ascending iterator over the queue’s contents.
The iterator goes from the least recently pushed items to the newest ones.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
let mut iter = queue.asc_iter();
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&4));
Sourcepub fn asc_iter_mut(&mut self) -> AscIterMut<'_, T>
pub fn asc_iter_mut(&mut self) -> AscIterMut<'_, T>
Returns a mutable ascending iterator over the queue’s contents.
The iterator goes from the least recently pushed items to the newest ones.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
let mut iter = queue.asc_iter_mut();
assert_eq!(iter.next(), Some(&mut 2));
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 4));
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Converts the queue into a Vec<T>
going from the most recently pushed items to the oldest
ones.
§Examples
use circular_queue::CircularQueue;
let mut queue = CircularQueue::with_capacity(3);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
let v = queue.into_vec();
assert_eq!(v, vec![4, 3, 2]);
Trait Implementations§
Source§impl<T: Clone> Clone for CircularQueue<T>
impl<T: Clone> Clone for CircularQueue<T>
Source§fn clone(&self) -> CircularQueue<T>
fn clone(&self) -> CircularQueue<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more