Generated on Tue Apr 18 10:21:52 2017 for Gecode by doxygen 1.6.3

distinct.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Gabor Szokoli <szokoli@gecode.org>
00006  *
00007  *  Contributing authors:
00008  *     Roberto Castaņeda Lozano <rcas@kth.se>
00009  *
00010  *  Copyright:
00011  *     Roberto Castaņeda Lozano, 2015
00012  *     Christian Schulte, 2002
00013  *     Gabor Szokoli, 2003
00014  *
00015  *  Last modified:
00016  *     $Date: 2016-05-23 22:18:23 +0200 (Mon, 23 May 2016) $ by $Author: schulte $
00017  *     $Revision: 15073 $
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/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 // STATISTICS: int-post