Skip to content

Commit

Permalink
[Store][Variable] Change MonotonicUpdate to Update. Be more generic, …
Browse files Browse the repository at this point in the history
…do not name operation with properties.
  • Loading branch information
ptal committed Mar 25, 2016
1 parent beb864d commit eefe819
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/libpcp/term/addition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<X, V, Domain, Store> StoreMonotonicUpdate<Store, Domain> for Addition<X, V>
}

impl<X, V, Domain, Store> StoreRead<Store> for Addition<X, V> where
Domain: Add<V, Output=Domain> + Clone,
Domain: Add<V, Output=Domain>,
V: Clone,
X: StoreRead<Store, Value=Domain>
{
Expand Down
2 changes: 1 addition & 1 deletion src/libpcp/term/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<Domain> VarIndex for Identity<Domain> {
}

impl<Domain, Store> StoreMonotonicUpdate<Store, Domain> for Identity<Domain> where
Store: MonotonicUpdate<usize, Domain>
Store: Update<usize, Domain>
{
fn update(&self, store: &mut Store, value: Domain) -> bool {
store.update(self.idx, value)
Expand Down
41 changes: 25 additions & 16 deletions src/libpcp/variable/delta_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ impl<Domain, Event> DeltaStore<Domain, Event>
}
}

impl<Domain, Event> DeltaStore<Domain, Event> where
Domain: Bounded + Cardinality + Subset,
Event: MonotonicEvent<Domain> + Merge + Clone
{
// FIXME: Need a rustc fix on borrowing rule, `updated` not needed.
fn merge_delta(&mut self, key: usize, old_dom: Domain) {
if let Some(delta) = Event::new(&self.store[key], &old_dom) {
let mut updated = false;
if let Some(old_delta) = self.delta.get_mut(&key) {
*old_delta = Merge::merge(old_delta.clone(), delta.clone());
updated = true;
}
if !updated {
self.delta.insert(key, delta);
}
}
}
}

impl<Domain, Event> Default for DeltaStore<Domain, Event>
{
fn default() -> DeltaStore<Domain, Event> {
Expand Down Expand Up @@ -113,27 +132,17 @@ impl<Domain, Event> Alloc<Domain> for DeltaStore<Domain, Event> where
}
}

impl<Domain, Event> MonotonicUpdate<usize, Domain> for DeltaStore<Domain, Event> where
impl<Domain, Event> Update<usize, Domain> for DeltaStore<Domain, Event> where
Domain: Bounded + Cardinality + Subset + Clone,
Event: MonotonicEvent<Domain> + Merge + Clone
{
fn update(&mut self, key: usize, dom: Domain) -> bool {
assert!(dom.is_subset(&self.store[key]), "Domain update must be monotonic.");
if dom.is_empty() { false }
else {
if let Some(event) = Event::new(&dom, &self.store[key]) {
let mut updated = false;
if let Some(old) = self.delta.get_mut(&key) {
*old = Merge::merge(old.clone(), event.clone());
updated = true;
}
if !updated {
self.delta.insert(key, event);
}
self.store.update(key, dom);
}
true
let old_dom = self.store[key].clone();
let update_success = self.store.update(key, dom);
if update_success {
self.merge_delta(key, old_dom);
}
update_success
}
}

Expand Down
50 changes: 25 additions & 25 deletions src/libpcp/variable/memory/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,28 @@ impl<Domain> Snapshot for FrozenCopyStore<Domain> where
}
}

#[cfg(test)]
mod test {
use super::*;
use variable::memory::ops::*;
use std::ops::Deref;

fn make_frozen_store(initial_data: Vec<u32>) -> FrozenCopyStore<u32> {
let mut copy_store = CopyStore::new();
copy_store.extend(initial_data);
copy_store.freeze()
}

#[test]
fn save_and_restore_identity() {
let initial_data = vec![1,2,3];
let mut frozen = make_frozen_store(initial_data.clone());
let snapshots: Vec<_> = (1..10).map(|_| frozen.snapshot()).collect();

for snapshot in snapshots {
let copy_store = frozen.restore(snapshot);
assert_eq!(initial_data.clone(), copy_store.deref().clone());
frozen = copy_store.freeze();
}
}
}
// #[cfg(test)]
// mod test {
// use super::*;
// use variable::memory::ops::*;
// use std::ops::Deref;

// fn make_frozen_store(initial_data: Vec<u32>) -> FrozenCopyStore<u32> {
// let mut copy_store = CopyStore::new();
// copy_store.extend(initial_data);
// copy_store.freeze()
// }

// #[test]
// fn save_and_restore_identity() {
// let initial_data = vec![1,2,3];
// let mut frozen = make_frozen_store(initial_data.clone());
// let snapshots: Vec<_> = (1..10).map(|_| frozen.snapshot()).collect();

// for snapshot in snapshots {
// let copy_store = frozen.restore(snapshot);
// assert_eq!(initial_data.clone(), copy_store.deref().clone());
// frozen = copy_store.freeze();
// }
// }
// }
2 changes: 1 addition & 1 deletion src/libpcp/variable/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait Failure
fn is_failed(&self) -> bool;
}

pub trait MonotonicUpdate<Key, Value>
pub trait Update<Key, Value>
{
fn update(&mut self, key: Key, value: Value) -> bool;
}
2 changes: 1 addition & 1 deletion src/libpcp/variable/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<Domain> Alloc<Domain> for Store<Domain> where
}
}

impl<Domain> MonotonicUpdate<usize, Domain> for Store<Domain> where
impl<Domain> Update<usize, Domain> for Store<Domain> where
Domain: Cardinality + Subset
{
fn update(&mut self, key: usize, dom: Domain) -> bool {
Expand Down

0 comments on commit eefe819

Please sign in to comment.