Skip to content

Commit 72412a0

Browse files
committed
add a reset_unifications method
1 parent bd11122 commit 72412a0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/snapshot_vec.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ impl<D: SnapshotVecDelegate> SnapshotVec<D> {
141141
}
142142
}
143143

144+
/// Updates all elements. Potentially more efficient -- but
145+
/// otherwise equivalent to -- invoking `set` for each element.
146+
pub fn set_all(&mut self, mut new_elems: impl FnMut(usize) -> D::Value) {
147+
if !self.in_snapshot() {
148+
for (slot, index) in self.values.iter_mut().zip(0..) {
149+
*slot = new_elems(index);
150+
}
151+
} else {
152+
for i in 0..self.values.len() {
153+
self.set(i, new_elems(i));
154+
}
155+
}
156+
}
157+
144158
pub fn update<OP>(&mut self, index: usize, op: OP)
145159
where
146160
OP: FnOnce(&mut D::Value),

src/unify/backing_vec.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ pub trait UnificationStore: ops::Index<usize, Output = VarValue<Key<Self>>> + Cl
2525

2626
fn commit(&mut self, snapshot: Self::Snapshot);
2727

28+
fn reset_unifications(
29+
&mut self,
30+
value: impl FnMut(u32) -> VarValue<Self::Key>,
31+
);
32+
2833
fn len(&self) -> usize;
2934

3035
fn push(&mut self, value: VarValue<Self::Key>);
@@ -71,6 +76,14 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
7176
self.values.commit(snapshot);
7277
}
7378

79+
#[inline]
80+
fn reset_unifications(
81+
&mut self,
82+
mut value: impl FnMut(u32) -> VarValue<Self::Key>,
83+
) {
84+
self.values.set_all(|i| value(i as u32));
85+
}
86+
7487
#[inline]
7588
fn len(&self) -> usize {
7689
self.values.len()
@@ -144,6 +157,19 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
144157
fn commit(&mut self, _snapshot: Self::Snapshot) {
145158
}
146159

160+
#[inline]
161+
fn reset_unifications(
162+
&mut self,
163+
mut value: impl FnMut(u32) -> VarValue<Self::Key>,
164+
) {
165+
// Without extending dogged, there isn't obviously a more
166+
// efficient way to do this. But it's pretty dumb. Maybe
167+
// dogged needs a `map`.
168+
for i in 0 .. self.values.len() {
169+
self.values[i] = value(i as u32);
170+
}
171+
}
172+
147173
#[inline]
148174
fn len(&self) -> usize {
149175
self.values.len()

src/unify/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,20 @@ impl<S: UnificationStore> UnificationTable<S> {
280280
self.values.reserve(num_new_keys);
281281
}
282282

283+
/// Clears all unifications that have been performed, resetting to
284+
/// the initial state. The values of each variable are given by
285+
/// the closure.
286+
pub fn reset_unifications(
287+
&mut self,
288+
mut value: impl FnMut(S::Key) -> S::Value,
289+
) {
290+
self.values.reset_unifications(|i| {
291+
let key = UnifyKey::from_index(i as u32);
292+
let value = value(key);
293+
VarValue::new_var(key, value)
294+
});
295+
}
296+
283297
/// Returns the number of keys created so far.
284298
pub fn len(&self) -> usize {
285299
self.values.len()

0 commit comments

Comments
 (0)