Generated on Mon Aug 27 2012 17:15:47 for Gecode by doxygen 1.8.1.2
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: 2010-03-04 03:32:21 +1100 (Thu, 04 Mar 2010) $ by $Author: schulte $
11  * $Revision: 10364 $
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(Space& 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(Space& 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(Space& 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