Struct compactmap::CompactMap
[−]
[src]
pub struct CompactMap<V> { /* fields omitted */ }
A map that chooses small integer keys for you. You store something into this map and then access it by ID returned by it. For small V entries are expected to take 16 bytes.
Example:
use compactmap::CompactMap; let mut mymap : CompactMap<String> = CompactMap::new(); let id_qwerty = mymap.insert("qwerty".to_string()); let id_qwertz = mymap.insert("qwertz".to_string()); assert_eq!(mymap[id_qwerty], "qwerty"); for (id, val) in mymap { println!("{}:{}", id, val); }
Methods
impl<V> CompactMap<V>
[src]
fn new() -> CompactMap<V>
[src]
Creates an empty CompactMap
.
Examples
use compactmap::CompactMap; let mut map: CompactMap<String> = CompactMap::new();
fn with_capacity(capacity: usize) -> Self
[src]
Creates an empty CompactMap
with space for at least capacity
elements before resizing.
Examples
use compactmap::CompactMap; let mut map: CompactMap<String> = CompactMap::with_capacity(10);
fn capacity(&self) -> usize
[src]
Returns capacity of the underlying vector.
fn reserve(&mut self, len: usize)
[src]
Reserves capacity for CompactMap
's underlying vector.
If you just cleared M elements from the map and want to insert N
more elements, you'll probably need to reserve N-M elements.
fn reserve_exact(&mut self, len: usize)
[src]
Reserves capacity for CompactMap
's underlying vector.
If you just cleared M elements from the map and want to insert N
more elements, you'll probably need to reserve N-M elements.
fn clear(&mut self)
[src]
Clears the map, removing all key-value pairs.
Examples
use compactmap::CompactMap; let mut a = CompactMap::new(); a.insert("a"); a.clear(); assert!(a.is_empty_slow());
fn is_empty_slow(&self) -> bool
[src]
Iterating the map to check if it is empty. O(n) where n is historical maximum element count.
fn insert(&mut self, v: V) -> usize
[src]
Inserts a value into the map. The map generates and returns ID of the inserted element.
Examples
use compactmap::CompactMap; let mut map = CompactMap::new(); assert_eq!(map.is_empty_slow(), true); assert_eq!(map.insert(37), 0); assert_eq!(map.is_empty_slow(), false); assert_eq!(map.insert(37), 1); assert_eq!(map.insert(37), 2); assert_eq!(map.insert(44), 3); assert_eq!(map.len_slow(), 4);
fn remove(&mut self, i: usize) -> Option<V>
[src]
Removes a key from the map, returning the value at the key if the key was previously in the map. ``` use compactmap::CompactMap;
let mut map = CompactMap::new(); let id = map.insert("a"); assert_eq!(map.remove(id), Some("a")); assert_eq!(map.remove(123), None); ```
fn get(&self, i: usize) -> Option<&V>
[src]
Returns a reference to the value corresponding to the key.
fn get_mut(&mut self, i: usize) -> Option<&mut V>
[src]
Returns a mutable reference to the value corresponding to the key.
fn iter(&self) -> Iter<V>
[src]
Returns an iterator visiting all key-value pairs in unspecified order.
The iterator's element type is (usize, &'r V)
.
Examples
use compactmap::CompactMap; let mut map = CompactMap::new(); map.insert("a"); map.insert("c"); map.insert("b"); // Print `1: a`, `2: b` and `3: c`. for (key, value) in map.iter() { println!("{}: {}", key, value); }
fn iter_mut(&mut self) -> IterMut<V>
[src]
Returns an iterator visiting all key-value pairs in unspecified order,
with mutable references to the values.
The iterator's element type is (usize, &'r mut V)
fn into_iter(self) -> IntoIter<V>
[src]
Returns an iterator visiting all key-value pairs in unspecified order,
the keys, consuming the original CompactMap
.
The iterator's element type is (usize, V)
.
fn keys(&self) -> Keys<V>
[src]
Returns an iterator visiting all keys in some order.
The iterator's element type is usize
.
fn values(&self) -> Values<V>
[src]
Returns an iterator visiting all values in ascending order of the keys.
The iterator's element type is &'r V
.
fn values_mut(&mut self) -> ValuesMut<V>
[src]
Returns an iterator visiting all values in ascending order of the keys.
The iterator's element type is &'r mut V
.
fn len_slow(&self) -> usize
[src]
Iterates the map to get number of elements. O(n) where n is historical maximum element count.
fn shrink_to_fit(&mut self)
[src]
Trims the CompactMap
of any excess capacity.
Rescans the whole map to reindex empty slots. O(n).
The collection may reserve more space to avoid frequent reallocations.
Examples
use compactmap::CompactMap; let mut map: CompactMap<&str> = CompactMap::with_capacity(10); assert_eq!(map.capacity(), 10); map.shrink_to_fit(); assert_eq!(map.capacity(), 0); map.insert("qwe"); map.insert("345"); map.insert("555"); map.remove(1); map.shrink_to_fit(); assert_eq!(map.capacity(), 2);
fn drain(&mut self) -> Drain<V>
[src]
Returns an iterator visiting all key-value pairs in ascending order of
the keys, emptying (but not consuming) the original CompactMap
.
The iterator's element type is (usize, V)
. Keeps the allocated memory for reuse.
Examples
use compactmap::CompactMap; let mut map = CompactMap::new(); map.insert("a"); map.insert("b"); map.insert("c"); map.remove(1); let vec: Vec<(usize, &str)> = map.drain().collect(); assert_eq!(vec, [(0, "a"), (2, "c")]); assert!(map.is_empty_slow()); assert!(map.capacity() > 0);
Trait Implementations
impl<V: Clone> Clone for CompactMap<V>
[src]
fn clone(&self) -> CompactMap<V>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<V> Default for CompactMap<V>
[src]
fn default() -> CompactMap<V>
[src]
Returns the "default value" for a type. Read more
impl<V> Hash for CompactMap<V> where
V: Hash,
[src]
V: Hash,
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
H: Hasher,
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<V: PartialEq> PartialEq for CompactMap<V>
[src]
fn eq(&self, other: &Self) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<V: Eq> Eq for CompactMap<V>
[src]
impl<V> PartialOrd<CompactMap<V>> for CompactMap<V> where
V: PartialOrd<V>,
[src]
V: PartialOrd<V>,
fn partial_cmp(&self, other: &CompactMap<V>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<V> Ord for CompactMap<V> where
V: Ord,
[src]
V: Ord,
fn cmp(&self, other: &CompactMap<V>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more
impl<V> FromIterator<V> for CompactMap<V>
[src]
fn from_iter<I>(iter: I) -> CompactMap<V> where
I: IntoIterator<Item = V>,
[src]
I: IntoIterator<Item = V>,
Creates a value from an iterator. Read more
impl<'a, V> FromIterator<&'a V> for CompactMap<V> where
V: Copy,
[src]
V: Copy,
fn from_iter<I>(iter: I) -> CompactMap<V> where
I: IntoIterator<Item = &'a V>,
[src]
I: IntoIterator<Item = &'a V>,
Creates a value from an iterator. Read more
impl<V> Extend<V> for CompactMap<V>
[src]
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = V>,
[src]
I: IntoIterator<Item = V>,
Extends a collection with the contents of an iterator. Read more
impl<'a, V> Extend<&'a V> for CompactMap<V> where
V: Copy,
[src]
V: Copy,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a V>,
[src]
I: IntoIterator<Item = &'a V>,
Extends a collection with the contents of an iterator. Read more
impl<V> Index<usize> for CompactMap<V>
[src]
type Output = V
The returned type after indexing.
fn index(&self, i: usize) -> &V
[src]
Performs the indexing (container[index]
) operation.
impl<'a, V> Index<&'a usize> for CompactMap<V>
[src]
type Output = V
The returned type after indexing.
fn index(&self, i: &usize) -> &V
[src]
Performs the indexing (container[index]
) operation.
impl<V> IndexMut<usize> for CompactMap<V>
[src]
fn index_mut(&mut self, i: usize) -> &mut V
[src]
Performs the mutable indexing (container[index]
) operation.
impl<'a, V> IndexMut<&'a usize> for CompactMap<V>
[src]
fn index_mut(&mut self, i: &usize) -> &mut V
[src]
Performs the mutable indexing (container[index]
) operation.
impl<V: Debug> Debug for CompactMap<V>
[src]
impl<'a, V> IntoIterator for &'a CompactMap<V>
[src]
type Item = (usize, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, V>
[src]
Creates an iterator from a value. Read more
impl<'a, V: 'a> IntoIterator for &'a mut CompactMap<V>
[src]
type Item = (usize, &'a mut V)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IterMut<'a, V>
[src]
Creates an iterator from a value. Read more