CyclicArray

Struct CyclicArray 

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

Basic circular buffer, or what Goodrich and Kloss call a circular deque.

This implementation allows push and pop from both ends of the buffer and supports insert and remove from arbitrary offsets.

Unlike the VecDeque in the standard library, this array has a fixed size and will panic if a push is performed while the array is already full.

Implementations§

Source§

impl<T> CyclicArray<T>

Source

pub fn new(capacity: usize) -> Self

Construct a new cyclic array with the given capacity.

Source

pub fn combine(a: CyclicArray<T>, b: CyclicArray<T>) -> Self

Take the elements from the two other cyclic arrays into a new cyclic array with the combined capacity.

Source

pub fn from(capacity: usize, other: CyclicArray<T>) -> Self

Take the elements from the other cyclic array into a new cyclic array with the given capacity.

Source

pub fn split(self) -> (CyclicArray<T>, CyclicArray<T>)

Split this cyclic buffer into two equal sized buffers.

The second buffer may be empty if all elements fit within the first buffer.

Source

pub fn push_back(&mut self, value: T)

Appends an element to the back of the cyclic array.

§Panic

Panics if the buffer is already full.

Source

pub fn push_front(&mut self, value: T)

Prepends an element to the front of the cyclic array.

§Panic

Panics if the buffer is already full.

Source

pub fn pop_back(&mut self) -> Option<T>

Removes the last element and returns it, or None if the cyclic array is empty.

Source

pub fn pop_front(&mut self) -> Option<T>

Removes the first element and returns it, or None if the cyclic array is empty.

Source

pub fn insert(&mut self, index: usize, value: T)

Inserts an element at position index within the array, possibly shifting some elements to the left or the right as needed.

Source

pub fn remove(&mut self, index: usize) -> T

Removes and returns the element at position index within the array, shifting some elements to the left or to the right.

Source

pub fn get(&self, index: usize) -> Option<&T>

Provides a reference to the element at the given index.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Returns a mutable reference to an element.

Source

pub fn clear(&mut self)

Clears the cyclic array, removing and dropping all values.

Source

pub fn len(&self) -> usize

Return the number of elements in the array.

Source

pub fn capacity(&self) -> usize

Returns the total number of elements the cyclic array can hold.

Source

pub fn is_empty(&self) -> bool

Returns true if the array has a length of 0.

Source

pub fn is_full(&self) -> bool

Returns true if the array has a length equal to its capacity.

Trait Implementations§

Source§

impl<T> Default for CyclicArray<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Display for CyclicArray<T>

Source§

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

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

impl<T> Drop for CyclicArray<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Index<usize> for CyclicArray<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<T> IndexMut<usize> for CyclicArray<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

Auto Trait Implementations§

§

impl<T> Freeze for CyclicArray<T>

§

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

§

impl<T> !Send for CyclicArray<T>

§

impl<T> !Sync for CyclicArray<T>

§

impl<T> Unpin for CyclicArray<T>

§

impl<T> UnwindSafe for CyclicArray<T>
where T: RefUnwindSafe,

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.