Skip to content

Commit

Permalink
Add values_since_snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Mar 14, 2019
1 parent a7b40f0 commit bdbaa65
Showing 1 changed file with 33 additions and 14 deletions.
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 {
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

0 comments on commit bdbaa65

Please sign in to comment.