Skip to content
Open
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
112 changes: 70 additions & 42 deletions src/unify/backing_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{self, Range};

use undo_log::{Rollback, Snapshots, UndoLogs, VecLog};

use super::{UnifyKey, UnifyValue, VarValue};
use super::{ExtraTraversalData, NoExtraTraversalData, UnifyKey, UnifyValue, VarValue};

#[allow(dead_code)] // rustc BUG
#[allow(type_alias_bounds)]
Expand All @@ -15,9 +15,12 @@ type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
/// 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 UnificationStoreBase: ops::Index<usize, Output = VarValue<Key<Self>>> {
pub trait UnificationStoreBase:
ops::Index<usize, Output = VarValue<Key<Self>, Self::ExtraTraversalData>>
{
type Key: UnifyKey<Value = Self::Value>;
type Value: UnifyValue;
type ExtraTraversalData: ExtraTraversalData<Self::Key>;

fn len(&self) -> usize;

Expand All @@ -27,15 +30,18 @@ pub trait UnificationStoreBase: ops::Index<usize, Output = VarValue<Key<Self>>>
}

pub trait UnificationStoreMut: UnificationStoreBase {
fn reset_unifications(&mut self, value: impl FnMut(u32) -> VarValue<Self::Key>);
fn reset_unifications(
&mut self,
value: impl FnMut(u32) -> VarValue<Self::Key, Self::ExtraTraversalData>,
);

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

fn reserve(&mut self, num_new_values: usize);

fn update<F>(&mut self, index: usize, op: F)
where
F: FnOnce(&mut VarValue<Self::Key>);
F: FnOnce(&mut VarValue<Self::Key, Self::ExtraTraversalData>);
}

