pub struct ResizingVec<T> { /* private fields */ }
Implementations§
Source§impl<T> ResizingVec<T>
impl<T> ResizingVec<T>
pub fn new() -> Self
Sourcepub fn prefill(capacity: usize) -> Self
pub fn prefill(capacity: usize) -> Self
Pre allocates enough space so that elements with ids < reserved_space()
fit without having to resize
Sourcepub fn reserved_space(&self) -> usize
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
Sourcepub fn filled(&self) -> usize
pub fn filled(&self) -> usize
Returns the amount of active values.
filled() <= reserved_space()
will always hold true.
Sourcepub fn get_mut(&mut self, idx: usize) -> Option<&mut T>
pub fn get_mut(&mut self, idx: usize) -> Option<&mut T>
Returns a mutable reference to element at the given index
pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> + '_
Sourcepub fn remove(&mut self, idx: usize) -> Option<T>
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
Sourcepub fn insert(&mut self, idx: usize, t: T) -> Option<T>
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.
Sourcepub fn resize(&mut self) -> Vec<Position>
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>
impl<T: Clone> Clone for ResizingVec<T>
Source§fn clone(&self) -> ResizingVec<T>
fn clone(&self) -> ResizingVec<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more