Generated on Thu Apr 11 13:59:00 2019 for Gecode by doxygen 1.6.3

eq.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, 2004
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 { namespace Bool {
00035 
00036   template<class BVA, class BVB>
00037   forceinline
00038   Eq<BVA,BVB>::Eq(Home home, BVA b0, BVB b1)
00039     : BoolBinary<BVA,BVB>(home,b0,b1) {}
00040 
00041   template<class BVA, class BVB>
00042   forceinline
00043   Eq<BVA,BVB>::Eq(Space& home, Eq<BVA,BVB>& p)
00044     : BoolBinary<BVA,BVB>(home,p) {}
00045 
00046   template<class BVA, class BVB>
00047   forceinline
00048   Eq<BVA,BVB>::Eq(Space& home, Propagator& p,
00049                   BVA b0, BVB b1)
00050     : BoolBinary<BVA,BVB>(home,p,b0,b1) {}
00051 
00052   template<class BVA, class BVB>
00053   Actor*
00054   Eq<BVA,BVB>::copy(Space& home) {
00055     return new (home) Eq<BVA,BVB>(home,*this);
00056   }
00057 
00058   template<class BVA, class BVB>
00059   inline ExecStatus
00060   Eq<BVA,BVB>::post(Home home, BVA b0, BVB b1) {
00061     switch (bool_test(b0,b1)) {
00062     case BT_SAME: return ES_OK;
00063     case BT_COMP: return ES_FAILED;
00064     case BT_NONE:
00065       if (b0.zero()) {
00066         GECODE_ME_CHECK(b1.zero(home));
00067       } else if (b0.one()) {
00068         GECODE_ME_CHECK(b1.one(home));
00069       } else if (b1.zero()) {
00070         GECODE_ME_CHECK(b0.zero(home));
00071       } else if (b1.one()) {
00072         GECODE_ME_CHECK(b0.one(home));
00073       } else {
00074         (void) new (home) Eq<BVA,BVB>(home,b0,b1);
00075       }
00076       break;
00077     default: GECODE_NEVER;
00078     }
00079     return ES_OK;
00080   }
00081 
00082   template<class BVA, class BVB>
00083   ExecStatus
00084   Eq<BVA,BVB>::propagate(Space& home, const ModEventDelta&) {
00085 #define GECODE_INT_STATUS(S0,S1) \
00086   ((BVA::S0<<(1*BVA::BITS))|(BVB::S1<<(0*BVB::BITS)))
00087     switch ((x0.status() << (1*BVA::BITS)) | (x1.status() << (0*BVB::BITS))) {
00088     case GECODE_INT_STATUS(NONE,NONE):
00089       GECODE_NEVER;
00090     case GECODE_INT_STATUS(NONE,ZERO):
00091       GECODE_ME_CHECK(x0.zero_none(home)); break;
00092     case GECODE_INT_STATUS(NONE,ONE):
00093       GECODE_ME_CHECK(x0.one_none(home)); break;
00094     case GECODE_INT_STATUS(ZERO,NONE):
00095       GECODE_ME_CHECK(x1.zero_none(home)); break;
00096     case GECODE_INT_STATUS(ZERO,ZERO):
00097       break;
00098     case GECODE_INT_STATUS(ZERO,ONE):
00099       return ES_FAILED;
00100     case GECODE_INT_STATUS(ONE,NONE):
00101       GECODE_ME_CHECK(x1.one_none(home)); break;
00102     case GECODE_INT_STATUS(ONE,ZERO):
00103       return ES_FAILED;
00104     case GECODE_INT_STATUS(ONE,ONE):
00105       break;
00106     default:
00107       GECODE_NEVER;
00108     }
00109     return home.ES_SUBSUMED(*this);
00110 #undef GECODE_INT_STATUS
00111   }
00112 
00113   template<class BV>
00114   forceinline
00115   NaryEq<BV>::NaryEq(Home home, ViewArray<BV>& x)
00116     : NaryPropagator<BV,PC_BOOL_VAL>(home,x) {}
00117 
00118   template<class BV>
00119   forceinline
00120   NaryEq<BV>::NaryEq(Space& home, NaryEq<BV>& p)
00121     : NaryPropagator<BV,PC_BOOL_VAL>(home,p) {}
00122 
00123   template<class BV>
00124   Actor*
00125   NaryEq<BV>::copy(Space& home) {
00126     return new (home) NaryEq<BV>(home,*this);
00127   }
00128 
00129   template<class BV>
00130   inline ExecStatus
00131   NaryEq<BV>::post(Home home, ViewArray<BV>& x) {
00132     x.unique();
00133     int n = x.size();
00134     if (n < 2)
00135       return ES_OK;
00136     if (n == 2)
00137       return Eq<BV,BV>::post(home,x[0],x[1]);
00138     for (int i=n; i--; )
00139       if (x[i].assigned()) {
00140         if (x[i].one()) {
00141           for (int j=0; j<i; j++)
00142             GECODE_ME_CHECK(x[j].one(home));
00143           for (int j=i+1; j<n; j++)
00144             GECODE_ME_CHECK(x[j].one_none(home));
00145         } else {
00146           for (int j=0; j<i; j++)
00147             GECODE_ME_CHECK(x[j].zero(home));
00148           for (int j=i+1; j<n; j++)
00149             GECODE_ME_CHECK(x[j].zero_none(home));
00150         }
00151         return ES_OK;
00152       }
00153     (void) new (home) NaryEq<BV>(home,x);
00154     return ES_OK;
00155   }
00156 
00157   template<class BV>
00158   PropCost
00159   NaryEq<BV>::cost(const Space&, const ModEventDelta&) const {
00160     return PropCost::unary(PropCost::LO);
00161   }
00162 
00163   template<class BV>
00164   ExecStatus
00165   NaryEq<BV>::propagate(Space& home, const ModEventDelta&) {
00166     int n=x.size();
00167     int i=0;
00168     while (true) {
00169       if (x[i].assigned()) {
00170         if (x[i].one()) {
00171           for (int j=0; j<i; j++)
00172             GECODE_ME_CHECK(x[j].one_none(home));
00173           for (int j=i+1; j<n; j++)
00174             GECODE_ME_CHECK(x[j].one(home));
00175         } else {
00176           for (int j=0; j<i; j++)
00177             GECODE_ME_CHECK(x[j].zero_none(home));
00178           for (int j=i+1; j<n; j++)
00179             GECODE_ME_CHECK(x[j].zero(home));
00180         }
00181         return home.ES_SUBSUMED(*this);
00182       }
00183       i++;
00184     }
00185     GECODE_NEVER;
00186     return ES_FIX;
00187   }
00188 
00189 }}}
00190 
00191 // STATISTICS: int-prop
00192