pub struct SparseMap<K, V, H = StdHash, E = StdEq, P = PowerOfTwo<2>, S = Medium> { /* private fields */ }Expand description
A hash map that trades a little insert speed for low memory use.
Type parameters:
K,V: key and value types.H: the hasher. Defaults toStdHash, which uses the standardBuildHasher. A hasher yields ausizedirectly.E: the key comparator. Defaults toStdEq.P: the growth policy. Defaults toPowerOfTwowith factor 2.S: the sparsity level. Defaults toMedium.
Implementations§
Source§impl<K, V> SparseMap<K, V, StdHash, StdEq, PowerOfTwo<2>, Medium>
impl<K, V> SparseMap<K, V, StdHash, StdEq, PowerOfTwo<2>, Medium>
Sourcepub fn new() -> Self
pub fn new() -> Self
An empty map with no allocation.
Construction places no bound on K. The Hash and Eq bounds belong
to the operations that hash or compare a key.
Sourcepub fn with_bucket_count(bucket_count: usize) -> Self
pub fn with_bucket_count(bucket_count: usize) -> Self
An empty map sized for at least bucket_count buckets.
§Panics
Panics when bucket_count exceeds the policy maximum. Use
SparseMap::try_with_bucket_count for the fallible form.
Sourcepub fn try_with_bucket_count(bucket_count: usize) -> Result<Self, LengthError>
pub fn try_with_bucket_count(bucket_count: usize) -> Result<Self, LengthError>
An empty map sized for at least bucket_count buckets, fallibly.
Source§impl<K, V, B, P, S> SparseMap<K, V, StdHash<B>, StdEq, P, S>
impl<K, V, B, P, S> SparseMap<K, V, StdHash<B>, StdEq, P, S>
Sourcepub fn with_hasher_and_bucket_count(bucket_count: usize) -> Self
pub fn with_hasher_and_bucket_count(bucket_count: usize) -> Self
An empty map that hashes with B and uses policy P and sparsity S.
§Panics
Panics when bucket_count exceeds the policy maximum.
Source§impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
Sourcepub fn with_parts(bucket_count: usize, hash: H, key_eq: E) -> Self
pub fn with_parts(bucket_count: usize, hash: H, key_eq: E) -> Self
Build a map from explicit hasher, comparator, policy, and sparsity.
§Panics
Panics when bucket_count exceeds the policy maximum.
Sourcepub fn try_with_parts(
bucket_count: usize,
hash: H,
key_eq: E,
) -> Result<Self, LengthError>
pub fn try_with_parts( bucket_count: usize, hash: H, key_eq: E, ) -> Result<Self, LengthError>
Build a map fallibly from explicit parts.
Sourcepub fn bucket_count(&self) -> usize
pub fn bucket_count(&self) -> usize
The logical bucket count. Zero for a fresh map.
Sourcepub fn max_bucket_count(&self) -> usize
pub fn max_bucket_count(&self) -> usize
The largest bucket count the map can hold.
Sourcepub fn load_factor(&self) -> f32
pub fn load_factor(&self) -> f32
Ratio of entries to buckets. Zero for an empty map.
Sourcepub fn max_load_factor(&self) -> f32
pub fn max_load_factor(&self) -> f32
The maximum load factor before a grow.
Sourcepub fn set_max_load_factor(&mut self, ml: f32)
pub fn set_max_load_factor(&mut self, ml: f32)
Set the maximum load factor, clamped to [0.1, 0.8].
Sourcepub fn hash_function(&self) -> &H
pub fn hash_function(&self) -> &H
The hasher.
Sourcepub fn reserve(&mut self, count: usize)
pub fn reserve(&mut self, count: usize)
Reserve room for count entries without exceeding the load factor.
Sourcepub fn insert(&mut self, key: K, value: V) -> bool
pub fn insert(&mut self, key: K, value: V) -> bool
Insert key with value, keeping the existing value on a collision.
Returns whether a new entry was created. insert never overwrites: when
key is already present the stored value stays and the passed value is
dropped. To keep the rejected value, use SparseMap::try_insert. To
overwrite, use SparseMap::insert_or_assign.
Sourcepub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, (K, V)>
pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, (K, V)>
Insert key with value only when key is absent.
Returns Ok(&mut V) with a reference to the newly stored value on a
fresh insert. Returns Err((key, value)) with the rejected pair when
key is already present, so an expensive or move-only value is never
silently dropped. The stored value is left unchanged on collision.
Sourcepub fn get_precalc<Q>(&self, key: &Q, hash: usize) -> Option<&V>
pub fn get_precalc<Q>(&self, key: &Q, hash: usize) -> Option<&V>
A reference to the value at key, using a precomputed hash.
The hash must equal hash_function().hash_key(key) or the result is
unspecified.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
A mutable reference to the value at key.
Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
A reference to the key-value pair at key.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Whether key is present.
Sourcepub fn contains_key_precalc<Q>(&self, key: &Q, hash: usize) -> bool
pub fn contains_key_precalc<Q>(&self, key: &Q, hash: usize) -> bool
Whether key is present, using a precomputed hash.
Sourcepub fn count<Q>(&self, key: &Q) -> usize
pub fn count<Q>(&self, key: &Q) -> usize
1 when key is present, 0 otherwise.
Membership reads more directly through SparseMap::contains_key. This
count form mirrors the container contract where a key maps to at most one
element.
Sourcepub fn count_precalc<Q>(&self, key: &Q, hash: usize) -> usize
pub fn count_precalc<Q>(&self, key: &Q, hash: usize) -> usize
1 when key is present, 0 otherwise, using a precomputed hash.
Sourcepub fn equal_range<Q>(&self, key: &Q) -> EqualRange<'_, K, V>
pub fn equal_range<Q>(&self, key: &Q) -> EqualRange<'_, K, V>
The range of entries equal to key.
A map holds at most one entry per key, so the range is empty or a single
entry. The returned iterator yields (&K, &V) and has length 0 or 1.
Sourcepub fn equal_range_precalc<Q>(
&self,
key: &Q,
hash: usize,
) -> EqualRange<'_, K, V>
pub fn equal_range_precalc<Q>( &self, key: &Q, hash: usize, ) -> EqualRange<'_, K, V>
The range of entries equal to key, using a precomputed hash.
Sourcepub fn at<Q>(&self, key: &Q) -> &V
pub fn at<Q>(&self, key: &Q) -> &V
A reference to the value at key, or a panic when absent.
Indexing with map[key] is the idiomatic panic-on-missing form and reads
the same value. Use SparseMap::get to handle a missing key without
panicking.
§Panics
Panics when key is not present.
Sourcepub fn at_precalc<Q>(&self, key: &Q, hash: usize) -> &V
pub fn at_precalc<Q>(&self, key: &Q, hash: usize) -> &V
Sourcepub fn erase<Q>(&mut self, key: &Q) -> usize
pub fn erase<Q>(&mut self, key: &Q) -> usize
Remove key. Returns 1 when erased, 0 otherwise.
Use SparseMap::remove to get the removed value back. erase skips
moving the value out, so it does no work beyond the tombstone.
Sourcepub fn erase_precalc<Q>(&mut self, key: &Q, hash: usize) -> usize
pub fn erase_precalc<Q>(&mut self, key: &Q, hash: usize) -> usize
Remove key using a precomputed hash. Returns 1 when erased, 0 otherwise.
Sourcepub fn pop_front(&mut self) -> Option<(K, V)>
pub fn pop_front(&mut self) -> Option<(K, V)>
Remove and return the first entry in iteration order.
Sourcepub fn erase_range(&mut self, skip: usize, count: usize)
pub fn erase_range(&mut self, skip: usize, count: usize)
Remove count entries starting at iteration index skip.
Entries are erased in iteration order. Erasing leaves a tombstone, which the next grow or cleanup reclaims. The walk is a single forward pass.
Sourcepub fn erase_all(&mut self)
pub fn erase_all(&mut self)
Remove every entry, leaving tombstones. Keeps the bucket count.
Use SparseMap::clear to also reset the tombstones and counters.
Source§impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
Sourcepub fn entry_or_default(&mut self, key: K) -> &mut Vwhere
V: Default,
pub fn entry_or_default(&mut self, key: K) -> &mut Vwhere
V: Default,
The value at key, inserting V::default() when absent.
This is the index-access behavior of a map that default-inserts.
Sourcepub fn try_emplace<F>(&mut self, key: K, make: F) -> (&mut V, bool)where
F: FnOnce() -> V,
pub fn try_emplace<F>(&mut self, key: K, make: F) -> (&mut V, bool)where
F: FnOnce() -> V,
Insert only when key is absent, building the value on demand.
Returns a reference to the value and whether it was newly inserted. The value closure runs only when the key is absent, so a redundant call does not build or consume a value.
Sourcepub fn insert_or_assign(&mut self, key: K, value: V) -> (&mut V, bool)
pub fn insert_or_assign(&mut self, key: K, value: V) -> (&mut V, bool)
Insert value at key, or overwrite the existing value.
Returns a reference to the value and whether it was newly inserted.
Source§impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
Source§impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
Sourcepub fn serialize<Sz: Serializer>(&self, serializer: &mut Sz)
pub fn serialize<Sz: Serializer>(&self, serializer: &mut Sz)
Write the map through serializer in protocol order.
Source§impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>where
H: HashKey<K> + Clone,
E: EqKey<K, K> + Clone,
P: GrowthPolicy,
S: Sparsity,
(K, V): Serialize + Deserialize,
impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>where
H: HashKey<K> + Clone,
E: EqKey<K, K> + Clone,
P: GrowthPolicy,
S: Sparsity,
(K, V): Serialize + Deserialize,
Sourcepub fn deserialize_with<D: Deserializer>(
deserializer: &mut D,
hash_compatible: bool,
hash: H,
key_eq: E,
) -> Result<Self, DeserializeError>
pub fn deserialize_with<D: Deserializer>( deserializer: &mut D, hash_compatible: bool, hash: H, key_eq: E, ) -> Result<Self, DeserializeError>
Read a map written by SparseMap::serialize.
See the engine docs for the meaning of hash_compatible.
Trait Implementations§
impl<K, V, H, E, P, S> Eq for SparseMap<K, V, H, E, P, S>
Source§impl<K, V, H, E, P, S> Extend<(K, V)> for SparseMap<K, V, H, E, P, S>
impl<K, V, H, E, P, S> Extend<(K, V)> for SparseMap<K, V, H, E, P, S>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Insert every pair from iter. A key already present keeps its value.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)