Generated on Fri Aug 24 2012 04:52:11 for Gecode by doxygen 1.8.1.2
bool-eq.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-13 22:25:20 +1000 (Sat, 13 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12288 $
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 #include <gecode/int/bool.hh>
40 
41 namespace Gecode { namespace Int { namespace NValues {
42 
43  template<class VY>
45  EqBool<VY>::EqBool(Home home, int status, ViewArray<BoolView>& x, VY y)
46  : BoolBase<VY>(home,status,x,y) {}
47 
48  template<class VY>
50  EqBool<VY>::EqBool(Space& home, bool share, EqBool<VY>& p)
51  : BoolBase<VY>(home,share,p) {}
52 
53  template<class VY>
54  Actor*
55  EqBool<VY>::copy(Space& home, bool share) {
56  return new (home) EqBool<VY>(home,share,*this);
57  }
58 
59  template<class VY>
60  inline ExecStatus
62  if (x.size() == 0) {
63  GECODE_ME_CHECK(y.eq(home,0));
64  return ES_OK;
65  }
66 
67  x.unique(home);
68 
69  if (x.size() == 1) {
70  GECODE_ME_CHECK(y.eq(home,1));
71  return ES_OK;
72  }
73 
74  GECODE_ME_CHECK(y.gq(home,1));
75  GECODE_ME_CHECK(y.lq(home,2));
76 
77  if (y.max() == 1) {
78  assert(y.assigned());
79  ViewArray<BoolView> xc(home,x);
80  return Bool::NaryEq<BoolView>::post(home,xc);
81  }
82 
83  if (y.min() == 2) {
84  assert(y.assigned());
85  ViewArray<BoolView> xc(home,x);
86  return Rel::NaryNq<BoolView>::post(home,xc);
87  }
88 
89  int n = x.size();
90  int status = 0;
91  for (int i=n; i--; )
92  if (x[i].zero()) {
93  if (status & VS_ONE) {
94  GECODE_ME_CHECK(y.eq(home,2));
95  return ES_OK;
96  }
97  x[i] = x[--n];
98  status |= VS_ZERO;
99  } else if (x[i].one()) {
100  if (status & VS_ZERO) {
101  GECODE_ME_CHECK(y.eq(home,2));
102  return ES_OK;
103  }
104  x[i] = x[--n];
105  status |= VS_ONE;
106  }
107 
108  assert(status != (VS_ZERO | VS_ONE));
109  if (n == 0) {
110  assert(status != 0);
111  GECODE_ME_CHECK(y.eq(home,1));
112  return ES_OK;
113  }
114  x.size(n);
115 
116  (void) new (home) EqBool<VY>(home,status,x,y);
117  return ES_OK;
118  }
119 
120  template<class VY>
121  ExecStatus
123  if (status == (VS_ZERO | VS_ONE)) {
124  GECODE_ME_CHECK(y.eq(home,2));
125  return home.ES_SUBSUMED(*this);
126  }
127 
128  if (c.empty()) {
129  assert(status != 0);
130  GECODE_ME_CHECK(y.eq(home,1));
131  return home.ES_SUBSUMED(*this);
132  }
133 
134  if (y.max() == 1) {
135  if (status == VS_ZERO) {
136  // Mark that everything is done
137  status = VS_ZERO | VS_ONE;
138  for (Advisors<ViewAdvisor<BoolView> > as(c); as(); ++as)
139  GECODE_ME_CHECK(as.advisor().view().zero(home));
140  return home.ES_SUBSUMED(*this);
141  }
142  if (status == VS_ONE) {
143  // Mark that everything is done
144  status = VS_ZERO | VS_ONE;
145  for (Advisors<ViewAdvisor<BoolView> > as(c); as(); ++as)
146  GECODE_ME_CHECK(as.advisor().view().one(home));
147  return home.ES_SUBSUMED(*this);
148  }
149  }
150 
151  if (y.min() == 2) {
153  assert(as());
155  ++as;
156  if (!as()) {
157  // Only a single view is left
158  if (status == VS_ZERO) {
159  GECODE_ME_CHECK(a.view().one(home));
160  } else if (status == VS_ONE) {
161  GECODE_ME_CHECK(a.view().zero(home));
162  } else {
163  return ES_FAILED;
164  }
165  return home.ES_SUBSUMED(*this);
166  }
167  }
168 
169  return ES_FIX;
170  }
171 
172 }}}
173 
174 // STATISTICS: int-prop