Generated on Sun Feb 17 15:24:21 2019 for Gecode by doxygen 1.6.3

bool-lq.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, 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 NValues {
00037 
00038   template<class VY>
00039   forceinline
00040   LqBool<VY>::LqBool(Home home, int status, ViewArray<BoolView>& x, VY y)
00041     : BoolBase<VY>(home,status,x,y) {}
00042 
00043   template<class VY>
00044   forceinline
00045   LqBool<VY>::LqBool(Space& home, LqBool<VY>& p)
00046     : BoolBase<VY>(home,p) {}
00047 
00048   template<class VY>
00049   Actor*
00050   LqBool<VY>::copy(Space& home) {
00051     return new (home) LqBool<VY>(home,*this);
00052   }
00053 
00054   template<class VY>
00055   inline ExecStatus
00056   LqBool<VY>::post(Home home, ViewArray<BoolView>& x, VY y) {
00057     if (x.size() == 0) {
00058       GECODE_ME_CHECK(y.gq(home,0));
00059       return ES_OK;
00060     }
00061 
00062     x.unique();
00063 
00064     GECODE_ME_CHECK(y.gq(home,1));
00065 
00066     if (x.size() == 1)
00067       return ES_OK;
00068 
00069     if (y.max() == 1) {
00070       assert(y.assigned());
00071       ViewArray<BoolView> xc(home,x);
00072       return Bool::NaryEq<BoolView>::post(home,xc);
00073     }
00074 
00075     if (y.min() >= 2)
00076       return ES_OK;
00077 
00078     int n = x.size();
00079     int status = 0;
00080     for (int i=n; i--; )
00081       if (x[i].zero()) {
00082         if (status & VS_ONE) {
00083           GECODE_ME_CHECK(y.gq(home,2));
00084           return ES_OK;
00085         }
00086         x[i] = x[--n];
00087         status |= VS_ZERO;
00088       } else if (x[i].one()) {
00089         if (status & VS_ZERO) {
00090           GECODE_ME_CHECK(y.gq(home,2));
00091           return ES_OK;
00092         }
00093         x[i] = x[--n];
00094         status |= VS_ONE;
00095       }
00096 
00097     assert(status != (VS_ZERO | VS_ONE));
00098     if (n == 0) {
00099       assert((status != 0) && (y.min() >= 1));
00100       return ES_OK;
00101     }
00102 
00103     x.size(n);
00104 
00105     (void) new (home) LqBool<VY>(home,status,x,y);
00106     return ES_OK;
00107   }
00108 
00109   template<class VY>
00110   ExecStatus
00111   LqBool<VY>::propagate(Space& home, const ModEventDelta&) {
00112     if (status == (VS_ZERO | VS_ONE)) {
00113       GECODE_ME_CHECK(y.gq(home,2));
00114       return home.ES_SUBSUMED(*this);
00115     }
00116 
00117     if (c.empty()) {
00118       assert((status != 0) && (y.min() >= 1));
00119       return home.ES_SUBSUMED(*this);
00120     }
00121 
00122     if (y.max() == 1) {
00123       if (status == VS_ZERO) {
00124         // Mark that everything is done
00125         status = VS_ZERO | VS_ONE;
00126         for (Advisors<ViewAdvisor<BoolView> > as(c); as(); ++as)
00127           GECODE_ME_CHECK(as.advisor().view().zero(home));
00128         return home.ES_SUBSUMED(*this);
00129       }
00130       if (status == VS_ONE) {
00131         // Mark that everything is done
00132         status = VS_ZERO | VS_ONE;
00133         for (Advisors<ViewAdvisor<BoolView> > as(c); as(); ++as)
00134           GECODE_ME_CHECK(as.advisor().view().one(home));
00135         return home.ES_SUBSUMED(*this);
00136       }
00137     }
00138 
00139     if (y.min() == 2)
00140       return home.ES_SUBSUMED(*this);
00141 
00142     return ES_FIX;
00143   }
00144 
00145 }}}
00146 
00147 // STATISTICS: int-prop