Generated on Tue Apr 18 10:22:14 2017 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: 2016-09-02 15:36:56 +0200 (Fri, 02 Sep 2016) $ by $Author: schulte $
00015  *     $Revision: 15162 $
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   forceinline void
00239   SingletonView::reschedule(Space& home, Propagator& p, PropCond pc) {
00240     x.reschedule(home,p,pc_settoint(pc));
00241   }
00242 
00243   forceinline void
00244   SingletonView::subscribe(Space& home, Advisor& a) {
00245     x.subscribe(home,a);
00246   }
00247   forceinline void
00248   SingletonView::cancel(Space& home, Advisor& a) {
00249     x.cancel(home,a);
00250   }
00251 
00252 
00253   forceinline void
00254   SingletonView::schedule(Space& home, Propagator& p, ModEvent me) {
00255     return Gecode::Int::IntView::schedule(home,p,me_settoint(me));
00256   }
00257   forceinline ModEvent
00258   SingletonView::me(const ModEventDelta& med) {
00259     return me_inttoset(Int::IntView::me(med));
00260   }
00261   forceinline ModEventDelta
00262   SingletonView::med(ModEvent me) {
00263     return SetView::med(me_settoint(me));
00264   }
00265 
00266 
00267   /*
00268    * Delta information for advisors
00269    *
00270    * For SingletonViews, a glb change means that the view is assigned.
00271    * Thus, the delta for the glb is always equal to the delta for the lub.
00272    *
00273    */
00274 
00275   forceinline ModEvent
00276   SingletonView::modevent(const Delta& d) {
00277     return me_inttoset(Int::IntView::modevent(d));
00278   }
00279 
00280   forceinline int
00281   SingletonView::glbMin(const Delta& d) const { return x.min(d); }
00282 
00283   forceinline int
00284   SingletonView::glbMax(const Delta& d) const { return x.max(d); }
00285 
00286   forceinline bool
00287   SingletonView::glbAny(const Delta& d) const { return x.any(d); }
00288 
00289   forceinline int
00290   SingletonView::lubMin(const Delta& d) const { return x.min(d); }
00291 
00292   forceinline int
00293   SingletonView::lubMax(const Delta& d) const { return x.max(d); }
00294 
00295   forceinline bool
00296   SingletonView::lubAny(const Delta& d) const { return x.any(d); }
00297 
00298   /*
00299    * Iterators
00300    *
00301    */
00302 
00307   template<>
00308   class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd {
00309   public:
00311 
00312 
00313     LubRanges(void);
00315     LubRanges(const SingletonView& x);
00317     void init(const SingletonView& x);
00319   };
00320 
00321   forceinline
00322   LubRanges<SingletonView>::LubRanges(void) {}
00323 
00324   forceinline
00325   LubRanges<SingletonView>::LubRanges(const SingletonView& s) :
00326     Gecode::Int::IntVarImpFwd(s.base().varimp()) {}
00327 
00328   forceinline void
00329   LubRanges<SingletonView>::init(const SingletonView& s) {
00330     Gecode::Int::IntVarImpFwd::init(s.base().varimp());
00331   }
00332 
00337   template<>
00338   class GlbRanges<SingletonView> {
00339   private:
00340     int  val;
00341     bool flag;
00342   public:
00344 
00345 
00346     GlbRanges(void);
00348     GlbRanges(const SingletonView& x);
00350     void init(const SingletonView& x);
00351 
00353 
00354 
00355     bool operator ()(void) const;
00357     void operator ++(void);
00359 
00361 
00362 
00363     int min(void) const;
00365     int max(void) const;
00367     unsigned int width(void) const;
00369   };
00370 
00371   forceinline
00372   GlbRanges<SingletonView>::GlbRanges(void) {}
00373 
00374   forceinline void
00375   GlbRanges<SingletonView>::init(const SingletonView& s) {
00376     if (s.base().assigned()) {
00377       val = s.base().val();
00378       flag = true;
00379     } else {
00380       val = 0;
00381       flag = false;
00382     }
00383   }
00384 
00385   forceinline
00386   GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) {
00387     init(s);
00388   }
00389 
00390   forceinline bool
00391   GlbRanges<SingletonView>::operator ()(void) const { return flag; }
00392 
00393   forceinline void
00394   GlbRanges<SingletonView>::operator ++(void) { flag=false; }
00395 
00396   forceinline int
00397   GlbRanges<SingletonView>::min(void) const { return val; }
00398   forceinline int
00399   GlbRanges<SingletonView>::max(void) const { return val; }
00400   forceinline unsigned int
00401   GlbRanges<SingletonView>::width(void) const { return 1; }
00402 
00403 }}
00404 
00405 // STATISTICS: set-var
00406