Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a values_since_snapshot method to UnificationStore #19

Merged
merged 2 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/snapshot_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<D> fmt::Debug for SnapshotVec<D>
// Snapshots are tokens that should be created/consumed linearly.
pub struct Snapshot {
// Length of the undo log at the time the snapshot was taken.
length: usize,
pub(crate) length: usize,
}

pub trait SnapshotVecDelegate {
Expand Down
47 changes: 33 additions & 14 deletions src/unify/backing_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -10,29 +11,35 @@ use super::{VarValue, UnifyKey, UnifyValue};
#[allow(type_alias_bounds)]
type Key<S: UnificationStore> = <S as UnificationStore>::Key;

pub trait Measurable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a doc comment to this trait? Maybe explain what this len represents? (I realize the old method didn't have a comment, sorry, I'm a hypocrite)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've named this trait Length, really.

Or rely on ExactSizeIterator::len somehow.

Copy link
Member Author

@varkor varkor Mar 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered that (and seeing whether I could incorporate ExactSizeIterator::len), but it didn't quite seem accurate: I wanted a word for something that had a length, rather than was a length. It's only supposed to be used internally though, so it's not too important.

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<usize, Output = VarValue<Key<Self>>> + Clone + Default
ops::Index<usize, Output = VarValue<Key<Self>>> + Measurable + Clone + Default
{
type Key: UnifyKey<Value = Self::Value>;
type Value: UnifyValue;
type Snapshot;
type Snapshot: Measurable;

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

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

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

fn values_since_snapshot(&mut self, snapshot: &Self::Snapshot) -> RangeInclusive<usize> {
snapshot.len()..=self.len()
}

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

fn len(&self) -> usize;

fn push(&mut self, value: VarValue<Self::Key>);

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

impl Measurable for sv::Snapshot {
#[inline]
fn len(&self) -> usize {
self.length
}
}

impl<K: UnifyKey> Measurable for InPlace<K> {
#[inline]
fn len(&self) -> usize {
self.values.len()
}
}

impl<K: UnifyKey> UnificationStore for InPlace<K> {
type Key = K;
type Value = K::Value;
Expand Down Expand Up @@ -87,11 +108,6 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
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::Key>) {
self.values.push(value);
Expand Down Expand Up @@ -143,6 +159,14 @@ impl<K: UnifyKey> Default for Persistent<K> {
}
}

#[cfg(feature = "persistent")]
impl<K: UnifyKey> Measurable for Persistent<K> {
#[inline]
fn len(&self) -> usize {
self.values.len()
}
}

#[cfg(feature = "persistent")]
impl<K: UnifyKey> UnificationStore for Persistent<K> {
type Key = K;
Expand Down Expand Up @@ -176,11 +200,6 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
}
}

#[inline]
fn len(&self) -> usize {
self.values.len()
}

#[inline]
fn push(&mut self, value: VarValue<Self::Key>) {
self.values.push(value);
Expand Down