Generated on Fri Jul 13 2018 06:08:30 for Gecode by doxygen 1.8.14
wait.hh
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  * Last modified:
10  * $Date: 2017-03-01 04:28:36 +0100 (Wed, 01 Mar 2017) $ by $Author: schulte $
11  * $Revision: 15541 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __GECODE_KERNEL_WAIT_HH__
39 #define __GECODE_KERNEL_WAIT_HH__
40 
41 #include <gecode/kernel.hh>
42 
43 namespace Gecode { namespace Kernel {
44 
51  template<class View>
52  class UnaryWait : public Propagator {
53  protected:
55  View x;
59  UnaryWait(Home home, View x, std::function<void(Space& home)> c0);
61  UnaryWait(Space& home, bool shared, UnaryWait& p);
62  public:
64  virtual Actor* copy(Space& home, bool share);
66  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
68  virtual void reschedule(Space& home);
70  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
72  static ExecStatus post(Home home, View x,
73  std::function<void(Space& home)> c);
75  virtual size_t dispose(Space& home);
76  };
77 
84  template<class View>
85  class NaryWait : public Propagator {
86  protected:
93  std::function<void(Space& home)> c0);
95  NaryWait(Space& home, bool shared, NaryWait& p);
96  public:
98  virtual Actor* copy(Space& home, bool share);
100  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
102  virtual void reschedule(Space& home);
104  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
106  static ExecStatus post(Home home, ViewArray<View>& x,
107  std::function<void(Space& home)> c);
109  virtual size_t dispose(Space& home);
110  };
111 
112 
113  /*
114  * Wait propagator for single view
115  *
116  */
117  template<class View>
120  std::function<void(Space& home)> c0)
121  : Propagator(home), x(x0), c(c0) {
122  x.subscribe(home,*this,PC_GEN_ASSIGNED);
123  home.notice(*this,AP_DISPOSE);
124  }
125  template<class View>
128  : Propagator(home,shared,p) {
129  x.update(home,shared,p.x);
130  c.update(home,shared,p.c);
131  }
132  template<class View>
133  Actor*
134  UnaryWait<View>::copy(Space& home, bool share) {
135  return new (home) UnaryWait<View>(home,share,*this);
136  }
137  template<class View>
138  PropCost
139  UnaryWait<View>::cost(const Space&, const ModEventDelta&) const {
141  }
142  template<class View>
143  void
145  x.reschedule(home,*this,PC_GEN_ASSIGNED);
146  }
147  template<class View>
148  ExecStatus
150  assert(x.assigned());
152  c()(home);
153  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
154  }
155  template<class View>
158  std::function<void(Space& home)> c) {
159  if (!c)
160  throw InvalidFunction("UnaryWait::post");
161  if (x.assigned()) {
162  c(home);
163  return home.failed() ? ES_FAILED : ES_OK;
164  } else {
165  (void) new (home) UnaryWait<View>(home,x,c);
166  return ES_OK;
167  }
168  }
169  template<class View>
170  size_t
172  x.cancel(home,*this,PC_GEN_ASSIGNED);
173  home.ignore(*this,AP_DISPOSE);
174  c.~SharedData<std::function<void(Space& home)>>();
175  (void) Propagator::dispose(home);
176  return sizeof(*this);
177  }
178 
179 
180  /*
181  * Wait propagator for several views
182  *
183  */
184  template<class View>
187  std::function<void(Space& home)> c0)
188  : Propagator(home), x(x0), c(c0) {
189  assert(!x[0].assigned());
190  x[0].subscribe(home,*this,PC_GEN_ASSIGNED);
191  home.notice(*this,AP_DISPOSE);
192  }
193  template<class View>
196  : Propagator(home,shared,p) {
197  x.update(home,shared,p.x);
198  c.update(home,shared,p.c);
199  }
200  template<class View>
201  Actor*
202  NaryWait<View>::copy(Space& home, bool share) {
203  assert(!x[0].assigned());
204  for (int i=x.size()-1; i>0; i--)
205  if (x[i].assigned())
206  x.move_lst(i);
207  assert(x.size() > 0);
208  return new (home) NaryWait<View>(home,share,*this);
209  }
210  template<class View>
211  PropCost
212  NaryWait<View>::cost(const Space&, const ModEventDelta&) const {
214  }
215  template<class View>
216  void
218  x[0].reschedule(home,*this,PC_GEN_ASSIGNED);
219  }
220  template<class View>
221  ExecStatus
223  assert(x[0].assigned());
224  for (int i=x.size()-1; i>0; i--)
225  if (x[i].assigned())
226  x.move_lst(i);
227  assert(x.size() > 0);
228  if (x.size() == 1) {
229  x.size(0);
231  c()(home);
232  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
233  } else {
234  // Create new subscription
235  x.move_lst(0);
236  assert(!x[0].assigned());
237  x[0].subscribe(home,*this,PC_GEN_ASSIGNED,false);
238  return ES_OK;
239  }
240  }
241  template<class View>
244  std::function<void(Space& home)> c) {
245  if (!c)
246  throw InvalidFunction("NaryWait::post");
247  for (int i=x.size(); i--; )
248  if (x[i].assigned())
249  x.move_lst(i);
250  if (x.size() == 0) {
251  c(home);
252  return home.failed() ? ES_FAILED : ES_OK;
253  } else {
254  x.unique(home);
255  if (x.size() == 1) {
256  return UnaryWait<View>::post(home,x[0],c);
257  } else {
258  (void) new (home) NaryWait<View>(home,x,c);
259  return ES_OK;
260  }
261  }
262  }
263  template<class View>
264  size_t
266  if (x.size() > 0)
267  x[0].cancel(home,*this,PC_GEN_ASSIGNED);
268  home.ignore(*this,AP_DISPOSE);
269  c.~SharedData<std::function<void(Space& home)>>();
270  (void) Propagator::dispose(home);
271  return sizeof(*this);
272  }
273 
274 }}
275 
276 #endif
277 
278 // STATISTICS: kernel-prop
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.hh:157
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3614
Actor must always be disposed.
Definition: core.hpp:630
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:85
bool assigned(void) const
Test whether view is assigned.
Definition: var.hpp:123
View x
View to wait for becoming assigned.
Definition: wait.hh:55
Wait propagator for single view.
Definition: wait.hh:52
Base-class for propagators.
Definition: core.hpp:1092
Wait propagator for several views.
Definition: wait.hh:85
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4858
Computation spaces.
Definition: core.hpp:1748
Base-class for both propagators and branchers.
Definition: core.hpp:696
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hh:144
UnaryWait(Home home, View x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition: wait.hh:119
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hh:222
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition: wait.hh:57
Execution has resulted in failure.
Definition: core.hpp:542
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hh:149
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:4095
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1103
ViewArray< View > x
Views to wait for becoming assigned.
Definition: wait.hh:88
const PropCond PC_GEN_ASSIGNED
Propagation condition for an assigned variable.
Definition: core.hpp:156
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: wait.hh:134
Expensive.
Definition: core.hpp:582
View arrays.
Definition: array.hpp:234
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.hh:243
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition: wait.hh:90
Exception: invalid function
Definition: exception.hpp:118
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as high unary)
Definition: wait.hh:212
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:98
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition: wait.hh:139
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:265
NaryWait(Home home, ViewArray< View > &x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition: wait.hh:186
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:4123
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3354
Class for sharing data between spaces.
Definition: shared-data.hpp:42
Propagation cost.
Definition: core.hpp:554
ExecStatus
Definition: core.hpp:540
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:173
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:171
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hh:217
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: wait.hh:202
Post propagator for SetVar x
Definition: set.hh:784
Execution is okay.
Definition: core.hpp:544
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:702
Gecode toplevel namespace
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
Home class for posting propagators
Definition: core.hpp:922