Generated on Tue Oct 22 2013 00:49:00 for Gecode by doxygen 1.8.4
branch.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2008
9  * Mikael Lagerkvist, 2008
10  *
11  * Last modified:
12  * $Date: 2013-05-03 10:00:50 +0200 (Fri, 03 May 2013) $ by $Author: schulte $
13  * $Revision: 13606 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 #include <gecode/kernel.hh>
41 
42 namespace Gecode {
43 
46  protected:
49  public:
51  Description(const Brancher& b, unsigned int a) : Choice(b,a) {}
53  virtual size_t size(void) const { return sizeof(Description); }
55  virtual void archive(Archive& e) const {
56  Choice::archive(e);
57  }
58  };
60  void (*f)(Space&);
62  bool done;
64  FunctionBranch(Home home, void (*f0)(Space&))
65  : Brancher(home), f(f0), done(false) {}
67  FunctionBranch(Space& home, bool share, FunctionBranch& b)
68  : Brancher(home,share,b), f(b.f), done(b.done) {}
69  public:
71  virtual bool status(const Space&) const {
72  return !done;
73  }
75  virtual const Choice* choice(Space&) {
76  assert(!done);
77  return new Description(*this,1);
78  }
80  virtual const Choice* choice(const Space&, Archive&) {
81  return new Description(*this,1);
82  }
84  virtual ExecStatus
85  commit(Space& home, const Choice&, unsigned int) {
86  done = true;
87  f(home);
88  return home.failed() ? ES_FAILED : ES_OK;
89  }
91  virtual void
92  print(const Space&, const Choice&, unsigned int,
93  std::ostream& o) const {
94  o << "FunctionBranch(" << f << ")";
95  }
97  virtual Actor* copy(Space& home, bool share) {
98  return new (home) FunctionBranch(home,share,*this);
99  }
101  static void post(Home home, void (*f)(Space&)) {
102  (void) new (home) FunctionBranch(home,f);
103  }
104  };
105 
106 
107  void
108  branch(Home home, void (*f)(Space& home)) {
109  if (home.failed())
110  return;
111  FunctionBranch::post(home,f);
112  }
113 
114 }
115 
116 // STATISTICS: kernel-branch