Generated on Sat Feb 7 2015 02:01:28 for Gecode by doxygen 1.8.9.1
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: 2013-07-04 17:03:13 +0200 (Thu, 04 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13801 $
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 ExecStatus propagate(Space& home, const ModEventDelta& med);
70  static ExecStatus post(Space& home, View x, void (*c)(Space&));
72  virtual size_t dispose(Space& home);
73  };
74 
81  template<class View>
82  class NaryWait : public Propagator {
83  protected:
87  void (*c)(Space&);
89  NaryWait(Home home, ViewArray<View>& x, void (*c0)(Space&));
91  NaryWait(Space& home, bool shared, NaryWait& p);
92  public:
94  virtual Actor* copy(Space& home, bool share);
96  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
98  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
100  static ExecStatus post(Space& home, ViewArray<View>& x, void (*c)(Space&));
102  virtual size_t dispose(Space& home);
103  };
104 
105 
106  /*
107  * Wait propagator for single view
108  *
109  */
110  template<class View>
112  UnaryWait<View>::UnaryWait(Home home, View x0, void (*c0)(Space&))
113  : Propagator(home), x(x0), c(c0) {
114  x.subscribe(home,*this,PC_GEN_ASSIGNED);
115  }
116  template<class View>
119  : Propagator(home,shared,p), c(p.c) {
120  x.update(home,shared,p.x);
121  }
122  template<class View>
123  Actor*
124  UnaryWait<View>::copy(Space& home, bool share) {
125  return new (home) UnaryWait<View>(home,share,*this);
126  }
127  template<class View>
128  PropCost
129  UnaryWait<View>::cost(const Space&, const ModEventDelta&) const {
131  }
132  template<class View>
133  ExecStatus
135  assert(x.assigned());
136  c(home);
137  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
138  }
139  template<class View>
140  ExecStatus
141  UnaryWait<View>::post(Space& home, View x, void (*c)(Space&)) {
142  if (x.assigned()) {
143  c(home);
144  return home.failed() ? ES_FAILED : ES_OK;
145  } else {
146  (void) new (home) UnaryWait<View>(home,x,c);
147  return ES_OK;
148  }
149  }
150  template<class View>
151  size_t
153  x.cancel(home,*this,PC_GEN_ASSIGNED);
154  (void) Propagator::dispose(home);
155  return sizeof(*this);
156  }
157 
158 
159  /*
160  * Wait propagator for several views
161  *
162  */
163  template<class View>
166  void (*c0)(Space&))
167  : Propagator(home), x(x0), c(c0) {
168  assert(!x[0].assigned());
169  x[0].subscribe(home,*this,PC_GEN_ASSIGNED);
170  }
171  template<class View>
174  : Propagator(home,shared,p), c(p.c) {
175  x.update(home,shared,p.x);
176  }
177  template<class View>
178  Actor*
179  NaryWait<View>::copy(Space& home, bool share) {
180  assert(!x[0].assigned());
181  for (int i=x.size()-1; i>0; i--)
182  if (x[i].assigned())
183  x.move_lst(i);
184  assert(x.size() > 0);
185  return new (home) NaryWait<View>(home,share,*this);
186  }
187  template<class View>
188  PropCost
189  NaryWait<View>::cost(const Space&, const ModEventDelta&) const {
191  }
192  template<class View>
193  ExecStatus
195  assert(x[0].assigned());
196  for (int i=x.size()-1; i>0; i--)
197  if (x[i].assigned())
198  x.move_lst(i);
199  assert(x.size() > 0);
200  if (x.size() == 1) {
201  x.size(0);
202  c(home);
203  return home.failed() ? ES_FAILED : home.ES_SUBSUMED(*this);
204  } else {
205  // Create new subscription
206  x.move_lst(0);
207  assert(!x[0].assigned());
208  x[0].subscribe(home,*this,PC_GEN_ASSIGNED,false);
209  return ES_OK;
210  }
211  }
212  template<class View>
213  ExecStatus
215  for (int i=x.size(); i--; )
216  if (x[i].assigned())
217  x.move_lst(i);
218  if (x.size() == 0) {
219  c(home);
220  return home.failed() ? ES_FAILED : ES_OK;
221  } else {
222  x.unique(home);
223  if (x.size() == 1) {
224  return UnaryWait<View>::post(home,x[0],c);
225  } else {
226  (void) new (home) NaryWait<View>(home,x,c);
227  return ES_OK;
228  }
229  }
230  }
231  template<class View>
232  size_t
234  if (x.size() > 0)
235  x[0].cancel(home,*this,PC_GEN_ASSIGNED);
236  (void) Propagator::dispose(home);
237  return sizeof(*this);
238  }
239 
240 }}
241 
242 #endif
243 
244 // 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(* c)(Space &)
Continuation to execute.
Definition: wait.hh:57
UnaryWait(Home home, View x, void(*c0)(Space &))
Constructor for creation.
Definition: wait.hh:112
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:84
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:755
Expensive.
Definition: core.hpp:564
Wait propagator for several views.
Definition: wait.hh:82
void(* c)(Space &)
Continuation to execute.
Definition: wait.hh:87
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition: core.hpp:4058
void unique(const Space &home)
Remove all duplicate views from array (changes element order)
Definition: array.hpp:1498
Computation spaces.
Definition: core.hpp:1362
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3442
Base-class for both propagators and branchers.
Definition: core.hpp:666
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:194
Execution has resulted in failure.
Definition: core.hpp:525
static ExecStatus post(Space &home, View x, void(*c)(Space &))
Post propagator that waits until x becomes assigned and then executes c.
Definition: wait.hh:141
void subscribe(Space &home, Propagator &p, PropCond pc, bool process=true)
Subscribe propagator p with propagation condition pc to variable.
Definition: array.hpp:1400
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: wait.hh:134
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:764
ViewArray< View > x
Views to wait for becoming assigned.
Definition: wait.hh:85
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition: wait.hh:129
const PropCond PC_GEN_ASSIGNED
Propagation condition for an assigned variable.
Definition: core.hpp:160
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: wait.hh:124
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
static ExecStatus post(Space &home, ViewArray< View > &x, void(*c)(Space &))
Post propagator that waits until x becomes assigned and then executes c.
Definition: wait.hh:214
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as high unary)
Definition: wait.hh:189
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:233
NaryWait(Home home, ViewArray< View > &x, void(*c0)(Space &))
Constructor for creation.
Definition: wait.hh:165
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition: core.hpp:2877
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
#define forceinline
Definition: config.hpp:132
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: wait.hh:152
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: wait.hh:179
Execution is okay.
Definition: core.hpp:527
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1215
Gecode toplevel namespace
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
bool assigned(void) const
Test if all variables are assigned.
Definition: array.hpp:1429