Struct ResizingVec

Source
pub struct ResizingVec<T> { /* private fields */ }

Implementations§

Source§

impl<T> ResizingVec<T>

Source

pub fn new() -> Self

Source

pub fn prefill(capacity: usize) -> Self

Pre allocates enough space so that elements with ids < reserved_space() fit without having to resize

Source

pub fn reserved_space(&self) -> usize

Returns the amount of space length the inner vector has. Creating a fresh ResizingVec and then inserting elements at index 0 & 2 would result in reserved_space() returning 3 as at position 1 would be an empty value

Source

pub fn filled(&self) -> usize

Returns the amount of active values. filled() <= reserved_space() will always hold true.

Source

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

Returns the element at the given index

Source

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

Returns a mutable reference to element at the given index

Source

pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> + '_

Source

pub fn remove(&mut self, idx: usize) -> Option<T>

Removes the element at the given index and returns the remove element. If the given index is out of bounds than None is being returned

Source

pub fn insert(&mut self, idx: usize, t: T) -> Option<T>

Inserts the element at the given index. IMPORTANT: The time complexity of this operation depends on whether it has to resize or not.

if reserved_space() < idx then insert will first insert (idx - reserved_space()) elements before inserting the given element at the index.

Source

pub fn clear(&mut self)

Clears the vector, removing all values

Source

pub fn resize(&mut self) -> Vec<Position>

Resizes the vector shrinks it so that every reserved space is being occupied by an element.

§Examples
use resizing_vec::{ResizingVec, Position};

let mut r_vec = ResizingVec::new();
r_vec.insert(5, "6th elem".to_string());
// ResizingVec { vec: [None, None, None, None, None, Some("5th elem")], active: 1 }
assert_eq!(6, r_vec.reserved_space());
assert_eq!(1, r_vec.filled());

let new_positions = r_vec.resize();
// ResizingVec { vec: [Some("5th elem")], active: 1 }
assert_eq!(new_positions, [Position {prev_idx: 5, new_idx: 0}]);
assert_eq!(1, r_vec.reserved_space());
assert_eq!(1, r_vec.filled());

Trait Implementations§

Source§

impl<T: Clone> Clone for ResizingVec<T>

Source§

fn clone(&self) -> ResizingVec<T>

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 ResizingVec<T>

Source§

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

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

impl<T> Default for ResizingVec<T>

Source§

fn default() -> Self

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

impl<T> From<Vec<T>> for ResizingVec<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> Index<usize> for ResizingVec<T>

Source§

type Output = Option<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 ResizingVec<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 ResizingVec<T>

§

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

§

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

§

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

§

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

§

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