pcp/variable/memory/
copy_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 gcollections::kind::*;
19use gcollections::ops::*;
20use gcollections::ops::sequence::ordering::*;
21use std::slice;
22use std::ops::{Deref, DerefMut, Index};
23use std::fmt::{Formatter, Display, Error};
24use std::rc::*;
25use std::mem;
26use std::fmt::Debug;
27
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct CopyMemory<Domain>
30{
31  variables: Vec<Domain>
32}
33
34impl<Domain> MemoryConcept for CopyMemory<Domain> where
35  Domain: Clone + Display + Debug
36{}
37
38impl<Domain> ImmutableMemoryConcept for CopyMemory<Domain> where
39  Domain: Clone + Display + Debug
40{}
41
42impl<Domain> Collection for CopyMemory<Domain>
43{
44  type Item = Domain;
45}
46
47impl<Domain> AssociativeCollection for CopyMemory<Domain>
48{
49  type Location = usize;
50}
51
52impl<Domain> Replace for CopyMemory<Domain>
53{
54  fn replace(&mut self, key: usize, dom: Domain) -> Domain {
55    mem::replace(&mut self.variables[key], dom)
56  }
57}
58
59impl<Domain> Index<usize> for CopyMemory<Domain>
60{
61  type Output = Domain;
62  fn index<'a>(&'a self, index: usize) -> &'a Domain {
63    &self.variables[index]
64  }
65}
66
67impl<Domain> CopyMemory<Domain>
68{
69  fn restore(variables: Vec<Domain>) -> CopyMemory<Domain> {
70    CopyMemory {
71      variables: variables
72    }
73  }
74}
75
76impl<Domain> Deref for CopyMemory<Domain>
77{
78  type Target = Vec<Domain>;
79  fn deref(&self) -> &Self::Target {
80    &self.variables
81  }
82}
83
84impl<Domain> DerefMut for CopyMemory<Domain>
85{
86  fn deref_mut(&mut self) -> &mut Self::Target {
87    &mut self.variables
88  }
89}
90
91impl<Domain> Empty for CopyMemory<Domain>
92{
93  fn empty() -> CopyMemory<Domain> {
94    CopyMemory {
95      variables: vec![]
96    }
97  }
98}
99
100impl<Domain> Cardinality for CopyMemory<Domain>
101{
102  type Size = usize;
103
104  fn size(&self) -> usize {
105    self.variables.len()
106  }
107}
108
109impl<Domain> Iterable for CopyMemory<Domain>
110{
111  fn iter<'a>(&'a self) -> slice::Iter<'a, Self::Item> {
112    self.variables.iter()
113  }
114}
115
116impl<Domain> Push<Back> for CopyMemory<Domain>
117{
118  fn push(&mut self, value: Domain) {
119    self.variables.push(value);
120  }
121}
122
123impl<Domain> Display for CopyMemory<Domain> where
124 Domain: Display
125{
126  fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
127    for v in &self.variables {
128      formatter.write_fmt(format_args!("{} ", v))?;
129    }
130    Ok(())
131  }
132}
133
134impl<Domain> Freeze for CopyMemory<Domain> where
135 Domain: Clone
136{
137  type FrozenState = FrozenCopyMemory<Domain>;
138  fn freeze(self) -> Self::FrozenState
139  {
140    FrozenCopyMemory::new(self)
141  }
142}
143
144pub struct FrozenCopyMemory<Domain>
145{
146  variables: Rc<Vec<Domain>>
147}
148
149impl<Domain> FrozenCopyMemory<Domain>
150{
151  fn new(store: CopyMemory<Domain>) -> FrozenCopyMemory<Domain> {
152    FrozenCopyMemory {
153      variables: Rc::new(store.variables)
154    }
155  }
156}
157
158impl<Domain> Snapshot for FrozenCopyMemory<Domain> where
159 Domain: Clone
160{
161  type Label = Rc<Vec<Domain>>;
162  type State = CopyMemory<Domain>;
163
164  fn label(&mut self) -> Self::Label {
165    self.variables.clone()
166  }
167
168  fn restore(self, label: Self::Label) -> Self::State {
169    let variables = Rc::try_unwrap(label).unwrap_or_else(|l| l.deref().clone());
170    CopyMemory::restore(variables)
171  }
172}