Struct ring_queue::Ring
source · pub struct Ring<T> { /* private fields */ }
Expand description
Double-ended queue implemented with a vector that reuses space.
Implementations
sourceimpl<T> Ring<T>where
T: Sized,
impl<T> Ring<T>where
T: Sized,
sourcepub fn with_capacity(capacity: usize) -> Ring<T>
pub fn with_capacity(capacity: usize) -> Ring<T>
Create a new empty Ring
with a predefined capacity.
sourcepub fn pop_left(&mut self) -> Option<T>
pub fn pop_left(&mut self) -> Option<T>
Removes the value on the left side of the ring, that is, the head. It will return the popped value, if there was any.
Example
use ring_queue::Ring;
let mut r = ring![1, 2];
assert_eq!(r.pop_left(), Some(1));
assert_eq!(r.pop_left(), Some(2));
assert_eq!(r.pop_left(), None);
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the value on the right side of the ring, that is, the tail. It will return the popped value, if there was any.
Example
use ring_queue::Ring;
let mut r = ring![1, 2];
assert_eq!(r.pop(), Some(2));
assert_eq!(r.pop(), Some(1));
assert_eq!(r.pop(), None);
sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Inserts a value at the right side of the queue, that is, at the tail.
Example
use ring_queue::Ring;
let mut r = ring![1, 2, 3];
r.push(4);
assert_eq!(r.collect_vec(), vec![1, 2, 3, 4]);
sourcepub fn push_left(&mut self, value: T)
pub fn push_left(&mut self, value: T)
Inserts a value at the left side of the queue, that is, at the head.
Example
use ring_queue::Ring;
let mut r = ring![1, 2, 3];
r.push_left(0);
assert_eq!(r.collect_vec(), vec![0, 1, 2, 3]);
sourcepub fn rotate(&mut self, n: isize) -> &mut Self
pub fn rotate(&mut self, n: isize) -> &mut Self
Rotate the deque n
steps to the right. If n
is negative, rotate to
the left.
Example
use ring_queue::Ring;
let mut r = ring![1, 2, 3, 4, 5];
r.rotate(1);
assert_eq!(r.collect_vec(), vec![5, 1, 2, 3, 4]);
r.rotate(-2);
assert_eq!(r.collect_vec(), vec![2, 3, 4, 5, 1]);
sourcepub fn peek(&self, n: isize) -> Option<&T>
pub fn peek(&self, n: isize) -> Option<&T>
Retrieve the element n
steps to the right from the head. If n
is
negative, the element n
steps to the left from the head.
This method can only be used if the element type implements the Copy
trait.
Example
use ring_queue::Ring;
let r = ring![1, 2, 3, 4, 5];
assert_eq!(r.peek(0), Some(&1));
assert_eq!(r.peek(1), Some(&2));
assert_eq!(r.peek(-1), Some(&5));
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the ring, removing all values. Note that this method has no effect on its allocated capacity.
Example
use ring_queue::Ring;
let mut r = Ring::with_capacity(5);
r.push(1);
r.clear();
assert_eq!(r.len(), 0);
assert_eq!(r.capacity(), 5);
sourcepub fn append(&mut self, other: &mut Ring<T>)
pub fn append(&mut self, other: &mut Ring<T>)
Moves all the elements of other
into the right of Self
, leaving
other
empty.
Example
use ring_queue::Ring;
let mut r = ring![1, 2, 3];
r.append(&mut ring![4, 5, 6]);
assert_eq!(r.collect_vec(), vec![1, 2, 3, 4, 5, 6]);
sourcepub fn append_left(&mut self, other: &mut Ring<T>)
pub fn append_left(&mut self, other: &mut Ring<T>)
Moves all the elements of other
into the left of Self
, leaving
other
empty.
Note that elements will be appended to the left in reverse with the
same order they had in the other ring. That means they will be, in
fact, in reverse order.
Example
use ring_queue::Ring;
let mut r = ring![4, 5, 6];
r.append_left(&mut ring![3, 2, 1]);
assert_eq!(r.collect_vec(), vec![1, 2, 3, 4, 5, 6]);
sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the elements in the ring.
Example
use ring_queue::Ring;
let mut r = ring![1, 2, 3];
r.reverse();
assert_eq!(r.collect_vec(), vec![3, 2, 1]);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the ring is empty or not.
Example
use ring_queue::Ring;
assert_eq!(ring![1, 2, 3].is_empty(), false);
assert_eq!(Ring::<i32>::new().is_empty(), true);
sourcepub fn collect_vec(&self) -> Vec<T>where
T: Copy,
pub fn collect_vec(&self) -> Vec<T>where
T: Copy,
Return all the elements inside the ring as a vector. In order to use
this method, the element type needs to implement the Copy
trait.
Example
use ring_queue::Ring;
let r = ring![1, 2, 3];
assert_eq!(r.collect_vec(), vec![1, 2, 3]);
Trait Implementations
sourceimpl<T> FromIterator<T> for Ring<T>
impl<T> FromIterator<T> for Ring<T>
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
sourceimpl<'a, T> IntoIterator for &'a Ring<T>where
T: Copy,
impl<'a, T> IntoIterator for &'a Ring<T>where
T: Copy,
sourcefn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator that will not consume the current ring. It will, instead, copy the elements one by one to the iterator.
Examples
use ring_queue::Ring;
let mut r = Ring::new();
r.push(1);
r.push(2);
for i in r.into_iter() {
println!("{}", i);
}
// r is not empty now
type Item = T
type Item = T
sourceimpl<'a, T> IntoIterator for &'a mut Ring<T>
impl<'a, T> IntoIterator for &'a mut Ring<T>
sourcefn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates a consuming iterator, that is, one that moves each value out of the ring (from head to tail). The ring will be empty.
Examples
use ring_queue::Ring;
struct Foo { a: i32 }
let mut r = Ring::new();
r.push(Foo{a: 1});
r.push(Foo{a: 2});
for i in r.into_iter() {
println!("{}", i.a);
}
// r is empty now