Generated on Thu Mar 16 2017 03:24:23 for Gecode by doxygen 1.8.13
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: 2016-06-29 17:28:17 +0200 (Wed, 29 Jun 2016) $ by $Author: schulte $
11  * $Revision: 15137 $
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;
57  void (*c)(Space&);
59  UnaryWait(Home home, View x, void (*c0)(Space&));
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, void (*c)(Space&));
74  virtual size_t dispose(Space& home);
75  };
76 
83  template<class View>
84  class NaryWait : public Propagator {
85  protected:
89  void (*c)(Space&);
91  NaryWait(Home home, ViewArray<View>& x, void (*c0)(Space&));
93  NaryWait(Space& home, bool shared, NaryWait& p);
94  public:
96  virtual Actor* copy(Space& home, bool share);
98  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
100  virtual void reschedule(Space& home);
102  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
104  static ExecStatus post(Home home, ViewArray<View>& x, void (*c)(Space&));
106  virtual size_t dispose(Space& home);
107  };
108 
109 
110  /*
111  * Wait propagator for single view
112  *
113  */
114  template<class View>
116  UnaryWait<View>::UnaryWait(Home home, View x0, void (*c0)(Space&))
117  : Propagator(home), x(x0), c(c0) {
118  x.subscribe(home,*this,PC_GEN_ASSIGNED);
119  }
120  template<class View>
123  : Propagator(home,shared,p), c(p.c) {
124  x.update(home,shared,p.x);
125  }
126  template<class View>
127  Actor*
128  UnaryWait<View>::copy(Space& home, bool share) {
129  return new (home) UnaryWait<View>(home,share,*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());
145  c(home);
146  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
147  }
148  template<class View>
149  ExecStatus
150  UnaryWait<View>::post(Home home, View x, void (*c)(Space&)) {
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  (void) Propagator::dispose(home);
164  return sizeof(*this);
165  }
166 
167 
168  /*
169  * Wait propagator for several views
170  *
171  */
172  template<class View>
175  void (*c0)(Space&))
176  : Propagator(home), x(x0), c(c0) {
177  assert(!x[0].assigned());
178  x[0].subscribe(home,*this,PC_GEN_ASSIGNED);
179  }
180  template<class View>
183  : Propagator(home,shared,p), c(p.c) {
184  x.update(home,shared,p.x);
185  }
186  template<class View>
187  Actor*
188  NaryWait<View>::copy(Space& home, bool share) {
189  assert(!x[0].assigned());
190  for (int i=x.size()-1; i>0; i--)
191  if (x[i].assigned())
192  x.move_lst(i);
193  assert(x.size() > 0);
194  return new (home) NaryWait<View>(home,share,*this);
195  }
196  template<class View>
197  PropCost
198  NaryWait<View>::cost(const Space&, const ModEventDelta&) const {
200  }
201  template<class View>
202  void
204  x[0].reschedule(home,*this,PC_GEN_ASSIGNED);
205  }
206  template<class View>
207  ExecStatus
209  assert(x[0].assigned());
210  for (int i=x.size()-1; i>0; i--)
211  if (x[i].assigned())
212  x.move_lst(i);
213  assert(x.size() > 0);
214  if (x.size() == 1) {
215  x.size(0);
216  c(home);
217  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
218  } else {
219  // Create new subscription
220  x.move_lst(0);
221  assert(!x[0].assigned());
222  x[0].subscribe(home,*this,PC_GEN_ASSIGNED,false);
223  return ES_OK;
224  }
225  }
226  template<class View>
227  ExecStatus
229  for (int i=x.size(); i--; )
230  if (x[i].assigned())
231  x.move_lst(i);
232  if (x.size() == 0) {
233  c(home);
234  return home.failed() ? ES_FAILED : ES_OK;
235  } else {
236  x.unique(home);
237  if (x.size() == 1) {
238  return UnaryWait<View>::post(home,x[0],c);
239  } else {
240  (void) new (home) NaryWait<View>(home,x,c);
241  return ES_OK;
242  }
243  }
244  }
245  template<class View>
246  size_t
248  if (x.size() > 0)
249  x[0].cancel(home,*this,PC_GEN_ASSIGNED);
250  (void) Propagator::dispose(home);
251  return sizeof(*this);
252  }
253 
254 }}
255 
256 #endif
257 
258 // STATISTICS: kernel-prop
void update(Space &, bool share, ViewArray< View > &a)
Update array to be a clone of array a.
Definition: array.hpp:1387
void reschedule(Space &home, Propagator &p, PropCond pc)
Re-schedule propagator p with propagation condition pc.
Definition: array.hpp:1429
void(* c)(Space &)
Continuation to execute.
Definition: wait.hh:57
UnaryWait(Home home, View x, void(*c0)(Space &))
Constructor for creation.
Definition: wait.hh:116
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1669
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3484
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:85
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:1012
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1400
Wait propagator for several views.
Definition: wait.hh:84
void(* c)(Space &)
Continuation to execute.
Definition: wait.hh:89
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4594
void unique(const Space &home)
Remove all duplicate views from array (changes element order)
Definition: array.hpp:1505
Computation spaces.
Definition: core.hpp:1672
Base-class for both propagators and branchers.
Definition: core.hpp:682
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hh:138
static ExecStatus post(Home home, View x, void(*c)(Space &))
Post propagator that waits until x becomes assigned and then executes c.
Definition: wait.hh:150
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:208
Execution has resulted in failure.
Definition: core.hpp:538
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hh:143
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3963
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:1022
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3967
ViewArray< View > x
Views to wait for becoming assigned.
Definition: wait.hh:87
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:128
Expensive.
Definition: core.hpp:578
View arrays.
Definition: array.hpp:234
void move_lst(int i)
Move view from position size()-1 to position i (truncate array by one)
Definition: array.hpp:1295
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as high unary)
Definition: wait.hh:198
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition: wait.hh:133
static ExecStatus post(Home home, ViewArray< View > &x, void(*c)(Space &))
Post propagator that waits until x becomes assigned and then executes c.
Definition: wait.hh:228
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:247
NaryWait(Home home, ViewArray< View > &x, void(*c0)(Space &))
Constructor for creation.
Definition: wait.hh:174
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:3281
Propagation cost.
Definition: core.hpp:550
ExecStatus
Definition: core.hpp:536
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:1436
#define forceinline
Definition: config.hpp:173
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:161
virtual void reschedule(Space &home)
Schedule function.
Definition: wait.hh:203
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: wait.hh:188
Execution is okay.
Definition: core.hpp:540
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:690
Gecode toplevel namespace
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
int ModEventDelta
Modification event deltas.
Definition: core.hpp:169
Home class for posting propagators
Definition: core.hpp:905