pcp/variable/memory/trail/
timestamp_trail.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 variable::memory::trail::SingleValueTrail;
16
17use variable::memory::ops::*;
18use gcollections::ops::*;
19use gcollections::kind::*;
20use vec_map::VecMap;
21use std::fmt::{Formatter, Display, Error};
22
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct TimestampTrail<Domain>
25{
26  trail: SingleValueTrail<Domain>,
27  /// Contains the change on the domain of the current instant.
28  timestamp: VecMap<Domain>
29}
30
31impl<Domain> Collection for TimestampTrail<Domain>
32{
33  type Item = Domain;
34}
35
36impl<Domain> AssociativeCollection for TimestampTrail<Domain>
37{
38  type Location = usize;
39}
40
41impl<Domain> TrailVariable for TimestampTrail<Domain>
42{
43  /// We do not trail the intermediate value, just the first change.
44  fn trail_variable(&mut self, loc: usize, value: Domain) {
45    self.timestamp.entry(loc).or_insert(value);
46  }
47}
48
49impl<Domain> TrailRestoration for TimestampTrail<Domain> where
50 Domain: Clone
51{
52  type Mark = usize;
53
54  fn commit(&mut self) {
55    for (loc, value) in self.timestamp.drain() {
56      self.trail.trail_variable(loc, value.clone());
57    }
58  }
59
60  fn mark(&mut self) -> Self::Mark {
61    self.trail.mark()
62  }
63
64  fn undo(&mut self, mark: Self::Mark, memory: &mut Vec<Domain>) {
65    self.trail.undo(mark, memory);
66  }
67}
68
69impl<Domain> Display for TimestampTrail<Domain> where
70 Domain: Display
71{
72  fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
73    self.trail.fmt(formatter)
74  }
75}
76
77impl<Domain> Empty for TimestampTrail<Domain>
78{
79  fn empty() -> TimestampTrail<Domain> {
80    TimestampTrail {
81      trail: SingleValueTrail::empty(),
82      timestamp: VecMap::new()
83    }
84  }
85}