pcp/propagation/events/
mod.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::Merge;
16use kernel::event::*;
17use propagation::events::FDEvent::*;
18use gcollections::kind::*;
19use gcollections::ops::*;
20use std::cmp::min;
21
22/// Failure or Nothing events are absents on purpose because they are not events that propagators should subscribe to. If a failure occurs, it's over. If nothing occurs, we don't care.
23#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
24pub enum FDEvent {
25  Assignment = 0,
26  Bound = 1,
27  Inner = 2
28}
29
30impl Merge for FDEvent {
31  fn merge(e: FDEvent, f: FDEvent) -> FDEvent {
32    min(e, f)
33  }
34}
35
36impl EventIndex for FDEvent {
37  fn to_index(self) -> usize {
38    self as usize
39  }
40
41  fn size() -> usize {
42    Inner.to_index() + 1
43  }
44}
45
46impl<Domain, Bound> MonotonicEvent<Domain> for FDEvent where
47  Domain: Subset + Cardinality + Bounded + Collection<Item=Bound>,
48  Bound: PartialEq + Eq
49{
50  fn new(little: &Domain, big: &Domain) -> Option<Self>
51  {
52    assert!(little.is_subset(big),
53      "Events are computed on the difference between `little` and `big`.\
54       So `little` must be a subset of `big`.");
55    if little.size() != big.size() {
56      let ev =
57        if little.is_singleton() { Assignment }
58        else if little.lower() != big.lower() ||
59                little.upper() != big.upper() { Bound }
60        else { Inner };
61      Some(ev)
62    } else {
63      None
64    }
65  }
66}