Struct VecMap

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

An associative array that uses a Vec of Options to map usize keys to elements.

Implementations§

Source§

impl<T> VecMap<T>

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 capacity(&self) -> usize

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

§Examples
let mut map = VecMap::<()>::new();
assert_eq!(map.capacity(), 0);
map.reserve(10);
assert!(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 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_eq!(map.len(), 0);
Source

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

Reserves capacity for at least additional more elements to be inserted in this 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 get(&self, i: usize) -> Option<&T>

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));
assert!(map.get(3).is_none());
Source

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

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.get(idx), Some(&124));
Source

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

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), Some(123));
assert!(map.insert(0, 123).is_none());
assert_eq!(map.get(idx), Some(&456));
Source

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

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(usize, &mut T) -> 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), Some(&3));
assert_eq!(map.len(), 1);
Source

pub fn iter( &self, ) -> FilterMap<Enumerate<Iter<'_, Option<T>>>, fn((usize, &Option<T>)) -> Option<(usize, &T)>>

Returns an iterator over this map.

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

let mut count = 0;
for (i, value) in map.iter() {
    assert_eq!(i, count * 2);
    count += 1;
}
assert_eq!(count, 10);
Source

pub fn iter_mut( &mut self, ) -> FilterMap<Enumerate<IterMut<'_, Option<T>>>, fn((usize, &mut Option<T>)) -> Option<(usize, &mut T)>>

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 * 2, i * 2);
}

let mut count = 0;
for (i, value) in map.iter_mut() {
    *value += 1;
    assert_eq!(i, count * 2);
    assert_eq!(*value, count * 2 + 1);
    count += 1;
}
assert_eq!(count, 10);

Trait Implementations§

Source§

impl<T> Clear for VecMap<T>

Source§

fn clear(&mut self)

Clears self, removing all values.
Source§

impl<T: Clone> Clone for VecMap<T>

Source§

fn clone(&self) -> VecMap<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 VecMap<T>

Source§

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

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

impl<T: Default> Default for VecMap<T>

Source§

fn default() -> VecMap<T>

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

impl<'de, T: Deserialize<'de>> Deserialize<'de> for VecMap<T>

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, T: Clone + 'a, I: Borrow<usize> + 'a> Extend<(I, &'a T)> for VecMap<T>

Source§

fn extend<It: IntoIterator<Item = (I, &'a T)>>(&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<T> Extend<(usize, T)> for VecMap<T>

Source§

fn extend<It: IntoIterator<Item = (usize, T)>>(&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, T: Clone + 'a, I: Borrow<usize> + 'a> FromIterator<(I, &'a T)> for VecMap<T>

Source§

fn from_iter<It: IntoIterator<Item = (I, &'a T)>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<(usize, T)> for VecMap<T>

Source§

fn from_iter<It: IntoIterator<Item = (usize, T)>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for VecMap<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

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

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

impl<'a, T> IntoIterator for &'a VecMap<T>

Source§

type Item = (usize, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = FilterMap<Enumerate<Iter<'a, Option<T>>>, fn((usize, &Option<T>)) -> Option<(usize, &T)>>

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, T> IntoIterator for &'a mut VecMap<T>

Source§

type Item = (usize, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = FilterMap<Enumerate<IterMut<'a, Option<T>>>, fn((usize, &mut Option<T>)) -> Option<(usize, &mut T)>>

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<T> IntoIterator for VecMap<T>

Source§

type Item = (usize, T)

The type of the elements being iterated over.
Source§

type IntoIter = FilterMap<Enumerate<IntoIter<Option<T>>>, fn((usize, Option<T>)) -> Option<(usize, T)>>

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<T> Len for VecMap<T>

Source§

fn len(&self) -> usize

Returns the number of elements in the collection.
Source§

fn is_empty(&self) -> bool

Returns true if the collection contains no elements.
Source§

impl<T> Map for VecMap<T>

Source§

type Key = usize

Key type
Source§

type Value = T

Value type
Source§

impl<T> MapGet<usize> for VecMap<T>

Source§

fn get(&self, key: &usize) -> Option<&Self::Value>

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

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

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

impl<T> MapInsert for VecMap<T>

Source§

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

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

impl<T> MapMut<usize> for VecMap<T>

Source§

fn get_mut(&mut self, key: &usize) -> Option<&mut Self::Value>

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

fn remove(&mut self, key: &usize) -> Option<Self::Value>

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

impl<T: Ord> Ord for VecMap<T>

Source§

fn cmp(&self, other: &VecMap<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for VecMap<T>

Source§

fn eq(&self, other: &VecMap<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for VecMap<T>

Source§

fn partial_cmp(&self, other: &VecMap<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Retain for VecMap<T>

Source§

type Key = usize

Key type
Source§

type Value = T

Value type
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<T: Serialize> Serialize for VecMap<T>

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
Source§

impl<T: Eq> Eq for VecMap<T>

Source§

impl<T> StructuralPartialEq for VecMap<T>

Auto Trait Implementations§

§

impl<T> Freeze for VecMap<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for VecMap<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> IntoDowncast<Box<dyn Any>> for T
where T: Any,

Source§

fn into(self) -> Box<dyn Any>

Converts self into Downcast type.
Source§

impl<T> IntoDowncast<Box<dyn Any + Send>> for T
where T: Any + Send,

Source§

fn into(self) -> Box<dyn Any + Send>

Converts self into Downcast type.
Source§

impl<T> IntoDowncast<Box<dyn Any + Sync + Send>> for T
where T: Any + Send + Sync,

Source§

fn into(self) -> Box<dyn Any + Sync + Send>

Converts self into Downcast type.
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>,