Generated on Thu Mar 7 2013 10:21:31 for Gecode by doxygen 1.8.3.1
prop.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, 2011
8  *
9  * Last modified:
10  * $Date: 2011-08-23 05:43:31 +1000 (Tue, 23 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12335 $
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 #include <gecode/int/rel.hh>
39 
40 namespace Gecode { namespace Int { namespace Member {
41 
42  template<class View>
44  Prop<View>::Prop(Home home, ValSet& vs0, ViewArray<View>& x, View y)
45  : NaryOnePropagator<View,PC_INT_DOM>(home,x,y),
46  vs(vs0) {}
47 
48  template<class View>
49  forceinline void
51  int n=x.size();
52  for (int i=n; i--; )
53  if (x[i].assigned()) {
54  vs.add(home, x[i].val());
55  x[i] = x[--n];
56  }
57  x.size(n);
58  }
59 
60  template<class View>
61  forceinline void
63  int n=x.size();
64  for (int i=n; i--; )
65  if ((rtest_eq_dom(x[i],y) == RT_FALSE) || vs.subset(x[i])) {
66  // x[i] is different from y or values are contained in vs
67  x[i].cancel(home, *this, PC_INT_DOM);
68  x[i] = x[--n];
69  }
70  x.size(n);
71  }
72 
73  template<class View>
74  inline ExecStatus
76  if (x.size() == 0)
77  return ES_FAILED;
78 
79  x.unique(home);
80 
81  if (x.size() == 1)
82  return Rel::EqDom<View,View>::post(home,x[0],y);
83 
84  if (x.same(home,y))
85  return ES_OK;
86 
87  // Eliminate assigned views and store them into the value set
88  ValSet vs;
89  add(home, vs, x);
90 
91  if (x.size() == 0) {
92  ValSet::Ranges vsr(vs);
93  GECODE_ME_CHECK(y.inter_r(home,vsr,false));
94  return ES_OK;
95  }
96 
97  (void) new (home) Prop<View>(home, vs, x, y);
98  return ES_OK;
99  }
100 
101  template<class View>
103  Prop<View>::post(Home home, ValSet& vs, ViewArray<View>& x, View y) {
104  (void) new (home) Prop<View>(home, vs, x, y);
105  return ES_OK;
106  }
107 
108  template<class View>
110  Prop<View>::Prop(Space& home, bool share, Prop<View>& p)
111  : NaryOnePropagator<View,PC_INT_DOM>(home, share, p) {
112  vs.update(home, share, p.vs);
113  }
114 
115  template<class View>
116  Propagator*
117  Prop<View>::copy(Space& home, bool share) {
118  return new (home) Prop<View>(home, share, *this);
119  }
120 
121  template<class View>
122  forceinline size_t
124  vs.dispose(home);
126  return sizeof(*this);
127  }
128 
129  template<class View>
130  PropCost
131  Prop<View>::cost(const Space&, const ModEventDelta&) const {
132  return PropCost::linear(PropCost::HI, x.size()+1);
133  }
134 
135  template<class View>
136  ExecStatus
138  // Add assigned views to value set
139  if (View::me(med) == ME_INT_VAL)
140  add(home,vs,x);
141 
142  // Eliminate views from x
143  eliminate(home);
144 
145  if (x.size() == 0) {
146  // y must have values in the value set
147  ValSet::Ranges vsr(vs);
148  GECODE_ME_CHECK(y.inter_r(home,vsr,false));
149  return home.ES_SUBSUMED(*this);
150  }
151 
152  // Constrain y to union of x and value set
153  Region r(home);
154 
155  assert(x.size() > 0);
156  ValSet::Ranges vsr(vs);
157  ViewRanges<View> xsr(x[x.size()-1]);
158  Iter::Ranges::NaryUnion u(r,vsr,xsr);
159  for (int i=x.size()-1; i--; ) {
160  ViewRanges<View> xir(x[i]);
161  u |= xir;
162  }
163 
164  GECODE_ME_CHECK(y.inter_r(home,u,false));
165 
166  // Check whether all values in y are already in the value set
167  if (vs.subset(y))
168  return home.ES_SUBSUMED(*this);
169 
170  return ES_FIX;
171  }
172 
173 }}}
174 
175 // STATISTICS: int-prop