Struct AHashMap

pub struct AHashMap<K, V, S = RandomState>(/* private field */);

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 creates 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 creates 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> where K: Hash + Eq, S: BuildHasher,

fn get<Q>(&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, 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>(&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(&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) -> 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) -> 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>(&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<K, V, S> AHashMap<K, V, S> where S: BuildHasher,

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

Trait Implementations

impl<'a, K, V, S> Extend<(&'a K, &'a V)> for AHashMap<K, V, S> where K: Eq + Hash + Copy + 'a, V: Copy + 'a, S: BuildHasher,

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

impl<K, Q, V, S> Index<&Q> for AHashMap<K, V, S> where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, S: BuildHasher,

type Output = V;
fn index(&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<HashMap<K, V, RandomState>> for AHashMap<K, V>

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

impl<K, V> FromIterator<(K, V)> for AHashMap<K, V, RandomState> where K: Eq + Hash,

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

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

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

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

impl<K, V, S> Debug for AHashMap<K, V, S> where K: Debug, V: Debug, S: BuildHasher,

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

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

type Target = HashMap<K, V, S>;
fn deref(&self) -> &Self::Target

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

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

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

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

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

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

type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter

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

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

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

impl<K, V, const N: usize> From<[(K, V); N]> for AHashMap<K, V> where K: Eq + Hash,

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: Clone, V: Clone, S: Clone> Clone for AHashMap<K, V, S>

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

Auto Trait Implementations

impl<K, V, S> Freeze for AHashMap<K, V, S> where HashMap<K, V, S>: Freeze,

impl<K, V, S> RefUnwindSafe for AHashMap<K, V, S> where HashMap<K, V, S>: RefUnwindSafe,

impl<K, V, S> Send for AHashMap<K, V, S> where HashMap<K, V, S>: Send,

impl<K, V, S> Sync for AHashMap<K, V, S> where HashMap<K, V, S>: Sync,

impl<K, V, S> Unpin for AHashMap<K, V, S> where HashMap<K, V, S>: Unpin,

impl<K, V, S> UnsafeUnpin for AHashMap<K, V, S> where HashMap<K, V, S>: UnsafeUnpin,

Blanket Implementations

impl<P, T> Receiver for AHashMap<K, V, S> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for AHashMap<K, V, S> where ST: ?Sized, DT: ?Sized,

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for AHashMap<K, V, S> where ST: ?Sized, DT: ?Sized,

impl<T> Any for AHashMap<K, V, S> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for AHashMap<K, V, S> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for AHashMap<K, V, S> where T: ?Sized,

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

impl<T> CloneToUninit for AHashMap<K, V, S> where T: Clone,

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

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

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Read<Exclusive, BecauseExclusive> for AHashMap<K, V, S> where T: ?Sized,

impl<T> ToOwned for AHashMap<K, V, S> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(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<U> for AHashMap<K, V, S> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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