pcp/search/
search_tree_visitor.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 search::search_tree_visitor::Status::*;
17use search::branching::branch::*;
18use std::cmp::PartialEq;
19use std::fmt::{Debug, Formatter, Error};
20
21pub enum Status<Space> where
22  Space: Freeze
23{
24  Satisfiable,
25  Unsatisfiable,
26  Unknown(Vec<Branch<Space>>),
27  EndOfSearch
28}
29
30impl<Space> Status<Space> where
31 Space: Freeze
32{
33  pub fn pruned() -> Status<Space> {
34    Unknown(vec![])
35  }
36}
37
38impl<Space> Debug for Status<Space> where
39  Space: Freeze
40{
41  fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
42    let name = match self {
43      &Satisfiable => "Satisfiable",
44      &Unsatisfiable => "Unsatisfiable",
45      &Unknown(ref branches) if branches.is_empty() => "Pruned",
46      &Unknown(_) => "Unknown",
47      &EndOfSearch => "End of search"
48    };
49    formatter.write_str(name)
50  }
51}
52
53impl<Space> PartialEq for Status<Space> where
54  Space: Freeze
55{
56  fn eq(&self, other: &Status<Space>) -> bool {
57    match (self, other) {
58      (&Satisfiable, &Satisfiable) => true,
59      (&Unsatisfiable, &Unsatisfiable) => true,
60      (&Unknown(ref b1), &Unknown(ref b2)) if b1.is_empty() && b2.is_empty() => true,
61      (&Unknown(_), &Unknown(_)) => panic!("Cannot compare unknown status."),
62      (&EndOfSearch, &EndOfSearch) => true,
63      (_, _) => false,
64    }
65  }
66}
67
68pub trait SearchTreeVisitor<Space> where
69  Space: Freeze
70{
71  fn start(&mut self, _space: &Space) {}
72  fn enter(&mut self, space: Space) -> (Space::FrozenState, Status<Space>);
73}