Generated on Tue Apr 18 10:21:41 2017 for Gecode by doxygen 1.6.3

nq.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  *  Last modified:
00010  *     $Date: 2016-11-08 17:23:24 +0100 (Tue, 08 Nov 2016) $ by $Author: schulte $
00011  *     $Revision: 15253 $
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 #include <algorithm>
00039 
00040 namespace Gecode { namespace Int { namespace Rel {
00041 
00042   /*
00043    * Disequality
00044    *
00045    */
00046   template<class V0, class V1>
00047   forceinline
00048   Nq<V0,V1>::Nq(Home home, V0 x0, V1 x1)
00049     : MixBinaryPropagator<V0,PC_INT_VAL,V1,PC_INT_VAL>(home,x0,x1) {}
00050 
00051   template<class V0, class V1>
00052   ExecStatus
00053   Nq<V0,V1>::post(Home home, V0 x0, V1 x1){
00054     if (x0.assigned()) {
00055       GECODE_ME_CHECK(x1.nq(home,x0.val()));
00056     } else if (x1.assigned()) {
00057       GECODE_ME_CHECK(x0.nq(home,x1.val()));
00058     } else if (same(x0,x1)) {
00059       return ES_FAILED;
00060     } else {
00061       (void) new (home) Nq<V0,V1>(home,x0,x1);
00062     }
00063     return ES_OK;
00064   }
00065 
00066   template<class V0, class V1>
00067   forceinline
00068   Nq<V0,V1>::Nq(Space& home, bool share, Nq<V0,V1>& p)
00069     : MixBinaryPropagator<V0,PC_INT_VAL,V1,PC_INT_VAL>(home,share,p) {}
00070 
00071   template<class V0, class V1>
00072   Actor*
00073   Nq<V0,V1>::copy(Space& home, bool share) {
00074     return new (home) Nq<V0,V1>(home,share,*this);
00075   }
00076 
00077   template<class V0, class V1>
00078   PropCost
00079   Nq<V0,V1>::cost(const Space&, const ModEventDelta&) const {
00080     return PropCost::unary(PropCost::LO);
00081   }
00082 
00083   template<class V0, class V1>
00084   ExecStatus
00085   Nq<V0,V1>::propagate(Space& home, const ModEventDelta&) {
00086     if (x0.assigned()) {
00087       GECODE_ME_CHECK(x1.nq(home,x0.val()));
00088     } else {
00089       GECODE_ME_CHECK(x0.nq(home,x1.val()));
00090     }
00091     return home.ES_SUBSUMED(*this);
00092   }
00093 
00094 
00095   /*
00096    * Nary disequality propagator
00097    */
00098   template<class View>
00099   forceinline
00100   NaryNq<View>::NaryNq(Home home, ViewArray<View>& x)
00101     : NaryPropagator<View,PC_INT_VAL>(home,x) {}
00102 
00103   template<class View>
00104   PropCost
00105   NaryNq<View>::cost(const Space&, const ModEventDelta&) const {
00106     return PropCost::linear(PropCost::LO,x.size());
00107   }
00108 
00109   template<class View>
00110   forceinline
00111   NaryNq<View>::NaryNq(Space& home, bool share, NaryNq<View>& p)
00112     : NaryPropagator<View,PC_INT_VAL>(home,share,p) {}
00113 
00114   template<class View>
00115   Actor*
00116   NaryNq<View>::copy(Space& home, bool share) {
00117     return new (home) NaryNq<View>(home,share,*this);
00118   }
00119 
00120   template<class View>
00121   inline ExecStatus
00122   NaryNq<View>::post(Home home, ViewArray<View>& x) {
00123     x.unique(home);
00124     // Try to find an assigned view
00125     int n = x.size();
00126     if (n <= 1)
00127       return ES_FAILED;
00128     for (int i=n; i--; )
00129       if (x[i].assigned()) {
00130         std::swap(x[0],x[i]);
00131         break;
00132       }
00133     if (x[0].assigned()) {
00134       int v = x[0].val();
00135       // Eliminate all equal views and possibly find disequal view
00136       for (int i=n-1; i>0; i--)
00137         if (!x[i].in(v)) {
00138           return ES_OK;
00139         } else if (x[i].assigned()) {
00140           assert(x[i].val() == v);
00141           x[i]=x[--n];
00142         }
00143       x.size(n);
00144     }
00145     if (n == 1)
00146       return ES_FAILED;
00147     if (n == 2)
00148       return Nq<View,View>::post(home,x[0],x[1]);
00149     (void) new (home) NaryNq(home,x);
00150     return ES_OK;
00151   }
00152 
00153   template<class View>
00154   forceinline size_t
00155   NaryNq<View>::dispose(Space& home) {
00156     (void) NaryPropagator<View,PC_INT_VAL>::dispose(home);
00157     return sizeof(*this);
00158   }
00159 
00160   template<class View>
00161   ExecStatus
00162   NaryNq<View>::propagate(Space& home, const ModEventDelta&) {
00163     // Make sure that x[0] is assigned
00164     if (!x[0].assigned()) {
00165       // Note that there is at least one assigned view
00166       for (int i=1; true; i++)
00167         if (x[i].assigned()) {
00168           std::swap(x[0],x[i]);
00169           break;
00170         }
00171     }
00172     int v = x[0].val();
00173     int n = x.size();
00174     for (int i=n-1; i>0; i--)
00175       if (!x[i].in(v)) {
00176         x.size(n);
00177         return home.ES_SUBSUMED(*this);
00178       } else if (x[i].assigned()) {
00179         assert(x[i].val() == v);
00180         x[i] = x[--n];
00181       }
00182     x.size(n);
00183     if (n == 1)
00184       return ES_FAILED;
00185     if (n == 2) {
00186       GECODE_ME_CHECK(x[1].nq(home,v));
00187       return home.ES_SUBSUMED(*this);
00188     }
00189     return ES_FIX;
00190   }
00191 
00192 
00193 
00194 }}}
00195 
00196 // STATISTICS: int-prop