pub struct DHashMap<K1, K2, V, S = RandomState> { /* private fields */ }

Implementations

Creates a new empty DHashMap with RandomState type of hash builder to hash keys.

The primary key is of type K1 and the secondary key is of type K2. The value is of type V.

Internally, two HashMap are created. One of type HashMap<K1, (K2, V)> to hold the (K2, V) tuple, and second one of type HashMap<K2, K1> just for holding the primary key of type K1. We hold the (K2, V) tuple inside first Hashmap for synchronization purpose, so that we can organize checking that both primary key of type K1 and the secondary key is of type K2 refers to the same value, and so on.

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

Examples
use double_map::DHashMap;
let mut map: DHashMap<u32, &str, i32> = DHashMap::new();
 
// The created DHashMap hold none elements
assert_eq!(map.len(), 0);
 
// The created DHashMap also didn't allocate memory
assert_eq!(map.capacity(), 0);
 
// Now we insert elements inside created DHashMap
map.insert(1, "One", 1);
// We can see that the DHashMap hold 1 element
assert_eq!(map.len(), 1);
// And it also allocate some capacity (by default it starts from 3 element)
assert!(map.capacity() > 1);

Creates an empty DHashMap with the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will not allocate.

Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, i32, &str> = DHashMap::with_capacity(5);
 
// The created DHashMap hold none elements
assert_eq!(map.len(), 0);
// But it can hold at least 5 elements without reallocating
let empty_map_capacity = map.capacity();
assert!(empty_map_capacity >= 5);
 
// Now we insert some 5 elements inside created DHashMap
map.insert("One",   1, "a");
map.insert("Two",   2, "b");
map.insert("Three", 3, "c");
map.insert("Four",  4, "d");
map.insert("Five",  5, "e");
 
// We can see that the DHashMap hold 5 elements
assert_eq!(map.len(), 5);
// But it capacity dosn't changed
assert_eq!(map.capacity(), empty_map_capacity)

Creates an empty DHashMap which will use the given hash builder to hash keys.

The created map has the default initial capacity, witch is equal to 0, so it will not allocate until it is first inserted into.

Warning: hash_builder is normally randomly generated, and is designed to allow DHashMap to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.

The hash_builder passed should implement the BuildHasher trait for the DHashMap to be useful, see its documentation for details. It also should implement the Clone trait because we create two HashMap inside DHashMap, so that we need to clone hash_builder for passing it inside two inner HashMap.

Examples
use double_map::DHashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map = DHashMap::with_hasher(s);
 
// The created DHashMap hold none elements
assert_eq!(map.len(), 0);
 
// The created DHashMap also didn't allocate memory
assert_eq!(map.capacity(), 0);
 
// Now we insert elements inside created DHashMap
map.insert("One", 1, 2);
// We can see that the DHashMap hold 1 element
assert_eq!(map.len(), 1);
// And it also allocate some capacity (by default it starts from 3 element)
assert!(map.capacity() > 1);

Creates an empty DHashMap with the specified capacity, using hash_builder to hash the keys.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will not allocate.

Warning: hash_builder is normally randomly generated, and is designed to allow HashMaps to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.

The hash_builder passed should implement the BuildHasher trait for the DHashMap to be useful, see its documentation for details. It also should implement the Clone trait because we create two HashMap inside DHashMap, so that we need to clone hash_builder for passing it inside two inner HashMap.

Examples
use double_map::DHashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map = DHashMap::with_capacity_and_hasher(5, s);
 
// The created DHashMap hold none elements
assert_eq!(map.len(), 0);
// But it can hold at least 5 elements without reallocating
let empty_map_capacity = map.capacity();
assert!(empty_map_capacity >= 5);
 
// Now we insert some 5 elements inside created DHashMap
map.insert("One",   1, "a");
map.insert("Two",   2, "b");
map.insert("Three", 3, "c");
map.insert("Four",  4, "d");
map.insert("Five",  5, "e");
 
// We can see that the DHashMap hold 5 elements
assert_eq!(map.len(), 5);
// But it capacity dosn't changed
assert_eq!(map.capacity(), empty_map_capacity)

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the DHashMap<K1, K2, V> collection might be able to hold more, but is guaranteed to be able to hold at least this many.

Examples
use double_map::DHashMap;
let map = DHashMap::<i32, &str, &str>::with_capacity(16);

// The created DHashMap can hold at least 16 elements
assert!(map.capacity() >= 16);
// But for not it dosn't hold any element 
assert_eq!(map.len(), 0);

Returns true if the map contains no elements.

Examples
use double_map::DHashMap;

let mut a = DHashMap::new();
// The created DHashMap didn't hold any element, so it's empty
assert!(a.is_empty()  && a.len() == 0);
// We insert one element
a.insert(1, "a", "One");
// And can be sure that DHashMap is not empty but hold one element
assert!(!a.is_empty() && a.len() == 1);

