pub struct VecMap<V> { /* private fields */ }
Expand description
A map optimized for small integer keys.
§Examples
use parry::utils::VecMap;
let mut months = VecMap::new();
months.insert(1, "Jan");
months.insert(2, "Feb");
months.insert(3, "Mar");
if !months.contains_key(12) {
println!("The end is near!");
}
assert_eq!(months.get(1), Some(&"Jan"));
if let Some(value) = months.get_mut(3) {
*value = "Venus";
}
assert_eq!(months.get(3), Some(&"Venus"));
// Print out all months
for (key, value) in &months {
println!("month {} is {}", key, value);
}
months.clear();
assert!(months.is_empty());
Implementations§
Source§impl<V> VecMap<V>
impl<V> VecMap<V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Source§impl<V> VecMap<V>
impl<V> VecMap<V>
Sourcepub fn reserve_len(&mut self, len: usize)
pub fn reserve_len(&mut self, len: usize)
Reserves capacity for the given VecMap
to contain len
distinct keys.
In the case of VecMap
this means reallocations will not occur as long
as all inserted keys are less than len
.
The collection may reserve more space to avoid frequent reallocations.
§Examples
use parry::utils::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len(10);
assert!(map.capacity() >= 10);
Sourcepub fn reserve_len_exact(&mut self, len: usize)
pub fn reserve_len_exact(&mut self, len: usize)
Reserves the minimum capacity for the given VecMap
to contain len
distinct keys.
In the case of VecMap
this means reallocations will not occur as long as all inserted
keys are less than len
.
Note that the allocator may give the collection more space than it requests.
Therefore capacity cannot be relied upon to be precisely minimal. Prefer
reserve_len
if future insertions are expected.
§Examples
use parry::utils::VecMap;
let mut map: VecMap<&str> = VecMap::new();
map.reserve_len_exact(10);
assert!(map.capacity() >= 10);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Sourcepub fn keys(&self) -> Keys<'_, V>
pub fn keys(&self) -> Keys<'_, V>
Returns an iterator visiting all keys in ascending order of the keys.
The iterator’s element type is usize
.
Sourcepub fn values(&self) -> Values<'_, V>
pub fn values(&self) -> Values<'_, V>
Returns an iterator visiting all values in ascending order of the keys.
The iterator’s element type is &'r V
.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, V>
Returns an iterator visiting all values in ascending order of the keys.
The iterator’s element type is &'r mut V
.
Sourcepub fn iter(&self) -> Iter<'_, V>
pub fn iter(&self) -> Iter<'_, V>
Returns an iterator visiting all key-value pairs in ascending order of the keys.
The iterator’s element type is (usize, &'r V)
.
§Examples
use parry::utils::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.insert(3, "c");
map.insert(2, "b");
// Print `1: a` then `2: b` then `3: c`
for (key, value) in map.iter() {
println!("{}: {}", key, value);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, V>
pub fn iter_mut(&mut self) -> IterMut<'_, V>
Returns an iterator visiting all key-value pairs in ascending order of the keys,
with mutable references to the values.
The iterator’s element type is (usize, &'r mut V)
.
§Examples
use parry::utils::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "c");
for (key, value) in map.iter_mut() {
*value = "x";
}
for (key, value) in &map {
assert_eq!(value, &"x");
}
Sourcepub fn append(&mut self, other: &mut Self)
pub fn append(&mut self, other: &mut Self)
Moves all elements from other
into the map while overwriting existing keys.
§Examples
use parry::utils::VecMap;
let mut a = VecMap::new();
a.insert(1, "a");
a.insert(2, "b");
let mut b = VecMap::new();
b.insert(3, "c");
b.insert(4, "d");
a.append(&mut b);
assert_eq!(a.len(), 4);
assert_eq!(b.len(), 0);
assert_eq!(a[1], "a");
assert_eq!(a[2], "b");
assert_eq!(a[3], "c");
assert_eq!(a[4], "d");
Sourcepub fn split_off(&mut self, at: usize) -> Self
pub fn split_off(&mut self, at: usize) -> Self
Splits the collection into two at the given key.
Returns a newly allocated Self
. self
contains elements [0, at)
,
and the returned Self
contains elements [at, max_key)
.
Note that the capacity of self
does not change.
§Examples
use parry::utils::VecMap;
let mut a = VecMap::new();
a.insert(1, "a");
a.insert(2, "b");
a.insert(3, "c");
a.insert(4, "d");
let b = a.split_off(3);
assert_eq!(a[1], "a");
assert_eq!(a[2], "b");
assert_eq!(b[3], "c");
assert_eq!(b[4], "d");
Sourcepub fn drain(&mut self) -> Drain<'_, V>
pub fn drain(&mut self) -> Drain<'_, V>
Returns an iterator visiting all key-value pairs in ascending order of
the keys, emptying (but not consuming) the original VecMap
.
The iterator’s element type is (usize, &'r V)
. Keeps the allocated memory for reuse.
§Examples
use parry::utils::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.insert(3, "c");
map.insert(2, "b");
let vec: Vec<(usize, &str)> = map.drain().collect();
assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);
Sourcepub fn contains_key(&self, key: usize) -> bool
pub fn contains_key(&self, key: usize) -> bool
Sourcepub fn insert(&mut self, key: usize, value: V) -> Option<V>
pub fn insert(&mut self, key: usize, value: V) -> Option<V>
Inserts a key-value pair into the map. If the key already had a value
present in the map, that value is returned. Otherwise, None
is returned.
WARNING: You should only use trusted values as keys in VecMap. Otherwise, a denial-of-service vulnerability may occur that deplets memory with a large key value.
§Examples
use parry::utils::VecMap;
let mut map = VecMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);
map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[37], "c");
Sourcepub fn entry(&mut self, key: usize) -> Entry<'_, V>
pub fn entry(&mut self, key: usize) -> Entry<'_, V>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
use parry::utils::VecMap;
let mut count: VecMap<u32> = VecMap::new();
// count the number of occurrences of numbers in the vec
for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4] {
*count.entry(x).or_insert(0) += 1;
}
assert_eq!(count[1], 3);
Trait Implementations§
Source§impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap<V>
impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap<V>
Source§fn extend<I: IntoIterator<Item = (usize, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (usize, &'a V)>>(&mut self, iter: I)
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
)Source§impl<V> Extend<(usize, V)> for VecMap<V>
impl<V> Extend<(usize, V)> for VecMap<V>
Source§fn extend<I: IntoIterator<Item = (usize, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (usize, V)>>(&mut self, iter: I)
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
)Source§impl<V> FromIterator<(usize, V)> for VecMap<V>
impl<V> FromIterator<(usize, V)> for VecMap<V>
Source§impl<'a, T> IntoIterator for &'a VecMap<T>
impl<'a, T> IntoIterator for &'a VecMap<T>
Source§impl<'a, T> IntoIterator for &'a mut VecMap<T>
impl<'a, T> IntoIterator for &'a mut VecMap<T>
Source§impl<T> IntoIterator for VecMap<T>
impl<T> IntoIterator for VecMap<T>
Source§fn into_iter(self) -> IntoIter<T>
fn into_iter(self) -> IntoIter<T>
Returns an iterator visiting all key-value pairs in ascending order of
the keys, consuming the original VecMap
.
The iterator’s element type is (usize, &'r V)
.
§Examples
use parry::utils::VecMap;
let mut map = VecMap::new();
map.insert(1, "a");
map.insert(3, "c");
map.insert(2, "b");
let vec: Vec<(usize, &str)> = map.into_iter().collect();
assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);
Source§impl<V: Ord> Ord for VecMap<V>
impl<V: Ord> Ord for VecMap<V>
Source§impl<V> PartialOrd for VecMap<V>where
V: PartialOrd,
impl<V> PartialOrd for VecMap<V>where
V: PartialOrd,
impl<V: Eq> Eq for VecMap<V>
Auto Trait Implementations§
impl<V> Freeze for VecMap<V>
impl<V> RefUnwindSafe for VecMap<V>where
V: RefUnwindSafe,
impl<V> Send for VecMap<V>where
V: Send,
impl<V> Sync for VecMap<V>where
V: Sync,
impl<V> Unpin for VecMap<V>where
V: Unpin,
impl<V> UnwindSafe for VecMap<V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.