Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
function.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$ by $Author$
13  * $Revision$
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 { namespace Kernel {
43 
45  class FunctionBranch : public Brancher {
46  protected:
48  class Description : public Choice {
49  public:
51  Description(const Brancher& b, unsigned int a);
53  virtual void archive(Archive& e) const;
54  };
58  bool done;
60  FunctionBranch(Home home, std::function<void(Space& home)> f0);
63  public:
65  virtual bool status(const Space& home) const;
67  virtual const Choice* choice(Space& home);
69  virtual const Choice* choice(const Space& home, Archive& a);
71  virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
73  virtual void print(const Space&, const Choice&, unsigned int,
74  std::ostream& o) const;
76  virtual Actor* copy(Space& home);
78  static void post(Home home, std::function<void(Space& home)> f);
80  virtual size_t dispose(Space& home);
81  };
82 
85  : Choice(b,a) {}
86  void
88  Choice::archive(e);
89  }
90 
93  std::function<void(Space& home)> f0)
94  : Brancher(home), f(f0), done(false) {
95  if (!f())
96  throw InvalidFunction("FunctionBranch::FunctionBranch");
97  home.notice(*this,AP_DISPOSE);
98  }
101  : Brancher(home,b), f(b.f), done(b.done) {
102  }
103  bool
105  return !done;
106  }
107  const Choice*
109  assert(!done);
110  return new Description(*this,1);
111  }
112  const Choice*
114  return new Description(*this,1);
115  }
116  ExecStatus
117  FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
118  done = true;
120  f()(home);
121  return home.failed() ? ES_FAILED : ES_OK;
122  }
123  void
124  FunctionBranch::print(const Space&, const Choice&, unsigned int,
125  std::ostream& o) const {
126  o << "FunctionBranch()";
127  }
128  Actor*
130  return new (home) FunctionBranch(home,*this);
131  }
132  forceinline void
133  FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
134  if (!f)
135  throw InvalidFunction("FunctionBranch::post");
136  (void) new (home) FunctionBranch(home,f);
137  }
138  size_t
140  home.ignore(*this,AP_DISPOSE);
141  f.~SharedData<std::function<void(Space& home)>>();
142  (void) Brancher::dispose(home);
143  return sizeof(*this);
144  }
145 
146 }}
147 
148 namespace Gecode {
149 
150  void
151  branch(Home home, std::function<void(Space& home)> f) {
153  }
154 
155 }
156 
157 // STATISTICS: kernel-branch
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition: function.cpp:56
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
virtual void archive(Archive &e) const
Archive into e.
Definition: function.cpp:87
Actor must always be disposed.
Definition: core.hpp:554
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition: function.cpp:133
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition: function.cpp:124
#define forceinline
Definition: config.hpp:182
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition: function.cpp:84
Computation spaces.
Definition: core.hpp:1668
Base-class for both propagators and branchers.
Definition: core.hpp:620
Base-class for branchers.
Definition: core.hpp:1368
Execution has resulted in failure.
Definition: core.hpp:466
~SharedData(void)
Destructors.
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3914
virtual const Choice * choice(Space &home)
Return choice.
Definition: function.cpp:108
Minimal brancher description storing no information.
Definition: function.cpp:48
Exception: invalid function
Definition: exception.hpp:118
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:98
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:3944
virtual void archive(Archive &e) const
Archive into e.
Definition: core.cpp:857
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
Choice for performing commit
Definition: core.hpp:1338
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3929
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3172
Class for sharing data between spaces.
Definition: shared-data.hpp:42
Archive representation
Definition: archive.hpp:46
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition: function.cpp:117
ExecStatus
Definition: core.hpp:464
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition: function.cpp:92
Execution is okay.
Definition: core.hpp:468
Gecode toplevel namespace
bool done
Call function just once.
Definition: function.cpp:58
Home class for posting propagators
Definition: core.hpp:846
Brancher for calling a function
Definition: function.cpp:45
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition: function.cpp:104
virtual Actor * copy(Space &home)
Copy brancher.
Definition: function.cpp:129
virtual size_t dispose(Space &home)
Dispose brancher.
Definition: function.cpp:139