pcp/variable/memory/
trail_memory.rs

1// Copyright 2016 Pierre Talbot (IRCAM)
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use kernel::*;
16use variable::concept::*;
17use variable::ops::*;
18use variable::memory::copy_memory::*;
19use variable::memory::ops::*;
20use gcollections::kind::*;
21use gcollections::ops::*;
22use gcollections::ops::sequence::ordering::*;
23use std::slice;
24use std::ops::{Index, DerefMut};
25use std::fmt::{Formatter, Display, Error, Debug};
26
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct TrailMemory<Trail, Domain>
29{
30  variables: CopyMemory<Domain>,
31  trail: Trail
32}
33
34impl<Trail, Domain> MemoryConcept for TrailMemory<Trail, Domain> where
35 Trail: TrailRestoration + AssociativeCollection<Location=usize, Item=Domain> + TrailVariable + Empty + Debug,
36 Domain: Clone + Display + Debug
37{}
38
39impl<Trail, Domain> ImmutableMemoryConcept for TrailMemory<Trail, Domain> where
40 Trail: TrailRestoration + AssociativeCollection<Location=usize, Item=Domain> + Empty + Debug,
41 Domain: Clone + Display + Debug
42{}
43
44impl<Trail, Domain> Collection for TrailMemory<Trail, Domain>
45{
46  type Item = Domain;
47}
48
49impl<Trail, Domain> AssociativeCollection for TrailMemory<Trail, Domain>
50{
51  type Location = usize;
52}
53
54impl<Trail, Domain> Empty for TrailMemory<Trail, Domain> where
55 Trail: Empty
56{
57  fn empty() -> TrailMemory<Trail, Domain> {
58    TrailMemory {
59      variables: CopyMemory::empty(),
60      trail: Trail::empty()
61    }
62  }
63}
64
65impl<Trail, Domain> Cardinality for TrailMemory<Trail, Domain>
66{
67  type Size = usize;
68  fn size(&self) -> usize {
69    self.variables.size()
70  }
71}
72
73impl<Trail, Domain> Iterable for TrailMemory<Trail, Domain>
74{
75  fn iter<'a>(&'a self) -> slice::Iter<'a, Domain> {
76    self.variables.iter()
77  }
78}
79
80impl<Trail, Domain> Push<Back> for TrailMemory<Trail, Domain> where
81 Trail: TrailVariable + AssociativeCollection<Location=usize, Item=Domain>,
82 Domain: Clone
83{
84  fn push(&mut self, dom: Domain) {
85    self.variables.push(dom);
86  }
87}
88
89impl<Trail, Domain> Replace for TrailMemory<Trail, Domain> where
90 Trail: TrailVariable + AssociativeCollection<Location=usize, Item=Domain>,
91 Domain: Clone
92{
93  fn replace(&mut self, key: usize, dom: Domain) -> Domain
94  {
95    let dom = self.variables.replace(key, dom);
96    self.trail.trail_variable(key, dom.clone());
97    dom
98  }
99}
100
101impl<Trail, Domain> Index<usize> for TrailMemory<Trail, Domain>
102{
103  type Output = Domain;
104  fn index<'a>(&'a self, index: usize) -> &'a Domain {
105    &self.variables[index]
106  }
107}
108
109impl<Trail, Domain> Display for TrailMemory<Trail, Domain> where
110 Domain: Display
111{
112  fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
113    self.variables.fmt(formatter)
114  }
115}
116
117impl<Trail, Domain> Freeze for TrailMemory<Trail, Domain> where
118 Trail: TrailRestoration + Collection<Item=Domain>
119{
120  type FrozenState = FrozenTrailMemory<Trail, Domain>;
121  fn freeze(mut self) -> Self::FrozenState
122  {
123    self.trail.commit();
124    FrozenTrailMemory::new(self)
125  }
126}
127
128pub struct FrozenTrailMemory<Trail, Domain>
129{
130  store: TrailMemory<Trail, Domain>
131}
132
133impl<Trail, Domain> FrozenTrailMemory<Trail, Domain>
134{
135  fn new(store: TrailMemory<Trail, Domain>) -> FrozenTrailMemory<Trail, Domain> {
136    FrozenTrailMemory {
137      store: store
138    }
139  }
140}
141
142impl<Trail, Domain> Snapshot for FrozenTrailMemory<Trail, Domain> where
143 Trail: TrailRestoration + Collection<Item=Domain>
144{
145  type Label = Trail::Mark;
146  type State = TrailMemory<Trail, Domain>;
147
148  fn label(&mut self) -> Self::Label {
149    self.store.trail.mark()
150  }
151
152  fn restore(mut self, label: Self::Label) -> Self::State {
153    self.store.trail.undo(label, self.store.variables.deref_mut());
154    self.store
155  }
156}