VecMap

Struct VecMap 

Source
pub struct VecMap<V, I: UnsignedNum = usize> { /* private fields */ }
Expand description

The VecMap is a type of associative array that uses a Vec of Options to map unsigned integer keys to elements.

Implementations§

Source§

impl<V, I: UnsignedNum> VecMap<V, I>

Source

pub fn new() -> Self

Constructs a new, empty VecMap. It will not allocate until elements are pushed onto it.

§Examples
let map = VecMap::<()>::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty VecMap with the specified capacity. It will be able to hold exactly capacity elements without reallocating. If capacity is 0, it will not allocate.

§Panic

Panics if the capacity overflows.

§Examples
let map = VecMap::<()>::with_capacity(10);
assert_eq!(map.capacity(), 10);
Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

§Examples
let map = VecMap::<()>::with_capacity(10);
assert_eq!(map.capacity(), 10);
Source

pub fn len(&self) -> usize

Returns the number of elements in the map, also referred to as its ‘length’.

§Examples
let mut map = VecMap::<()>::new();
assert_eq!(map.len(), 0);
map.insert(1, ());
assert_eq!(map.len(), 1);
Source

pub fn get(&self, i: &I) -> Option<&V>

Returns a reference to the value corresponding to the index i .

§Examples
let mut map = VecMap::<i32>::new();
let idx = 2;
map.insert(idx, 123);

assert_eq!(map.get(&idx), Some(&123));
map.remove(&idx);
assert!(map.get(&idx).is_none());
Source

pub fn get_mut(&mut self, i: &I) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the index i .

§Examples
let mut map = VecMap::<i32>::new();
let idx = 1;
map.insert(idx, 123);

*map.get_mut(&idx).unwrap() += 1;
assert_eq!(map.remove(&idx), Some(124));
assert!(map.get_mut(&idx).is_none());
Source

pub fn clear(&mut self)

Clears the map, removing all values. Note that this method has no effect on the allocated capacity of the map.

§Examples
let mut map = VecMap::<()>::new();
map.insert(1, ());
map.clear();
assert!(map.len() == 0);
Source

pub fn insert(&mut self, i: I, v: V) -> Option<V>

Inserts value into the map, allocating more capacity if necessary. The existing key-value in the map is returned.

§Panics

Panics if the capacity overflows.

§Examples
let mut map = VecMap::<i32>::new();
let idx = 1;
assert!(map.insert(idx, 123).is_none());
assert_eq!(map.insert(idx, 456).unwrap(), 123);
assert!(map.insert(0, 123).is_none());
assert_eq!(*map.get(&idx).unwrap(), 456);
Source

pub fn remove(&mut self, i: &I) -> Option<V>

Removes and returns the element at index i from the map if exists.

§Examples
let mut map = VecMap::<i32>::new();
map.insert(1, 123);
assert_eq!(map.remove(&1), Some(123));
assert_eq!(map.remove(&1), None);
Source

pub fn retain(&mut self, f: impl FnMut(&I, &mut V) -> bool)

Retains only the elements specified by the predicate, passing a mutable reference to it. In other words, removes all elements such that f(index, &value) returns false.

§Examples
let mut map = VecMap::<i32>::new();
map.insert(1, 1);
map.insert(0, 2);
map.retain(|_, val| { if *val == 1 { *val = 3; true } else { false } });
assert_eq!(*map.get(&1).unwrap(), 3);
assert!(map.get(&0).is_none());
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given map. The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the capacity overflows.

§Examples
let mut map = VecMap::<()>::new();
map.reserve(10);
assert!(map.capacity() >= 10);
Source

pub fn iter(&self) -> Iter<'_, V, I>

Returns an iterator over the map.

§Examples
let mut map = VecMap::<usize>::new();
for i in 0..10 {
    map.insert(i, i * i);
}

for (idx, value) in &map {
    println!("{} is at index {:?}", value, idx);
}
Source

pub fn iter_mut(&mut self) -> IterMut<'_, V, I>

Returns an iterator that allows modifying each value over this map.

§Examples
let mut map = VecMap::<usize>::new();
for i in 0..10 {
    map.insert(i, i * i);
}

