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

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);

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);

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();

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);

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();

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);

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);

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);

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);

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());

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);
}

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);
}

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);
}

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);
}

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);
}

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());

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);

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);

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);

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);

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);

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);

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());
source

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);

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);

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

Examples
use thincollections::thin_map::ThinMap;

let mut a = ThinMap::new();
a.insert(1, 20);
a.clear();
assert!(a.is_empty());

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Creates an empty ThinMap<K, V, S>, with the Default value for the hasher.

Executes the destructor for this type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more

Returns a reference to the value corresponding to the supplied key.

Panics

Panics if the key is not present in the ThinMap.

The returned type after indexing.
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

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();
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.