this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Contributing authors: 7 * Gabor Szokoli <szokoli@gecode.org> 8 * 9 * Copyright: 10 * Guido Tack, 2004, 2005 11 * 12 * This file is part of Gecode, the generic constraint 13 * development environment: 14 * http://www.gecode.org 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining 17 * a copy of this software and associated documentation files (the 18 * "Software"), to deal in the Software without restriction, including 19 * without limitation the rights to use, copy, modify, merge, publish, 20 * distribute, sublicense, and/or sell copies of the Software, and to 21 * permit persons to whom the Software is furnished to do so, subject to 22 * the following conditions: 23 * 24 * The above copyright notice and this permission notice shall be 25 * included in all copies or substantial portions of the Software. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 34 * 35 */ 36 37#include <gecode/set/rel.hh> 38#include <gecode/set/rel-op.hh> 39#include <gecode/set/int.hh> 40 41namespace Gecode { namespace Set { 42 43 template<class View0, class View1> 44 forceinline void 45 rel_post(Home home, View0 x0, SetRelType r, View1 x1) { 46 using namespace Set::Rel; 47 using namespace Set::RelOp; 48 GECODE_POST; 49 switch (r) { 50 case SRT_EQ: 51 GECODE_ES_FAIL((Eq<View0,View1>::post(home,x0,x1))); 52 break; 53 case SRT_NQ: 54 GECODE_ES_FAIL((Distinct<View0,View1>::post(home,x0,x1))); 55 break; 56 case SRT_SUB: 57 GECODE_ES_FAIL((Subset<View0,View1>::post(home, x0,x1))); 58 break; 59 case SRT_SUP: 60 GECODE_ES_FAIL((Subset<View1,View0>::post(home, x1,x0))); 61 break; 62 case SRT_DISJ: 63 { 64 EmptyView emptyset; 65 GECODE_ES_FAIL((SuperOfInter<View0,View1,EmptyView> 66 ::post(home, x0, x1, emptyset))); 67 } 68 break; 69 case SRT_CMPL: 70 { 71 ComplementView<View0> cx0(x0); 72 GECODE_ES_FAIL((Eq<ComplementView<View0>, View1> 73 ::post(home, cx0, x1))); 74 } 75 break; 76 case SRT_LQ: 77 GECODE_ES_FAIL((Lq<View0,View1,false>::post(home,x0,x1))); 78 break; 79 case SRT_LE: 80 GECODE_ES_FAIL((Lq<View0,View1,true>::post(home,x0,x1))); 81 break; 82 case SRT_GQ: 83 GECODE_ES_FAIL((Lq<View1,View0,false>::post(home,x1,x0))); 84 break; 85 case SRT_GR: 86 GECODE_ES_FAIL((Lq<View1,View0,true>::post(home,x1,x0))); 87 break; 88 default: 89 throw UnknownRelation("Set::rel"); 90 } 91 } 92 93 template<class View0, class View1, ReifyMode rm> 94 forceinline void 95 rel_re(Home home, View0 x, SetRelType r, View1 y, BoolVar b) { 96 using namespace Set::Rel; 97 using namespace Set::RelOp; 98 GECODE_POST; 99 switch (r) { 100 case SRT_EQ: 101 GECODE_ES_FAIL((ReEq<View0,View1,Gecode::Int::BoolView,rm> 102 ::post(home, x,y,b))); 103 break; 104 case SRT_NQ: 105 { 106 Gecode::Int::NegBoolView notb(b); 107 switch (rm) { 108 case RM_EQV: 109 GECODE_ES_FAIL((ReEq<View0,View1,Gecode::Int::NegBoolView,RM_EQV> 110 ::post(home,x,y,notb))); 111 break; 112 case RM_IMP: 113 GECODE_ES_FAIL((ReEq<View0,View1,Gecode::Int::NegBoolView,RM_PMI> 114 ::post(home,x,y,notb))); 115 break; 116 case RM_PMI: 117 GECODE_ES_FAIL((ReEq<View0,View1,Gecode::Int::NegBoolView,RM_IMP> 118 ::post(home,x,y,notb))); 119 break; 120 default: throw Gecode::Int::UnknownReifyMode("Set::rel"); 121 } 122 } 123 break; 124 case SRT_SUB: 125 GECODE_ES_FAIL((ReSubset<View0,View1,Gecode::Int::BoolView,rm>::post(home, x,y,b))); 126 break; 127 case SRT_SUP: 128 GECODE_ES_FAIL((ReSubset<View1,View0,Gecode::Int::BoolView,rm>::post(home, y,x,b))); 129 break; 130 case SRT_DISJ: 131 { 132 // x||y <=> b is equivalent to 133 // ( y <= complement(x) ) <=> b 134 135 ComplementView<View0> xc(x); 136 GECODE_ES_FAIL((ReSubset<View1,ComplementView<View0>, 137 Gecode::Int::BoolView,rm> 138 ::post(home, y, xc, b))); 139 } 140 break; 141 case SRT_CMPL: 142 { 143 ComplementView<View0> xc(x); 144 GECODE_ES_FAIL((ReEq<ComplementView<View0>,View1, 145 Gecode::Int::BoolView,rm> 146 ::post(home, xc, y, b))); 147 } 148 break; 149 case SRT_LQ: 150 GECODE_ES_FAIL((ReLq<View0,View1,rm,false>::post(home,x,y,b))); 151 break; 152 case SRT_LE: 153 GECODE_ES_FAIL((ReLq<View0,View1,rm,true>::post(home,x,y,b))); 154 break; 155 case SRT_GQ: 156 GECODE_ES_FAIL((ReLq<View1,View0,rm,false>::post(home,y,x,b))); 157 break; 158 case SRT_GR: 159 GECODE_ES_FAIL((ReLq<View1,View0,rm,true>::post(home,y,x,b))); 160 break; 161 default: 162 throw UnknownRelation("Set::rel"); 163 } 164 } 165 166}} 167 168namespace Gecode { 169 170 void 171 rel(Home home, SetVar x, SetRelType r, SetVar y) { 172 using namespace Set; 173 rel_post<SetView,SetView>(home,x,r,y); 174 } 175 176 void 177 rel(Home home, SetVar s, SetRelType r, IntVar x) { 178 using namespace Set; 179 Gecode::Int::IntView xv(x); 180 SingletonView xsingle(xv); 181 rel_post<SetView,SingletonView>(home,s,r,xv); 182 } 183 184 void 185 rel(Home home, IntVar x, SetRelType r, SetVar s) { 186 using namespace Set; 187 switch (r) { 188 case SRT_SUB: 189 rel(home, s, SRT_SUP, x); 190 break; 191 case SRT_SUP: 192 rel(home, s, SRT_SUB, x); 193 break; 194 default: 195 rel(home, s, r, x); 196 } 197 } 198 199 void 200 rel(Home home, SetVar x, SetRelType rt, SetVar y, Reify r) { 201 using namespace Set; 202 switch (r.mode()) { 203 case RM_EQV: 204 rel_re<SetView,SetView,RM_EQV>(home,x,rt,y,r.var()); 205 break; 206 case RM_IMP: 207 rel_re<SetView,SetView,RM_IMP>(home,x,rt,y,r.var()); 208 break; 209 case RM_PMI: 210 rel_re<SetView,SetView,RM_PMI>(home,x,rt,y,r.var()); 211 break; 212 default: throw Gecode::Int::UnknownReifyMode("Set::rel"); 213 } 214 } 215 216 void 217 rel(Home home, SetVar s, SetRelType rt, IntVar x, Reify r) { 218 using namespace Set; 219 Gecode::Int::IntView xv(x); 220 SingletonView xsingle(xv); 221 switch (r.mode()) { 222 case RM_EQV: 223 rel_re<SetView,SingletonView,RM_EQV>(home,s,rt,xsingle,r.var()); 224 break; 225 case RM_IMP: 226 rel_re<SetView,SingletonView,RM_IMP>(home,s,rt,xsingle,r.var()); 227 break; 228 case RM_PMI: 229 rel_re<SetView,SingletonView,RM_PMI>(home,s,rt,xsingle,r.var()); 230 break; 231 default: throw Gecode::Int::UnknownReifyMode("Set::rel"); 232 } 233 } 234 235 void 236 rel(Home home, IntVar x, SetRelType rt, SetVar s, Reify r) { 237 using namespace Set; 238 switch (rt) { 239 case SRT_SUB: 240 rel(home, s, SRT_SUP, x, r); 241 break; 242 case SRT_SUP: 243 rel(home, s, SRT_SUB, x, r); 244 break; 245 default: 246 rel(home, s, rt, x, r); 247 } 248 } 249 250} 251 252// STATISTICS: set-post