Generated on Thu Mar 22 10:39:40 2012 for Gecode by doxygen 1.6.3

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