pub struct PersistentHashMap { /* private fields */ }Expand description
An immutable hash map that preserves insertion order when iterated.
Lookups go through a rpds::HashTrieMap from key to insertion sequence
number; the sequence number then indexes a rpds::RedBlackTreeMap that
holds the actual (key, value) pairs in insertion order. Re-associating
an existing key keeps its original position (matching PersistentArrayMap
and how most ordered-map implementations behave), so iteration order is
deterministic and matches the order keys were first written, rather than
depending on hash-bucket layout (which — since rpds seeds its hasher
randomly per instance — would otherwise vary from run to run).
Small maps (≤8 entries) are represented as PersistentArrayMap instead;
the two types share the same Value::Map variant. PersistentHashMap is
used once the entry count exceeds the array-map threshold.
Implementations§
Source§impl PersistentHashMap
impl PersistentHashMap
pub fn empty() -> Self
Sourcepub fn new(map: HashTrieMapSync<Value, Value>) -> Self
pub fn new(map: HashTrieMapSync<Value, Value>) -> Self
Build from a raw HashTrieMap, in its (arbitrary) iteration order.
Prefer from_pairs/assoc when the caller has a meaningful source
order to preserve.
pub fn count(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn contains_key(&self, key: &Value) -> bool
Sourcepub fn assoc(&self, key: Value, value: Value) -> Self
pub fn assoc(&self, key: Value, value: Value) -> Self
Return a new map with key → value.
Re-associating a key that is already present keeps its original insertion position; a brand-new key is appended at the end.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&Value, &Value)>
pub fn iter(&self) -> impl Iterator<Item = (&Value, &Value)>
Iterate over all (key, value) pairs in insertion order.
Sourcepub fn merge(&self, other: &Self) -> Self
pub fn merge(&self, other: &Self) -> Self
Merge two maps; right-hand side wins on key collision.
Sourcepub fn from_pairs<I: IntoIterator<Item = (Value, Value)>>(iter: I) -> Self
pub fn from_pairs<I: IntoIterator<Item = (Value, Value)>>(iter: I) -> Self
Build from an iterator of (key, value) pairs, in the given order.
Sourcepub fn from_array_map(am: &PersistentArrayMap) -> Self
pub fn from_array_map(am: &PersistentArrayMap) -> Self
Promote from a PersistentArrayMap when the threshold is exceeded.
Trait Implementations§
Source§impl Clone for PersistentHashMap
impl Clone for PersistentHashMap
Source§fn clone(&self) -> PersistentHashMap
fn clone(&self) -> PersistentHashMap
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more