Struct ring_queue::Ring

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

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

Implementations

Create a new empty Ring.

Create a new empty Ring with a predefined capacity.

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

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

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

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

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

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

Returns the length of the ring.

Returns the current capacity of the ring.

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

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

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

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

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

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Creates a value from an iterator. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more

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
The type of the elements being iterated over.
Which kind of iterator are we turning this into?

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
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.