pub struct OwnedMap<K: HashWork, V, W: Work = Memory> { /* private fields */ }Expand description
A hash map whose backing allocation and operations share one resource domain.
Hashbrown supplies bucket/probing/SIMD machinery. Reservations include its control bytes and alignment, not merely the initialized entries. Hashing and actual collision comparisons are admitted before examining candidate keys.
Implementations§
Source§impl<K: HashWork, V, W: Work> OwnedMap<K, V, W>
impl<K: HashWork, V, W: Work> OwnedMap<K, V, W>
Sourcepub fn try_iter<'a, E: Work>(
&'a self,
execution: &E,
) -> Result<impl Iterator<Item = (&'a K, &'a V)> + 'a + use<'a, K, V, W, E>, MemoryError>
pub fn try_iter<'a, E: Work>( &'a self, execution: &E, ) -> Result<impl Iterator<Item = (&'a K, &'a V)> + 'a + use<'a, K, V, W, E>, MemoryError>
Borrowed entries after admitting a full control-bucket traversal in the current execution domain, independently of retained storage. Sparse holes count too. Partial consumption retains the conservative prepayment; the opaque iterator exposes no cloning/restart operation.
§Examples
let mut map = memory.map();
map.try_insert(1usize, 42)?;
let mut entries = map.try_iter(&memory)?;
assert_eq!(entries.next(), Some((&1, &42)));Sourcepub fn try_retain<E>(
&mut self,
execution: &impl Work,
predicate: impl FnMut(&K, &mut V) -> Result<bool, E>,
) -> Result<(), RetainError<E>>
pub fn try_retain<E>( &mut self, execution: &impl Work, predicate: impl FnMut(&K, &mut V) -> Result<bool, E>, ) -> Result<(), RetainError<E>>
Retains entries selected by a fallible predicate, without allocating.
The supplied current execution admits the entire control-bucket scan, including sparse holes, before the first predicate call. Every erase separately admits its metadata work before removal. The predicate must admit its own data-dependent work; arbitrary key/value destructors are likewise caller-owned code. No key hashing or equality is performed.
Entries are visited in unspecified order. On the first refusal no more predicates or key-work metadata callbacks run; the engine finishes its already admitted scan while retaining all remaining entries. Earlier removals and predicate value edits, including an edit made by a failing predicate, remain. Refusal never reports a successful partial filter.
The original allocation, capacity charge and storage account remain unchanged. Future growth still uses the map’s original execution. Filtering under another execution does not rebind that allocator.
§Examples
use structured_asn1::memory::{Memory, MemoryAccount};
let memory = Memory::new(Arc::new(MemoryAccount::new(1024, 1024)));
let mut map = memory.map();
map.try_insert(1usize, 10).unwrap();
map.try_insert(2usize, 20).unwrap();
map.try_retain(&memory, |key, value| {
*value += 1;
Ok::<_, core::convert::Infallible>(*key == 2)
}).unwrap();
assert_eq!(map.len(), 1);
assert_eq!(map.get(&2, &memory).unwrap(), Some(&21));Sourcepub fn get(
&self,
key: &K,
execution: &impl Work,
) -> Result<Option<&V>, MemoryError>
pub fn get( &self, key: &K, execution: &impl Work, ) -> Result<Option<&V>, MemoryError>
Looks up a key after admitting its complete possible probe work. The current execution domain pays independently of stored capacity.
§Examples
assert_eq!(map.get(&1, &memory)?, Some(&42));
assert_eq!(map.get(&2, &memory)?, None);Sourcepub fn get_key_value_borrowed<Q: HashWork + ?Sized>(
&self,
key: &Q,
execution: &impl Work,
) -> Result<Option<(&K, &V)>, MemoryError>where
K: Borrow<Q>,
pub fn get_key_value_borrowed<Q: HashWork + ?Sized>(
&self,
key: &Q,
execution: &impl Work,
) -> Result<Option<(&K, &V)>, MemoryError>where
K: Borrow<Q>,
Finds the canonical stored key and value using an allocation-free query.
Owned and borrowed forms must have identical Hash/Eq semantics, as
required by Borrow. Current work admits query hashing, table probes
and every candidate comparison before executing them. The stored key’s
HashWork bound must also cover its Borrow<Q> operation, which must
not allocate. A refusal returns Err, never successful absence or a
synthetic match. No table/account is rebound and no query key is copied.
Returning the canonical key lets an interner clone its existing shared owner without a second lookup or a temporary owned query allocation.
use std::sync::Arc;
use structured_asn1::memory::{Memory, MemoryAccount};
let storage = Memory::new(Arc::new(MemoryAccount::new(4096, 4096)));
let mut map = storage.map();
map.try_insert(7usize, "retained")?;
let query = Memory::new(Arc::new(MemoryAccount::new(0, 1024)));
assert_eq!(map.get_key_value_borrowed(&7, &query)?, Some((&7, &"retained")));Sourcepub fn get_mut(
&mut self,
key: &K,
execution: &impl Work,
) -> Result<Option<&mut V>, MemoryError>
pub fn get_mut( &mut self, key: &K, execution: &impl Work, ) -> Result<Option<&mut V>, MemoryError>
Looks up an entry for mutation without exposing table growth. Hashing, probing and equality use the supplied execution domain.
§Examples
if let Some(value) = map.get_mut(&1, &memory)? { *value = 7; }
assert_eq!(map.get(&1, &memory)?, Some(&7));Sourcepub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, MemoryError>
pub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, MemoryError>
Inserts a value fallibly, or replaces the value for an existing key. This storage mutation runs in the collection’s original resource domain, including hashing, rehashing, allocation and copying work. To mutate under a different domain, build a new collection there from explicitly admitted borrowed entries; retained capacity is not rebound.
Sourcepub fn remove(
&mut self,
key: &K,
execution: &impl Work,
) -> Result<Option<V>, MemoryError>
pub fn remove( &mut self, key: &K, execution: &impl Work, ) -> Result<Option<V>, MemoryError>
Removes an entry, retaining table capacity and its storage charge. The supplied execution domain pays for lookup and removal before mutation.
§Examples
let capacity_charge = memory.used();
assert_eq!(map.remove(&1, &memory)?, Some(42));
assert_eq!(memory.used(), capacity_charge);