Generated on Thu Mar 22 10:39:40 2012 for Gecode by doxygen 1.6.3

nvalues.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, 2011
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-21 16:27:00 +0200 (Sun, 21 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12319 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <gecode/int/nvalues.hh>
00039 #include <gecode/int/rel.hh>
00040 
00041 namespace Gecode {
00042 
00043   void
00044   nvalues(Home home, const IntVarArgs& x, IntRelType r, int y,
00045           IntConLevel) {
00046     using namespace Int;
00047     Limits::check(y,"Int::nvalues");
00048     // Due to the quadratic Boolean matrix used in propagation
00049     Limits::check(static_cast<double>(x.size())*static_cast<double>(x.size()),
00050                   "Int::nvalues");
00051 
00052     if (home.failed()) return;
00053 
00054     ViewArray<IntView> xv(home,x);
00055 
00056     switch (r) {
00057     case IRT_EQ:
00058       {
00059         ConstIntView yv(y);
00060         GECODE_ES_FAIL(NValues::EqInt<ConstIntView>::post(home,xv,yv));
00061       }
00062       break;
00063     case IRT_NQ:
00064       {
00065         IntVar z(home,0,x.size());
00066         GECODE_ME_FAIL(IntView(z).nq(home,y));
00067         GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,z));
00068       }
00069       break;
00070     case IRT_LE:
00071       y--;
00072       // Fall through
00073     case IRT_LQ:
00074       {
00075         ConstIntView yv(y);
00076         GECODE_ES_FAIL(NValues::LqInt<ConstIntView>::post(home,xv,yv));
00077       }
00078       break;
00079     case IRT_GR:
00080       y++;
00081       // Fall through
00082     case IRT_GQ:
00083       {
00084         ConstIntView yv(y);
00085         GECODE_ES_FAIL(NValues::GqInt<ConstIntView>::post(home,xv,yv));
00086       }
00087       break;
00088     default:
00089       throw UnknownRelation("Int::nvalues");
00090     }
00091   }
00092 
00093   void
00094   nvalues(Home home, const IntVarArgs& x, IntRelType r, IntVar y,
00095           IntConLevel) {
00096     using namespace Int;
00097     // Due to the quadratic Boolean matrix used in propagation
00098     Limits::check(static_cast<double>(x.size())*static_cast<double>(x.size()),
00099                   "Int::nvalues");
00100 
00101     if (home.failed()) return;
00102 
00103     if (y.assigned()) {
00104       nvalues(home, x, r, y.val());
00105       return;
00106     }
00107 
00108     ViewArray<IntView> xv(home,x);
00109 
00110     switch (r) {
00111     case IRT_EQ:
00112       GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,y));
00113       break;
00114     case IRT_NQ:
00115       {
00116         IntVar z(home,0,x.size());
00117         GECODE_ES_FAIL(Rel::Nq<IntView>::post(home,y,z));
00118         GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,z));
00119       }
00120       break;
00121     case IRT_LE:
00122       {
00123         OffsetView z(y,-1);
00124         GECODE_ES_FAIL(NValues::LqInt<OffsetView>::post(home,xv,z));
00125       }
00126       break;
00127     case IRT_LQ:
00128       GECODE_ES_FAIL(NValues::LqInt<IntView>::post(home,xv,y));
00129       break;
00130     case IRT_GR:
00131       {
00132         OffsetView z(y,1);
00133         GECODE_ES_FAIL(NValues::GqInt<OffsetView>::post(home,xv,z));
00134       }
00135       break;
00136     case IRT_GQ:
00137       GECODE_ES_FAIL(NValues::GqInt<IntView>::post(home,xv,y));
00138       break;
00139     default:
00140       throw UnknownRelation("Int::nvalues");
00141     }
00142   }
00143 
00144   void
00145   nvalues(Home home, const BoolVarArgs& x, IntRelType r, int y,
00146           IntConLevel) {
00147     using namespace Int;
00148     Limits::check(y,"Int::nvalues");
00149 
00150     if (home.failed()) return;
00151 
00152     Region region(home);
00153     ViewArray<BoolView> xv(region,x);
00154 
00155     switch (r) {
00156     case IRT_EQ:
00157       {
00158         ConstIntView yv(y);
00159         GECODE_ES_FAIL(NValues::EqBool<ConstIntView>::post(home,xv,yv));
00160       }
00161       break;
00162     case IRT_NQ:
00163       {
00164         IntVar z(home,0,2);
00165         GECODE_ME_FAIL(IntView(z).nq(home,y));
00166         GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,z));
00167       }
00168       break;
00169     case IRT_LE:
00170       y--;
00171       // Fall through
00172     case IRT_LQ:
00173       {
00174         ConstIntView yv(y);
00175         GECODE_ES_FAIL(NValues::LqBool<ConstIntView>::post(home,xv,yv));
00176       }
00177       break;
00178     case IRT_GR:
00179       y++;
00180       // Fall through
00181     case IRT_GQ:
00182       {
00183         ConstIntView yv(y);
00184         GECODE_ES_FAIL(NValues::GqBool<ConstIntView>::post(home,xv,yv));
00185       }
00186       break;
00187     default:
00188       throw UnknownRelation("Int::nvalues");
00189     }
00190   }
00191 
00192   void
00193   nvalues(Home home, const BoolVarArgs& x, IntRelType r, IntVar y,
00194           IntConLevel) {
00195     using namespace Int;
00196 
00197     if (home.failed()) return;
00198 
00199     if (y.assigned()) {
00200       nvalues(home, x, r, y.val());
00201       return;
00202     }
00203 
00204     Region region(home);
00205     ViewArray<BoolView> xv(region,x);
00206 
00207     switch (r) {
00208     case IRT_EQ:
00209       GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,y));
00210       break;
00211     case IRT_NQ:
00212       {
00213         IntVar z(home,0,2);
00214         GECODE_ES_FAIL(Rel::Nq<IntView>::post(home,y,z));
00215         GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,z));
00216       }
00217       break;
00218     case IRT_LE:
00219       {
00220         OffsetView z(y,-1);
00221         GECODE_ES_FAIL(NValues::LqBool<OffsetView>::post(home,xv,z));
00222       }
00223       break;
00224     case IRT_LQ:
00225       GECODE_ES_FAIL(NValues::LqBool<IntView>::post(home,xv,y));
00226       break;
00227     case IRT_GR:
00228       {
00229         OffsetView z(y,1);
00230         GECODE_ES_FAIL(NValues::GqBool<OffsetView>::post(home,xv,z));
00231       }
00232       break;
00233     case IRT_GQ:
00234       GECODE_ES_FAIL(NValues::GqBool<IntView>::post(home,xv,y));
00235       break;
00236     default:
00237       throw UnknownRelation("Int::nvalues");
00238     }
00239   }
00240 
00241 }
00242 
00243 // STATISTICS: int-post
00244