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§

source§

impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>

source

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

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

impl<K: ThinSentinel + Eq + Hash, V> ThinMap<K, V, OneFieldHasherBuilder>

source

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

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

impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> ThinMap<K, V, H>

source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn clear(&mut self)

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

impl<K, V, S> ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel + Debug, V: Debug, S: BuildHasher,

source

pub fn debug(&self)

Trait Implementations§

source§

impl<K, V, S> Clone for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel + Clone, V: PartialEq + Clone, S: BuildHasher + Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K, V, S> Debug for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel + Debug, V: Debug, S: BuildHasher,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K, V, S> Default for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher + Default,

source§

fn default() -> ThinMap<K, V, S>

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

source§

impl<K: ThinSentinel + Eq + Hash, V, H: BuildHasher> Drop for ThinMap<K, V, H>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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,

source§

fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V, S> Extend<(K, V)> for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher,

source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K, V, S> FromIterator<(K, V)> for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher + Default,

source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> ThinMap<K, V, S>

Creates a value from an iterator. Read more
source§

impl<'a, K, V, S> Index<&'a K> for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel + 'a, S: BuildHasher,

source§

fn index(&self, key: &K) -> &V

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

Panics

Panics if the key is not present in the ThinMap.

§

type Output = V

The returned type after indexing.
source§

impl<'a, K, V, S> IntoIterator for &'a ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher,

§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
source§

impl<'a, K, V, S> IntoIterator for &'a mut ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher,

§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterMut<'a, K, V>

Creates an iterator from a value. Read more
source§

impl<K, V, S> IntoIterator for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, S: BuildHasher,

source§

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

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

impl<K, V, S> PartialEq<ThinMap<K, V, S>> for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, V: PartialEq, S: BuildHasher,

source§

fn eq(&self, other: &ThinMap<K, V, S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, S> Eq for ThinMap<K, V, S>where K: Eq + Hash + ThinSentinel, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, H> RefUnwindSafe for ThinMap<K, V, H>where H: RefUnwindSafe, K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, H = OneFieldHasherBuilder> !Send for ThinMap<K, V, H>

§

impl<K, V, H = OneFieldHasherBuilder> !Sync for ThinMap<K, V, H>

§

impl<K, V, H> Unpin for ThinMap<K, V, H>where H: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, H> UnwindSafe for ThinMap<K, V, H>where H: UnwindSafe, K: UnwindSafe + RefUnwindSafe, V: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.