gecode » int » gcc

00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Patrick Pekczynski <pekczynski@ps.uni-sb.de> 00005 * 00006 * Contributing authors: 00007 * Christian Schulte <schulte@gecode.org> 00008 * Guido Tack <tack@gecode.org> 00009 * 00010 * Copyright: 00011 * Patrick Pekczynski, 2004/2005 00012 * Christian Schulte, 2009 00013 * Guido Tack, 2009 00014 * 00015 * Last modified: 00016 * $Date: 2009-11-29 15:38:21 +0100 (Sun, 29 Nov 2009) $ by $Author: tack $ 00017 * $Revision: 10140 $ 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/linear.hh> 00045 #include <gecode/int/distinct.hh> 00046 00047 namespace Gecode { namespace Int { namespace GCC { 00048 00049 template<class Card> 00050 class CardLess : public Support::Less<Card> { 00051 public: 00052 bool operator ()(const Card& x, const Card& y) { 00053 return x.card() < y.card(); 00054 } 00055 }; 00056 00061 template<class Card> 00062 ExecStatus 00063 postSideConstraints(Home home, ViewArray<IntView>& x, ViewArray<Card>& k) { 00064 CardLess<Card> cl; 00065 Support::quicksort(&k[0], k.size(), cl); 00066 Region r(home); 00067 00068 { 00069 int smin = 0; 00070 int smax = 0; 00071 for (int i = k.size(); i--; ) { 00072 GECODE_ME_CHECK((k[i].gq(home, 0))); 00073 GECODE_ME_CHECK((k[i].lq(home, x.size()))); 00074 smin += k[i].min(); 00075 smax += k[i].max(); 00076 } 00077 00078 // not enough variables to satisfy min req 00079 if ((x.size() < smin) || (smax < x.size())) 00080 return ES_FAILED; 00081 } 00082 00083 // Remove all values from the x that are not in v 00084 { 00085 int* v = r.alloc<int>(k.size()); 00086 for (int i=k.size(); i--;) 00087 v[i]=k[i].card(); 00088 Support::quicksort(v,k.size()); 00089 for (int i=x.size(); i--; ) { 00090 Iter::Values::Array iv(v,k.size()); 00091 GECODE_ME_CHECK(x[i].inter_v(home, iv, false)); 00092 } 00093 } 00094 00095 // Remove all values with 0 max occurrence 00096 // and remove corresponding occurrence variables from k 00097 { 00098 // The number of zeroes 00099 int n_z = 0; 00100 for (int i=k.size(); i--;) 00101 if (k[i].max() == 0) 00102 n_z++; 00103 00104 if (n_z > 0) { 00105 int* z = r.alloc<int>(n_z); 00106 n_z = 0; 00107 int n_k = 0; 00108 for (int i=0; i<k.size(); i++) 00109 if (k[i].max() == 0) { 00110 z[n_z++] = k[i].card(); 00111 } else { 00112 k[n_k++] = k[i]; 00113 } 00114 k.size(n_k); 00115 Support::quicksort(z,n_z); 00116 for (int i=x.size(); i--;) { 00117 Iter::Values::Array zi(z,n_z); 00118 GECODE_ME_CHECK(x[i].minus_v(home,zi,false)); 00119 } 00120 } 00121 } 00122 00123 if (Card::propagate) { 00124 Linear::Term<IntView>* t = r.alloc<Linear::Term<IntView> >(k.size()); 00125 for (int i = k.size(); i--; ) { 00126 t[i].a=1; t[i].x=k[i].base(); 00127 } 00128 Linear::post(home,t,k.size(),IRT_EQ,x.size(),ICL_BND); 00129 } 00130 00131 return ES_OK; 00132 } 00133 00134 00139 template<class Card> 00140 inline bool 00141 isDistinct(Home home, ViewArray<IntView>& x, ViewArray<Card>& k) { 00142 if (Card::propagate) { 00143 Region r(home); 00144 ViewRanges<IntView>* xrange = r.alloc<ViewRanges<IntView> >(x.size()); 00145 for (int i = x.size(); i--; ){ 00146 ViewRanges<IntView> iter(x[i]); 00147 xrange[i] = iter; 00148 } 00149 Iter::Ranges::NaryUnion<ViewRanges<IntView> > drl(&xrange[0], x.size()); 00150 if (static_cast<unsigned int>(k.size()) == Iter::Ranges::size(drl)) { 00151 for (int i=k.size(); i--;) 00152 if (k[i].min() != 1 || k[i].max() != 1) 00153 return false; 00154 return true; 00155 } else { 00156 return false; 00157 } 00158 } else { 00159 for (int i=k.size(); i--;) 00160 if (k[i].min() != 0 || k[i].max() != 1) 00161 return false; 00162 return true; 00163 } 00164 } 00165 00166 }}} 00167 00168 // STATISTICS: int-prop