Generated on Tue May 22 09:39:55 2018 for Gecode by doxygen 1.6.3

eqv.cpp

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, 2011
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 #include <gecode/int/bool.hh>
00035 
00036 namespace Gecode { namespace Int { namespace Bool {
00037 
00038   /*
00039    * N-ary Boolean equivalence propagator
00040    *
00041    */
00042 
00043   PropCost
00044   NaryEqv::cost(const Space&, const ModEventDelta&) const {
00045     return PropCost::binary(PropCost::LO);
00046   }
00047 
00048   Actor*
00049   NaryEqv::copy(Space& home) {
00050     return new (home) NaryEqv(home,*this);
00051   }
00052 
00053   ExecStatus
00054   NaryEqv::post(Home home, ViewArray<BoolView>& x, int pm2) {
00055     int n = x.size();
00056     for (int i=n; i--; )
00057       if (x[i].assigned()) {
00058         pm2 ^= x[i].val();
00059         x[i] = x[--n];
00060       }
00061     if (n == 0)
00062       return (pm2 == 1) ? ES_OK : ES_FAILED;
00063     if (n == 1) {
00064       GECODE_ME_CHECK(x[0].eq(home,1^pm2));
00065       return ES_OK;
00066     }
00067     if (n == 2) {
00068       if (pm2 == 1) {
00069         return Bool::Eq<BoolView,BoolView>::post(home,x[0],x[1]);
00070       } else {
00071         NegBoolView nx(x[1]);
00072         return Bool::Eq<BoolView,NegBoolView>::post(home,x[0],nx);
00073       }
00074     }
00075     x.size(n);
00076     (void) new (home) NaryEqv(home,x,pm2);
00077     return ES_OK;
00078   }
00079 
00080   ExecStatus
00081   NaryEqv::propagate(Space& home, const ModEventDelta&) {
00082     resubscribe(home,x0);
00083     resubscribe(home,x1);
00084     if (x.size() == 0) {
00085       if (x0.assigned() && x1.assigned()) {
00086         return (pm2 == 1) ? home.ES_SUBSUMED(*this) : ES_FAILED;
00087       } else if (x0.assigned()) {
00088         GECODE_ME_CHECK(x1.eq(home,1^pm2));
00089         return home.ES_SUBSUMED(*this);
00090       } else if (x1.assigned()) {
00091         GECODE_ME_CHECK(x0.eq(home,1^pm2));
00092         return home.ES_SUBSUMED(*this);
00093       }
00094     }
00095     return ES_FIX;
00096   }
00097 
00098 }}}
00099 
00100 // STATISTICS: int-prop