pub trait UnificationStore: UnificationStoreMut {
Expand All @@ -55,47 +61,60 @@ pub trait UnificationStore: UnificationStoreMut {
#[derive(Clone, Debug)]
pub struct InPlace<
K: UnifyKey,
V: sv::VecLike<Delegate<K>> = Vec<VarValue<K>>,
L = VecLog<sv::UndoLog<Delegate<K>>>,
TD: ExtraTraversalData<K> = NoExtraTraversalData,
V: sv::VecLike<Delegate<K, TD>> = Vec<VarValue<K, TD>>,
L = VecLog<sv::UndoLog<Delegate<K, TD>>>,
> {
pub(crate) values: sv::SnapshotVec<Delegate<K>, V, L>,
pub(crate) values: sv::SnapshotVec<Delegate<K, TD>, V, L>,
}

// HACK(eddyb) manual impl avoids `Default` bound on `K`.
impl<K: UnifyKey, V: sv::VecLike<Delegate<K>> + Default, L: Default> Default for InPlace<K, V, L> {
impl<
K: UnifyKey,
TD: ExtraTraversalData<K>,
V: sv::VecLike<Delegate<K, TD>> + Default,
L: Default,
> Default for InPlace<K, TD, V, L>
{
fn default() -> Self {
InPlace {
values: sv::SnapshotVec::new(),
}
}
}

impl<K, V, L> UnificationStoreBase for InPlace<K, V, L>
impl<K, TD, V, L> UnificationStoreBase for InPlace<K, TD, V, L>
where
K: UnifyKey,
V: sv::VecLike<Delegate<K>>,
V: sv::VecLike<Delegate<K, TD>>,
TD: ExtraTraversalData<K>,
{
type Key = K;
type Value = K::Value;
type ExtraTraversalData = TD;

fn len(&self) -> usize {
self.values.len()
}
}

impl<K, V, L> UnificationStoreMut for InPlace<K, V, L>
impl<K, TD, V, L> UnificationStoreMut for InPlace<K, TD, V, L>
where
K: UnifyKey,
V: sv::VecLike<Delegate<K>>,
L: UndoLogs<sv::UndoLog<Delegate<K>>>,
TD: ExtraTraversalData<K>,
V: sv::VecLike<Delegate<K, TD>>,
L: UndoLogs<sv::UndoLog<Delegate<K, TD>>>,
{
#[inline]
fn reset_unifications(&mut self, mut value: impl FnMut(u32) -> VarValue<Self::Key>) {
fn reset_unifications(
&mut self,
mut value: impl FnMut(u32) -> VarValue<Self::Key, Self::ExtraTraversalData>,
) {
self.values.set_all(|i| value(i as u32));
}

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

Expand All @@ -107,17 +126,18 @@ where
#[inline]
fn update<F>(&mut self, index: usize, op: F)
where
F: FnOnce(&mut VarValue<Self::Key>),
F: FnOnce(&mut VarValue<Self::Key, Self::ExtraTraversalData>),
{
self.values.update(index, op)
}
}

impl<K, V, L> UnificationStore for InPlace<K, V, L>
impl<K, TD, V, L> UnificationStore for InPlace<K, TD, V, L>
where
K: UnifyKey,
V: sv::VecLike<Delegate<K>>,
L: Snapshots<sv::UndoLog<Delegate<K>>>,
TD: ExtraTraversalData<K>,
V: sv::VecLike<Delegate<K, TD>>,
L: Snapshots<sv::UndoLog<Delegate<K, TD>>>,
{
type Snapshot = sv::Snapshot<L::Snapshot>;

Expand All @@ -142,43 +162,46 @@ where
}
}

impl<K, V, L> ops::Index<usize> for InPlace<K, V, L>
impl<K, TD, V, L> ops::Index<usize> for InPlace<K, TD, V, L>
where
V: sv::VecLike<Delegate<K>>,
V: sv::VecLike<Delegate<K, TD>>,
K: UnifyKey,
TD: ExtraTraversalData<K>,
{
type Output = VarValue<K>;
fn index(&self, index: usize) -> &VarValue<K> {
type Output = VarValue<K, TD>;
fn index(&self, index: usize) -> &VarValue<K, TD> {
&self.values[index]
}
}

#[doc(hidden)]
#[derive(Copy, Clone, Debug)]
pub struct Delegate<K>(PhantomData<K>);
pub struct Delegate<K, TD>(PhantomData<(K, TD)>);

impl<K: UnifyKey> sv::SnapshotVecDelegate for Delegate<K> {
type Value = VarValue<K>;
impl<K: UnifyKey, TD: ExtraTraversalData<K>> sv::SnapshotVecDelegate for Delegate<K, TD> {
type Value = VarValue<K, TD>;
type Undo = ();

fn reverse(_: &mut Vec<VarValue<K>>, _: ()) {}
fn reverse(_: &mut Vec<VarValue<K, TD>>, _: ()) {}
}

impl<K: UnifyKey> Rollback<sv::UndoLog<Delegate<K>>> for super::UnificationTableStorage<K> {
fn reverse(&mut self, undo: sv::UndoLog<Delegate<K>>) {
impl<K: UnifyKey, TD: ExtraTraversalData<K>> Rollback<sv::UndoLog<Delegate<K, TD>>>
for super::UnificationTableStorage<K, TD>
{
fn reverse(&mut self, undo: sv::UndoLog<Delegate<K, TD>>) {
self.values.values.reverse(undo);
}
}

#[cfg(feature = "persistent")]
#[derive(Clone, Debug)]
pub struct Persistent<K: UnifyKey> {
values: DVec<VarValue<K>>,
pub struct Persistent<K: UnifyKey, TD: ExtraTraversalData<K>> {
values: DVec<VarValue<K, TD>>,
}

// HACK(eddyb) manual impl avoids `Default` bound on `K`.
#[cfg(feature = "persistent")]
impl<K: UnifyKey> Default for Persistent<K> {
impl<K: UnifyKey, TD: ExtraTraversalData<K>> Default for Persistent<K, TD> {
fn default() -> Self {
Persistent {
values: DVec::new(),
Expand All @@ -187,19 +210,23 @@ impl<K: UnifyKey> Default for Persistent<K> {
}

#[cfg(feature = "persistent")]
impl<K: UnifyKey> UnificationStoreBase for Persistent<K> {
impl<K: UnifyKey, TD: ExtraTraversalData<K>> UnificationStoreBase for Persistent<K, TD> {
type Key = K;
type Value = K::Value;
type ExtraTraversalData = TD;

fn len(&self) -> usize {
self.values.len()
}
}

#[cfg(feature = "persistent")]
impl<K: UnifyKey> UnificationStoreMut for Persistent<K> {
impl<K: UnifyKey, TD: ExtraTraversalData<K>> UnificationStoreMut for Persistent<K, TD> {
#[inline]
fn reset_unifications(&mut self, mut value: impl FnMut(u32) -> VarValue<Self::Key>) {
fn reset_unifications(
&mut self,
mut value: impl FnMut(u32) -> VarValue<Self::Key, Self::ExtraTraversalData>,
) {
// Without extending dogged, there isn't obviously a more
// efficient way to do this. But it's pretty dumb. Maybe
// dogged needs a `map`.
Expand All @@ -209,7 +236,7 @@ impl<K: UnifyKey> UnificationStoreMut for Persistent<K> {
}

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

Expand All @@ -221,15 +248,15 @@ impl<K: UnifyKey> UnificationStoreMut for Persistent<K> {
#[inline]
fn update<F>(&mut self, index: usize, op: F)
where
F: FnOnce(&mut VarValue<Self::Key>),
F: FnOnce(&mut VarValue<Self::Key, Self::ExtraTraversalData>),
{
let p = &mut self.values[index];
op(p);
}
}

#[cfg(feature = "persistent")]
impl<K: UnifyKey> UnificationStore for Persistent<K> {
impl<K: UnifyKey, TD: ExtraTraversalData<K>> UnificationStore for Persistent<K, TD> {
type Snapshot = Self;

#[inline]
Expand All @@ -252,12 +279,13 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
}

#[cfg(feature = "persistent")]
impl<K> ops::Index<usize> for Persistent<K>
impl<K, TD> ops::Index<usize> for Persistent<K, TD>
where
K: UnifyKey,
TD: ExtraTraversalData<K>,
{
type Output = VarValue<K>;
fn index(&self, index: usize) -> &VarValue<K> {
type Output = VarValue<K, TD>;
fn index(&self, index: usize) -> &VarValue<K, TD> {
&self.values[index]
}
}
Loading