pub struct DenseMap<T> { /* private fields */ }Expand description
A contiguous array with sparse index, written as DenseMap<T>, short for ‘dense map’.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(0);
assert_eq!(densemap.get(key), Some(&0));For more information see Crate documentation.
Implementations§
source§impl<T> DenseMap<T>
impl<T> DenseMap<T>
sourcepub const fn new() -> DenseMap<T>
pub const fn new() -> DenseMap<T>
Constructs a new, empty DenseMap<T>.
The dense map will not allocate until elements are inserted onto it.
Examples
use densemap::DenseMap;
let densemap: DenseMap<i32> = DenseMap::new();
assert_eq!(densemap.len(), 0);sourcepub fn with_capacity(
sparse_capacity: usize,
dense_capacity: usize
) -> DenseMap<T>
pub fn with_capacity( sparse_capacity: usize, dense_capacity: usize ) -> DenseMap<T>
Constructs a new, empty DenseMap<T> with at least the specified capacity.
Panics
Panics if the new capacity exceeds isize::MAX bytes.
Examples
use densemap::DenseMap;
let densemap: DenseMap<i32> = DenseMap::with_capacity(10, 2);
assert_eq!(densemap.len(), 0);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 2 <= dense);sourcepub fn capacity(&self) -> (usize, usize)
pub fn capacity(&self) -> (usize, usize)
Returns the total number of elements the dense map can hold without reallocating.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::with_capacity(10, 2);
densemap.insert(42);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 2 <= dense);sourcepub fn try_reserve(
&mut self,
sparse_additional: usize,
dense_additional: usize
) -> Result<(), TryReserveError>
pub fn try_reserve( &mut self, sparse_additional: usize, dense_additional: usize ) -> Result<(), TryReserveError>
Tries to reserves capacity for at least additional more elements to be inserted
in the DenseMap.
Panics
Panics if the new capacity exceeds isize::MAX.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.try_reserve(10, 2).expect("can't reserve capacity");
densemap.insert(1);sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the dense map as much as possible.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::with_capacity(100, 100);
densemap.insert(3);
densemap.insert(4);
let (sparse, dense) = densemap.capacity();
assert!(100 <= sparse && 100 <= dense);
densemap.shrink_to_fit();
let (sparse, dense) = densemap.capacity();
assert!(2 <= sparse && 2 <= dense);sourcepub fn shrink_to(&mut self, sparse_capacity: usize, dense_capacity: usize)
pub fn shrink_to(&mut self, sparse_capacity: usize, dense_capacity: usize)
Shrinks the capacity of the map with a lower limit.
If the current capacity is less than the lower limit, this is a no-op.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::with_capacity(100, 100);
densemap.insert(3);
densemap.insert(4);
let (sparse, dense) = densemap.capacity();
assert!(100 <= sparse && 100 <= dense);
densemap.shrink_to(10, 10);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 10 <= dense);sourcepub fn keys(&self) -> Keys<'_> ⓘ
pub fn keys(&self) -> Keys<'_> ⓘ
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a Key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(1);
let keys = densemap.keys();sourcepub fn into_keys(self) -> IntoKeys ⓘ
pub fn into_keys(self) -> IntoKeys ⓘ
Creates a consuming iterator visiting all the keys in arbitrary order.
The dense map cannot be used after calling this.
The iterator element type is Key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(1);
let keys = densemap.keys();sourcepub fn values(&self) -> Values<'_, T> ⓘ
pub fn values(&self) -> Values<'_, T> ⓘ
An iterator visiting all values in arbitrary order.
The iterator element type is &'a T.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.values();sourcepub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
An iterator visiting all values mutably in arbitrary order.
The iterator element type is &'a mut T.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.values_mut();sourcepub fn into_values(self) -> IntoValues<T> ⓘ
pub fn into_values(self) -> IntoValues<T> ⓘ
Creates a consuming iterator visiting all the values in arbitrary order.
The dense map cannot be used after calling this.
The iterator element type is T.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.into_values();sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a Key, &'a T).
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(12);
densemap.insert(34);
let mut iter = densemap.iter();sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
An iterator visiting all key-value pairs in arbitrary order,
with mutable references to the values.
The iterator element type is (&'a Key, &'a mut T).
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(12);
densemap.insert(34);
let mut iter = densemap.iter();sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the dense map.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(3);
assert_eq!(densemap.len(), 1);sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the dense map contains no elements.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
assert!(densemap.is_empty());
densemap.insert(1);
assert!(!densemap.is_empty());sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Clears the dense map, removing all key-value pairs as an iterator. Keeps the allocated memory for reuse.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(3);
densemap.insert(4);
let iter = densemap.drain();sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the dense map, removing all values and index.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.insert(3);
densemap.clear();
assert!(densemap.is_empty())sourcepub fn contain_key(&self, key: Key) -> bool
pub fn contain_key(&self, key: Key) -> bool
Returns true if the dense map contains a value for the specified key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
assert!(densemap.is_empty());
let key = densemap.insert(1);
assert!(densemap.contain_key(key));sourcepub fn get(&self, key: Key) -> Option<&T>
pub fn get(&self, key: Key) -> Option<&T>
Returns a reference to the value corresponding to the key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.get(key), Some(&3));sourcepub fn get_key_value(&self, key: Key) -> Option<(&Key, &T)>
pub fn get_key_value(&self, key: Key) -> Option<(&Key, &T)>
Returns a reference to the value corresponding to the key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
let (key, value) = densemap.get_key_value(key).unwrap();sourcepub fn get_mut(&mut self, key: Key) -> Option<&mut T>
pub fn get_mut(&mut self, key: Key) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the key.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
if let Some(value) = densemap.get_mut(key) {
*value = 24;
}
assert_eq!(densemap.get(key), Some(&24));sourcepub fn insert(&mut self, value: T) -> Key
pub fn insert(&mut self, value: T) -> Key
Inserts an element to the back of collection and returns key as stable identity.
Panics
Panics if the new capacity exceeds isize::MAX.
Panics if a index or generation of element in the sparse layer exceeds u32::MAX.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.get(key), Some(&3));sourcepub fn insert_with_key<F>(&mut self, f: F) -> Keywhere
F: FnOnce(Key) -> T,
pub fn insert_with_key<F>(&mut self, f: F) -> Keywhere F: FnOnce(Key) -> T,
Inserts a value given by f into the map. The key where the
value will be stored is passed into f. This is useful to store value
that contain their own key.
Panics
Panics if the new capacity exceeds isize::MAX.
Panics if a index or generation of element in the sparse layer exceeds u32::MAX.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert_with_key(|key| (key, 3));
assert_eq!(densemap.get(key), Some(&(key, 3)));sourcepub fn remove(&mut self, key: Key) -> Option<T>
pub fn remove(&mut self, key: Key) -> Option<T>
Removes a key from the map, returning the value at the key if the key was previously in the map.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.remove(key), Some(3));sourcepub fn remove_entry(&mut self, key: Key) -> Option<(Key, T)>
pub fn remove_entry(&mut self, key: Key) -> Option<(Key, T)>
Removes a key from the dense map, returning the stored key and value if the key was previously in the dense map.
Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
let key = densemap.insert(3);
let (key, value) = densemap.remove_entry(key).unwrap();Trait Implementations§
source§impl<T> Extend<T> for DenseMap<T>
impl<T> Extend<T> for DenseMap<T>
source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&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)