Struct AHashMap

struct AHashMap<K, V, S = crate::RandomState>(_)

A HashMap using RandomState to hash the items. (Requires the std feature to be enabled.)

Implementations

impl<K, V> AHashMap<K, V, RandomState>

fn new() -> Self

This crates a hashmap using [RandomState::new] which obtains its keys from [RandomSource]. See the documentation in [RandomSource] for notes about key strength.

fn with_capacity(capacity: usize) -> Self

This crates a hashmap with the specified capacity using [RandomState::new]. See the documentation in [RandomSource] for notes about key strength.

impl<K, V, S> AHashMap<K, V, S>

fn with_hasher(hash_builder: S) -> Self
fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

impl<K, V, S> AHashMap<K, V, S>

fn get<Q>(self: &Self, k: &Q) -> Option<&V>
where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized

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 std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
fn get_key_value<Q>(self: &Self, k: &Q) -> Option<(&K, &V)>
where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized

Returns the key-value pair corresponding to the supplied key.

The supplied 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 std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);
fn get_mut<Q>(self: &mut Self, k: &Q) -> Option<&mut V>
where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized

Returns a mutable 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 std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");
fn insert(self: &mut Self, k: K, v: 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 std::collections::HashMap;

let mut map = HashMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");
fn into_keys(self: Self) -> IntoKeys<K, V>

Creates a consuming iterator visiting all the keys in arbitrary order. The map cannot be used after calling this. The iterator element type is K.

Examples

use std::collections::HashMap;

let map = HashMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

let mut vec: Vec<&str> = map.into_keys().collect();
// The `IntoKeys` iterator produces keys in arbitrary order, so the
// keys must be sorted to test them against a sorted array.
vec.sort_unstable();
assert_eq!(vec, ["a", "b", "c"]);

Performance

In the current implementation, iterating over keys takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

fn into_values(self: Self) -> IntoValues<K, V>

Creates a consuming iterator visiting all the values in arbitrary order. The map cannot be used after calling this. The iterator element type is V.

Examples

use std::collections::HashMap;

let map = HashMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

let mut vec: Vec<i32> = map.into_values().collect();
// The `IntoValues` iterator produces values in arbitrary order, so
// the values must be sorted to test them against a sorted array.
vec.sort_unstable();
assert_eq!(vec, [1, 2, 3]);

Performance

In the current implementation, iterating over values takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

fn remove<Q>(self: &mut Self, k: &Q) -> Option<V>
where
    K: Borrow<Q>,
    Q: Hash + Eq + ?Sized

Removes a key from the map, returning the value at the key if the key was previously in the map.

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 std::collections::HashMap;

let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

impl<'a, K, V, S> Extend for AHashMap<K, V, S>

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

impl<K, Q, V, S> Index for AHashMap<K, V, S>

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

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

Panics

Panics if the key is not present in the HashMap.

impl<K, V> Default for AHashMap<K, V, RandomState>

fn default() -> AHashMap<K, V, RandomState>

impl<K, V> From for AHashMap<K, V>

fn from(item: HashMap<K, V, RandomState>) -> Self

impl<K, V> FromIterator for AHashMap<K, V, RandomState>

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

This crates a hashmap from the provided iterator using [RandomState::new]. See the documentation in [RandomSource] for notes about key strength.

impl<K, V> Into for AHashMap<K, V>

fn into(self: Self) -> HashMap<K, V, RandomState>

impl<K, V, N: usize> From for AHashMap<K, V>

fn from(arr: [(K, V); N]) -> Self

Examples

use ahash::AHashMap;

let map1 = AHashMap::from([(1, 2), (3, 4)]);
let map2: AHashMap<_, _> = [(1, 2), (3, 4)].into();
assert_eq!(map1, map2);

impl<K, V, S> Debug for AHashMap<K, V, S>

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

impl<K, V, S> Deref for AHashMap<K, V, S>

fn deref(self: &Self) -> &<Self as >::Target

impl<K, V, S> DerefMut for AHashMap<K, V, S>

fn deref_mut(self: &mut Self) -> &mut <Self as >::Target

impl<K, V, S> Eq for AHashMap<K, V, S>

impl<K, V, S> Extend for AHashMap<K, V, S>

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

impl<K, V, S> Freeze for AHashMap<K, V, S>

impl<K, V, S> IntoIterator for AHashMap<K, V, S>

fn into_iter(self: Self) -> <Self as >::IntoIter

impl<K, V, S> PartialEq for AHashMap<K, V, S>

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

impl<K, V, S> RefUnwindSafe for AHashMap<K, V, S>

impl<K, V, S> Send for AHashMap<K, V, S>

impl<K, V, S> Sync for AHashMap<K, V, S>

impl<K, V, S> Unpin for AHashMap<K, V, S>

impl<K, V, S> UnsafeUnpin for AHashMap<K, V, S>

impl<K, V, S> UnwindSafe for AHashMap<K, V, S>

impl<K: $crate::clone::Clone, V: $crate::clone::Clone, S: $crate::clone::Clone> Clone for AHashMap<K, V, S>

fn clone(self: &Self) -> AHashMap<K, V, S>

impl<P, T> Receiver for AHashMap<K, V, S>

impl<T> Any for AHashMap<K, V, S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for AHashMap<K, V, S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for AHashMap<K, V, S>

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

impl<T> CloneToUninit for AHashMap<K, V, S>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for AHashMap<K, V, S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for AHashMap<K, V, S>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for AHashMap<K, V, S>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for AHashMap<K, V, S>

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

impl<T, U> TryInto for AHashMap<K, V, S>

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