this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2011 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#include <gecode/int/bool.hh> 35 36namespace Gecode { namespace Int { namespace Bool { 37 38 /* 39 * N-ary Boolean equivalence propagator 40 * 41 */ 42 43 PropCost 44 NaryEqv::cost(const Space&, const ModEventDelta&) const { 45 return PropCost::binary(PropCost::LO); 46 } 47 48 Actor* 49 NaryEqv::copy(Space& home) { 50 return new (home) NaryEqv(home,*this); 51 } 52 53 ExecStatus 54 NaryEqv::post(Home home, ViewArray<BoolView>& x, int pm2) { 55 int n = x.size(); 56 for (int i=n; i--; ) 57 if (x[i].assigned()) { 58 pm2 ^= x[i].val(); 59 x[i] = x[--n]; 60 } 61 if (n == 0) 62 return (pm2 == 1) ? ES_OK : ES_FAILED; 63 if (n == 1) { 64 GECODE_ME_CHECK(x[0].eq(home,1^pm2)); 65 return ES_OK; 66 } 67 if (n == 2) { 68 if (pm2 == 1) { 69 return Bool::Eq<BoolView,BoolView>::post(home,x[0],x[1]); 70 } else { 71 NegBoolView nx(x[1]); 72 return Bool::Eq<BoolView,NegBoolView>::post(home,x[0],nx); 73 } 74 } 75 x.size(n); 76 (void) new (home) NaryEqv(home,x,pm2); 77 return ES_OK; 78 } 79 80 ExecStatus 81 NaryEqv::propagate(Space& home, const ModEventDelta&) { 82 resubscribe(home,x0); 83 resubscribe(home,x1); 84 if (x.size() == 0) { 85 if (x0.assigned() && x1.assigned()) { 86 return (pm2 == 1) ? home.ES_SUBSUMED(*this) : ES_FAILED; 87 } else if (x0.assigned()) { 88 GECODE_ME_CHECK(x1.eq(home,1^pm2)); 89 return home.ES_SUBSUMED(*this); 90 } else if (x1.assigned()) { 91 GECODE_ME_CHECK(x0.eq(home,1^pm2)); 92 return home.ES_SUBSUMED(*this); 93 } 94 } 95 return ES_FIX; 96 } 97 98}}} 99 100// STATISTICS: int-prop