TwoSidedVec

Struct TwoSidedVec 

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

A simple ‘two sided’ vector, that can grow both forwards and backwards.

The front and the back can be viewed as seperate and independent vectors, with negative indexing accessing the back and positive indexing accessing the front. This allows you to append to the back without modifying positive indexes. Unless you actually need pushing to the back to appear to shift the front forward, like VecDeque does, this negative index system will probably be better for your situation.

Internally this allows a much simpler and faster implementation, since there’s only a single pointer to the middle that grows up and down. Internally, we have to reallocate the buffer if we run out of capacity in either the negative or positive difes grow separately and Although bounds checks are slightly slower since they involve two comparisons, the access itself should be just as fast.

Implementations§

Source§

impl<T> TwoSidedVec<T>

Source

pub fn new() -> Self

Source

pub fn with_capacity(back: usize, front: usize) -> Self

Source

pub fn front(&self) -> &[T]

Take a slice of the front of this queue

Source

pub fn back(&self) -> &[T]

Take a slice of the back of this queue

Source

pub fn front_mut(&mut self) -> &mut [T]

Take a mutable slice of the front of this queue

Source

pub fn back_mut(&mut self) -> &mut [T]

Take a mutable slice of the back of this queue

Source

pub fn split(&self) -> (&[T], &[T])

Take seperate slices of the back and the front of the vector respectively.

Source

pub fn split_mut(&mut self) -> (&mut [T], &mut [T])

Take seperate mutable slices of the back and front of the vector respectively.

Source

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

Source

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

Source

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

Source

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

Push the specified value into the front of this queue, without modifying its end or touching the front of the queue.

This effectively preserves all positive indexes, which may or may not be useful for your situation.

Source

pub fn reserve_back(&mut self, amount: usize)

Source

pub fn reserve_front(&mut self, amount: usize)

Source

pub fn capacity_back(&self) -> usize

Source

pub fn capacity_front(&self) -> usize

Source

pub fn len(&self) -> usize

Return the length of the entire vector, which is the sum of the lengths of the front and back parts.

The length isn’t where the vector ends, since it could have elements in the back with negative indexes. Use vec.start() and vec.end() if you want to know the start and end indexes. The total length is exactly equivalent to vec.len_back() + vec.len_front()

Source

pub fn is_empty(&self) -> bool

Source

pub fn len_back(&self) -> usize

Return the length of the back of the vector.

Source

pub fn len_front(&self) -> usize

Return the length of the front of the vector

Source

pub fn start(&self) -> isize

Give the (inclusive) start of the queue’s elements. which may be negative if the queue’s back isn’t empty

This is exactly equivelant to -vec.back().len().

Source

pub fn end(&self) -> isize

Give the (exclusive) end of the queue’s elements, which may be less than the length if the queue’s back contains some elements.

This is exactly equivalent to vec.front().len()

Source

pub fn range(&self) -> Range<isize>

Return the [start, end) range of the element indices, equivalent to a tuple of (queue.start(), queue.end()).

Source

pub fn iter_entire(&self) -> Iter<'_, T>

Iterate over the entire vector, including both the back and front.

Source

pub fn get<I: TwoSidedIndex<T>>(&self, index: I) -> Option<&I::Output>

Source

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

Source

pub unsafe fn get_unchecked<I: TwoSidedIndex<T>>(&self, index: I) -> &I::Output

Get a reference to value at the specified index

§Safety

Undefined behavior if the index is out of bounds

Source

pub unsafe fn get_unchecked_mut<I: TwoSidedIndex<T>>( &mut self, index: I, ) -> &mut I::Output

Get a mutable reference to value at the specified index

§Safety

Undefined behavior if the index is out of bounds

Source

pub fn start_ptr(&self) -> *mut T

Give a raw pointer to the start of the elements

Source

pub fn middle_ptr(&self) -> *mut T

Give a raw pointer to the middle of the elements

Source

pub fn end_ptr(&self) -> *mut T

Source

pub fn split_at(&self, index: isize) -> (&[T], &[T])

Source

pub fn clear(&mut self)

Source

pub fn enumerate_back(&self) -> SignedEnumerate<Iter<'_, T>>

Enumerate the indices and values of the elements in the back of the vector.

The primary advantage over regular enumeration is that it gives proper negative indices since the elements are in the back.

Source

pub fn enumerate_front(&self) -> SignedEnumerate<Iter<'_, T>>

Enumerate the indices and values of the elements in the front of the vector.

The only possible advantage over regular enumeration is that it gives positive isize indices for consistency with enumeration over the back.

Source

pub fn enumerate(&self) -> SignedEnumerate<Iter<'_, T>>

Enumerate the indices and values of each element in the front and back.

The primary advantage over regular enumeration is that it gives proper negative indices for elements that are in the back.

Source

pub fn enumerate_mut(&mut self) -> SignedEnumerate<IterMut<'_, T>>

Mutably enumerate the indices and values of each element in the front and back.

The primary advantage over regular enumeration is that it gives proper negative indices for elements that are in the back.

Source

pub fn truncate_back(&mut self, len: usize)

Source

pub fn truncate_front(&mut self, len: usize)

Source

pub fn retain<F: FnMut(isize, &mut T) -> bool>(&mut self, pred: F)

Source

pub fn retain_back<F: FnMut(isize, &mut T) -> bool>(&mut self, pred: F)

Source

pub fn retain_front<F: FnMut(isize, &mut T) -> bool>(&mut self, pred: F)

Source

pub fn drain_filter_back<F: FnMut(isize, &mut T) -> bool>( &mut self, pred: F, ) -> DrainFilterBack<'_, T, F>

Source

pub fn drain_filter_front<F: FnMut(isize, &mut T) -> bool>( &mut self, pred: F, ) -> DrainFilterFront<'_, T, F>

Trait Implementations§

Source§

impl<T: Clone> Clone for TwoSidedVec<T>

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> Debug for TwoSidedVec<T>

Source§

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

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

impl<T> Default for TwoSidedVec<T>

Source§

fn default() -> Self

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

impl<T> Drop for TwoSidedVec<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<Vec<T>> for TwoSidedVec<T>

Source§

fn from(original: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for TwoSidedVec<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, I: TwoSidedIndex<T>> Index<I> for TwoSidedVec<T>

Source§

type Output = <I as TwoSidedIndex<T>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &I::Output

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

impl<T, I: TwoSidedIndex<T>> IndexMut<I> for TwoSidedVec<T>

Source§

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

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

impl<T: PartialEq<U>, U> PartialEq<TwoSidedVec<U>> for TwoSidedVec<T>

Source§

fn eq(&self, other: &TwoSidedVec<U>) -> 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<'a, T: Copy + 'a> TwoSidedExtend<&'a T> for TwoSidedVec<T>

Source§

fn extend_back<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)

Source§

fn extend_front<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)

Source§

impl<T> TwoSidedExtend<T> for TwoSidedVec<T>

Source§

fn extend_back<I: IntoIterator<Item = T>>(&mut self, iter: I)

Source§

fn extend_front<I: IntoIterator<Item = T>>(&mut self, iter: I)

Source§

impl<T: Eq> Eq for TwoSidedVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for TwoSidedVec<T>

§

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

§

impl<T> !Send for TwoSidedVec<T>

§

impl<T> !Sync for TwoSidedVec<T>

§

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

§

impl<T> UnwindSafe for TwoSidedVec<T>

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.