int.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Guido Tack <tack@gecode.org> 00005 * Christian Schulte <schulte@gecode.org> 00006 * 00007 * Copyright: 00008 * Guido Tack, 2004 00009 * Christian Schulte, 2004 00010 * 00011 * Last modified: 00012 * $Date: 2010-03-03 17:40:32 +0100 (Wed, 03 Mar 2010) $ by $Author: schulte $ 00013 * $Revision: 10365 $ 00014 * 00015 * This file is part of Gecode, the generic constraint 00016 * development environment: 00017 * http://www.gecode.org 00018 * 00019 * Permission is hereby granted, free of charge, to any person obtaining 00020 * a copy of this software and associated documentation files (the 00021 * "Software"), to deal in the Software without restriction, including 00022 * without limitation the rights to use, copy, modify, merge, publish, 00023 * distribute, sublicense, and/or sell copies of the Software, and to 00024 * permit persons to whom the Software is furnished to do so, subject to 00025 * the following conditions: 00026 * 00027 * The above copyright notice and this permission notice shall be 00028 * included in all copies or substantial portions of the Software. 00029 * 00030 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00031 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00032 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00033 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00034 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00035 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00036 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00037 * 00038 */ 00039 00040 #include <gecode/set.hh> 00041 00042 #include <gecode/set/int.hh> 00043 #include <gecode/set/rel.hh> 00044 00045 using namespace Gecode::Int; 00046 00047 namespace Gecode { 00048 00049 void 00050 rel(Home home, SetVar s, IntRelType r, IntVar x) { 00051 if (home.failed()) return; 00052 switch (r) { 00053 case IRT_EQ: 00054 { 00055 Gecode::Int::IntView xv(x); 00056 Set::SingletonView xsingle(xv); 00057 GECODE_ES_FAIL( 00058 (Set::Rel::Eq<Set::SetView,Set::SingletonView> 00059 ::post(home,s,xsingle))); 00060 00061 } 00062 break; 00063 case IRT_NQ: 00064 { 00065 Gecode::Set::SetView sv(s); 00066 GECODE_ME_FAIL( sv.cardMin(home, 1)); 00067 Gecode::Int::IntView xv(x); 00068 Set::SingletonView xsingle(xv); 00069 GECODE_ES_FAIL( 00070 (Set::Rel::NoSubset<Set::SingletonView,Set::SetView> 00071 ::post(home,xsingle,sv))); 00072 00073 } 00074 break; 00075 case IRT_LQ: 00076 { 00077 IntVar tmp(home, Int::Limits::min, Int::Limits::max); 00078 rel(home, tmp, IRT_LQ, x); 00079 GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp)); 00080 } 00081 break; 00082 case IRT_LE: 00083 { 00084 IntVar tmp(home, Int::Limits::min, Int::Limits::max); 00085 rel(home, tmp, IRT_LE, x); 00086 GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp)); 00087 } 00088 break; 00089 case IRT_GQ: 00090 { 00091 IntVar tmp(home, Int::Limits::min, Int::Limits::max); 00092 rel(home, tmp, IRT_GQ, x); 00093 GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp)); 00094 } 00095 break; 00096 case IRT_GR: 00097 { 00098 IntVar tmp(home, Int::Limits::min, Int::Limits::max); 00099 rel(home, tmp, IRT_GR, x); 00100 GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp)); 00101 } 00102 break; 00103 default: 00104 throw UnknownRelation("Set::rel"); 00105 } 00106 00107 } 00108 00109 void 00110 rel(Home home, IntVar x, IntRelType r, SetVar s) { 00111 IntRelType rr; 00112 switch (r) { 00113 case IRT_LE: rr=IRT_GR; break; 00114 case IRT_LQ: rr=IRT_GQ; break; 00115 case IRT_GR: rr=IRT_LE; break; 00116 case IRT_GQ: rr=IRT_LQ; break; 00117 default: rr=r; 00118 } 00119 rel(home, s, rr, x); 00120 } 00121 00122 void 00123 min(Home home, SetVar s, IntVar x){ 00124 if (home.failed()) return; 00125 GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,x)); 00126 } 00127 void 00128 notMin(Home home, SetVar s, IntVar x){ 00129 if (home.failed()) return; 00130 GECODE_ES_FAIL(Set::Int::NotMinElement<Set::SetView>::post(home,s,x)); 00131 } 00132 void 00133 min(Home home, SetVar s, IntVar x, BoolVar b){ 00134 if (home.failed()) return; 00135 GECODE_ES_FAIL( 00136 Set::Int::ReMinElement<Set::SetView>::post(home,s,x,b)); 00137 } 00138 void 00139 max(Home home, SetVar s, IntVar x){ 00140 if (home.failed()) return; 00141 GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,x)); 00142 } 00143 void 00144 notMax(Home home, SetVar s, IntVar x){ 00145 if (home.failed()) return; 00146 GECODE_ES_FAIL(Set::Int::NotMaxElement<Set::SetView>::post(home,s,x)); 00147 } 00148 void 00149 max(Home home, SetVar s, IntVar x, BoolVar b){ 00150 if (home.failed()) return; 00151 GECODE_ES_FAIL( 00152 Set::Int::ReMaxElement<Set::SetView>::post(home,s,x,b)); 00153 } 00154 00155 void 00156 channel(Home home, const IntVarArgs& x, SetVar s) { 00157 if (home.failed()) return; 00158 ViewArray<IntView> xa(home,x); 00159 GECODE_ES_FAIL(Set::Int::Match<Set::SetView>::post(home,s,xa)); 00160 } 00161 00162 void 00163 channel(Home home, const IntVarArgs& x, const SetVarArgs& y) { 00164 if (home.failed()) return; 00165 ViewArray<Int::IntView> xa(home,x); 00166 ViewArray<Set::SetView> ya(home,y); 00167 GECODE_ES_FAIL((Set::Int::ChannelInt<Set::SetView> 00168 ::post(home,xa,ya))); 00169 } 00170 00171 void 00172 channel(Home home, const BoolVarArgs& x, SetVar y) { 00173 if (home.failed()) return; 00174 ViewArray<Int::BoolView> xv(home,x); 00175 GECODE_ES_FAIL((Set::Int::ChannelBool<Set::SetView> 00176 ::post(home,xv,y))); 00177 } 00178 00179 void weights(Home home, const IntArgs& elements0, const IntArgs& weights0, 00180 SetVar x, IntVar y) { 00181 if (home.failed()) return; 00182 SharedArray<int> elements(elements0.size()); 00183 for (int i=elements0.size(); i--;) 00184 elements[i] = elements0[i]; 00185 SharedArray<int> weights(weights0.size()); 00186 for (int i=weights0.size(); i--;) 00187 weights[i] = weights0[i]; 00188 GECODE_ES_FAIL(Set::Int::Weights<Set::SetView>::post(home,elements, 00189 weights,x,y)); 00190 } 00191 00192 } 00193 00194 // STATISTICS: set-post