Generated on Mon Aug 25 11:35:35 2008 for Gecode by doxygen 1.5.6

eq.icc

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  *  Last modified:
00010  *     $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 6017 $
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 { namespace Bool {
00039 
00040   template<class BVA, class BVB>
00041   forceinline
00042   Eq<BVA,BVB>::Eq(Space* home, BVA b0, BVB b1)
00043     : BoolBinary<BVA,BVB>(home,b0,b1) {}
00044 
00045   template<class BVA, class BVB>
00046   forceinline
00047   Eq<BVA,BVB>::Eq(Space* home, bool share, Eq<BVA,BVB>& p)
00048     : BoolBinary<BVA,BVB>(home,share,p) {}
00049 
00050   template<class BVA, class BVB>
00051   forceinline
00052   Eq<BVA,BVB>::Eq(Space* home, bool share, Propagator& p,
00053                   BVA b0, BVB b1)
00054     : BoolBinary<BVA,BVB>(home,share,p,b0,b1) {}
00055 
00056   template<class BVA, class BVB>
00057   Actor*
00058   Eq<BVA,BVB>::copy(Space* home, bool share) {
00059     return new (home) Eq<BVA,BVB>(home,share,*this);
00060   }
00061 
00062   template<class BVA, class BVB>
00063   inline ExecStatus
00064   Eq<BVA,BVB>::post(Space* home, BVA b0, BVB b1) {
00065     switch (bool_test(b0,b1)) {
00066     case BT_SAME: return ES_OK;
00067     case BT_COMP: return ES_FAILED;
00068     case BT_NONE:
00069       if (b0.zero()) {
00070         GECODE_ME_CHECK(b1.zero(home));
00071       } else if (b0.one()) {
00072         GECODE_ME_CHECK(b1.one(home));
00073       } else if (b1.zero()) {
00074         GECODE_ME_CHECK(b0.zero(home));
00075       } else if (b1.one()) {
00076         GECODE_ME_CHECK(b0.one(home));
00077       } else {
00078         (void) new (home) Eq<BVA,BVB>(home,b0,b1);
00079       }
00080       break;
00081     default: GECODE_NEVER;
00082     }
00083     return ES_OK;
00084   }
00085 
00086   template<class BVA, class BVB>
00087   ExecStatus
00088   Eq<BVA,BVB>::propagate(Space* home, ModEventDelta) {
00089 #define GECODE_INT_STATUS(S0,S1) \
00090   ((BVA::S0<<(1*BVA::BITS))|(BVB::S1<<(0*BVB::BITS)))
00091     switch ((x0.status() << (1*BVA::BITS)) | (x1.status() << (0*BVB::BITS))) {
00092     case GECODE_INT_STATUS(NONE,NONE):
00093       GECODE_NEVER;
00094     case GECODE_INT_STATUS(NONE,ZERO):
00095       GECODE_ME_CHECK(x0.zero_none(home)); break;
00096     case GECODE_INT_STATUS(NONE,ONE):
00097       GECODE_ME_CHECK(x0.one_none(home)); break;
00098     case GECODE_INT_STATUS(ZERO,NONE):
00099       GECODE_ME_CHECK(x1.zero_none(home)); break;
00100     case GECODE_INT_STATUS(ZERO,ZERO):
00101       break;
00102     case GECODE_INT_STATUS(ZERO,ONE):
00103       return ES_FAILED;
00104     case GECODE_INT_STATUS(ONE,NONE):
00105       GECODE_ME_CHECK(x1.one_none(home)); break;
00106     case GECODE_INT_STATUS(ONE,ZERO):
00107       return ES_FAILED;
00108     case GECODE_INT_STATUS(ONE,ONE):
00109       break;
00110     default:
00111       GECODE_NEVER;
00112     }
00113     return ES_SUBSUMED(this,sizeof(*this));
00114 #undef GECODE_INT_STATUS
00115   }
00116 
00117   template<class BVA, class BVB>
00118   inline Support::Symbol
00119   Eq<BVA,BVB>::ati(void) {
00120     return Reflection::mangle<BVA,BVB>("Gecode::Int::Bool::Eq");
00121   }
00122 
00123   template<class BVA, class BVB>
00124   Reflection::ActorSpec
00125   Eq<BVA,BVB>::spec(const Space* home, Reflection::VarMap& m) const {
00126     return BoolBinary<BVA,BVB>::spec(home, m, ati());
00127   }
00128 
00129   template<class BVA, class BVB>
00130   void
00131   Eq<BVA,BVB>::post(Space* home, Reflection::VarMap& vars,
00132                     const Reflection::ActorSpec& spec) {
00133     spec.checkArity(2);
00134     BVA b0(home, vars, spec[0]);
00135     BVB b1(home, vars, spec[1]);
00136     (void) new (home) Eq<BVA,BVB>(home,b0,b1);
00137   }
00138   
00139   template<class BV>
00140   forceinline
00141   NaryEq<BV>::NaryEq(Space* home, ViewArray<BV>& x)
00142     : NaryPropagator<BV,PC_BOOL_VAL>(home,x) {}
00143 
00144   template<class BV>
00145   forceinline
00146   NaryEq<BV>::NaryEq(Space* home, bool share, NaryEq<BV>& p)
00147     : NaryPropagator<BV,PC_BOOL_VAL>(home,share,p) {}
00148 
00149   template<class BV>
00150   Actor*
00151   NaryEq<BV>::copy(Space* home, bool share) {
00152     return new (home) NaryEq<BV>(home,share,*this);
00153   }
00154 
00155   template<class BV>
00156   inline ExecStatus
00157   NaryEq<BV>::post(Space* home, ViewArray<BV>& x) {
00158     x.unique();
00159     int n = x.size();
00160     if (n < 2)
00161       return ES_OK;
00162     if (n == 2)
00163       return Eq<BV,BV>::post(home,x[0],x[1]);
00164     for (int i=n; i--; )
00165       if (x[i].assigned()) {
00166         if (x[i].one()) {
00167           for (int j=i; j--; )
00168             GECODE_ME_CHECK(x[j].one(home));
00169           for (int j=i+1; j<n; j++)
00170             GECODE_ME_CHECK(x[j].one_none(home));
00171         } else {
00172           for (int j=i; j--; )
00173             GECODE_ME_CHECK(x[j].zero(home));
00174           for (int j=i+1; j<n; j++)
00175             GECODE_ME_CHECK(x[j].zero_none(home));
00176         }
00177         return ES_OK;
00178       }
00179     (void) new (home) NaryEq<BV>(home,x);
00180     return ES_OK;
00181   }
00182 
00183   template<class BV>
00184   void
00185   NaryEq<BV>::post(Space* home, Reflection::VarMap& vars,
00186                    const Reflection::ActorSpec& spec) {
00187     spec.checkArity(1);
00188     ViewArray<BV> x(home, vars, spec[0]);
00189     (void) new (home) NaryEq<BV>(home, x);
00190   }
00191 
00192   template<class BV>
00193   PropCost
00194   NaryEq<BV>::cost(ModEventDelta) const {
00195     return PC_UNARY_LO;
00196   }
00197 
00198   template<class BV>
00199   ExecStatus
00200   NaryEq<BV>::propagate(Space* home, ModEventDelta) {
00201     int n=x.size();
00202     int i=0;
00203     while (true) {
00204       if (x[i].assigned()) {
00205         if (x[i].one()) {
00206           for (int j=0; j<i; j++)
00207             GECODE_ME_CHECK(x[j].one_none(home));
00208           for (int j=i+1; j<n; j++)
00209             GECODE_ME_CHECK(x[j].one(home));
00210         } else {
00211           for (int j=0; j<i; j++)
00212             GECODE_ME_CHECK(x[j].zero_none(home));
00213           for (int j=i+1; j<n; j++)
00214             GECODE_ME_CHECK(x[j].zero(home));
00215         }
00216         return ES_SUBSUMED(this,sizeof(*this));
00217       }
00218       i++;
00219     }
00220     GECODE_NEVER;
00221     return ES_FIX;
00222   }
00223 
00224   template<class BV>
00225   Support::Symbol
00226   NaryEq<BV>::ati(void) {
00227     return Reflection::mangle<BV>("Gecode::Int::Bool::NaryEq");
00228   }
00229   
00230   template<class BV>
00231   Reflection::ActorSpec
00232   NaryEq<BV>::spec(const Space* home, Reflection::VarMap& m) const {
00233     return NaryPropagator<BV,PC_BOOL_VAL>::spec(home, m, ati());
00234   }
00235 
00236 }}}
00237 
00238 // STATISTICS: int-prop
00239