Generated on Tue May 22 09:39:59 2018 for Gecode by doxygen 1.6.3

int.hpp

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  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2005
00009  *     Mikael Lagerkvist, 2006
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 namespace Test { namespace Int {
00037 
00038   /*
00039    * Assignments
00040    *
00041    */
00042   inline
00043   Assignment::Assignment(int n0, const Gecode::IntSet& d0)
00044     : n(n0), d(d0) {}
00045   inline int
00046   Assignment::size(void) const {
00047     return n;
00048   }
00049   inline
00050   Assignment::~Assignment(void) {}
00051 
00052 
00053   inline
00054   CpltAssignment::CpltAssignment(int n, const Gecode::IntSet& d)
00055     : Assignment(n,d),
00056       dsv(new Gecode::IntSetValues[static_cast<unsigned int>(n)]) {
00057     for (int i=n; i--; )
00058       dsv[i].init(d);
00059   }
00060   inline bool
00061   CpltAssignment::operator()(void) const {
00062     return dsv[0]();
00063   }
00064   inline int
00065   CpltAssignment::operator[](int i) const {
00066     assert((i>=0) && (i<n));
00067     return dsv[i].val();
00068   }
00069   inline
00070   CpltAssignment::~CpltAssignment(void) {
00071     delete [] dsv;
00072   }
00073 
00074 
00075   forceinline int
00076   RandomAssignment::randval(void) {
00077     unsigned int skip = Base::rand(d.size());
00078     for (Gecode::IntSetRanges it(d); true; ++it) {
00079       if (it.width() > skip)
00080         return it.min() + static_cast<int>(skip);
00081       skip -= it.width();
00082     }
00083     GECODE_NEVER;
00084     return 0;
00085   }
00086 
00087   inline
00088   RandomAssignment::RandomAssignment(int n, const Gecode::IntSet& d, int a0)
00089     : Assignment(n,d), vals(new int[n]), a(a0) {
00090     for (int i=n; i--; )
00091       vals[i] = randval();
00092   }
00093 
00094   inline bool
00095   RandomAssignment::operator()(void) const {
00096     return a>0;
00097   }
00098   inline int
00099   RandomAssignment::operator[](int i) const {
00100     assert((i>=0) && (i<n));
00101     return vals[i];
00102   }
00103   inline
00104   RandomAssignment::~RandomAssignment(void) {
00105     delete [] vals;
00106   }
00107 
00108   forceinline int
00109   RandomMixAssignment::randval(const Gecode::IntSet& d) {
00110     unsigned int skip = Base::rand(d.size());
00111     for (Gecode::IntSetRanges it(d); true; ++it) {
00112       if (it.width() > skip)
00113         return it.min() + static_cast<int>(skip);
00114       skip -= it.width();
00115     }
00116     GECODE_NEVER;
00117     return 0;
00118   }
00119 
00120   inline
00121   RandomMixAssignment::RandomMixAssignment(int n0, const Gecode::IntSet& d0,
00122                                            int n1, const Gecode::IntSet& d1,
00123                                            int a0)
00124     : Assignment(n0+n1,d0),vals(new int[n0+n1]),a(a0),_n1(n1),_d1(d1) {
00125     for (int i=n0; i--; )
00126       vals[i] = randval(d);
00127     for (int i=n1; i--; )
00128       vals[n0+i] = randval(_d1);
00129   }
00130 
00131   inline bool
00132   RandomMixAssignment::operator()(void) const {
00133     return a>0;
00134   }
00135 
00136   inline int
00137   RandomMixAssignment::operator[](int i) const {
00138     assert((i>=0) && (i<n));
00139     return vals[i];
00140   }
00141 
00142   inline
00143   RandomMixAssignment::~RandomMixAssignment(void) {
00144     delete [] vals;
00145   }
00146 
00147   /*
00148    * Tests with integer constraints
00149    *
00150    */
00151   forceinline bool
00152   Test::eqv(void) const {
00153     return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
00154   }
00155   forceinline bool
00156   Test::imp(void) const {
00157     return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
00158   }
00159   forceinline bool
00160   Test::pmi(void) const {
00161     return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
00162   }
00163   inline
00164   Test::Test(const std::string& p, const std::string& s,
00165              int a, const Gecode::IntSet& d, bool r,
00166              Gecode::IntPropLevel i)
00167     : Base(p+s), arity(a), dom(d),
00168       reified(r), rms((1 << Gecode::RM_EQV) |
00169                       (1 << Gecode::RM_IMP) |
00170                       (1 << Gecode::RM_PMI)),
00171       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00172       testsearch(true), testfix(true) {}
00173 
00174   inline
00175   Test::Test(const std::string& s,
00176              int a, const Gecode::IntSet& d, bool r,
00177              Gecode::IntPropLevel i)
00178     : Base("Int::"+s), arity(a), dom(d),
00179       reified(r), rms((1 << Gecode::RM_EQV) |
00180                       (1 << Gecode::RM_IMP) |
00181                       (1 << Gecode::RM_PMI)),
00182       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00183       testsearch(true), testfix(true) {}
00184 
00185   inline
00186   Test::Test(const std::string& p, const std::string& s,
00187              int a, int min, int max, bool r,
00188              Gecode::IntPropLevel i)
00189     : Base(p+s), arity(a), dom(min,max),
00190       reified(r), rms((1 << Gecode::RM_EQV) |
00191                       (1 << Gecode::RM_IMP) |
00192                       (1 << Gecode::RM_PMI)),
00193       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00194       testsearch(true), testfix(true) {}
00195 
00196   inline
00197   Test::Test(const std::string& s,
00198              int a, int min, int max, bool r, Gecode::IntPropLevel i)
00199     : Base("Int::"+s), arity(a), dom(min,max),
00200       reified(r), rms((1 << Gecode::RM_EQV) |
00201                       (1 << Gecode::RM_IMP) |
00202                       (1 << Gecode::RM_PMI)),
00203       ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
00204       testsearch(true), testfix(true) {}
00205 
00206   inline
00207   std::string
00208   Test::str(Gecode::IntPropLevel ipl) {
00209     using namespace Gecode;
00210     std::stringstream s;
00211     switch (vbd(ipl)) {
00212     case IPL_VAL: s << "Val"; break;
00213     case IPL_BND: s << "Bnd"; break;
00214     case IPL_DOM: s << "Dom"; break;
00215     default: s << "Def"; break;
00216     }
00217     if (ipl & IPL_BASIC) s << "+B";
00218     if (ipl & IPL_ADVANCED) s << "+A";
00219     return s.str();
00220   }
00221 
00222   inline
00223   std::string
00224   Test::str(Gecode::IntRelType irt) {
00225     using namespace Gecode;
00226     switch (irt) {
00227     case IRT_LQ: return "Lq";
00228     case IRT_LE: return "Le";
00229     case IRT_GQ: return "Gq";
00230     case IRT_GR: return "Gr";
00231     case IRT_EQ: return "Eq";
00232     case IRT_NQ: return "Nq";
00233     default: ;
00234     }
00235     GECODE_NEVER;
00236     return "NONE";
00237   }
00238 
00239   inline std::string
00240   Test::str(Gecode::BoolOpType bot) {
00241     using namespace Gecode;
00242     switch (bot) {
00243     case BOT_AND: return "And";
00244     case BOT_OR:  return "Or";
00245     case BOT_IMP: return "Imp";
00246     case BOT_EQV: return "Eqv";
00247     case BOT_XOR: return "Xor";
00248     default: GECODE_NEVER;
00249     }
00250     GECODE_NEVER;
00251     return "NONE";
00252   }
00253 
00254   inline
00255   std::string
00256   Test::str(int i) {
00257     std::stringstream s;
00258     s << i;
00259     return s.str();
00260   }
00261 
00262   inline
00263   std::string
00264   Test::str(const Gecode::IntArgs& x) {
00265     std::string s = "";
00266     for (int i=0; i<x.size()-1; i++)
00267       s += str(x[i]) + ",";
00268     return "[" + s + str(x[x.size()-1]) + "]";
00269   }
00270 
00271   template<class T>
00272   inline bool
00273   Test::cmp(T x, Gecode::IntRelType r, T y) {
00274     using namespace Gecode;
00275     switch (r) {
00276     case IRT_EQ: return x == y;
00277     case IRT_NQ: return x != y;
00278     case IRT_LQ: return x <= y;
00279     case IRT_LE: return x < y;
00280     case IRT_GR: return x > y;
00281     case IRT_GQ: return x >= y;
00282     default: ;
00283     }
00284     return false;
00285   }
00286 
00287 
00288 
00289   inline
00290   IntPropLevels::IntPropLevels(void)
00291     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
00292   inline bool
00293   IntPropLevels::operator()(void) const {
00294     return i>=0;
00295   }
00296   inline void
00297   IntPropLevels::operator++(void) {
00298     i--;
00299   }
00300   inline Gecode::IntPropLevel
00301   IntPropLevels::ipl(void) const {
00302     return ipls[i];
00303   }
00304 
00305 
00306   inline
00307   IntPropBasicAdvanced::IntPropBasicAdvanced(void)
00308     : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
00309   inline bool
00310   IntPropBasicAdvanced::operator()(void) const {
00311     return i>=0;
00312   }
00313   inline void
00314   IntPropBasicAdvanced::operator++(void) {
00315     i--;
00316   }
00317   inline Gecode::IntPropLevel
00318   IntPropBasicAdvanced::ipl(void) const {
00319     return ipls[i];
00320   }
00321 
00322 
00323   inline
00324   IntRelTypes::IntRelTypes(void)
00325     : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
00326   inline void
00327   IntRelTypes::reset(void) {
00328     i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
00329   }
00330   inline bool
00331   IntRelTypes::operator()(void) const {
00332     return i>=0;
00333   }
00334   inline void
00335   IntRelTypes::operator++(void) {
00336     i--;
00337   }
00338   inline Gecode::IntRelType
00339   IntRelTypes::irt(void) const {
00340     return irts[i];
00341   }
00342 
00343   inline
00344   BoolOpTypes::BoolOpTypes(void)
00345     : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
00346   inline bool
00347   BoolOpTypes::operator()(void) const {
00348     return i>=0;
00349   }
00350   inline void
00351   BoolOpTypes::operator++(void) {
00352     i--;
00353   }
00354   inline Gecode::BoolOpType
00355   BoolOpTypes::bot(void) const {
00356     return bots[i];
00357   }
00358 
00359 }}
00360 
00361 // STATISTICS: test-int
00362