Generated on Tue Jul 27 2010 21:59:19 for Gecode by doxygen 1.7.1

singleton.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  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *
00009  *  Copyright:
00010  *     Guido Tack, 2004
00011  *     Christian Schulte, 2004
00012  *
00013  *  Last modified:
00014  *     $Date: 2010-06-29 10:39:13 +0200 (Tue, 29 Jun 2010) $ by $Author: schulte $
00015  *     $Revision: 11118 $
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 namespace Gecode { namespace Set {
00043 
00044   forceinline
00045   SingletonView::SingletonView(void) {}
00046 
00047   forceinline
00048   SingletonView::SingletonView(Gecode::Int::IntView& y)
00049     : DerivedView<Gecode::Int::IntView>(y) {}
00050 
00051   forceinline PropCond
00052   SingletonView::pc_settoint(PropCond pc) {
00053     switch(pc) {
00054     case PC_SET_VAL:
00055     case PC_SET_CGLB:
00056     case PC_SET_CARD:
00057       return Gecode::Int::PC_INT_VAL;
00058     default:
00059       return Gecode::Int::PC_INT_DOM;
00060     }
00061   }
00062 
00063   forceinline ModEvent
00064   SingletonView::me_inttoset(ModEvent me) {
00065     switch(me) {
00066     case Gecode::Int::ME_INT_FAILED:
00067       return ME_SET_FAILED;
00068     case Gecode::Int::ME_INT_NONE:
00069       return ME_SET_NONE;
00070     case Gecode::Int::ME_INT_VAL:
00071       return ME_SET_VAL;
00072     case Gecode::Int::ME_INT_DOM:
00073       return ME_SET_LUB;
00074     default:
00075       return ME_SET_LUB;
00076     }
00077   }
00078 
00079   forceinline ModEvent
00080   SingletonView::me_settoint(ModEvent me) {
00081     switch(me) {
00082     case ME_SET_FAILED:
00083       return Gecode::Int::ME_INT_FAILED;
00084     case ME_SET_NONE:
00085       return Gecode::Int::ME_INT_NONE;
00086     case ME_SET_VAL:
00087       return Gecode::Int::ME_INT_VAL;
00088     default:
00089       return Gecode::Int::ME_INT_DOM;
00090     }
00091   }
00092 
00093   forceinline unsigned int
00094   SingletonView::glbSize(void) const { 
00095     return x.assigned() ? 1U : 0U; 
00096   }
00097 
00098   forceinline unsigned int
00099   SingletonView::lubSize(void) const { return x.size(); }
00100 
00101   forceinline unsigned int
00102   SingletonView::unknownSize(void) const {
00103     return lubSize() - glbSize();
00104   }
00105 
00106   forceinline bool
00107   SingletonView::contains(int n) const { return x.assigned() ?
00108       (x.val()==n) : false; }
00109 
00110   forceinline bool
00111   SingletonView::notContains(int n) const { return !x.in(n); }
00112 
00113   forceinline unsigned int
00114   SingletonView::cardMin() const { return 1; }
00115 
00116   forceinline unsigned int
00117   SingletonView::cardMax() const { return 1; }
00118 
00119   forceinline int
00120   SingletonView::lubMin() const { return x.min(); }
00121 
00122   forceinline int
00123   SingletonView::lubMax() const { return x.max(); }
00124 
00125   forceinline int
00126   SingletonView::glbMin() const { return x.assigned() ?
00127       x.val() : BndSet::MIN_OF_EMPTY; }
00128 
00129   forceinline int
00130   SingletonView::glbMax() const { return x.assigned() ?
00131       x.val() : BndSet::MAX_OF_EMPTY; }
00132 
00133   forceinline ModEvent
00134   SingletonView::cardMin(Space&, unsigned int c) {
00135     return c<=1 ? ME_SET_NONE : ME_SET_FAILED;
00136   }
00137 
00138   forceinline ModEvent
00139   SingletonView::cardMax(Space&, unsigned int c) {
00140     return c<1 ? ME_SET_FAILED : ME_SET_NONE;
00141   }
00142 
00143   forceinline ModEvent
00144   SingletonView::include(Space& home,int c) {
00145     return me_inttoset(x.eq(home,c));
00146   }
00147 
00148   forceinline ModEvent
00149   SingletonView::intersect(Space& home,int c) {
00150     return me_inttoset(x.eq(home,c));
00151   }
00152 
00153   forceinline ModEvent
00154   SingletonView::intersect(Space& home,int i, int j) {
00155     ModEvent me1 = me_inttoset(x.gq(home,i));
00156     ModEvent me2 = me_inttoset(x.lq(home,j));
00157     if (me_failed(me1) || me_failed(me2))
00158       return ME_SET_FAILED;
00159     switch (me1) {
00160     case ME_SET_NONE:
00161     case ME_SET_LUB:
00162       return me2;
00163     case ME_SET_VAL:
00164       return ME_SET_VAL;
00165     default:
00166       GECODE_NEVER;
00167       return ME_SET_VAL;
00168     }
00169   }
00170 
00171   forceinline ModEvent
00172   SingletonView::exclude(Space& home,int c) {
00173     return me_inttoset(x.nq(home,c));
00174   }
00175 
00176   forceinline ModEvent
00177   SingletonView::include(Space& home, int j, int k) {
00178     return j==k ? me_inttoset(x.eq(home,j)) : ME_SET_FAILED ;
00179   }
00180 
00181   forceinline ModEvent
00182   SingletonView::exclude(Space& home, int j, int k) {
00183     ModEvent me1 = me_inttoset(x.gr(home,j));
00184     ModEvent me2 = me_inttoset(x.le(home,k));
00185     if (me_failed(me1) || me_failed(me2))
00186       return ME_SET_FAILED;
00187     switch (me1) {
00188     case ME_SET_NONE:
00189     case ME_SET_LUB:
00190       return me2;
00191     case ME_SET_VAL:
00192       return ME_SET_VAL;
00193     default:
00194       GECODE_NEVER;
00195       return ME_SET_VAL;
00196     }
00197   }
00198 
00199   template<class I> ModEvent
00200   SingletonView::excludeI(Space& home, I& iter) {
00201     return me_inttoset(x.minus_r(home,iter));
00202   }
00203 
00204   template<class I> ModEvent
00205   SingletonView::includeI(Space& home, I& iter) {
00206     Iter::Ranges::IsRangeIter<I>();
00207     if (!iter())
00208       return ME_SET_NONE;
00209 
00210     if (iter.min()!=iter.max())
00211       return ME_SET_FAILED;
00212 
00213     int val = iter.min();
00214     ++iter;
00215     if ( iter() )
00216       return ME_SET_FAILED;
00217 
00218     return me_inttoset(x.eq(home, val));
00219   }
00220 
00221   template<class I> ModEvent
00222   SingletonView::intersectI(Space& home, I& iter) {
00223     return me_inttoset(x.inter_r(home,iter));
00224   }
00225 
00226   forceinline void
00227   SingletonView::subscribe(Space& home, Propagator& p, PropCond pc,
00228                            bool schedule) {
00229     x.subscribe(home,p,pc_settoint(pc),schedule);
00230   }
00231   forceinline void
00232   SingletonView::cancel(Space& home, Propagator& p, PropCond pc) {
00233     x.cancel(home,p,pc_settoint(pc));
00234   }
00235 
00236   forceinline void
00237   SingletonView::subscribe(Space& home, Advisor& a) {
00238     x.subscribe(home,a);
00239   }
00240   forceinline void
00241   SingletonView::cancel(Space& home, Advisor& a) {
00242     x.cancel(home,a);
00243   }
00244 
00245 
00246   forceinline void
00247   SingletonView::schedule(Space& home, Propagator& p, ModEvent me) {
00248     return Gecode::Int::IntView::schedule(home,p,me_settoint(me));
00249   }
00250   forceinline ModEvent
00251   SingletonView::me(const ModEventDelta& med) {
00252     return me_inttoset(Int::IntView::me(med));
00253   }
00254   forceinline ModEventDelta
00255   SingletonView::med(ModEvent me) {
00256     return SetView::med(me_settoint(me));
00257   }
00258 
00259 
00260   /*
00261    * Delta information for advisors
00262    *
00263    * For SingletonViews, a glb change means that the view is assigned.
00264    * Thus, the delta for the glb is always equal to the delta for the lub.
00265    *
00266    */
00267 
00268   forceinline ModEvent
00269   SingletonView::modevent(const Delta& d) {
00270     return me_inttoset(Int::IntView::modevent(d));
00271   }
00272 
00273   forceinline int
00274   SingletonView::glbMin(const Delta& d) const { return x.min(d); }
00275 
00276   forceinline int
00277   SingletonView::glbMax(const Delta& d) const { return x.max(d); }
00278 
00279   forceinline bool
00280   SingletonView::glbAny(const Delta& d) const { return x.any(d); }
00281 
00282   forceinline int
00283   SingletonView::lubMin(const Delta& d) const { return x.min(d); }
00284 
00285   forceinline int
00286   SingletonView::lubMax(const Delta& d) const { return x.max(d); }
00287 
00288   forceinline bool
00289   SingletonView::lubAny(const Delta& d) const { return x.any(d); }
00290 
00291   /*
00292    * Iterators
00293    *
00294    */
00295 
00300   template<>
00301   class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd {
00302   public:
00304 
00305 
00306     LubRanges(void);
00308     LubRanges(const SingletonView& x);
00310     void init(const SingletonView& x);
00312   };
00313 
00314   forceinline
00315   LubRanges<SingletonView>::LubRanges(void) {}
00316 
00317   forceinline
00318   LubRanges<SingletonView>::LubRanges(const SingletonView& s) :
00319     Gecode::Int::IntVarImpFwd(s.base().varimp()) {}
00320 
00321   forceinline void
00322   LubRanges<SingletonView>::init(const SingletonView& s) {
00323     Gecode::Int::IntVarImpFwd::init(s.base().varimp());
00324   }
00325 
00330   template<>
00331   class GlbRanges<SingletonView> {
00332   private:
00333     int  val;
00334     bool flag;
00335   public:
00337 
00338 
00339     GlbRanges(void);
00341     GlbRanges(const SingletonView& x);
00343     void init(const SingletonView& x);
00344 
00346 
00347 
00348     bool operator ()(void) const;
00350     void operator ++(void);
00352 
00354 
00355 
00356     int min(void) const;
00358     int max(void) const;
00360     unsigned int width(void) const;
00362   };
00363 
00364   forceinline
00365   GlbRanges<SingletonView>::GlbRanges(void) {}
00366 
00367   forceinline void
00368   GlbRanges<SingletonView>::init(const SingletonView& s) {
00369     if (s.base().assigned()) {
00370       val = s.base().val();
00371       flag = true;
00372     } else {
00373       val = 0;
00374       flag = false;
00375     }
00376   }
00377 
00378   forceinline
00379   GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) {
00380     init(s);
00381   }
00382 
00383   forceinline bool
00384   GlbRanges<SingletonView>::operator ()(void) const { return flag; }
00385 
00386   forceinline void
00387   GlbRanges<SingletonView>::operator ++(void) { flag=false; }
00388 
00389   forceinline int
00390   GlbRanges<SingletonView>::min(void) const { return val; }
00391   forceinline int
00392   GlbRanges<SingletonView>::max(void) const { return val; }
00393   forceinline unsigned int
00394   GlbRanges<SingletonView>::width(void) const { return 1; }
00395 
00396 }}
00397 
00398 // STATISTICS: set-var
00399