pub struct LowMap<T> { /* private fields */ }
Expand description
A convenient wrapper around a Vec<Option<T>>
.
It is called a LowMap
because it maps a given index to a value, and this
index must be small as it will be used as the inner Vec
index.
let mut map = LowMap::new();
map.insert(0, "hey");
map.insert(2, "hoy");
map.insert(3, "foo");
map.insert(2, "bar");
assert_eq!(map.get(0), Some(&"hey"));
assert_eq!(map.get(1), None);
assert_eq!(map.get(2), Some(&"bar"));
assert_eq!(map.get(3), Some(&"foo"));
map[2] = "hoho";
assert_eq!(map.get(2), Some(&"hoho"));
Implementations§
Source§impl<T> LowMap<T>
impl<T> LowMap<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new empty LowMap<T>
with the given capacity
.
Sourcepub fn insert(&mut self, index: usize, value: T) -> Option<T>
pub fn insert(&mut self, index: usize, value: T) -> Option<T>
Inserts an element at position index
within the vector, returning the
previous value if present.
Sourcepub fn stack_insert(&mut self, index: usize, value: T) -> StackInsert<'_, T>
pub fn stack_insert(&mut self, index: usize, value: T) -> StackInsert<'_, T>
Constructs a wrapper that temporarily inserts an element.
The returned wrapper will undo the insert when dropped.
Sourcepub fn remove(&mut self, index: usize) -> Option<T>
pub fn remove(&mut self, index: usize) -> Option<T>
Removes and returns the element at position index
if present.
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at position index
if present.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at position index
if
present.
Sourcepub fn contains(&self, index: usize) -> bool
pub fn contains(&self, index: usize) -> bool
Tests whether an element is present at position index
or not.
Sourcepub fn push(&mut self, elem: T) -> usize
pub fn push(&mut self, elem: T) -> usize
Finds the first free index to store the given elem
.
Has a maxium complexity of O(n).
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
Returns a mutable iterator over the value of stored elements.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator over the index and value of stored elements.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns a mutable iterator over the index and value of stored elements.
Trait Implementations§
Source§impl<T> Extend<(usize, T)> for LowMap<T>
impl<T> Extend<(usize, T)> for LowMap<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (usize, T)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (usize, T)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)