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

re-prop.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-22 21:43:31 +0200 (Mon, 22 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12335 $
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 Member {
00041 
00042   template<class View>
00043   forceinline
00044   ReProp<View>::ReProp(Home home, ValSet& vs, ViewArray<View>& x, View y, 
00045                        BoolView b0)
00046     : Prop<View>(home,vs,x,y), b(b0) {
00047     b.subscribe(home,*this,PC_BOOL_VAL);
00048   }
00049 
00050   template<class View>
00051   inline ExecStatus
00052   ReProp<View>::post(Home home, ViewArray<View>& x, View y, BoolView b) {
00053     if (x.size() == 0) {
00054       GECODE_ME_CHECK(b.zero(home));
00055       return ES_OK;
00056     }
00057 
00058     x.unique(home);
00059 
00060     if (x.size() == 1)
00061       return Rel::ReEqDom<View,BoolView>::post(home,x[0],y,b);
00062 
00063     if (x.same(home,y)) {
00064       GECODE_ME_CHECK(b.one(home));
00065       return ES_OK;
00066     }
00067     
00068     // Eliminate assigned views and store them into the value set
00069     ValSet vs;
00070     add(home, vs, x);
00071 
00072     switch (vs.compare(y)) {
00073     case Iter::Ranges::CS_SUBSET:
00074       GECODE_ME_CHECK(b.one(home));
00075       return ES_OK;
00076     case Iter::Ranges::CS_DISJOINT:
00077       if (x.size() == 0) {
00078         GECODE_ME_CHECK(b.zero(home));
00079         return ES_OK;
00080       }
00081       break;
00082     case Iter::Ranges::CS_NONE:
00083       break;
00084     default:
00085       GECODE_NEVER;
00086     }
00087 
00088     (void) new (home) ReProp<View>(home, vs, x, y, b);
00089     return ES_OK;
00090   }
00091     
00092   template<class View>
00093   forceinline
00094   ReProp<View>::ReProp(Space& home, bool share, ReProp<View>& p)
00095     : Prop<View>(home, share, p) {
00096     b.update(home, share, p.b);
00097   }
00098 
00099   template<class View>
00100   Propagator*
00101   ReProp<View>::copy(Space& home, bool share) {
00102     return new (home) ReProp<View>(home, share, *this);
00103   }
00104 
00105   template<class View>
00106   forceinline size_t
00107   ReProp<View>::dispose(Space& home) {
00108     b.cancel(home, *this, PC_BOOL_VAL);
00109     (void) Prop<View>::dispose(home);
00110     return sizeof(*this);
00111   }
00112 
00113   template<class View>
00114   ExecStatus
00115   ReProp<View>::propagate(Space& home, const ModEventDelta& med) {
00116     // Add assigned views to value set
00117     if (View::me(med) == ME_INT_VAL)
00118       add(home,vs,x);
00119 
00120     if (b.one()) {
00121       ValSet vsc(vs);
00122       vs.flush();
00123       GECODE_REWRITE(*this,Prop<View>::post(home,vsc,x,y));
00124     }
00125 
00126     if (b.zero()) {
00127       ValSet::Ranges vsr(vs);
00128       GECODE_ME_CHECK(y.minus_r(home,vsr,false));
00129       for (int i=x.size(); i--; )
00130         GECODE_ES_CHECK(Rel::Nq<View>::post(home,x[i],y));
00131       return home.ES_SUBSUMED(*this);
00132     }
00133 
00134     // Eliminate views from x
00135     eliminate(home);
00136     
00137     switch (vs.compare(y)) {
00138     case Iter::Ranges::CS_SUBSET:
00139       GECODE_ME_CHECK(b.one(home));
00140       return home.ES_SUBSUMED(*this);
00141     case Iter::Ranges::CS_DISJOINT:
00142       if (x.size() == 0) {
00143         GECODE_ME_CHECK(b.zero(home));
00144         return home.ES_SUBSUMED(*this);
00145       }
00146       break;
00147     case Iter::Ranges::CS_NONE:
00148       break;
00149     default:
00150       GECODE_NEVER;
00151     }
00152 
00153     // Check whether y is in union of x and value set
00154     if (x.size() > 0) {
00155       Region r(home);
00156 
00157       ValSet::Ranges vsr(vs);
00158       ViewRanges<View> xsr(x[x.size()-1]);
00159       Iter::Ranges::NaryUnion  u(r,vsr,xsr);
00160       for (int i=x.size()-1; i--; ) {
00161         ViewRanges<View> xir(x[i]);
00162         u |= xir;
00163       }
00164 
00165       ViewRanges<View> yr(y);
00166       
00167       if (Iter::Ranges::disjoint(u,yr)) {
00168         GECODE_ME_CHECK(b.zero(home));
00169         return home.ES_SUBSUMED(*this);
00170       }
00171     }
00172 
00173     return ES_FIX;
00174   }
00175 
00176 }}}
00177 
00178 // STATISTICS: int-prop