Generated on Sat Jun 2 2018 07:17:44 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  * This file is part of Gecode, the generic constraint
12  * development environment:
13  * http://www.gecode.org
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining
16  * a copy of this software and associated documentation files (the
17  * "Software"), to deal in the Software without restriction, including
18  * without limitation the rights to use, copy, modify, merge, publish,
19  * distribute, sublicense, and/or sell copies of the Software, and to
20  * permit persons to whom the Software is furnished to do so, subject to
21  * the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 #include <gecode/kernel.hh>
37 
38 namespace Gecode { namespace Kernel {
39 
41  class FunctionBranch : public Brancher {
42  protected:
44  class Description : public Choice {
45  public:
47  Description(const Brancher& b, unsigned int a);
49  virtual void archive(Archive& e) const;
50  };
54  bool done;
56  FunctionBranch(Home home, std::function<void(Space& home)> f0);
59  public:
61  virtual bool status(const Space& home) const;
63  virtual const Choice* choice(Space& home);
65  virtual const Choice* choice(const Space& home, Archive& a);
67  virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
69  virtual void print(const Space&, const Choice&, unsigned int,
70  std::ostream& o) const;
72  virtual Actor* copy(Space& home);
74  static void post(Home home, std::function<void(Space& home)> f);
76  virtual size_t dispose(Space& home);
77  };
78 
81  : Choice(b,a) {}
82  void
84  Choice::archive(e);
85  }
86 
89  std::function<void(Space& home)> f0)
90  : Brancher(home), f(f0), done(false) {
91  if (!f())
92  throw InvalidFunction("FunctionBranch::FunctionBranch");
93  home.notice(*this,AP_DISPOSE);
94  }
97  : Brancher(home,b), f(b.f), done(b.done) {
98  }
99  bool
101  return !done;
102  }
103  const Choice*
105  assert(!done);
106  return new Description(*this,1);
107  }
108  const Choice*
110  return new Description(*this,1);
111  }
112  ExecStatus
113  FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
114  done = true;
116  f()(home);
117  return home.failed() ? ES_FAILED : ES_OK;
118  }
119  void
120  FunctionBranch::print(const Space&, const Choice&, unsigned int,
121  std::ostream& o) const {
122  o << "FunctionBranch()";
123  }
124  Actor*
126  return new (home) FunctionBranch(home,*this);
127  }
128  forceinline void
129  FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
130  if (!f)
131  throw InvalidFunction("FunctionBranch::post");
132  (void) new (home) FunctionBranch(home,f);
133  }
134  size_t
136  home.ignore(*this,AP_DISPOSE);
137  f.~SharedData<std::function<void(Space& home)>>();
138  (void) Brancher::dispose(home);
139  return sizeof(*this);
140  }
141 
142 }}
143 
144 namespace Gecode {
145 
146  void
147  branch(Home home, std::function<void(Space& home)> f) {
149  }
150 
151 }
152 
153 // STATISTICS: kernel-branch
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition: function.cpp:52
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:39
virtual void archive(Archive &e) const
Archive into e.
Definition: function.cpp:83
Actor must always be disposed.
Definition: core.hpp:561
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition: function.cpp:129
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition: function.cpp:120
#define forceinline
Definition: config.hpp:185
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition: function.cpp:80
Computation spaces.
Definition: core.hpp:1701
Base-class for both propagators and branchers.
Definition: core.hpp:627
Base-class for branchers.
Definition: core.hpp:1401
Execution has resulted in failure.
Definition: core.hpp:473
~SharedData(void)
Destructors.
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3963
virtual const Choice * choice(Space &home)
Return choice.
Definition: function.cpp:104
Minimal brancher description storing no information.
Definition: function.cpp:44
Exception: invalid function
Definition: exception.hpp:114
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:94
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:3993
virtual void archive(Archive &e) const
Archive into e.
Definition: core.cpp:863
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
Choice for performing commit
Definition: core.hpp:1371
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3978
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3209
Class for sharing data between spaces.
Definition: shared-data.hpp:38
Archive representation
Definition: archive.hpp:42
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition: function.cpp:113
ExecStatus
Definition: core.hpp:471
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition: function.cpp:88
Execution is okay.
Definition: core.hpp:475
Gecode toplevel namespace
bool done
Call function just once.
Definition: function.cpp:54
Home class for posting propagators
Definition: core.hpp:853
Brancher for calling a function
Definition: function.cpp:41
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition: function.cpp:100
virtual Actor * copy(Space &home)
Copy brancher.
Definition: function.cpp:125
virtual size_t dispose(Space &home)
Dispose brancher.
Definition: function.cpp:135