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

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