Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
38namespace 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 };
51
52 SharedData<std::function<void(Space& home)>> f;
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
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 }
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
144namespace Gecode {
145
146 void
147 branch(Home home, std::function<void(Space& home)> f) {
149 }
150
151}
152
153// STATISTICS: kernel-branch
Base-class for both propagators and branchers.
Definition core.hpp:628
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3256
Archive representation
Definition archive.hpp:42
friend class Space
Definition core.hpp:1446
Brancher(Home home)
Constructor for creation.
Definition core.hpp:3612
friend class Choice
Definition core.hpp:1447
Choice for performing commit
Definition core.hpp:1414
Choice(const Brancher &b, const unsigned int a)
Initialize for particular brancher b and alternatives a.
Definition core.hpp:3773
virtual void archive(Archive &e) const
Archive into e.
Definition core.cpp:892
Home class for posting propagators
Definition core.hpp:856
Exception: invalid function
Minimal brancher description storing no information.
Definition function.cpp:44
virtual void archive(Archive &e) const
Archive into e.
Definition function.cpp:83
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition function.cpp:80
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition function.cpp:52
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition function.cpp:113
virtual const Choice * choice(Space &home)
Return choice.
Definition function.cpp:104
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition function.cpp:100
bool done
Call function just once.
Definition function.cpp:54
virtual Actor * copy(Space &home)
Copy brancher.
Definition function.cpp:125
virtual size_t dispose(Space &home)
Dispose brancher.
Definition function.cpp:135
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition function.cpp:120
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition function.cpp:129
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition function.cpp:88
Class for sharing data between spaces.
Computation spaces.
Definition core.hpp:1744
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4081
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4051
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:4066
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Kernel functionality
Gecode toplevel namespace
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
#define forceinline
Definition config.hpp:194
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition macros.hpp:94