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

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