Generated on Tue Apr 18 10:22:01 2017 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  *  Last modified:
00010  *     $Date: 2017-03-09 09:51:58 +0100 (Thu, 09 Mar 2017) $ by $Author: schulte $
00011  *     $Revision: 15565 $
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 namespace Gecode { namespace Int {
00039 
00040   /*
00041    * Creation of new variable implementations
00042    *
00043    */
00044   forceinline
00045   BoolVarImp::BoolVarImp(int n) {
00046     assert(bits() == 0);
00047     bits() |= (n << 1) | n;
00048   }
00049   forceinline
00050   BoolVarImp::BoolVarImp(Space& home, int min, int max)
00051     : BoolVarImpBase(home) {
00052     assert(bits() == 0);
00053     bits() |= (max << 1) | min;
00054   }
00055 
00056 
00057   /*
00058    * Operations on Boolean variable implementations
00059    *
00060    */
00061   forceinline BoolStatus
00062   BoolVarImp::status(void) const {
00063     return bits() & 3;
00064   }
00065   forceinline int
00066   BoolVarImp::min(void) const {
00067     return static_cast<int>(bits() & 1);
00068   }
00069   forceinline int
00070   BoolVarImp::max(void) const {
00071     return static_cast<int>((bits() & 2) >> 1);
00072   }
00073   forceinline int
00074   BoolVarImp::med(void) const {
00075     return min();
00076   }
00077 
00078   forceinline int
00079   BoolVarImp::val(void) const {
00080     assert(status() != NONE);
00081     return min();
00082   }
00083 
00084   forceinline bool
00085   BoolVarImp::range(void) const {
00086     return true;
00087   }
00088   forceinline bool
00089   BoolVarImp::assigned(void) const {
00090     return status() != NONE;
00091   }
00092 
00093 
00094   forceinline unsigned int
00095   BoolVarImp::width(void) const {
00096     return assigned() ? 1U : 2U;
00097   }
00098 
00099   forceinline unsigned int
00100   BoolVarImp::size(void) const {
00101     return assigned() ? 1U : 2U;
00102   }
00103 
00104   forceinline unsigned int
00105   BoolVarImp::regret_min(void) const {
00106     return assigned() ? 1U : 0U;
00107   }
00108   forceinline unsigned int
00109   BoolVarImp::regret_max(void) const {
00110     return assigned() ? 1U : 0U;
00111   }
00112 
00113 
00114 
00115   /*
00116    * Tests
00117    *
00118    */
00119 
00120   forceinline bool
00121   BoolVarImp::in(int n) const {
00122     return (n >= min()) && (n <= max());
00123   }
00124   forceinline bool
00125   BoolVarImp::in(long long int n) const {
00126     return (n >= min()) && (n <= max());
00127   }
00128 
00129 
00130   /*
00131    * Boolean domain tests
00132    *
00133    */
00134   forceinline bool
00135   BoolVarImp::zero(void) const {
00136     return status() < NONE;
00137   }
00138   forceinline bool
00139   BoolVarImp::one(void) const {
00140     return status() > NONE;
00141   }
00142   forceinline bool
00143   BoolVarImp::none(void) const {
00144     return status() == NONE;
00145   }
00146 
00147 
00148   /*
00149    * Support for delta information
00150    *
00151    */
00152   forceinline ModEvent
00153   BoolVarImp::modevent(const Delta&) {
00154     return ME_BOOL_VAL;
00155   }
00156   forceinline int
00157   BoolVarImp::min(const Delta& d) {
00158     return static_cast<const IntDelta&>(d).min();
00159   }
00160   forceinline int
00161   BoolVarImp::max(const Delta& d) {
00162     return static_cast<const IntDelta&>(d).min();
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, bool share, BoolVarImp& x)
00256     : BoolVarImpBase(home,share,x) {}
00257   forceinline BoolVarImp*
00258   BoolVarImp::copy(Space& home, bool share) {
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,share,*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 me) {
00406     if (me == ME_GEN_ASSIGNED)
00407       BoolVarImpBase::schedule(home,p,me);
00408   }
00409 
00410   forceinline ModEventDelta
00411   BoolVarImp::med(ModEvent me) {
00412     return BoolVarImpBase::med(me);
00413   }
00414 
00415 }}
00416 
00417 // STATISTICS: int-var