Returns a reference to the map’s BuildHasher.

Examples
use double_map::DHashMap;
use std::collections::hash_map::RandomState;

let hasher = RandomState::new();
let map: DHashMap<i32, i32, i32> = DHashMap::with_hasher(hasher);
let hasher: &RandomState = map.hasher();

Reserves capacity for at least additional more elements to be inserted in the DHashMap<K1, K2, V>. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new allocation size overflows [usize::Max] / 2.

Examples
use double_map::DHashMap;
let mut a = DHashMap::<&str, i128, &str>::new();
a.insert("apple",  1, "a");
a.insert("banana", 2, "b");
a.insert("Cherry", 3, "c");
 
// We reserve space for additional 10 elements 
a.reserve(10);
// And can see that created DHashMap can hold at least 13 elements
assert!(a.capacity() >= 13);

Tries to reserve capacity for at least additional more elements to be inserted in the given DHashMap<K1, K2, V>. The collection may reserve more space to avoid frequent reallocations.

Errors

If the capacity overflows, or the allocator reports a failure, then an error is returned.

Examples
use double_map::DHashMap;

let mut map: DHashMap<i32, &str, isize> = DHashMap::new();
map.try_reserve(10).expect("something go wrong");
assert!(map.capacity() >= 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.

Note that in the general case the capacity is not guaranteed to shrink, but a zero-length DHashMap should generally shrink to capacity zero.

Examples
use double_map::DHashMap;
let mut a = DHashMap::<i32, &str, &str>::with_capacity(16);
 
// This DHashMap can hold at least 16 element
let capacity_before_shrink = a.capacity();
assert!(capacity_before_shrink >= 16);
 
// And after shrink it capacity is less that before
a.shrink_to_fit();
assert!(a.capacity() < capacity_before_shrink);
 
// If we reserve some memory and insert some elements
a.reserve(100);
a.insert(1, "a", "One");
a.insert(1, "b", "Two");
assert!(a.capacity() >= 100);
 
// After shrink_to_fit method the capacity If we reserve some memory and insert some elements
a.shrink_to_fit();
assert!(a.capacity() >= 2 && a.capacity() < 100);

Shrinks the capacity of the map with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

If the current capacity is less than the lower limit, this is a no-op.

Examples
use double_map::DHashMap;

let mut map: DHashMap<i32, i32, i32> = DHashMap::with_capacity(100);
map.insert(1, 2, 3);
map.insert(4, 5, 6);
map.insert(7, 8, 9);
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10 && map.capacity() < 100);
map.shrink_to(0);
assert!(map.capacity() >= 3  && map.capacity() < 10);

Tries to gets the given keys’ corresponding entry in the map for in-place manipulation.

Returns Entry enum if all of the following is true:

  • Both key #1 and key #2 are vacant or already exists with some value.
  • Both key #1 and key #2 refer to the same value.

When the above statements is false, entry method return EntryError structure which contains the ErrorKind enum, and the values of provided keys (that can be used for another purpose).

Depending on the points below, different ErrorKind variants may be returned:

Examples
use double_map::{DHashMap, ErrorKind};

let mut letters = DHashMap::new();

for ch in "a short treatise on fungi".chars() {
    if let Ok(entry) = letters.entry(ch.clone(), ch) {
        let counter = entry.or_insert(0);
        *counter += 1;
    }
}
 
// Return [`ErrorKind::OccupiedK1AndVacantK2`] if key #1 is already exists with some value, but key #2 is vacant.
let error_kind = letters.entry('s', 'y').unwrap_err().error;
assert_eq!(error_kind, ErrorKind::OccupiedK1AndVacantK2); 
// Return [`ErrorKind::VacantK1AndOccupiedK2`] if key #1 is vacant, but key #2 is already exists with some value.
let error_kind = letters.entry('y', 's').unwrap_err().error;
assert_eq!(error_kind, ErrorKind::VacantK1AndOccupiedK2);
 
// Return [`ErrorKind::KeysPointsToDiffEntries`] if both key #1 and key #2 are already exists with some values,
// but points to different entries (values).
let error_kind = letters.entry('s', 't').unwrap_err().error;
assert_eq!(error_kind, ErrorKind::KeysPointsToDiffEntries);

Returns a reference to the value corresponding to the given primary key (key #1).

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 double_map::DHashMap;

let mut map = DHashMap::new();
map.insert(1, "a", "One");
assert_eq!(map.get_key1(&1), Some(&"One"));
assert_eq!(map.get_key1(&2), None);

Returns a reference to the value corresponding to the given secondary key (key #2).

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 double_map::DHashMap;

let mut map = DHashMap::new();
map.insert(1, "a", "One");
assert_eq!(map.get_key2(&"a"), Some(&"One"));
assert_eq!(map.get_key2(&"b"), None);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. 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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.