Ring

Struct Ring 

Source
pub struct Ring<T> { /* private fields */ }
Expand description

Double-ended queue implemented with a vector that reuses space.

Implementations§

Source§

impl<T> Ring<T>
where T: Sized,

Source

pub fn new() -> Ring<T>

Create a new empty Ring.

Source

pub fn with_capacity(capacity: usize) -> Ring<T>

Create a new empty Ring with a predefined capacity.

Source

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);
Source

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);
Source

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]);
Source

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]);
Source

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]);
Source

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));
Source

pub fn len(&self) -> usize

Returns the length of the ring.

Source

pub fn capacity(&self) -> usize

Returns the current capacity of the ring.

Source

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);
Source

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]);
Source

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]);
Source

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]);
Source

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);
Source

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§

Source§

impl<T> Clone for Ring<T>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Copy> Debug for Ring<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> FromIterator<T> for Ring<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<isize> for Ring<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, idx: isize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Ring<T>
where T: Copy,

Source§

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
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

impl<'a, T> IntoIterator for &'a mut Ring<T>

Source§

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
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<T>

Which kind of iterator are we turning this into?
Source§

impl<T: PartialEq> PartialEq for Ring<T>

Source§

fn eq(&self, other: &Ring<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for Ring<T>

Auto Trait Implementations§

§

impl<T> Freeze for Ring<T>

§

impl<T> RefUnwindSafe for Ring<T>
where T: RefUnwindSafe,

§

impl<T> Send for Ring<T>
where T: Send,

§

impl<T> Sync for Ring<T>
where T: Sync,

§

impl<T> Unpin for Ring<T>
where T: Unpin,

§

impl<T> UnwindSafe for Ring<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.