Generated on Thu Apr 11 13:59:01 2019 for Gecode by doxygen 1.6.3

view.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *     Guido Tack <tack@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Patrick Pekczynski, 2004
00012  *     Christian Schulte, 2009
00013  *     Guido Tack, 2009
00014  *
00015  *  This file is part of Gecode, the generic constrain
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *
00020  *  Permission is hereby granted, free of charge, to any person obtaining
00021  *  a copy of this software and associated documentation files (the
00022  *  "Software"), to deal in the Software without restriction, including
00023  *  without limitation the rights to use, copy, modify, merge, publish,
00024  *  distribute, sublicense, and/or sell copies of the Software, and to
00025  *  permit persons to whom the Software is furnished to do so, subject to
00026  *  the following conditions:
00027  *
00028  *  The above copyright notice and this permission notice shall be
00029  *  included in all copies or substantial portions of the Software.
00030  *
00031  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00032  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00033  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00034  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00035  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00036  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00037  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00038  */
00039 
00040 namespace Gecode { namespace Int { namespace GCC {
00041 
00043   template<class T>
00044   forceinline bool
00045   lookupValue(T& a, int v, int& i) {
00046     int l = 0;
00047     int r = a.size() - 1;
00048 
00049     while (l <= r) {
00050       int m = l + (r - l) / 2;
00051       if (v == a[m].card()) {
00052         i=m; return true;
00053       } else if (l == r) {
00054         return false;
00055       } else if (v < a[m].card()) {
00056         r=m-1;
00057       } else {
00058         l=m+1;
00059       }
00060     }
00061     return false;
00062   }
00063 
00065   class CardConst {
00066   private:
00068     int _min;
00070     int _max;
00072     int _card;
00074     int _counter;
00075   public:
00077     static const bool propagate = false;
00078 
00080 
00081 
00082     CardConst(void);
00084     void init(Space& home, int min, int max, int c);
00086 
00088 
00089 
00090     int min(void) const;
00092     int max(void) const;
00094     int card(void) const;
00096     int counter(void) const;
00098 
00102     bool assigned(void) const;
00104 
00108     void counter(int n);
00110     ModEvent inc(void);
00112     ModEvent lq(Space& home, int n);
00114     ModEvent gq(Space& home, int n);
00116     ModEvent eq(Space& home, int n);
00118 
00122     void subscribe(Space& home, Propagator& p, PropCond pc, bool process=true);
00124     void cancel(Space& home, Propagator& p, PropCond pc);
00126     void reschedule(Space& home, Propagator& p, PropCond pc);
00128 
00132     void update(Space& home, CardConst& x);
00134 
00136     IntView base(void) const;
00137   };
00138 
00140   class CardView : public DerivedView<IntView> {
00141   protected:
00142     using DerivedView<IntView>::x;
00144     int _card;
00146     int _counter;
00147   public:
00149     static const bool propagate = true;
00151 
00152 
00153     CardView(void);
00155     void init(const IntView& y, int c);
00157     void init(Space& home, const IntSet& s, int c);
00159 
00161 
00162 
00163     int min(void) const;
00165     int max(void) const;
00167     unsigned int size(void) const;
00169     int counter(void) const;
00171     int card(void) const;
00173 
00177     void counter(int n);
00179     ModEvent inc(void);
00181     ModEvent lq(Space& home, int n);
00183     ModEvent gq(Space& home, int n);
00185     ModEvent eq(Space& home, int n);
00187 
00189 
00190 
00191     template<class I>
00192     ModEvent narrow_v(Space& home, I& i, bool depends=true);
00194     template<class I>
00195     ModEvent inter_v(Space& home, I& i, bool depends=true);
00197     template<class I>
00198     ModEvent minus_v(Space& home, I& i, bool depends=true);
00200 
00204     void update(Space& home, CardView& x);
00206   };
00207 
00208 
00209 
00210   /*
00211    * Constant cardinality view
00212    *
00213    */
00214   forceinline
00215   CardConst::CardConst(void) {}
00216   forceinline void
00217   CardConst::init(Space&, int min, int max, int c) {
00218     _min = min; _max=max; _card = c; _counter = 0;
00219   }
00220 
00221   forceinline int
00222   CardConst::min(void) const {
00223     return _min;
00224   }
00225   forceinline int
00226   CardConst::max(void) const {
00227     return _max;
00228   }
00229   forceinline int
00230   CardConst::card(void) const {
00231     return _card;
00232   }
00233   forceinline int
00234   CardConst::counter(void) const {
00235     return _counter;
00236   }
00237   forceinline bool
00238   CardConst::assigned(void) const {
00239     return _min==_max;
00240   }
00241 
00242 
00243   forceinline void
00244   CardConst::counter(int n) {
00245     _counter = n;
00246   }
00247   forceinline ModEvent
00248   CardConst::inc(void) {
00249     if (++_counter > _max)
00250       return ME_INT_FAILED;
00251     return ME_INT_NONE;
00252   }
00253   forceinline ModEvent
00254   CardConst::lq(Space&, int n) {
00255     if (_min > n)
00256       return ME_INT_FAILED;
00257     return ME_INT_NONE;
00258   }
00259   forceinline ModEvent
00260   CardConst::gq(Space&, int n) {
00261     if (_max < n)
00262       return ME_INT_FAILED;
00263     return ME_INT_NONE;
00264   }
00265   forceinline ModEvent
00266   CardConst::eq(Space&, int n) {
00267     if ((_min > n) || (_max < n))
00268       return ME_INT_FAILED;
00269     return ME_INT_NONE;
00270   }
00271 
00272   forceinline void
00273   CardConst::subscribe(Space&, Propagator&, PropCond, bool) {}
00274   forceinline void
00275   CardConst::cancel(Space&, Propagator&, PropCond) {}
00276   forceinline void
00277   CardConst::reschedule(Space&, Propagator&, PropCond) {}
00278 
00279   forceinline void
00280   CardConst::update(Space&, CardConst& x) {
00281     _min=x._min; _max=x._max; _card=x._card; _counter=x._counter;
00282   }
00283 
00284   forceinline IntView
00285   CardConst::base(void) const {
00286     GECODE_NEVER;
00287     return IntView();
00288   }
00289 
00290 
00291 
00292   /*
00293    * Cardinality integer view
00294    *
00295    */
00296   forceinline
00297   CardView::CardView(void) {}
00298   forceinline void
00299   CardView::init(const IntView& y, int c) {
00300     x = y; _card = c; _counter = 0;
00301   }
00302   forceinline void
00303   CardView::init(Space& home, const IntSet& s, int c) {
00304     x = IntVar(home,s); _card = c; _counter = 0;
00305   }
00306 
00307   forceinline int
00308   CardView::counter(void) const {
00309     return _counter;
00310   }
00311   forceinline int
00312   CardView::card(void) const {
00313     return _card;
00314   }
00315   forceinline int
00316   CardView::min(void) const {
00317     return x.min();
00318   }
00319   forceinline int
00320   CardView::max(void) const {
00321     return x.max();
00322   }
00323   forceinline unsigned int
00324   CardView::size(void) const {
00325     return x.size();
00326   }
00327 
00328   forceinline void
00329   CardView::counter(int n) {
00330     _counter = n;
00331   }
00332   forceinline ModEvent
00333   CardView::inc(void) {
00334     if (++_counter > this->max())
00335       return ME_INT_FAILED;
00336     return ME_GEN_NONE;
00337   }
00338   forceinline ModEvent
00339   CardView::lq(Space& home, int n) {
00340     return x.lq(home,n);
00341   }
00342   forceinline ModEvent
00343   CardView::gq(Space& home, int n) {
00344     return x.gq(home,n);
00345   }
00346   forceinline ModEvent
00347   CardView::eq(Space& home, int n) {
00348     return x.eq(home,n);
00349   }
00350 
00351   template<class I>
00352   forceinline ModEvent
00353   CardView::narrow_v(Space& home, I& i, bool depends) {
00354     return x.narrow_v(home,i,depends);
00355   }
00356   template<class I>
00357   forceinline ModEvent
00358   CardView::inter_v(Space& home, I& i, bool depends) {
00359     return x.inter_v(home,i,depends);
00360   }
00361   template<class I>
00362   forceinline ModEvent
00363   CardView::minus_v(Space& home, I& i, bool depends) {
00364     return x.minus_v(home,i,depends);
00365   }
00366 
00367   forceinline void
00368   CardView::update(Space& home, CardView& y) {
00369     x.update(home,y.x);
00370     _card = y._card; _counter = y._counter;
00371   }
00372 
00373 }
00374 
00375 
00379   template<>
00380   class ViewRanges<GCC::CardView>
00381     : public Gecode::Int::ViewRanges<IntView> {
00382   public:
00386     ViewRanges(void);
00388     ViewRanges(const GCC::CardView& x);
00390     void init(const GCC::CardView& x);
00392   };
00393 
00394   forceinline
00395   ViewRanges<GCC::CardView>::ViewRanges(void) :
00396     Gecode::Int::ViewRanges<IntView>()  {}
00397 
00398   forceinline
00399   ViewRanges<GCC::CardView>::ViewRanges (const GCC::CardView& x)
00400     : Gecode::Int::ViewRanges<IntView>(x.base())  {}
00401 
00402   forceinline void
00403   ViewRanges<GCC::CardView>::init(const GCC::CardView& x) {
00404     Gecode::Int::ViewRanges<IntView> xi(x.base());
00405   }
00406 
00407 }}
00408 
00409 
00410 
00411 // STATISTICS: int-prop