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

channel-int.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *     Christian Schulte <schulte@gecode.org>
00006  *     Gabor Szokoli <szokoli@gecode.org>
00007  *
00008  *  Copyright:
00009  *     Guido Tack, 2004
00010  *     Christian Schulte, 2004
00011  *     Gabor Szokoli, 2004
00012  *
00013  *  Last modified:
00014  *     $Date: 2011-05-02 00:24:42 +0200 (Mon, 02 May 2011) $ by $Author: tack $
00015  *     $Revision: 11973 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 #include <gecode/int.hh>
00043 
00044 namespace Gecode { namespace Set { namespace Int {
00045 
00046   template<class View>
00047   forceinline
00048   ChannelInt<View>::ChannelInt(Home home,
00049                                ViewArray<Gecode::Int::CachedView<
00050                                 Gecode::Int::IntView> >& xs0,
00051                                ViewArray<CachedView<View> >& ys0)
00052     : Propagator(home), xs(xs0), ys(ys0) {
00053     for (int i=xs.size(); i--;)
00054       xs[i].initCache(home,IntSet(0,ys.size()-1));
00055     for (int i=ys.size(); i--;)
00056       ys[i].initCache(home,IntSet::empty,IntSet(0,xs.size()-1));
00057     xs.subscribe(home,*this, Gecode::Int::PC_INT_DOM);
00058     ys.subscribe(home,*this, PC_SET_ANY);
00059   }
00060 
00061   template<class View>
00062   forceinline
00063   ChannelInt<View>::ChannelInt(Space& home, bool share, ChannelInt& p)
00064     : Propagator(home,share,p) {
00065     xs.update(home,share,p.xs);
00066     ys.update(home,share,p.ys);
00067   }
00068 
00069   template<class View>
00070   forceinline ExecStatus
00071   ChannelInt<View>::post(Home home,
00072                          ViewArray<Gecode::Int::CachedView<
00073                           Gecode::Int::IntView> >& xs,
00074                          ViewArray<CachedView<View> >& ys) {
00075     // Sharing of ys is taken care of in the propagator:
00076     // The ys are propagated to be disjoint, so shared variables
00077     // result in failure.
00078     int xssize = xs.size();
00079     for (int i=ys.size(); i--;) {
00080       GECODE_ME_CHECK(ys[i].exclude(home, xssize, Limits::max));
00081       GECODE_ME_CHECK(ys[i].exclude(home, Limits::min, -1));
00082     }
00083     int yssize = ys.size();
00084     if (yssize > Gecode::Int::Limits::max)
00085       return ES_FAILED;
00086     for (int i=xs.size(); i--;) {
00087       GECODE_ME_CHECK(xs[i].gq(home, 0));
00088       GECODE_ME_CHECK(xs[i].le(home, static_cast<int>(yssize)));
00089     }
00090 
00091     (void) new (home) ChannelInt(home,xs,ys);
00092     return ES_OK;
00093   }
00094 
00095   template<class View>
00096   PropCost
00097   ChannelInt<View>::cost(const Space&, const ModEventDelta&) const {
00098     return PropCost::quadratic(PropCost::LO, xs.size()+ys.size());
00099   }
00100 
00101   template<class View>
00102   forceinline size_t
00103   ChannelInt<View>::dispose(Space& home) {
00104     xs.cancel(home,*this, Gecode::Int::PC_INT_DOM);
00105     ys.cancel(home,*this, PC_SET_ANY);
00106     (void) Propagator::dispose(home);
00107     return sizeof(*this);
00108   }
00109 
00110   template<class View>
00111   Actor*
00112   ChannelInt<View>::copy(Space& home, bool share) {
00113     return new (home) ChannelInt(home,share,*this);
00114   }
00115 
00116   template<class View>
00117   ExecStatus
00118   ChannelInt<View>::propagate(Space& home, const ModEventDelta&) {
00119     int assigned = 0;
00120     for (int v=xs.size(); v--;) {
00121       if (xs[v].assigned()) {
00122         assigned++;
00123         if (xs[v].modified())
00124           GECODE_ME_CHECK(ys[xs[v].val()].include(home,v));
00125       }
00126       if (xs[v].modified()) {
00127         Gecode::Int::ViewDiffRanges<Gecode::Int::IntView> d(xs[v]);
00128         Iter::Ranges::ToValues<Gecode::Int::ViewDiffRanges<
00129           Gecode::Int::IntView> > dv(d);
00130         for (; dv(); ++dv)
00131           GECODE_ME_CHECK(ys[dv.val()].exclude(home, v));
00132         xs[v].cache(home);
00133       }
00134     }
00135 
00136     for (int i=ys.size(); i--;) {
00137       if (ys[i].glbModified()) {
00138         GlbDiffRanges<View> yilb(ys[i]);
00139         Iter::Ranges::ToValues<GlbDiffRanges<View> > dv(yilb);
00140         for (;dv();++dv)
00141           GECODE_ME_CHECK(xs[dv.val()].eq(home,i));
00142         ys[i].cacheGlb(home);
00143       }
00144       if (ys[i].lubModified()) {
00145         LubDiffRanges<View> yiub(ys[i]);
00146         Iter::Ranges::ToValues<LubDiffRanges<View> > dv(yiub);
00147         for (;dv();++dv)
00148           GECODE_ME_CHECK(xs[dv.val()].nq(home,i));
00149         ys[i].cacheLub(home);
00150       }
00151     }
00152 
00153     return (assigned==xs.size()) ? home.ES_SUBSUMED(*this) : ES_NOFIX;
00154   }
00155 
00156 }}}
00157 
00158 // STATISTICS: set-prop