Skip to content

Commit 7ad2655

Browse files
authored
Merge pull request #21 from varkor/vars_since_snapshot
Add a `vars_since_snapshot` method to `UnificationTable`
2 parents 38fcb1c + e7ad85c commit 7ad2655

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

src/snapshot_vec.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ impl<D> fmt::Debug for SnapshotVec<D>
6060

6161
// Snapshots are tokens that should be created/consumed linearly.
6262
pub struct Snapshot {
63+
// Number of values at the time the snapshot was taken.
64+
pub(crate) value_count: usize,
6365
// Length of the undo log at the time the snapshot was taken.
64-
pub(crate) length: usize,
66+
undo_len: usize,
6567
}
6668

6769
pub trait SnapshotVecDelegate {
@@ -173,27 +175,29 @@ impl<D: SnapshotVecDelegate> SnapshotVec<D> {
173175
}
174176

175177
pub fn start_snapshot(&mut self) -> Snapshot {
176-
let length = self.undo_log.len();
177178
self.num_open_snapshots += 1;
178-
Snapshot { length: length }
179+
Snapshot {
180+
value_count: self.values.len(),
181+
undo_len: self.undo_log.len(),
182+
}
179183
}
180184

181185
pub fn actions_since_snapshot(&self, snapshot: &Snapshot) -> &[UndoLog<D>] {
182-
&self.undo_log[snapshot.length..]
186+
&self.undo_log[snapshot.undo_len..]
183187
}
184188

185189
fn assert_open_snapshot(&self, snapshot: &Snapshot) {
186190
// Failures here may indicate a failure to follow a stack discipline.
187-
assert!(self.undo_log.len() >= snapshot.length);
191+
assert!(self.undo_log.len() >= snapshot.undo_len);
188192
assert!(self.num_open_snapshots > 0);
189193
}
190194

191195
pub fn rollback_to(&mut self, snapshot: Snapshot) {
192-
debug!("rollback_to({})", snapshot.length);
196+
debug!("rollback_to({})", snapshot.undo_len);
193197

194198
self.assert_open_snapshot(&snapshot);
195199

196-
while self.undo_log.len() > snapshot.length {
200+
while self.undo_log.len() > snapshot.undo_len {
197201
match self.undo_log.pop().unwrap() {
198202
NewElem(i) => {
199203
self.values.pop();
@@ -216,15 +220,15 @@ impl<D: SnapshotVecDelegate> SnapshotVec<D> {
216220
/// Commits all changes since the last snapshot. Of course, they
217221
/// can still be undone if there is a snapshot further out.
218222
pub fn commit(&mut self, snapshot: Snapshot) {
219-
debug!("commit({})", snapshot.length);
223+
debug!("commit({})", snapshot.undo_len);
220224

221225
self.assert_open_snapshot(&snapshot);
222226

223227
if self.num_open_snapshots == 1 {
224228
// The root snapshot. It's safe to clear the undo log because
225229
// there's no snapshot further out that we might need to roll back
226230
// to.
227-
assert!(snapshot.length == 0);
231+
assert!(snapshot.undo_len == 0);
228232
self.undo_log.clear();
229233
}
230234

src/unify/backing_vec.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#[cfg(feature = "persistent")]
22
use dogged::DVec;
33
use snapshot_vec as sv;
4-
use std::ops;
5-
use std::ops::RangeInclusive;
4+
use std::ops::{self, Range};
65
use std::marker::PhantomData;
76

87
use super::{VarValue, UnifyKey, UnifyValue};
@@ -11,35 +10,31 @@ use super::{VarValue, UnifyKey, UnifyValue};
1110
#[allow(type_alias_bounds)]
1211
type Key<S: UnificationStore> = <S as UnificationStore>::Key;
1312

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

2823
fn start_snapshot(&mut self) -> Self::Snapshot;
2924

3025
fn rollback_to(&mut self, snapshot: Self::Snapshot);
3126

3227
fn commit(&mut self, snapshot: Self::Snapshot);
3328

34-
fn values_since_snapshot(&mut self, snapshot: &Self::Snapshot) -> RangeInclusive<usize> {
35-
snapshot.len()..=self.len()
36-
}
29+
fn values_since_snapshot(&self, snapshot: &Self::Snapshot) -> Range<usize>;
3730

3831
fn reset_unifications(
3932
&mut self,
4033
value: impl FnMut(u32) -> VarValue<Self::Key>,
4134
);
4235

36+
fn len(&self) -> usize;
37+
4338
fn push(&mut self, value: VarValue<Self::Key>);
4439

4540
fn reserve(&mut self, num_new_values: usize);
@@ -66,20 +61,6 @@ impl<K: UnifyKey> Default for InPlace<K> {
6661
}
6762
}
6863

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-
8364
impl<K: UnifyKey> UnificationStore for InPlace<K> {
8465
type Key = K;
8566
type Value = K::Value;
@@ -100,6 +81,11 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
10081
self.values.commit(snapshot);
10182
}
10283

84+
#[inline]
85+
fn values_since_snapshot(&self, snapshot: &Self::Snapshot) -> Range<usize> {
86+
snapshot.value_count..self.len()
87+
}
88+
10389
#[inline]
10490
fn reset_unifications(
10591
&mut self,
@@ -108,6 +94,10 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
10894
self.values.set_all(|i| value(i as u32));
10995
}
11096

97+
fn len(&self) -> usize {
98+
self.values.len()
99+
}
100+
111101
#[inline]
112102
fn push(&mut self, value: VarValue<Self::Key>) {
113103
self.values.push(value);
@@ -159,14 +149,6 @@ impl<K: UnifyKey> Default for Persistent<K> {
159149
}
160150
}
161151

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-
170152
#[cfg(feature = "persistent")]
171153
impl<K: UnifyKey> UnificationStore for Persistent<K> {
172154
type Key = K;
@@ -184,7 +166,11 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
184166
}
185167

186168
#[inline]
187-
fn commit(&mut self, _snapshot: Self::Snapshot) {
169+
fn commit(&mut self, _snapshot: Self::Snapshot) {}
170+
171+
#[inline]
172+
fn values_since_snapshot(&self, snapshot: &Self::Snapshot) -> Range<usize> {
173+
snapshot.len()..self.len()
188174
}
189175

190176
#[inline]
@@ -200,6 +186,10 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
200186
}
201187
}
202188

189+
fn len(&self) -> usize {
190+
self.values.len()
191+
}
192+
203193
#[inline]
204194
fn push(&mut self, value: VarValue<Self::Key>) {
205195
self.values.push(value);

src/unify/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
use std::marker;
3535
use std::fmt::Debug;
36+
use std::ops::Range;
3637

3738
mod backing_vec;
3839
pub use self::backing_vec::{InPlace, UnificationStore};
@@ -299,6 +300,15 @@ impl<S: UnificationStore> UnificationTable<S> {
299300
self.values.len()
300301
}
301302

303+
/// Returns the keys of all variables created since the `snapshot`.
304+
pub fn vars_since_snapshot(
305+
&self,
306+
snapshot: &Snapshot<S>,
307+
) -> Range<S::Key> {
308+
let range = self.values.values_since_snapshot(&snapshot.snapshot);
309+
S::Key::from_index(range.start as u32)..S::Key::from_index(range.end as u32)
310+
}
311+
302312
/// Obtains the current value for a particular key.
303313
/// Not for end-users; they can use `probe_value`.
304314
fn value(&self, key: S::Key) -> &VarValue<S::Key> {

0 commit comments

Comments
 (0)