pub struct HashMapJunction { /* private fields */ }Expand description
One relationship’s junction: each source id mapped to its ordered target ids.
The junction of an exclusive relationship (one-to-one, one-to-many, ordered one-to-many) also keeps a reverse index, target to the sources holding it, so that “this target already belongs to another source” costs one lookup per target of a write instead of a walk over every row of the relationship. Without it, creating N rows that each own a child is O(N²).
The rows are private, so nothing can change them behind the index: every
mutation goes through Self::insert, Self::remove or
Self::remove_target, which update both in the same call, and a
whole-junction clone (snapshot, savepoint, HashMapStore::freeze,
restore) carries both together. Both are im::HashMaps, so that clone
stays O(1).
The read methods mirror the map it wraps (get, iter, contains_key, …),
so code that only reads a junction reads it as before.
Implementations§
Source§impl HashMapJunction
impl HashMapJunction
Sourcepub fn exclusive() -> Self
pub fn exclusive() -> Self
An empty junction for an exclusive relationship, with its reverse index.
An empty junction for a shared relationship, without a reverse index.
Sourcepub fn is_exclusive(&self) -> bool
pub fn is_exclusive(&self) -> bool
Whether this junction belongs to an exclusive relationship.
Sourcepub fn get(&self, source: &EntityId) -> Option<&Vec<EntityId>>
pub fn get(&self, source: &EntityId) -> Option<&Vec<EntityId>>
The ordered targets of source, if it has a row.
Sourcepub fn contains_key(&self, source: &EntityId) -> bool
pub fn contains_key(&self, source: &EntityId) -> bool
Whether source has a row (possibly empty).
Sourcepub fn iter(&self) -> impl Iterator<Item = (&EntityId, &Vec<EntityId>)>
pub fn iter(&self) -> impl Iterator<Item = (&EntityId, &Vec<EntityId>)>
Every row, as (source, targets), in no particular order.
Sourcepub fn keys(&self) -> impl Iterator<Item = &EntityId>
pub fn keys(&self) -> impl Iterator<Item = &EntityId>
Every source that has a row, in no particular order.
Sourcepub fn values(&self) -> impl Iterator<Item = &Vec<EntityId>>
pub fn values(&self) -> impl Iterator<Item = &Vec<EntityId>>
Every row’s targets, in no particular order.
Sourcepub fn sources_of(&self, target: &EntityId) -> Vec<EntityId> ⓘ
pub fn sources_of(&self, target: &EntityId) -> Vec<EntityId> ⓘ
Every source whose row holds target, once per occurrence: none or one
in a consistent exclusive junction. One lookup on an exclusive junction;
a walk over every row on a shared one, which has no index.
Sourcepub fn insert(
&mut self,
source: EntityId,
targets: Vec<EntityId>,
) -> Option<Vec<EntityId>>
pub fn insert( &mut self, source: EntityId, targets: Vec<EntityId>, ) -> Option<Vec<EntityId>>
Replace the row of source with targets, returning the previous row.
Sourcepub fn remove(&mut self, source: &EntityId) -> Option<Vec<EntityId>>
pub fn remove(&mut self, source: &EntityId) -> Option<Vec<EntityId>>
Remove the row of source, returning it.
Sourcepub fn remove_target(&mut self, target: &EntityId)
pub fn remove_target(&mut self, target: &EntityId)
Remove target from every row holding it, as when the target entity is
deleted. Visits only those rows on an exclusive junction, every row on a
shared one.
Sourcepub fn check_index(&self) -> Result<(), String>
pub fn check_index(&self) -> Result<(), String>
Compare the reverse index with one rebuilt from the rows.
A diagnostic for tests, O(junction): see
HashMapStore::check_junction_indexes. A shared junction has no index
and always passes.