gcc.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Patrick Pekczynski <pekczynski@ps.uni-sb.de> 00005 * Guido Tack <tack@gecode.org> 00006 * 00007 * Contributing authors: 00008 * Christian Schulte <schulte@gecode.org> 00009 * 00010 * Copyright: 00011 * Patrick Pekczynski, 2004 00012 * Christian Schulte, 2009 00013 * Guido Tack, 2006 00014 * 00015 * Last modified: 00016 * $Date: 2010-03-03 17:40:32 +0100 (Wed, 03 Mar 2010) $ by $Author: schulte $ 00017 * $Revision: 10365 $ 00018 * 00019 * This file is part of Gecode, the generic constraint 00020 * development environment: 00021 * http://www.gecode.org 00022 * 00023 * Permission is hereby granted, free of charge, to any person obtaining 00024 * a copy of this software and associated documentation files (the 00025 * "Software"), to deal in the Software without restriction, including 00026 * without limitation the rights to use, copy, modify, merge, publish, 00027 * distribute, sublicense, and/or sell copies of the Software, and to 00028 * permit persons to whom the Software is furnished to do so, subject to 00029 * the following conditions: 00030 * 00031 * The above copyright notice and this permission notice shall be 00032 * included in all copies or substantial portions of the Software. 00033 * 00034 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00035 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00036 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00037 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00038 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00039 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00040 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00041 * 00042 */ 00043 00044 #include <gecode/int/gcc.hh> 00045 00046 namespace Gecode { 00047 00048 void count(Home home, const IntVarArgs& x, 00049 const IntVarArgs& c, const IntArgs& v, 00050 IntConLevel icl) { 00051 using namespace Int; 00052 if (v.size() != c.size()) 00053 throw ArgumentSizeMismatch("Int::count"); 00054 if (x.same(home)) 00055 throw ArgumentSame("Int::count"); 00056 if (home.failed()) 00057 return; 00058 00059 ViewArray<IntView> xv(home, x); 00060 ViewArray<GCC::CardView> cv(home, c.size()); 00061 // set the cardinality 00062 for (int i = v.size(); i--; ) 00063 cv[i].init(c[i],v[i]); 00064 switch (icl) { 00065 case ICL_BND: 00066 GECODE_ES_FAIL( 00067 (GCC::Bnd<GCC::CardView>::post(home,xv,cv))); 00068 break; 00069 case ICL_DOM: 00070 GECODE_ES_FAIL( 00071 (GCC::Dom<GCC::CardView>::post(home,xv,cv))); 00072 break; 00073 default: 00074 GECODE_ES_FAIL( 00075 (GCC::Val<GCC::CardView>::post(home,xv,cv))); 00076 } 00077 } 00078 00079 // domain is 0..|cards|- 1 00080 void count(Home home, const IntVarArgs& x, const IntVarArgs& c, 00081 IntConLevel icl) { 00082 IntArgs values(c.size()); 00083 for (int i = c.size(); i--; ) 00084 values[i] = i; 00085 count(home, x, c, values, icl); 00086 } 00087 00088 // constant cards 00089 void count(Home home, const IntVarArgs& x, 00090 const IntSetArgs& c, const IntArgs& v, 00091 IntConLevel icl) { 00092 using namespace Int; 00093 if (v.size() != c.size()) 00094 throw ArgumentSizeMismatch("Int::count"); 00095 if (x.same(home)) 00096 throw ArgumentSame("Int::count"); 00097 for (int i=c.size(); i--; ) { 00098 Limits::check(v[i],"Int::count"); 00099 Limits::check(c[i].min(),"Int::count"); 00100 Limits::check(c[i].max(),"Int::count"); 00101 } 00102 00103 if (home.failed()) 00104 return; 00105 00106 ViewArray<IntView> xv(home, x); 00107 00108 for (int i = v.size(); i--; ) { 00109 if (c[i].ranges() > 1) { 00110 // Found hole, so create temporary variables 00111 ViewArray<GCC::CardView> cv(home, v.size()); 00112 for (int j = v.size(); j--; ) 00113 cv[j].init(home,c[j],v[j]); 00114 switch (icl) { 00115 case ICL_BND: 00116 GECODE_ES_FAIL( 00117 (GCC::Bnd<GCC::CardView>::post(home, xv, cv))); 00118 break; 00119 case ICL_DOM: 00120 GECODE_ES_FAIL( 00121 (GCC::Dom<GCC::CardView>::post(home, xv, cv))); 00122 break; 00123 default: 00124 GECODE_ES_FAIL( 00125 (GCC::Val<GCC::CardView>::post(home, xv, cv))); 00126 } 00127 return; 00128 } 00129 } 00130 00131 // No holes: create CardConsts 00132 ViewArray<GCC::CardConst> cv(home, c.size()); 00133 00134 for (int i = c.size(); i--; ) 00135 cv[i].init(home,c[i].min(),c[i].max(),v[i]); 00136 00137 switch (icl) { 00138 case ICL_BND: 00139 GECODE_ES_FAIL( 00140 (GCC::Bnd<GCC::CardConst>::post(home, xv, cv))); 00141 break; 00142 case ICL_DOM: 00143 GECODE_ES_FAIL( 00144 (GCC::Dom<GCC::CardConst>::post(home, xv, cv))); 00145 break; 00146 default: 00147 GECODE_ES_FAIL( 00148 (GCC::Val<GCC::CardConst>::post(home, xv, cv))); 00149 } 00150 } 00151 00152 // domain is 0..|cards|- 1 00153 void count(Home home, const IntVarArgs& x, const IntSetArgs& c, 00154 IntConLevel icl) { 00155 IntArgs values(c.size()); 00156 for (int i = c.size(); i--; ) 00157 values[i] = i; 00158 count(home, x, c, values, icl); 00159 } 00160 00161 void count(Home home, const IntVarArgs& x, 00162 const IntSet& c, const IntArgs& v, 00163 IntConLevel icl) { 00164 IntSetArgs cards(v.size()); 00165 for (int i = v.size(); i--; ) 00166 cards[i] = c; 00167 count(home, x, cards, v, icl); 00168 } 00169 00170 } 00171 00172 // STATISTICS: int-post