Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
wait.hpp
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 *
6 * Copyright:
7 * Christian Schulte, 2009
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode {
35
42 template<class View>
43 class UnaryWait : public Propagator {
44 protected:
48 SharedData<std::function<void(Space& home)>> c;
50 UnaryWait(Home home, View x, std::function<void(Space& home)> c0);
52 UnaryWait(Space& home, UnaryWait& p);
53 public:
55 virtual Actor* copy(Space& home);
57 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
59 virtual void reschedule(Space& home);
61 virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
63 static ExecStatus post(Home home, View x,
64 std::function<void(Space& home)> c);
66 virtual size_t dispose(Space& home);
67 };
68
75 template<class View>
76 class NaryWait : public Propagator {
77 protected:
81 SharedData<std::function<void(Space& home)>> c;
84 std::function<void(Space& home)> c0);
86 NaryWait(Space& home, NaryWait& p);
87 public:
89 virtual Actor* copy(Space& home);
91 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
93 virtual void reschedule(Space& home);
95 virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
97 static ExecStatus post(Home home, ViewArray<View>& x,
98 std::function<void(Space& home)> c);
100 virtual size_t dispose(Space& home);
101 };
102
103
104 /*
105 * Wait propagator for single view
106 *
107 */
108 template<class View>
111 std::function<void(Space& home)> c0)
112 : Propagator(home), x(x0), c(c0) {
113 x.subscribe(home,*this,PC_GEN_ASSIGNED);
114 home.notice(*this,AP_DISPOSE);
115 }
116 template<class View>
119 : Propagator(home,p), c(p.c) {
120 x.update(home,p.x);
121 }
122 template<class View>
123 Actor*
125 return new (home) UnaryWait<View>(home,*this);
126 }
127 template<class View>
131 }
132 template<class View>
133 void
135 x.reschedule(home,*this,PC_GEN_ASSIGNED);
136 }
137 template<class View>
140 assert(x.assigned());
142 c()(home);
143 return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
144 }
145 template<class View>
148 std::function<void(Space& home)> c) {
149 if (!c)
150 throw InvalidFunction("UnaryWait::post");
151 if (x.assigned()) {
152 c(home);
153 return home.failed() ? ES_FAILED : ES_OK;
154 } else {
155 (void) new (home) UnaryWait<View>(home,x,c);
156 return ES_OK;
157 }
158 }
159 template<class View>
160 size_t
162 x.cancel(home,*this,PC_GEN_ASSIGNED);
163 home.ignore(*this,AP_DISPOSE);
164 c.~SharedData<std::function<void(Space& home)>>();
165 (void) Propagator::dispose(home);
166 return sizeof(*this);
167 }
168
169
170 /*
171 * Wait propagator for several views
172 *
173 */
174 template<class View>
177 std::function<void(Space& home)> c0)
178 : Propagator(home), x(x0), c(c0) {
179 assert(!x[0].assigned());
180 x[0].subscribe(home,*this,PC_GEN_ASSIGNED);
181 home.notice(*this,AP_DISPOSE);
182 }
183 template<class View>
186 : Propagator(home,p), c(p.c) {
187 x.update(home,p.x);
188 }
189 template<class View>
190 Actor*
192 assert(!x[0].assigned());
193 for (int i=x.size()-1; i>0; i--)
194 if (x[i].assigned())
195 x.move_lst(i);
196 assert(x.size() > 0);
197 return new (home) NaryWait<View>(home,*this);
198 }
199 template<class View>
203 }
204 template<class View>
205 void
207 x[0].reschedule(home,*this,PC_GEN_ASSIGNED);
208 }
209 template<class View>
212 assert(x[0].assigned());
213 for (int i=x.size()-1; i>0; i--)
214 if (x[i].assigned())
215 x.move_lst(i);
216 assert(x.size() > 0);
217 if (x.size() == 1) {
218 x.size(0);
220 c()(home);
221 return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
222 } else {
223 // Create new subscription
224 x.move_lst(0);
225 assert(!x[0].assigned());
226 x[0].subscribe(home,*this,PC_GEN_ASSIGNED,false);
227 return ES_OK;
228 }
229 }
230 template<class View>
233 std::function<void(Space& home)> c) {
234 if (!c)
235 throw InvalidFunction("NaryWait::post");
236 for (int i=x.size(); i--; )
237 if (x[i].assigned())
238 x.move_lst(i);
239 if (x.size() == 0) {
240 c(home);
241 return home.failed() ? ES_FAILED : ES_OK;
242 } else {
243 x.unique();
244 if (x.size() == 1) {
245 return UnaryWait<View>::post(home,x[0],c);
246 } else {
247 (void) new (home) NaryWait<View>(home,x,c);
248 return ES_OK;
249 }
250 }
251 }
252 template<class View>
253 size_t
255 if (x.size() > 0)
256 x[0].cancel(home,*this,PC_GEN_ASSIGNED);
257 home.ignore(*this,AP_DISPOSE);
258 c.~SharedData<std::function<void(Space& home)>>();
259 (void) Propagator::dispose(home);
260 return sizeof(*this);
261 }
262
263}
264
265// STATISTICS: kernel-prop
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
Home class for posting propagators
Definition core.hpp:856
Exception: invalid function
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as high unary)
Definition wait.hpp:201
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition wait.hpp:254
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition wait.hpp:191
NaryWait(Home home, ViewArray< View > &x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition wait.hpp:176
virtual void reschedule(Space &home)
Schedule function.
Definition wait.hpp:206
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition wait.hpp:81
ViewArray< View > x
Views to wait for becoming assigned.
Definition wait.hpp:79
static ExecStatus post(Home home, ViewArray< View > &x, std::function< void(Space &home)> c)
Post propagator that waits until x becomes assigned and then executes c.
Definition wait.hpp:232
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition wait.hpp:211
Propagation cost.
Definition core.hpp:486
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition core.hpp:4820
@ HI
Expensive.
Definition core.hpp:514
friend class Space
Definition core.hpp:1068
ModEventDelta med
A set of modification events (used during propagation)
Definition core.hpp:1077
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3505
Class for sharing data between spaces.
View x
View to wait for becoming assigned.
Definition wait.hpp:46
virtual void reschedule(Space &home)
Schedule function.
Definition wait.hpp:134
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition wait.hpp:161
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition wait.hpp:48
static ExecStatus post(Home home, View x, std::function< void(Space &home)> c)
Post propagator that waits until x becomes assigned and then executes c.
Definition wait.hpp:147
UnaryWait(Home home, View x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition wait.hpp:110
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition wait.hpp:124
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition wait.hpp:129
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition wait.hpp:139
View arrays.
Definition array.hpp:253
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3570
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4081
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
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
Gecode toplevel namespace
const PropCond PC_GEN_ASSIGNED
Propagation condition for an assigned variable.
Definition core.hpp:76
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