Skip to content

Commit

Permalink
[VStore][Restoration] Implements basic operation on the copy store. R…
Browse files Browse the repository at this point in the history
…elated to #20.
  • Loading branch information
ptal committed Mar 27, 2016
1 parent 1a1f619 commit 04a8973
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
58 changes: 57 additions & 1 deletion src/libpcp/variable/memory/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
// limitations under the License.

use variable::memory::ops::*;
use std::ops::{DerefMut, Deref};
use gcollections::ops::cardinality::*;
use gcollections::ops::sequence::*;
use gcollections::ops::sequence::ordering::*;
use std::ops::{DerefMut, Deref, Index};
use std::fmt::{Formatter, Display, Error};
use std::rc::*;
use std::mem;

pub struct CopyStore<Domain>
{
Expand All @@ -36,6 +41,57 @@ impl<Domain> CopyStore<Domain>
}
}

impl<Domain> Cardinality for CopyStore<Domain> {
type Size = usize;

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


impl<'a, Domain> IntoIterator for &'a CopyStore<Domain> {
type Item = &'a Domain;
type IntoIter = ::std::slice::Iter<'a, Domain>;

fn into_iter(self) -> Self::IntoIter {
self.variables.iter()
}
}

impl<Domain> Push<Back, Domain> for CopyStore<Domain> {
fn push(&mut self, value: Domain) {
self.variables.push(value);
}
}

impl<Domain> Update<usize, Domain> for CopyStore<Domain>
{
fn update(&mut self, key: usize, mut dom: Domain) -> Option<Domain> {
mem::swap(&mut dom, &mut self.variables[key]);
Some(dom)
}
}

impl<Domain> Index<usize> for CopyStore<Domain>
{
type Output = Domain;
fn index<'a>(&'a self, index: usize) -> &'a Domain {
&self.variables[index]
}
}

impl<Domain> Display for CopyStore<Domain> where
Domain: Display
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
for v in &self.variables {
try!(formatter.write_fmt(format_args!("{} ", v)));
}
Ok(())
}
}

impl<Domain> Freeze for CopyStore<Domain> where
Domain: Clone
{
Expand Down
17 changes: 17 additions & 0 deletions src/libpcp/variable/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@

pub mod ops;
pub mod copy;

use variable::memory::ops::*;
use gcollections::ops::cardinality::*;
use gcollections::ops::sequence::*;
use gcollections::ops::sequence::ordering::*;
use std::ops::Index;
use std::fmt::Display;

pub trait MemoryConcept<Domain> :
Cardinality<Size=usize>
+ IntoIterator
+ Push<Back, Domain>
+ Update<usize, Domain>
+ Index<usize, Output=Domain>
+ Freeze
+ Display
{}
9 changes: 7 additions & 2 deletions src/libpcp/variable/memory/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub use term::ops::*;

pub trait Freeze
{
type FrozenState : Snapshot;
Expand All @@ -27,3 +25,10 @@ pub trait Snapshot {
fn snapshot(&mut self) -> Self::Label;
fn restore(self, label: Self::Label) -> Self::UnfrozenState;
}

pub trait Update<Key, Value>
{
// `None` if the update operation failed.
// `Some(value)` if it succeeded, where `value` is the old one.
fn update(&mut self, key: Key, value: Value) -> Option<Value>;
}

0 comments on commit 04a8973

Please sign in to comment.