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