Struct double_map::DHashMap
source · [−]pub struct DHashMap<K1, K2, V, S = RandomState> { /* private fields */ }Implementations
sourceimpl<K1, K2, V> DHashMap<K1, K2, V, RandomState>
impl<K1, K2, V> DHashMap<K1, K2, V, RandomState>
sourcepub fn new() -> DHashMap<K1, K2, V, RandomState>
pub fn new() -> DHashMap<K1, K2, V, RandomState>
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);sourcepub fn with_capacity(capacity: usize) -> DHashMap<K1, K2, V, RandomState>
pub fn with_capacity(capacity: usize) -> DHashMap<K1, K2, V, RandomState>
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)sourceimpl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
S: Clone,
impl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
S: Clone,
sourcepub fn with_hasher(hash_builder: S) -> DHashMap<K1, K2, V, S>
pub fn with_hasher(hash_builder: S) -> DHashMap<K1, K2, V, S>
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);sourcepub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: S
) -> DHashMap<K1, K2, V, S>
pub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: S
) -> DHashMap<K1, K2, V, S>
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)sourceimpl<K1, K2, V, S> DHashMap<K1, K2, V, S>
impl<K1, K2, V, S> DHashMap<K1, K2, V, S>
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 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);pub fn len(&self) -> usize
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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);pub fn clear(&mut self)
sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
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();sourceimpl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
K1: Eq + Hash,
K2: Eq + Hash,
S: BuildHasher,
impl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
K1: Eq + Hash,
K2: Eq + Hash,
S: BuildHasher,
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 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);sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
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);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.
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);sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
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);sourcepub fn entry(
&mut self,
k1: K1,
k2: K2
) -> Result<Entry<'_, K1, K2, V>, EntryError<K1, K2>>
pub fn entry(
&mut self,
k1: K1,
k2: K2
) -> Result<Entry<'_, K1, K2, V>, EntryError<K1, K2>>
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:
- When key #1 is vacant, but key #2 is already exists with some value the returned
ErrorKindvariant isErrorKind::VacantK1AndOccupiedK2. - When key #1 is already exists with some value, but key #2 is vacant the returned
ErrorKindvariant isErrorKind::OccupiedK1AndVacantK2. - When both key #1 and key #2 is already exists with some values, but points to different entries (values)
the returned
ErrorKindvariant isErrorKind::KeysPointsToDiffEntries.
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);sourcepub fn get_key1<Q: ?Sized>(&self, k1: &Q) -> Option<&V> where
K1: Borrow<Q>,
Q: Hash + Eq,
pub fn get_key1<Q: ?Sized>(&self, k1: &Q) -> Option<&V> where
K1: Borrow<Q>,
Q: Hash + Eq,
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);sourcepub fn get_key2<Q: ?Sized>(&self, k2: &Q) -> Option<&V> where
K2: Borrow<Q>,
Q: Hash + Eq,
pub fn get_key2<Q: ?Sized>(&self, k2: &Q) -> Option<&V> where
K2: Borrow<Q>,
Q: Hash + Eq,
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);pub fn get_mut_key1<Q: ?Sized>(&mut self, k1: &Q) -> Option<&mut V> where
K1: Borrow<Q>,
Q: Hash + Eq,
pub fn get_mut_key2<Q: ?Sized>(&mut self, k2: &Q) -> Option<&mut V> where
K2: Borrow<Q>,
Q: Hash + Eq,
sourceimpl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
K1: Eq + Hash + Clone,
K2: Eq + Hash + Clone,
S: BuildHasher,
impl<K1, K2, V, S> DHashMap<K1, K2, V, S> where
K1: Eq + Hash + Clone,
K2: Eq + Hash + Clone,
S: BuildHasher,
pub fn insert_unchecked(&mut self, k1: K1, k2: K2, v: V) -> Option<V>
pub fn insert(
&mut self,
k1: K1,
k2: K2,
v: V
) -> Option<Result<V, InsertError<K1, K2, V>>>
pub fn try_insert(
&mut self,
k1: K1,
k2: K2,
v: V
) -> Result<&mut V, TryInsertError<'_, K1, K2, V>>
pub fn remove_key1<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K1: Borrow<Q>,
Q: Hash + Eq,
pub fn remove_key2<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K2: Borrow<Q>,
Q: Hash + Eq,
Trait Implementations
Auto Trait Implementations
impl<K1, K2, V, S> RefUnwindSafe for DHashMap<K1, K2, V, S> where
K1: RefUnwindSafe,
K2: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
impl<K1, K2, V, S> Send for DHashMap<K1, K2, V, S> where
K1: Send,
K2: Send,
S: Send,
V: Send,
impl<K1, K2, V, S> Sync for DHashMap<K1, K2, V, S> where
K1: Sync,
K2: Sync,
S: Sync,
V: Sync,
impl<K1, K2, V, S> Unpin for DHashMap<K1, K2, V, S> where
K1: Unpin,
K2: Unpin,
S: Unpin,
V: Unpin,
impl<K1, K2, V, S> UnwindSafe for DHashMap<K1, K2, V, S> where
K1: UnwindSafe,
K2: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more