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

bool.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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2006
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 namespace Gecode { namespace Int {
00035 
00036   /*
00037    * Creation of new variable implementations
00038    *
00039    */
00040   forceinline
00041   BoolVarImp::BoolVarImp(int n) {
00042     assert(bits() == 0);
00043     bits() |= (n << 1) | n;
00044   }
00045   forceinline
00046   BoolVarImp::BoolVarImp(Space& home, int min, int max)
00047     : BoolVarImpBase(home) {
00048     assert(bits() == 0);
00049     bits() |= (max << 1) | min;
00050   }
00051 
00052 
00053   /*
00054    * Operations on Boolean variable implementations
00055    *
00056    */
00057   forceinline BoolStatus
00058   BoolVarImp::status(void) const {
00059     return bits() & 3;
00060   }
00061   forceinline int
00062   BoolVarImp::min(void) const {
00063     return static_cast<int>(bits() & 1);
00064   }
00065   forceinline int
00066   BoolVarImp::max(void) const {
00067     return static_cast<int>((bits() & 2) >> 1);
00068   }
00069   forceinline int
00070   BoolVarImp::med(void) const {
00071     return min();
00072   }
00073 
00074   forceinline int
00075   BoolVarImp::val(void) const {
00076     assert(status() != NONE);
00077     return min();
00078   }
00079 
00080   forceinline bool
00081   BoolVarImp::range(void) const {
00082     return true;
00083   }
00084   forceinline bool
00085   BoolVarImp::assigned(void) const {
00086     return status() != NONE;
00087   }
00088 
00089 
00090   forceinline unsigned int
00091   BoolVarImp::width(void) const {
00092     return assigned() ? 1U : 2U;
00093   }
00094 
00095   forceinline unsigned int
00096   BoolVarImp::size(void) const {
00097     return assigned() ? 1U : 2U;
00098   }
00099 
00100   forceinline unsigned int
00101   BoolVarImp::regret_min(void) const {
00102     return assigned() ? 1U : 0U;
00103   }
00104   forceinline unsigned int
00105   BoolVarImp::regret_max(void) const {
00106     return assigned() ? 1U : 0U;
00107   }
00108 
00109 
00110 
00111   /*
00112    * Tests
00113    *
00114    */
00115 
00116   forceinline bool
00117   BoolVarImp::in(int n) const {
00118     return (n >= min()) && (n <= max());
00119   }
00120   forceinline bool
00121   BoolVarImp::in(long long int n) const {
00122     return (n >= min()) && (n <= max());
00123   }
00124 
00125 
00126   /*
00127    * Boolean domain tests
00128    *
00129    */
00130   forceinline bool
00131   BoolVarImp::zero(void) const {
00132     return status() < NONE;
00133   }
00134   forceinline bool
00135   BoolVarImp::one(void) const {
00136     return status() > NONE;
00137   }
00138   forceinline bool
00139   BoolVarImp::none(void) const {
00140     return status() == NONE;
00141   }
00142 
00143 
00144   /*
00145    * Support for delta information
00146    *
00147    */
00148   forceinline ModEvent
00149   BoolVarImp::modevent(const Delta&) {
00150     return ME_BOOL_VAL;
00151   }
00152   forceinline int
00153   BoolVarImp::min(const Delta& d) {
00154     return static_cast<const IntDelta&>(d).min();
00155   }
00156   forceinline int
00157   BoolVarImp::max(const Delta& d) {
00158     return static_cast<const IntDelta&>(d).min();
00159   }
00160   forceinline unsigned int
00161   BoolVarImp::width(const Delta& d) {
00162     return static_cast<const IntDelta&>(d).width();
00163   }
00164   forceinline bool
00165   BoolVarImp::any(const Delta&) {
00166     return false;
00167   }
00168   forceinline bool
00169   BoolVarImp::zero(const Delta& d) {
00170     return static_cast<const IntDelta&>(d).min() != 0;
00171   }
00172   forceinline bool
00173   BoolVarImp::one(const Delta& d) {
00174     return static_cast<const IntDelta&>(d).min() == 0;
00175   }
00176 
00177 
00178   /*
00179    * Boolean tell operations
00180    *
00181    */
00182   forceinline ModEvent
00183   BoolVarImp::zero(Space& home) {
00184     if (one())  return ME_BOOL_FAILED;
00185     if (zero()) return ME_BOOL_NONE;
00186     return zero_none(home);
00187   }
00188   forceinline ModEvent
00189   BoolVarImp::one(Space& home) {
00190     if (one())  return ME_BOOL_NONE;
00191     if (zero()) return ME_BOOL_FAILED;
00192     return one_none(home);
00193   }
00194 
00195 
00196   /*
00197    * Tell operations
00198    *
00199    */
00200   forceinline ModEvent
00201   BoolVarImp::gq(Space& home, int n) {
00202     if (n <= 0) return ME_INT_NONE;
00203     if (n > 1)  return fail(home);
00204     return one(home);
00205   }
00206   forceinline ModEvent
00207   BoolVarImp::gq(Space& home, long long int n) {
00208     if (n <= 0) return ME_INT_NONE;
00209     if (n > 1)  return fail(home);
00210     return one(home);
00211   }
00212 
00213   forceinline ModEvent
00214   BoolVarImp::lq(Space& home, int n) {
00215     if (n < 0)  return fail(home);
00216     if (n >= 1) return ME_INT_NONE;
00217     return zero(home);
00218   }
00219   forceinline ModEvent
00220   BoolVarImp::lq(Space& home, long long int n) {
00221     if (n < 0)  return fail(home);
00222     if (n >= 1) return ME_INT_NONE;
00223     return zero(home);
00224   }
00225 
00226   forceinline ModEvent
00227   BoolVarImp::eq(Space& home, int n) {
00228     if ((n < 0) || (n > 1)) return fail(home);
00229     return (n == 0) ? zero(home): one(home);
00230   }
00231   forceinline ModEvent
00232   BoolVarImp::eq(Space& home, long long int n) {
00233     if ((n < 0) || (n > 1)) return fail(home);
00234     return (n == 0) ? zero(home): one(home);
00235   }
00236 
00237   forceinline ModEvent
00238   BoolVarImp::nq(Space& home, int n) {
00239     if ((n < 0) || (n > 1)) return ME_INT_NONE;
00240     return (n == 0) ? one(home): zero(home);
00241   }
00242   forceinline ModEvent
00243   BoolVarImp::nq(Space& home, long long int n) {
00244     if ((n < 0) || (n > 1)) return ME_INT_NONE;
00245     return (n == 0) ? one(home): zero(home);
00246   }
00247 
00248 
00249   /*
00250    * Copying a variable
00251    *
00252    */
00253 
00254   forceinline
00255   BoolVarImp::BoolVarImp(Space& home, BoolVarImp& x)
00256     : BoolVarImpBase(home,x) {}
00257   forceinline BoolVarImp*
00258   BoolVarImp::copy(Space& home) {
00259     if (copied())
00260       return static_cast<BoolVarImp*>(forward());
00261     else if (zero())
00262       return &s_zero;
00263     else if (one())
00264       return &s_one;
00265     else
00266       return new (home) BoolVarImp(home,*this);
00267   }
00268 
00269 
00270   /*
00271    * Iterator-based domain operations
00272    *
00273    */
00274   template<class I>
00275   forceinline ModEvent
00276   BoolVarImp::narrow_r(Space& home, I& i, bool) {
00277     // Is new domain empty?
00278     if (!i())
00279       return fail(home);
00280     assert((i.min() == 0) || (i.min() == 1));
00281     assert((i.max() == 0) || (i.max() == 1));
00282     if (i.max() == 0) {
00283       assert(!one());
00284       // Assign domain to be zero (domain cannot be one)
00285       return zero(home);
00286     }
00287     if (i.min() == 1) {
00288       // Assign domain to be one (domain cannot be zero)
00289       assert(!zero());
00290       return one(home);
00291     }
00292     assert(none());
00293     return ME_INT_NONE;
00294   }
00295   template<class I>
00296   forceinline ModEvent
00297   BoolVarImp::inter_r(Space& home, I& i, bool) {
00298     // Skip all ranges that are too small
00299     while (i() && (i.max() < 0))
00300       ++i;
00301     // Is new domain empty?
00302     if (!i() || (i.min() > 1))
00303       return fail(home);
00304     assert(i.min() <= 1);
00305     if (i.min() == 1)
00306       return one(home);
00307     if (i.max() == 0)
00308       return zero(home);
00309     assert((i.min() <= 0) && (i.max() >= 1));
00310     return ME_INT_NONE;
00311   }
00312   template<class I>
00313   forceinline ModEvent
00314   BoolVarImp::minus_r(Space& home, I& i, bool) {
00315     // Skip all ranges that are too small
00316     while (i() && (i.max() < 0))
00317       ++i;
00318     // Is new domain empty?
00319     if (!i() || (i.min() > 1))
00320       return ME_INT_NONE;
00321     assert(i.min() <= 1);
00322     if (i.min() == 1)
00323       return zero(home);
00324     if (i.max() == 0)
00325       return one(home);
00326     assert((i.min() <= 0) && (i.max() >= 1));
00327     return fail(home);
00328   }
00329 
00330   template<class I>
00331   forceinline ModEvent
00332   BoolVarImp::narrow_v(Space& home, I& i, bool) {
00333     if (!i())
00334       return fail(home);
00335     if (!none())
00336       return ME_INT_NONE;
00337     if (i.val() == 0) {
00338       do {
00339         ++i;
00340       } while (i() && (i.val() == 0));
00341       if (!i())
00342         return zero_none(home);
00343       return ME_INT_NONE;
00344     } else {
00345       assert(i.val() == 1);
00346       return one_none(home);
00347     }
00348   }
00349   template<class I>
00350   forceinline ModEvent
00351   BoolVarImp::inter_v(Space& home, I& i, bool) {
00352     while (i() && (i.val() < 0))
00353       ++i;
00354     if (!i() || (i.val() > 1))
00355       return fail(home);
00356     if (i.val() == 0) {
00357       do {
00358         ++i;
00359       } while (i() && (i.val() == 0));
00360       if (!i() || (i.val() > 1))
00361         return zero(home);
00362       return ME_INT_NONE;
00363     } else {
00364       assert(i.val() == 1);
00365       return one(home);
00366     }
00367   }
00368   template<class I>
00369   forceinline ModEvent
00370   BoolVarImp::minus_v(Space& home, I& i, bool) {
00371     while (i() && (i.val() < 0))
00372       ++i;
00373     if (!i() || (i.val() > 1))
00374       return ME_INT_NONE;
00375     if (i.val() == 0) {
00376       do {
00377         ++i;
00378       } while (i() && (i.val() == 0));
00379       if (!i() || (i.val() > 1))
00380         return one(home);
00381       return fail(home);
00382     } else {
00383       assert(i.val() == 1);
00384       return zero(home);
00385     }
00386   }
00387 
00388 
00389 
00390   /*
00391    * Dependencies
00392    *
00393    */
00394   forceinline void
00395   BoolVarImp::cancel(Space& home, Propagator& p, PropCond) {
00396     BoolVarImpBase::cancel(home,p,PC_BOOL_VAL);
00397   }
00398 
00399   forceinline void
00400   BoolVarImp::cancel(Space& home, Advisor& a, bool fail) {
00401     BoolVarImpBase::cancel(home,a,fail);
00402   }
00403 
00404   forceinline void
00405   BoolVarImp::schedule(Space& home, Propagator& p, ModEvent) {
00406     BoolVarImpBase::schedule(home,p,ME_BOOL_VAL);
00407   }
00408 
00409   forceinline ModEventDelta
00410   BoolVarImp::med(ModEvent me) {
00411     return BoolVarImpBase::med(me);
00412   }
00413 
00414 }}
00415 
00416 // STATISTICS: int-var