for (_, value) in &mut map {
    *value += 5;
}

Trait Implementations§

Source§

impl<V: Clone, I: Clone + UnsignedNum> Clone for VecMap<V, I>

Source§

fn clone(&self) -> VecMap<V, I>

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<V: Debug, I: Debug + UnsignedNum> Debug for VecMap<V, I>

Source§

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

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

impl<V, I: UnsignedNum> Default for VecMap<V, I>

Source§

fn default() -> Self

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

impl<'de, V, I> Deserialize<'de> for VecMap<V, I>
where V: Deserialize<'de>, I: UnsignedNum,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, V, I> Extend<(&'a I, &'a V)> for VecMap<V, I>
where I: UnsignedNum + 'a, V: Copy + 'a,

Source§

fn extend<It: IntoIterator<Item = (&'a I, &'a V)>>(&mut self, iter: It)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, V, I> Extend<(I, &'a V)> for VecMap<V, I>
where I: UnsignedNum + 'a, V: Copy + 'a,

Source§

fn extend<It: IntoIterator<Item = (I, &'a V)>>(&mut self, iter: It)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<V, I: UnsignedNum> Extend<(I, V)> for VecMap<V, I>

Source§

fn extend<It: IntoIterator<Item = (I, V)>>(&mut self, iter: It)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<V, I: UnsignedNum> FromIterator<(I, V)> for VecMap<V, I>

Source§

fn from_iter<It: IntoIterator<Item = (I, V)>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<V, I: UnsignedNum> Index<I> for VecMap<V, I>

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<V, I: UnsignedNum> IndexMut<I> for VecMap<V, I>

Source§

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

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

impl<'a, V, I: UnsignedNum> IntoIterator for &'a VecMap<V, I>

Source§

type Item = (I, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, V, I>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, V, I: UnsignedNum> IntoIterator for &'a mut VecMap<V, I>

Source§

type Item = (I, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, V, I>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V, I: UnsignedNum> IntoIterator for VecMap<V, I>

Source§

type Item = (I, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<V, I>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V, I: UnsignedNum> Map for VecMap<V, I>

Source§

type Key = I

Key type
Source§

type Value = V

Value type
Source§

fn len(&self) -> usize

Returns the number of elements in the map, also referred to as its ‘length’.
Source§

fn get(&self, i: &Self::Key) -> Option<&Self::Value>

Returns a reference to the value corresponding to the key if exists.
Source§

fn contains_key(&self, key: &Self::Key) -> bool

Returns true if the map contains a value for the key.
Source§

fn is_empty(&self) -> bool

Returns true if the map contains no elements.
Source§

impl<V, I: UnsignedNum> MapMut for VecMap<V, I>

Source§

fn clear(&mut self)

Clears the map, removing all values.
Source§

fn get_mut(&mut self, i: &Self::Key) -> Option<&mut Self::Value>

Returns a mutable reference to the value corresponding to the key if exists.
Source§

fn insert(&mut self, i: Self::Key, v: Self::Value) -> Option<Self::Value>

Inserts value into the map. The existing value in the map is returned.
Source§

fn remove(&mut self, i: &Self::Key) -> Option<Self::Value>

Removes and returns the element at key from the map if exists.
Source§

fn retain(&mut self, f: impl FnMut(&Self::Key, &mut Self::Value) -> bool)

Retains only the elements specified by the predicate, passing a mutable reference to it. In other words, removes all elements such that f(&index, &mut value) returns false.
Source§

impl<V, I> Serialize for VecMap<V, I>
where V: Serialize, I: UnsignedNum,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<V, I> Freeze for VecMap<V, I>

§

impl<V, I> RefUnwindSafe for VecMap<V, I>

§

impl<V, I> Send for VecMap<V, I>
where I: Send, V: Send,

§

impl<V, I> Sync for VecMap<V, I>
where I: Sync, V: Sync,

§

impl<V, I> Unpin for VecMap<V, I>
where I: Unpin, V: Unpin,

§

impl<V, I> UnwindSafe for VecMap<V, I>
where I: UnwindSafe, V: 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,