Skip to content

Commit 8e2d120

Browse files
authored
Merge pull request #19 from varkor/vars_since_snapshot
Add a `values_since_snapshot` method to `UnificationStore`
2 parents 49d9270 + bdbaa65 commit 8e2d120

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/snapshot_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<D> fmt::Debug for SnapshotVec<D>
6161
// Snapshots are tokens that should be created/consumed linearly.
6262
pub struct Snapshot {
6363
// Length of the undo log at the time the snapshot was taken.
64-
length: usize,
64+
pub(crate) length: usize,
6565
}
6666

6767
pub trait SnapshotVecDelegate {

src/unify/backing_vec.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use dogged::DVec;
33
use snapshot_vec as sv;
44
use std::ops;
5+
use std::ops::RangeInclusive;
56
use std::marker::PhantomData;
67

78
use super::{VarValue, UnifyKey, UnifyValue};
@@ -10,29 +11,35 @@ use super::{VarValue, UnifyKey, UnifyValue};
1011
#[allow(type_alias_bounds)]
1112
type Key<S: UnificationStore> = <S as UnificationStore>::Key;
1213

14+
pub trait Measurable {
15+
fn len(&self) -> usize;
16+
}
17+
1318
/// Largely internal trait implemented by the unification table
1419
/// backing store types. The most common such type is `InPlace`,
1520
/// which indicates a standard, mutable unification table.
1621
pub trait UnificationStore:
17-
ops::Index<usize, Output = VarValue<Key<Self>>> + Clone + Default
22+
ops::Index<usize, Output = VarValue<Key<Self>>> + Measurable + Clone + Default
1823
{
1924
type Key: UnifyKey<Value = Self::Value>;
2025
type Value: UnifyValue;
21-
type Snapshot;
26+
type Snapshot: Measurable;
2227

2328
fn start_snapshot(&mut self) -> Self::Snapshot;
2429

2530
fn rollback_to(&mut self, snapshot: Self::Snapshot);
2631

2732
fn commit(&mut self, snapshot: Self::Snapshot);
2833

34+
fn values_since_snapshot(&mut self, snapshot: &Self::Snapshot) -> RangeInclusive<usize> {
35+
snapshot.len()..=self.len()
36+
}
37+
2938
fn reset_unifications(
3039
&mut self,
3140
value: impl FnMut(u32) -> VarValue<Self::Key>,
3241
);
3342

34-
fn len(&self) -> usize;
35-
3643
fn push(&mut self, value: VarValue<Self::Key>);
3744

3845
fn reserve(&mut self, num_new_values: usize);
@@ -59,6 +66,20 @@ impl<K: UnifyKey> Default for InPlace<K> {
5966
}
6067
}
6168

69+
impl Measurable for sv::Snapshot {
70+
#[inline]
71+
fn len(&self) -> usize {
72+
self.length
73+
}
74+
}
75+
76+
impl<K: UnifyKey> Measurable for InPlace<K> {
77+
#[inline]
78+
fn len(&self) -> usize {
79+
self.values.len()
80+
}
81+
}
82+
6283
impl<K: UnifyKey> UnificationStore for InPlace<K> {
6384
type Key = K;
6485
type Value = K::Value;
@@ -87,11 +108,6 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
87108
self.values.set_all(|i| value(i as u32));
88109
}
89110

90-
#[inline]
91-
fn len(&self) -> usize {
92-
self.values.len()
93-
}
94-
95111
#[inline]
96112
fn push(&mut self, value: VarValue<Self::Key>) {
97113
self.values.push(value);
@@ -143,6 +159,14 @@ impl<K: UnifyKey> Default for Persistent<K> {
143159
}
144160
}
145161

162+
#[cfg(feature = "persistent")]
163+
impl<K: UnifyKey> Measurable for Persistent<K> {
164+
#[inline]
165+
fn len(&self) -> usize {
166+
self.values.len()
167+
}
168+
}
169+
146170
#[cfg(feature = "persistent")]
147171
impl<K: UnifyKey> UnificationStore for Persistent<K> {
148172
type Key = K;
@@ -176,11 +200,6 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
176200
}
177201
}
178202

179-
#[inline]
180-
fn len(&self) -> usize {
181-
self.values.len()
182-
}
183-
184203
#[inline]
185204
fn push(&mut self, value: VarValue<Self::Key>) {
186205
self.values.push(value);

0 commit comments

Comments
 (0)