pcp/term/
identity.rs

1// Copyright 2015 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 model::*;
17use term::ops::*;
18use propagation::events::*;
19use std::marker::PhantomData;
20use concept::*;
21
22#[derive(Clone, Debug, PartialEq, Eq, Copy)]
23pub struct Identity<Domain> {
24  idx: usize,
25  phantom: PhantomData<Domain>
26}
27
28impl<Domain> Identity<Domain> {
29  pub fn new(idx: usize) -> Identity<Domain> {
30    Identity {
31      idx: idx,
32      phantom: PhantomData
33    }
34  }
35
36  pub fn index(&self) -> usize {
37    self.idx
38  }
39}
40
41impl<Domain> DisplayStateful<Model> for Identity<Domain>
42{
43  fn display(&self, model: &Model) {
44    print!("{}", model.var_name(self.idx));
45  }
46}
47
48impl<VStore, Domain> StoreMonotonicUpdate<VStore> for Identity<Domain> where
49 VStore: VStoreConcept<Item=Domain>
50{
51  fn update(&mut self, store: &mut VStore, value: VStore::Item) -> bool {
52    store.update(self, value)
53  }
54}
55
56impl<VStore, Domain> StoreRead<VStore> for Identity<Domain> where
57  VStore: VStoreConcept<Item=Domain>,
58  Domain: Clone
59{
60  fn read(&self, store: &VStore) -> VStore::Item {
61    store[self.idx].clone()
62  }
63}
64
65impl<Domain> ViewDependencies<FDEvent> for Identity<Domain>
66{
67  fn dependencies(&self, event: FDEvent) -> Vec<(usize, FDEvent)> {
68    vec![(self.idx, event)]
69  }
70}
71
72#[cfg(test)]
73mod test {
74  use gcollections::ops::*;
75  use variable::VStoreFD;
76  use term::ops::*;
77  use interval::interval::*;
78
79  type VStore = VStoreFD;
80
81  #[test]
82  fn identity_read_update() {
83    let dom0_10 = (0,10).to_interval();
84    let dom0_5 = (0,5).to_interval();
85    let mut store = VStore::empty();
86    let mut v = store.alloc(dom0_10);
87
88    assert_eq!(v.read(&store), dom0_10);
89    assert_eq!(v.update(&mut store, dom0_5), true);
90    assert_eq!(v.read(&store), dom0_5);
91  }
92}