Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
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 namespace Gecode {
39 
46  template<class View>
47  class UnaryWait : public Propagator {
48  protected:
50  View x;
54  UnaryWait(Home home, View x, std::function<void(Space& home)> c0);
56  UnaryWait(Space& home, UnaryWait& p);
57  public:
59  virtual Actor* copy(Space& home);
61  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
63  virtual void reschedule(Space& home);
65  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
67  static ExecStatus post(Home home, View x,
68  std::function<void(Space& home)> c);
70  virtual size_t dispose(Space& home);
71  };
72 
79  template<class View>
80  class NaryWait : public Propagator {
81  protected:
87  NaryWait(Home home, ViewArray<View>& x,
88  std::function<void(Space& home)> c0);
90  NaryWait(Space& home, NaryWait& p);
91  public:
93  virtual Actor* copy(Space& home);
95  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
97  virtual void reschedule(Space& home);
99  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
101  static ExecStatus post(Home home, ViewArray<View>& x,
102  std::function<void(Space& home)> c);
104  virtual size_t dispose(Space& home);
105  };
106 
107 
108  /*
109  * Wait propagator for single view
110  *
111  */
112  template<class View>
115  std::function<void(Space& home)> c0)
116  : Propagator(home), x(x0), c(c0) {
117  x.subscribe(home,*this,PC_GEN_ASSIGNED);
118  home.notice(*this,AP_DISPOSE);
119  }
120  template<class View>
123  : Propagator(home,p), c(p.c) {
124  x.update(home,p.x);
125  }
126  template<class View>
127  Actor*
129  return new (home) UnaryWait<View>(home,*this);
130  }
131  template<class View>
132  PropCost
133  UnaryWait<View>::cost(const Space&, const ModEventDelta&) const {
135  }
136  template<class View>
137  void
139  x.reschedule(home,*this,PC_GEN_ASSIGNED);
140  }
141  template<class View>
142  ExecStatus
144  assert(x.assigned());
146  c()(home);
147  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
148  }
149  template<class View>
152  std::function<void(Space& home)> c) {
153  if (!c)
154  throw InvalidFunction("UnaryWait::post");
155  if (x.assigned()) {
156  c(home);
157  return home.failed() ? ES_FAILED : ES_OK;
158  } else {
159  (void) new (home) UnaryWait<View>(home,x,c);
160  return ES_OK;
161  }
162  }
163  template<class View>
164  size_t
166  x.cancel(home,*this,PC_GEN_ASSIGNED);
167  home.ignore(*this,AP_DISPOSE);
168  c.~SharedData<std::function<void(Space& home)>>();
169  (void) Propagator::dispose(home);
170  return sizeof(*this);
171  }
172 
173 
174  /*
175  * Wait propagator for several views
176  *
177  */
178  template<class View>
181  std::function<void(Space& home)> c0)
182  : Propagator(home), x(x0), c(c0) {
183  assert(!x[0].assigned());
184  x[0].subscribe(home,*this,PC_GEN_ASSIGNED);
185  home.notice(*this,AP_DISPOSE);
186  }
187  template<class View>
190  : Propagator(home,p), c(p.c) {
191  x.update(home,p.x);
192  }
193  template<class View>
194  Actor*
196  assert(!x[0].assigned());
197  for (int i=x.size()-1; i>0; i--)
198  if (x[i].assigned())
199  x.move_lst(i);
200  assert(x.size() > 0);
201  return new (home) NaryWait<View>(home,*this);
202  }
203  template<class View>
204  PropCost
205  NaryWait<View>::cost(const Space&, const ModEventDelta&) const {
207  }
208  template<class View>
209  void
211  x[0].reschedule(home,*this,PC_GEN_ASSIGNED);
212  }
213  template<class View>
214  ExecStatus
216  assert(x[0].assigned());
217  for (int i=x.size()-1; i>0; i--)
218  if (x[i].assigned())
219  x.move_lst(i);
220  assert(x.size() > 0);
221  if (x.size() == 1) {
222  x.size(0);
224  c()(home);
225  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
226  } else {
227  // Create new subscription
228  x.move_lst(0);
229  assert(!x[0].assigned());
230  x[0].subscribe(home,*this,PC_GEN_ASSIGNED,false);
231  return ES_OK;
232  }
233  }
234  template<class View>
237  std::function<void(Space& home)> c) {
238  if (!c)
239  throw InvalidFunction("NaryWait::post");
240  for (int i=x.size(); i--; )
241  if (x[i].assigned())
242  x.move_lst(i);
243  if (x.size() == 0) {
244  c(home);
245  return home.failed() ? ES_FAILED : ES_OK;
246  } else {
247  x.unique();
248  if (x.size() == 1) {
249  return UnaryWait<View>::post(home,x[0],c);
250  } else {
251  (void) new (home) NaryWait<View>(home,x,c);
252  return ES_OK;
253  }
254  }
255  }
256  template<class View>
257  size_t
259  if (x.size() > 0)
260  x[0].cancel(home,*this,PC_GEN_ASSIGNED);
261  home.ignore(*this,AP_DISPOSE);
262  c.~SharedData<std::function<void(Space& home)>>();
263  (void) Propagator::dispose(home);
264  return sizeof(*this);
265  }
266 
267 }
268 
269 // STATISTICS: kernel-prop
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hpp:138
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition: wait.hpp:128
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: array.hpp:1417
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hpp:258
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1657
void update(Space &home, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1375
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3433
Actor must always be disposed.
Definition: core.hpp:554
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:85
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hpp:215
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as high unary)
Definition: wait.hpp:205
Base-class for propagators.
Definition: core.hpp:1016
Wait propagator for single view.
Definition: wait.hpp:47
UnaryWait(Home home, View x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition: wait.hpp:114
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1388
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition: wait.hpp:52
#define forceinline
Definition: config.hpp:182
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4660
Computation spaces.
Definition: core.hpp:1668
Base-class for both propagators and branchers.
Definition: core.hpp:620
SharedData< std::function< void(Space &home)> > c
Continuation to execute.
Definition: wait.hpp:85
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Execution has resulted in failure.
Definition: core.hpp:466
~SharedData(void)
Destructors.
Wait propagator for several views.
Definition: wait.hpp:80
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3914
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1027
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hpp:165
const PropCond PC_GEN_ASSIGNED
Propagation condition for an assigned variable.
Definition: core.hpp:78
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:151
Expensive.
Definition: core.hpp:506
View arrays.
Definition: array.hpp:228
View x
View to wait for becoming assigned.
Definition: wait.hpp:50
Exception: invalid function
Definition: exception.hpp:118
void move_lst(int i)
Move view from position size()-1 to position i (truncate array by one)
Definition: array.hpp:1283
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:98
NaryWait(Home home, ViewArray< View > &x, std::function< void(Space &home)> c0)
Constructor for creation.
Definition: wait.hpp:180
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:3944
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
Propagation cost.
Definition: core.hpp:478
ExecStatus
Definition: core.hpp:464
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition: wait.hpp:195
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
bool assigned(void) const
Test if all variables are assigned.
Definition: array.hpp:1424
Execution is okay.
Definition: core.hpp:468
void unique(void)
Remove all duplicate views from array (changes element order)
Definition: array.hpp:1493
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hpp:143
Gecode toplevel namespace
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hpp:210
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1203
int ModEventDelta
Modification event deltas.
Definition: core.hpp:91
Home class for posting propagators
Definition: core.hpp:846
ViewArray< View > x
Views to wait for becoming assigned.
Definition: wait.hpp:83
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition: wait.hpp:133
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:236