00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <gecode/int/distinct.hh>
00045 #include <gecode/int/bool.hh>
00046
00047 namespace Gecode {
00048
00049 void
00050 distinct(Home home, const IntVarArgs& x, IntPropLevel ipl) {
00051 using namespace Int;
00052 if (x.same(home))
00053 throw ArgumentSame("Int::distinct");
00054 GECODE_POST;
00055 ViewArray<IntView> xv(home,x);
00056 switch (vbd(ipl)) {
00057 case IPL_BND:
00058 GECODE_ES_FAIL(Distinct::Bnd<IntView>::post(home,xv));
00059 break;
00060 case IPL_DOM:
00061 GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,xv));
00062 break;
00063 default:
00064 GECODE_ES_FAIL(Distinct::Val<IntView>::post(home,xv));
00065 }
00066 }
00067
00068 void
00069 distinct(Home home, const IntArgs& c, const IntVarArgs& x,
00070 IntPropLevel ipl) {
00071 using namespace Int;
00072 if (x.same(home))
00073 throw ArgumentSame("Int::distinct");
00074 if (c.size() != x.size())
00075 throw ArgumentSizeMismatch("Int::distinct");
00076 GECODE_POST;
00077 ViewArray<OffsetView> cx(home,x.size());
00078 for (int i = c.size(); i--; ) {
00079 long long int cx_min = (static_cast<long long int>(c[i]) +
00080 static_cast<long long int>(x[i].min()));
00081 long long int cx_max = (static_cast<long long int>(c[i]) +
00082 static_cast<long long int>(x[i].max()));
00083 Limits::check(c[i],"Int::distinct");
00084 Limits::check(cx_min,"Int::distinct");
00085 Limits::check(cx_max,"Int::distinct");
00086 cx[i] = OffsetView(x[i],c[i]);
00087 }
00088 switch (vbd(ipl)) {
00089 case IPL_BND:
00090 GECODE_ES_FAIL(Distinct::Bnd<OffsetView>::post(home,cx));
00091 break;
00092 case IPL_DOM:
00093 GECODE_ES_FAIL(Distinct::Dom<OffsetView>::post(home,cx));
00094 break;
00095 default:
00096 GECODE_ES_FAIL(Distinct::Val<OffsetView>::post(home,cx));
00097 }
00098 }
00099
00100 void
00101 distinct(Home home, const BoolVarArgs& b, const IntVarArgs& x,
00102 IntPropLevel ipl) {
00103 using namespace Int;
00104 if (x.same(home))
00105 throw ArgumentSame("Int::distinct");
00106 if (b.size() != x.size())
00107 throw ArgumentSizeMismatch("Int::distinct");
00108 GECODE_POST;
00109
00110 int n = x.size();
00111 int min = Limits::max;
00112 int max = Limits::min;
00113 int m = 0;
00114 for (int i=n; i--; )
00115 if (!b[i].zero()) {
00116 min = std::min(min,x[i].min());
00117 max = std::max(max,x[i].max());
00118 m++;
00119 }
00120
00121 if (m < 2)
00122 return;
00123
00124 int start;
00125 if (max < Limits::max-m)
00126 start = max+1;
00127 else if (min > Limits::min+m)
00128 start = min-(m+1);
00129 else
00130 throw OutOfLimits("Int::distinct");
00131
00132 ViewArray<IntView> y(home,m);
00133 int j = 0;
00134 for (int i=n; i--; )
00135 if (b[i].one()) {
00136 y[j] = x[i]; j++;
00137 } else if (b[i].none()) {
00138 y[j] = IntVar(home, Limits::min, Limits::max);
00139 GECODE_ES_FAIL((Bool::IteDom<IntView,ConstIntView,IntView>::post
00140 (home, b[i], x[i], start+j, y[j])));
00141 j++;
00142 }
00143 assert(j == m);
00144
00145 switch (vbd(ipl)) {
00146 case IPL_BND:
00147 GECODE_ES_FAIL(Distinct::Bnd<IntView>::post(home,y));
00148 break;
00149 case IPL_DOM:
00150 GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,y));
00151 break;
00152 default:
00153 GECODE_ES_FAIL(Distinct::Val<IntView>::post(home,y));
00154 }
00155 }
00156
00157 void
00158 distinct(Home home, const IntVarArgs& x, int c,
00159 IntPropLevel ipl) {
00160 using namespace Int;
00161 if (x.same(home))
00162 throw ArgumentSame("Int::distinct");
00163 GECODE_POST;
00164
00165 int n = x.size();
00166 int min = Limits::max;
00167 int max = Limits::min;
00168 int m = 0;
00169 for (int i=n; i--; )
00170 if (!(x[i].assigned() && (x[i].val() == c))) {
00171 min = std::min(min,x[i].min());
00172 max = std::max(max,x[i].max());
00173 m++;
00174 }
00175
00176 if (m < 2)
00177 return;
00178
00179 int start;
00180 if (max < Limits::max-m)
00181 start = max+1;
00182 else if (min > Limits::min+m)
00183 start = min-(m+1);
00184 else
00185 throw OutOfLimits("Int::distinct");
00186
00187 ViewArray<IntView> y(home,m);
00188 int j = 0;
00189 for (int i=n; i--; )
00190 if (!x[i].in(c)) {
00191 y[j] = x[i]; j++;
00192 } else if (!(x[i].assigned() && (x[i].val() == c))) {
00193 y[j] = IntVar(home, Limits::min, Limits::max);
00194 GECODE_ES_FAIL(Distinct::EqIte::post
00195 (home, x[i], y[j], c, start+j));
00196 j++;
00197 }
00198 assert(j == m);
00199
00200 switch (vbd(ipl)) {
00201 case IPL_BND:
00202 GECODE_ES_FAIL(Distinct::Bnd<IntView>::post(home,y));
00203 break;
00204 case IPL_DOM:
00205 GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,y));
00206 break;
00207 default:
00208 GECODE_ES_FAIL(Distinct::Val<IntView>::post(home,y));
00209 }
00210 }
00211
00212 }
00213
00214