diff --git a/src/unify/backing_vec.rs b/src/unify/backing_vec.rs index f1e720c..b53e3f5 100644 --- a/src/unify/backing_vec.rs +++ b/src/unify/backing_vec.rs @@ -2,6 +2,7 @@ use dogged::DVec; use snapshot_vec as sv; use std::ops; +use std::ops::RangeInclusive; use std::marker::PhantomData; use super::{VarValue, UnifyKey, UnifyValue}; @@ -10,15 +11,19 @@ use super::{VarValue, UnifyKey, UnifyValue}; #[allow(type_alias_bounds)] type Key = ::Key; +pub trait Measurable { + fn len(&self) -> usize; +} + /// Largely internal trait implemented by the unification table /// backing store types. The most common such type is `InPlace`, /// which indicates a standard, mutable unification table. pub trait UnificationStore: - ops::Index>> + Clone + Default + ops::Index>> + Measurable + Clone + Default { type Key: UnifyKey; type Value: UnifyValue; - type Snapshot; + type Snapshot: Measurable; fn start_snapshot(&mut self) -> Self::Snapshot; @@ -26,13 +31,15 @@ pub trait UnificationStore: fn commit(&mut self, snapshot: Self::Snapshot); + fn values_since_snapshot(&mut self, snapshot: &Self::Snapshot) -> RangeInclusive { + snapshot.len()..=self.len() + } + fn reset_unifications( &mut self, value: impl FnMut(u32) -> VarValue, ); - fn len(&self) -> usize; - fn push(&mut self, value: VarValue); fn reserve(&mut self, num_new_values: usize); @@ -59,6 +66,20 @@ impl Default for InPlace { } } +impl Measurable for sv::Snapshot { + #[inline] + fn len(&self) -> usize { + self.length + } +} + +impl Measurable for InPlace { + #[inline] + fn len(&self) -> usize { + self.values.len() + } +} + impl UnificationStore for InPlace { type Key = K; type Value = K::Value; @@ -87,11 +108,6 @@ impl UnificationStore for InPlace { self.values.set_all(|i| value(i as u32)); } - #[inline] - fn len(&self) -> usize { - self.values.len() - } - #[inline] fn push(&mut self, value: VarValue) { self.values.push(value); @@ -143,6 +159,14 @@ impl Default for Persistent { } } +#[cfg(feature = "persistent")] +impl Measurable for Persistent { + #[inline] + fn len(&self) -> usize { + self.values.len() + } +} + #[cfg(feature = "persistent")] impl UnificationStore for Persistent { type Key = K;