Generated on Fri Oct 19 11:25:02 2018 for Gecode by doxygen 1.6.3

count.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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2002
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/int/count.hh>
00035 #include <gecode/int/rel.hh>
00036 
00037 namespace Gecode {
00038 
00039   void
00040   count(Home home, const IntVarArgs& x, int n,
00041         IntRelType irt, int m, IntPropLevel) {
00042     using namespace Int;
00043     Limits::check(n,"Int::count");
00044     Limits::check(m,"Int::count");
00045 
00046     GECODE_POST;
00047 
00048     ViewArray<IntView> xv(home,x);
00049     ConstIntView y(n);
00050 
00051     switch (irt) {
00052     case IRT_EQ:
00053       GECODE_ES_FAIL((Count::EqInt<IntView,ConstIntView>
00054                       ::post(home,xv,y,m)));
00055       break;
00056     case IRT_NQ:
00057       {
00058         IntVar z(home,0,x.size());
00059         GECODE_ME_FAIL(IntView(z).nq(home,m));
00060         GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
00061                         ::post(home,xv,y,z,0)));
00062       }
00063       break;
00064     case IRT_LE:
00065       m--; // FALL THROUGH
00066     case IRT_LQ:
00067       GECODE_ES_FAIL((Count::LqInt<IntView,ConstIntView>
00068                       ::post(home,xv,y,m)));
00069       break;
00070     case IRT_GR:
00071       m++; // FALL THROUGH
00072     case IRT_GQ:
00073       GECODE_ES_FAIL((Count::GqInt<IntView,ConstIntView>
00074                       ::post(home,xv,y,m)));
00075       break;
00076     default:
00077       throw UnknownRelation("Int::count");
00078     }
00079   }
00080 
00081   void
00082   count(Home home, const IntVarArgs& x, IntVar y,
00083         IntRelType irt, int m, IntPropLevel ipl) {
00084     using namespace Int;
00085     Limits::check(m,"Int::count");
00086     GECODE_POST;
00087     ViewArray<IntView> xv(home,x);
00088 
00089     switch (irt) {
00090     case IRT_EQ:
00091       {
00092         ConstIntView z(m);
00093         if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
00094           GECODE_ES_FAIL((Count::EqView<IntView,IntView,ConstIntView,true,true>
00095                           ::post(home,xv,y,z,0)));
00096         else
00097           GECODE_ES_FAIL((Count::EqView<IntView,IntView,ConstIntView,true,false>
00098                           ::post(home,xv,y,z,0)));
00099       }
00100       break;
00101     case IRT_NQ:
00102       {
00103         IntVar z(home,0,x.size());
00104         GECODE_ME_FAIL(IntView(z).nq(home,m));
00105         GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
00106                         ::post(home,xv,y,z,0)));
00107       }
00108       break;
00109     case IRT_LE:
00110       m--; // FALL THROUGH
00111     case IRT_LQ:
00112       GECODE_ES_FAIL((Count::LqInt<IntView,IntView>
00113                       ::post(home,xv,y,m)));
00114       break;
00115     case IRT_GR:
00116       m++; // FALL THROUGH
00117     case IRT_GQ:
00118       {
00119         ConstIntView z(m);
00120         if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
00121           GECODE_ES_FAIL((Count::GqView<IntView,IntView,ConstIntView,true,true>
00122                           ::post(home,xv,y,z,0)));
00123         else
00124           GECODE_ES_FAIL((Count::GqView<IntView,IntView,ConstIntView,true,false>
00125                           ::post(home,xv,y,z,0)));
00126       }
00127       break;
00128     default:
00129       throw UnknownRelation("Int::count");
00130     }
00131   }
00132 
00133   void
00134   count(Home home, const IntVarArgs& x, const IntSet& y,
00135         IntRelType irt, int m, IntPropLevel) {
00136     using namespace Int;
00137 
00138     if (y.size() == 1) {
00139       count(home,x,y.min(),irt,m);
00140       return;
00141     }
00142 
00143     Limits::check(y.min(),"Int::count");
00144     Limits::check(y.max(),"Int::count");
00145     Limits::check(m,"Int::count");
00146 
00147     GECODE_POST;
00148 
00149     ViewArray<IntView> xv(home,x);
00150     switch (irt) {
00151     case IRT_EQ:
00152       GECODE_ES_FAIL((Count::EqInt<IntView,IntSet>::post(home,xv,y,m)));
00153       break;
00154     case IRT_NQ:
00155       {
00156         IntVar z(home,0,x.size());
00157         GECODE_ME_FAIL(IntView(z).nq(home,m));
00158         GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
00159                         ::post(home,xv,y,z,0)));
00160       }
00161       break;
00162     case IRT_LE:
00163       m--; // FALL THROUGH
00164     case IRT_LQ:
00165       GECODE_ES_FAIL((Count::LqInt<IntView,IntSet>::post(home,xv,y,m)));
00166       break;
00167     case IRT_GR:
00168       m++; // FALL THROUGH
00169     case IRT_GQ:
00170       GECODE_ES_FAIL((Count::GqInt<IntView,IntSet>::post(home,xv,y,m)));
00171       break;
00172     default:
00173       throw UnknownRelation("Int::count");
00174     }
00175   }
00176 
00177   void
00178   count(Home home, const IntVarArgs& x, const IntArgs& y,
00179         IntRelType irt, int m, IntPropLevel) {
00180     using namespace Int;
00181     if (x.size() != y.size())
00182       throw ArgumentSizeMismatch("Int::count");
00183     Limits::check(m,"Int::count");
00184     GECODE_POST;
00185 
00186     ViewArray<OffsetView> xy(home,x.size());
00187     for (int i=0; i<x.size(); i++)
00188       xy[i] = OffsetView(x[i],-y[i]);
00189 
00190     ZeroIntView zero;
00191     switch (irt) {
00192     case IRT_EQ:
00193       GECODE_ES_FAIL((Count::EqInt<OffsetView,ZeroIntView>
00194                       ::post(home,xy,zero,m)));
00195       break;
00196     case IRT_NQ:
00197       {
00198         IntVar z(home,0,x.size());
00199         GECODE_ME_FAIL(IntView(z).nq(home,m));
00200         GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
00201                         ::post(home,xy,zero,z,0)));
00202       }
00203       break;
00204     case IRT_LE:
00205       m--; // FALL THROUGH
00206     case IRT_LQ:
00207       GECODE_ES_FAIL((Count::LqInt<OffsetView,ZeroIntView>
00208                       ::post(home,xy,zero,m)));
00209       break;
00210     case IRT_GR:
00211       m++; // FALL THROUGH
00212     case IRT_GQ:
00213       GECODE_ES_FAIL((Count::GqInt<OffsetView,ZeroIntView>
00214                       ::post(home,xy,zero,m)));
00215       break;
00216     default:
00217       throw UnknownRelation("Int::count");
00218     }
00219   }
00220 
00221   void
00222   count(Home home, const IntVarArgs& x, int n,
00223         IntRelType irt, IntVar z, IntPropLevel) {
00224     using namespace Int;
00225     Limits::check(n,"Int::count");
00226     GECODE_POST;
00227     ViewArray<IntView> xv(home,x);
00228     ConstIntView yv(n);
00229     switch (irt) {
00230     case IRT_EQ:
00231       GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
00232                       ::post(home,xv,yv,z,0)));
00233       break;
00234     case IRT_NQ:
00235       {
00236         IntVar nz(home,0,x.size());
00237         GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
00238         GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
00239                         ::post(home,xv,yv,nz,0)));
00240       }
00241       break;
00242     case IRT_LE:
00243       GECODE_ES_FAIL((Count::LqView<IntView,ConstIntView,IntView,true>
00244                            ::post(home,xv,yv,z,-1)));
00245       break;
00246     case IRT_LQ:
00247       GECODE_ES_FAIL((Count::LqView<IntView,ConstIntView,IntView,true>
00248                            ::post(home,xv,yv,z,0)));
00249       break;
00250     case IRT_GR:
00251       GECODE_ES_FAIL((Count::GqView<IntView,ConstIntView,IntView,true,false>
00252                       ::post(home,xv,yv,z,1)));
00253       break;
00254     case IRT_GQ:
00255       GECODE_ES_FAIL((Count::GqView<IntView,ConstIntView,IntView,true,false>
00256                       ::post(home,xv,yv,z,0)));
00257       break;
00258     default:
00259       throw UnknownRelation("Int::count");
00260     }
00261   }
00262 
00263   void
00264   count(Home home, const IntVarArgs& x, IntVar y,
00265         IntRelType irt, IntVar z, IntPropLevel ipl) {
00266     using namespace Int;
00267     GECODE_POST;
00268     ViewArray<IntView> xv(home,x);
00269     switch (irt) {
00270     case IRT_EQ:
00271       if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
00272         GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,true>
00273                         ::post(home,xv,y,z,0)));
00274       else
00275         GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
00276                         ::post(home,xv,y,z,0)));
00277       break;
00278     case IRT_NQ:
00279       {
00280         IntVar nz(home,0,x.size());
00281         GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
00282         GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
00283                         ::post(home,xv,y,nz,0)));
00284       }
00285       break;
00286     case IRT_LE:
00287       GECODE_ES_FAIL((Count::LqView<IntView,IntView,IntView,true>
00288                       ::post(home,xv,y,z,-1)));
00289       break;
00290     case IRT_LQ:
00291       GECODE_ES_FAIL((Count::LqView<IntView,IntView,IntView,true>
00292                       ::post(home,xv,y,z,0)));
00293       break;
00294     case IRT_GR:
00295       if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
00296         GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,true>
00297                         ::post(home,xv,y,z,1)));
00298       else
00299         GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,false>
00300                         ::post(home,xv,y,z,1)));
00301       break;
00302     case IRT_GQ:
00303       if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
00304         GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,true>
00305                         ::post(home,xv,y,z,0)));
00306       else
00307         GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,false>
00308                         ::post(home,xv,y,z,0)));
00309       break;
00310     default:
00311       throw UnknownRelation("Int::count");
00312     }
00313   }
00314 
00315   void
00316   count(Home home, const IntVarArgs& x, const IntSet& y,
00317         IntRelType irt, IntVar z, IntPropLevel) {
00318     using namespace Int;
00319 
00320     if (y.size() == 1) {
00321       count(home,x,y.min(),irt,z);
00322       return;
00323     }
00324 
00325     Limits::check(y.min(),"Int::count");
00326     Limits::check(y.max(),"Int::count");
00327 
00328     GECODE_POST;
00329     ViewArray<IntView> xv(home,x);
00330     switch (irt) {
00331     case IRT_EQ:
00332       GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
00333                       ::post(home,xv,y,z,0)));
00334       break;
00335     case IRT_NQ:
00336       {
00337         IntVar nz(home,0,x.size());
00338         GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
00339         GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
00340                         ::post(home,xv,y,nz,0)));
00341       }
00342       break;
00343     case IRT_LE:
00344       GECODE_ES_FAIL((Count::LqView<IntView,IntSet,IntView,true>
00345                       ::post(home,xv,y,z,-1)));
00346       break;
00347     case IRT_LQ:
00348       GECODE_ES_FAIL((Count::LqView<IntView,IntSet,IntView,true>
00349                       ::post(home,xv,y,z,0)));
00350       break;
00351     case IRT_GR:
00352       GECODE_ES_FAIL((Count::GqView<IntView,IntSet,IntView,true,false>
00353                       ::post(home,xv,y,z,1)));
00354       break;
00355     case IRT_GQ:
00356       GECODE_ES_FAIL((Count::GqView<IntView,IntSet,IntView,true,false>
00357                       ::post(home,xv,y,z,0)));
00358       break;
00359     default:
00360       throw UnknownRelation("Int::count");
00361     }
00362   }
00363 
00364   void
00365   count(Home home, const IntVarArgs& x, const IntArgs& y,
00366         IntRelType irt, IntVar z, IntPropLevel) {
00367     using namespace Int;
00368     if (x.size() != y.size())
00369       throw ArgumentSizeMismatch("Int::count");
00370     GECODE_POST;
00371 
00372     ViewArray<OffsetView> xy(home,x.size());
00373     for (int i=0; i<x.size(); i++)
00374       xy[i] = OffsetView(x[i],-y[i]);
00375 
00376     ZeroIntView u;
00377     switch (irt) {
00378     case IRT_EQ:
00379       GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
00380                       ::post(home,xy,u,z,0)));
00381       break;
00382     case IRT_NQ:
00383       {
00384         IntVar nz(home,0,x.size());
00385         GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
00386         GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
00387                         ::post(home,xy,u,nz,0)));
00388       }
00389       break;
00390     case IRT_LE:
00391       GECODE_ES_FAIL((Count::LqView<OffsetView,ZeroIntView,IntView,true>
00392                       ::post(home,xy,u,z,-1)));
00393       break;
00394     case IRT_LQ:
00395       GECODE_ES_FAIL((Count::LqView<OffsetView,ZeroIntView,IntView,true>
00396                       ::post(home,xy,u,z,0)));
00397       break;
00398     case IRT_GR:
00399       GECODE_ES_FAIL((Count::GqView<OffsetView,ZeroIntView,IntView,true,false>
00400                       ::post(home,xy,u,z,1)));
00401       break;
00402     case IRT_GQ:
00403       GECODE_ES_FAIL((Count::GqView<OffsetView,ZeroIntView,IntView,true,false>
00404                       ::post(home,xy,u,z,0)));
00405       break;
00406     default:
00407       throw UnknownRelation("Int::count");
00408     }
00409   }
00410 
00411 }
00412 
00413 // STATISTICS: int-post