Struct thincollections::thin_map::ThinMap
source · pub struct ThinMap<K: ThinSentinel + Eq + Hash, V, H: BuildHasher = OneFieldHasherBuilder> { /* private fields */ }
Expand description
A fast, low memory replacement for HashMap.
Keys must implement ThinSentinel
, which is already implemented for all primitives.
Ideally, mem::size_of::<(K,V)>()
should be 18 bytes or less. The key size is the
critical factor for performance, and generally should be 64 bits or less. The map will
work fine for larger V sizes, but it will start to lose its advantage over HashMap
.
Keys that have a Drop
impl have not been tested and should be avoided (it’s theoretically
possible to have such keys with a proper implementation of ThinSentinel
, but it’s hard).
Implementations
sourceimpl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>
impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>
sourcepub fn with_hasher(hash_builder: H) -> ThinMap<K, V, H>
pub fn with_hasher(hash_builder: H) -> ThinMap<K, V, H>
Creates an empty ThinMap
which will use the given hash builder to hash
keys.
The hash map is initially created with a capacity of 0, so it will not heap-allocate until it is first inserted into. On first insert, enough room is reserved for 8 pairs.
Examples
use thincollections::thin_map::ThinMap;
use thincollections::thin_hasher::OneFieldHasherBuilder;
let s = OneFieldHasherBuilder::new();
let mut map = ThinMap::with_hasher(s);
map.insert(1, 2);
sourcepub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: H
) -> ThinMap<K, V, H>
pub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: H
) -> ThinMap<K, V, H>
Creates an empty ThinMap
with the specified capacity, using hash_builder
to hash the keys.
The map will be able to hold at least capacity
elements without
reallocating. If capacity
is 0, the map will not heap-allocate.
Examples
use thincollections::thin_map::ThinMap;
use thincollections::thin_hasher::OneFieldHasherBuilder;
let s = OneFieldHasherBuilder::new();
let mut map: ThinMap<u64, i32, OneFieldHasherBuilder> = ThinMap::with_capacity_and_hasher(10, s);
map.insert(1, 2);
sourceimpl<K: ThinSentinel + Eq + Hash, V> ThinMap<K, V, OneFieldHasherBuilder>
impl<K: ThinSentinel + Eq + Hash, V> ThinMap<K, V, OneFieldHasherBuilder>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty ThinMap
.
The hash map is initially created with a capacity of 0, so it will not heap-allocate until it is first inserted into. On first insert, enough room is reserved for 8 pairs.
Examples
use thincollections::thin_map::ThinMap;
let mut map: ThinMap<u64, i32> = ThinMap::new();
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty ThinMap
with the specified capacity, using OneFieldHasherBuilder
The hash map will be able to hold at least capacity
elements without
reallocating. If capacity
is 0, the map will not heap-allocate.
Examples
use thincollections::thin_map::ThinMap;
let mut map: ThinMap<u64, i32> = ThinMap::with_capacity(10);
sourceimpl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>
impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>
sourcepub fn hasher(&self) -> &H
pub fn hasher(&self) -> &H
Returns a reference to the map’s BuildHasher
.
Examples
use thincollections::thin_map::ThinMap;
use thincollections::thin_hasher::OneFieldHasherBuilder;
let s = OneFieldHasherBuilder::new();
let map: ThinMap<i32, u64> = ThinMap::with_hasher(s);
let hasher: &OneFieldHasherBuilder = map.hasher();
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the ThinMap<K, V>
might be able to hold
more, but is guaranteed to be able to hold at least this many.
Examples
use thincollections::thin_map::ThinMap;
let map: ThinMap<i32, i32> = ThinMap::with_capacity(100);
assert!(map.capacity() >= 100);
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the ThinMap
. The collection may reserve more space to avoid
frequent reallocations.
Panics
Panics if the new allocation size overflows usize
.
Examples
use thincollections::thin_map::ThinMap;
let mut map: ThinMap<u64, i32> = ThinMap::new();
map.reserve(10);
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Examples
use thincollections::thin_map::ThinMap;
let mut map: ThinMap<i32, i32> = ThinMap::with_capacity(100);
map.insert(7, 2);
map.insert(3, 4);
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() < 100);
sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Examples
use thincollections::thin_map::ThinMap;
let mut letters = ThinMap::new();
for ch in "a short treatise on fungi".chars() {
let counter = letters.entry(ch).or_insert(0);
*counter += 1;
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
sourcepub fn drain(&mut self) -> Drain<'_, K, V>
pub fn drain(&mut self) -> Drain<'_, K, V>
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
Examples
use thincollections::thin_map::ThinMap;
let mut a = ThinMap::new();
a.insert(1, 100);
a.insert(2, 101);
for (k, v) in a.drain().take(1) {
assert!(k == 1 || k == 2);
assert!(v == 100 || v == 200);
}
assert!(a.is_empty());
sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a K, &'a V)
.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(101, 1);
map.insert(202, 2);
map.insert(303, 3);
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}
sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
An iterator visiting all values in arbitrary order.
The iterator element type is &'a V
.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(100, 1);
map.insert(-100, 2);
map.insert(17, 3);
for val in map.values() {
println!("{}", val);
}
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
An iterator visiting all values mutably in arbitrary order.
The iterator element type is &'a mut V
.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(100, 1);
map.insert(101, 2);
map.insert(102, 3);
for val in map.values_mut() {
*val = *val + 10;
}
for val in map.values() {
println!("{}", val);
}
sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a K
.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(700, 1);
map.insert(800, 2);
map.insert(900, 3);
for key in map.keys() {
println!("{}", key);
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
An iterator visiting all key-value pairs in arbitrary order,
with mutable references to the values.
The iterator element type is (&'a K, &'a mut V)
.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(0, 1);
map.insert(1, 2);
map.insert(4, 3);
// Update all values
for (_, val) in map.iter_mut() {
*val *= 2;
}
for (key, val) in &map {
println!("key: {} val: {}", key, val);
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
Examples
use thincollections::thin_map::ThinMap;
let mut a = ThinMap::new();
assert!(a.is_empty());
a.insert(1, 2);
assert!(!a.is_empty());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
Examples
use thincollections::thin_map::ThinMap;
let mut a = ThinMap::new();
assert_eq!(a.len(), 0);
a.insert(1, 17);
assert_eq!(a.len(), 1);
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be ==
without being identical. See the module-level
documentation for more.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
assert_eq!(map.insert(37, 123), None);
assert_eq!(map.is_empty(), false);
map.insert(37, 289);
assert_eq!(map.insert(37, 333), Some(289));
assert_eq!(map[&37], 333);
sourcepub fn get_key_value(&self, key: &K) -> Option<(&K, &V)>
pub fn get_key_value(&self, key: &K) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(1, 17);
map.insert(7, 42);
assert_eq!(map.get_key_value(&1), Some((&1, &17)));
assert_eq!(map.get_key_value(&7), Some((&7, &42)));
assert_eq!(map.get_key_value(&2), None);
sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(1, 200);
map.insert(7, 300);
assert_eq!(map.get(&1), Some(&200));
assert_eq!(map.get(&7), Some(&300));
assert_eq!(map.get(&2), None);
sourcepub fn get_mut(&self, key: &K) -> Option<&mut V>
pub fn get_mut(&self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(1, 200);
map.insert(7, 300);
if let Some(x) = map.get_mut(&1) {
*x = 42;
}
assert_eq!(map[&1], 42);
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the map contains a value for the specified key.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(1, 200);
map.insert(7, 42);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&7), true);
assert_eq!(map.contains_key(&2), false);
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(7, 123);
assert_eq!(1, map.len());
assert_eq!(map.remove(&7), Some(123));
assert_eq!(map.remove(&7), None);
assert!(map.is_empty());
sourcepub fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(7, 123);
assert_eq!(map.remove_entry(&7), Some((7, 123)));
assert_eq!(map.remove(&7), None);
sourcepub fn retain<F>(&mut self, retain_fn: F)where
F: FnMut(&K, &mut V) -> bool,
pub fn retain<F>(&mut self, retain_fn: F)where
F: FnMut(&K, &mut V) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v)
such that f(&k,&mut v)
returns false
.
Examples
use thincollections::thin_map::ThinMap;
let mut map: ThinMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);
sourceimpl<K, V, S> ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Debug,
V: Debug,
S: BuildHasher,
impl<K, V, S> ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Debug,
V: Debug,
S: BuildHasher,
Trait Implementations
sourceimpl<K, V, S> Clone for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Clone,
V: PartialEq + Clone,
S: BuildHasher + Clone,
impl<K, V, S> Clone for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Clone,
V: PartialEq + Clone,
S: BuildHasher + Clone,
sourceimpl<K, V, S> Debug for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Debug,
V: Debug,
S: BuildHasher,
impl<K, V, S> Debug for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel + Debug,
V: Debug,
S: BuildHasher,
sourceimpl<K, V, S> Default for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher + Default,
impl<K, V, S> Default for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher + Default,
sourceimpl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> Drop for ThinMap<K, V, H>
impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> Drop for ThinMap<K, V, H>
sourceimpl<'a, K, V, S> Extend<(&'a K, &'a V)> for ThinMap<K, V, S>where
K: Eq + Hash + Copy + ThinSentinel,
V: Copy,
S: BuildHasher,
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for ThinMap<K, V, S>where
K: Eq + Hash + Copy + ThinSentinel,
V: Copy,
S: BuildHasher,
sourcefn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<K, V, S> Extend<(K, V)> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
impl<K, V, S> Extend<(K, V)> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
sourcefn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<K, V, S> FromIterator<(K, V)> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher + Default,
impl<K, V, S> FromIterator<(K, V)> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher + Default,
sourceimpl<'a, K: 'a, V, S> Index<&'a K> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
impl<'a, K: 'a, V, S> Index<&'a K> for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
sourceimpl<'a, K, V, S> IntoIterator for &'a ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
impl<'a, K, V, S> IntoIterator for &'a ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
sourceimpl<'a, K, V, S> IntoIterator for &'a mut ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
impl<'a, K, V, S> IntoIterator for &'a mut ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
sourceimpl<K, V, S> IntoIterator for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
impl<K, V, S> IntoIterator for ThinMap<K, V, S>where
K: Eq + Hash + ThinSentinel,
S: BuildHasher,
sourcefn into_iter(self) -> IntoIter<K, V>
fn into_iter(self) -> IntoIter<K, V>
Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order. The map cannot be used after calling this.
Examples
use thincollections::thin_map::ThinMap;
let mut map = ThinMap::new();
map.insert(100, 1);
map.insert(101, 2);
map.insert(102, 3);
// Not possible with .iter()
let vec: Vec<(u8, i32)> = map.into_iter().collect();