SegmentArray

Struct SegmentArray 

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

Append-only growable array that uses a list of progressivly larger segments to avoid the allocate-and-copy that many growable data structures typically employ.

Implementations§

Source§

impl<T> SegmentArray<T>

Source

pub fn new() -> Self

Return an empty segment array with zero capacity.

Note that pre-allocating capacity has no benefit with this data structure since append operations are always constant time and no reallocation and copy is ever performed.

Source

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

Appends an element to the back of a collection.

§Panics

Panics if a new segment is allocated that would exceed isize::MAX bytes.

§Time complexity

Constant time.

Source

pub fn push_within_capacity(&mut self, value: T) -> Result<(), T>

Appends an element if there is sufficient spare capacity, otherwise an error is returned with the element.

§Time complexity

Constant time.

Source

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

Removes the last element from a vector and returns it, or None if it is empty.

§Time complexity

Constant time.

Source

pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T>

Removes and returns the last element from a vector if the predicate returns true, or None if the predicate returns false or the vector is empty (the predicate will not be called in that case).

§Time complexity

Constant time.

Source

pub fn len(&self) -> usize

Return the number of elements in the array.

§Time complexity

Constant time.

Source

pub fn capacity(&self) -> usize

Returns the total number of elements the segment array can hold without reallocating.

§Time complexity

Constant time.

Source

pub fn is_empty(&self) -> bool

Returns true if the array has a length of 0.

§Time complexity

Constant time.

Source

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

Retrieve a reference to the element at the given offset.

§Time complexity

Constant time.

Source

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

Returns a mutable reference to an element.

§Time complexity

Constant time.

Source

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

Removes an element from the vector and returns it.

The removed element is replaced by the last element of the vector.

This does not preserve ordering of the remaining elements.

§Time complexity

Constant time.

Source

pub fn iter(&self) -> SegArrayIter<'_, T>

Returns an iterator over the segment array.

The iterator yields all items from start to end.

Source

pub fn clear(&mut self)

Clears the segment array, removing and dropping all values.

Note that this method has no effect on the allocated capacity of the segment array.

Trait Implementations§

Source§

impl<T> Default for SegmentArray<T>

Source§

fn default() -> Self

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

impl<T> Display for SegmentArray<T>

Source§

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

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

impl<T> Drop for SegmentArray<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<A> FromIterator<A> for SegmentArray<A>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T> Index<usize> for SegmentArray<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 SegmentArray<T>

Source§

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

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

impl<T> IntoIterator for SegmentArray<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = SegArrayIntoIter<<SegmentArray<T> as IntoIterator>::Item>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SegmentArray<T>

§

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

§

impl<T> !Send for SegmentArray<T>

§

impl<T> !Sync for SegmentArray<T>

§

impl<T> Unpin for SegmentArray<T>

§

impl<T> UnwindSafe for SegmentArray<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.