Generated on Tue Apr 18 10:21:43 2017 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  *  Last modified: $Date: 2016-06-29 17:28:17 +0200 (Wed, 29 Jun 2016) $ by $Author: schulte $
00016  *  $Revision: 15137 $
00017  *
00018  *  This file is part of Gecode, the generic constrain
00019  *  development environment:
00020  *     http://www.gecode.org
00021  *
00022  *
00023  *  Permission is hereby granted, free of charge, to any person obtaining
00024  *  a copy of this software and associated documentation files (the
00025  *  "Software"), to deal in the Software without restriction, including
00026  *  without limitation the rights to use, copy, modify, merge, publish,
00027  *  distribute, sublicense, and/or sell copies of the Software, and to
00028  *  permit persons to whom the Software is furnished to do so, subject to
00029  *  the following conditions:
00030  *
00031  *  The above copyright notice and this permission notice shall be
00032  *  included in all copies or substantial portions of the Software.
00033  *
00034  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00035  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00036  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00037  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00038  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00039  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00040  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00041  */
00042 
00043 namespace Gecode { namespace Int { namespace GCC {
00044 
00046   template<class T>
00047   forceinline bool
00048   lookupValue(T& a, int v, int& i) {
00049     int l = 0;
00050     int r = a.size() - 1;
00051 
00052     while (l <= r) {
00053       int m = l + (r - l) / 2;
00054       if (v == a[m].card()) {
00055         i=m; return true;
00056       } else if (l == r) {
00057         return false;
00058       } else if (v < a[m].card()) {
00059         r=m-1;
00060       } else {
00061         l=m+1;
00062       }
00063     }
00064     return false;
00065   }
00066 
00068   class CardConst {
00069   private:
00071     int _min;
00073     int _max;
00075     int _card;
00077     int _counter;
00078   public:
00080     static const bool propagate = false;
00081 
00083 
00084 
00085     CardConst(void);
00087     void init(Space& home, int min, int max, int c);
00089 
00091 
00092 
00093     int min(void) const;
00095     int max(void) const;
00097     int card(void) const;
00099     int counter(void) const;
00101 
00105     bool assigned(void) const;
00107 
00111     void counter(int n);
00113     ModEvent inc(void);
00115     ModEvent lq(Space& home, int n);
00117     ModEvent gq(Space& home, int n);
00119     ModEvent eq(Space& home, int n);
00121 
00125     void subscribe(Space& home, Propagator& p, PropCond pc, bool process=true);
00127     void cancel(Space& home, Propagator& p, PropCond pc);
00129     void reschedule(Space& home, Propagator& p, PropCond pc);
00131 
00135     void update(Space& home, bool share, CardConst& x);
00137 
00139     IntView base(void) const;
00140   };
00141 
00143   class CardView : public DerivedView<IntView> {
00144   protected:
00145     using DerivedView<IntView>::x;
00147     int _card;
00149     int _counter;
00150   public:
00152     static const bool propagate = true;
00154 
00155 
00156     CardView(void);
00158     void init(const IntView& y, int c);
00160     void init(Space& home, const IntSet& s, int c);
00162 
00164 
00165 
00166     int min(void) const;
00168     int max(void) const;
00170     unsigned int size(void) const;
00172     int counter(void) const;
00174     int card(void) const;
00176 
00180     void counter(int n);
00182     ModEvent inc(void);
00184     ModEvent lq(Space& home, int n);
00186     ModEvent gq(Space& home, int n);
00188     ModEvent eq(Space& home, int n);
00190 
00192 
00193 
00194     template<class I>
00195     ModEvent narrow_v(Space& home, I& i, bool depends=true);
00197     template<class I>
00198     ModEvent inter_v(Space& home, I& i, bool depends=true);
00200     template<class I>
00201     ModEvent minus_v(Space& home, I& i, bool depends=true);
00203 
00207     void update(Space& home, bool share, CardView& x);
00209   };
00210 
00211 
00212 
00213   /*
00214    * Constant cardinality view
00215    *
00216    */
00217   forceinline
00218   CardConst::CardConst(void) {}
00219   forceinline void
00220   CardConst::init(Space&, int min, int max, int c) {
00221     _min = min; _max=max; _card = c; _counter = 0;
00222   }
00223 
00224   forceinline int
00225   CardConst::min(void) const {
00226     return _min;
00227   }
00228   forceinline int
00229   CardConst::max(void) const {
00230     return _max;
00231   }
00232   forceinline int
00233   CardConst::card(void) const {
00234     return _card;
00235   }
00236   forceinline int
00237   CardConst::counter(void) const {
00238     return _counter;
00239   }
00240   forceinline bool
00241   CardConst::assigned(void) const {
00242     return _min==_max;
00243   }
00244 
00245 
00246   forceinline void
00247   CardConst::counter(int n) {
00248     _counter = n;
00249   }
00250   forceinline ModEvent
00251   CardConst::inc(void) {
00252     if (++_counter > _max)
00253       return ME_INT_FAILED;
00254     return ME_INT_NONE;
00255   }
00256   forceinline ModEvent
00257   CardConst::lq(Space&, int n) {
00258     if (_min > n)
00259       return ME_INT_FAILED;
00260     return ME_INT_NONE;
00261   }
00262   forceinline ModEvent
00263   CardConst::gq(Space&, int n) {
00264     if (_max < n)
00265       return ME_INT_FAILED;
00266     return ME_INT_NONE;
00267   }
00268   forceinline ModEvent
00269   CardConst::eq(Space&, int n) {
00270     if ((_min > n) || (_max < n))
00271       return ME_INT_FAILED;
00272     return ME_INT_NONE;
00273   }
00274 
00275   forceinline void
00276   CardConst::subscribe(Space&, Propagator&, PropCond, bool) {}
00277   forceinline void
00278   CardConst::cancel(Space&, Propagator&, PropCond) {}
00279   forceinline void
00280   CardConst::reschedule(Space&, Propagator&, PropCond) {}
00281 
00282   forceinline void
00283   CardConst::update(Space&, bool, CardConst& x) {
00284     _min=x._min; _max=x._max; _card=x._card; _counter=x._counter;
00285   }
00286 
00287   forceinline IntView
00288   CardConst::base(void) const {
00289     GECODE_NEVER;
00290     return IntView();
00291   }
00292 
00293 
00294 
00295   /*
00296    * Cardinality integer view
00297    *
00298    */
00299   forceinline
00300   CardView::CardView(void) {}
00301   forceinline void
00302   CardView::init(const IntView& y, int c) {
00303     x = y; _card = c; _counter = 0;
00304   }
00305   forceinline void
00306   CardView::init(Space& home, const IntSet& s, int c) {
00307     x = IntVar(home,s); _card = c; _counter = 0;
00308   }
00309 
00310   forceinline int
00311   CardView::counter(void) const {
00312     return _counter;
00313   }
00314   forceinline int
00315   CardView::card(void) const {
00316     return _card;
00317   }
00318   forceinline int
00319   CardView::min(void) const {
00320     return x.min();
00321   }
00322   forceinline int
00323   CardView::max(void) const {
00324     return x.max();
00325   }
00326   forceinline unsigned int
00327   CardView::size(void) const {
00328     return x.size();
00329   }
00330 
00331   forceinline void
00332   CardView::counter(int n) {
00333     _counter = n;
00334   }
00335   forceinline ModEvent
00336   CardView::inc(void) {
00337     if (++_counter > this->max())
00338       return ME_INT_FAILED;
00339     return ME_GEN_NONE;
00340   }
00341   forceinline ModEvent
00342   CardView::lq(Space& home, int n) {
00343     return x.lq(home,n);
00344   }
00345   forceinline ModEvent
00346   CardView::gq(Space& home, int n) {
00347     return x.gq(home,n);
00348   }
00349   forceinline ModEvent
00350   CardView::eq(Space& home, int n) {
00351     return x.eq(home,n);
00352   }
00353 
00354   template<class I>
00355   forceinline ModEvent
00356   CardView::narrow_v(Space& home, I& i, bool depends) {
00357     return x.narrow_v(home,i,depends);
00358   }
00359   template<class I>
00360   forceinline ModEvent
00361   CardView::inter_v(Space& home, I& i, bool depends) {
00362     return x.inter_v(home,i,depends);
00363   }
00364   template<class I>
00365   forceinline ModEvent
00366   CardView::minus_v(Space& home, I& i, bool depends) {
00367     return x.minus_v(home,i,depends);
00368   }
00369 
00370   forceinline void
00371   CardView::update(Space& home, bool share, CardView& y) {
00372     x.update(home,share,y.x);
00373     _card = y._card; _counter = y._counter;
00374   }
00375 
00376 }
00377 
00378 
00382   template<>
00383   class ViewRanges<GCC::CardView>
00384     : public Gecode::Int::ViewRanges<IntView> {
00385   public:
00389     ViewRanges(void);
00391     ViewRanges(const GCC::CardView& x);
00393     void init(const GCC::CardView& x);
00395   };
00396 
00397   forceinline
00398   ViewRanges<GCC::CardView>::ViewRanges(void) :
00399     Gecode::Int::ViewRanges<IntView>()  {}
00400 
00401   forceinline
00402   ViewRanges<GCC::CardView>::ViewRanges (const GCC::CardView& x)
00403     : Gecode::Int::ViewRanges<IntView>(x.base())  {}
00404 
00405   forceinline void
00406   ViewRanges<GCC::CardView>::init(const GCC::CardView& x) {
00407     Gecode::Int::ViewRanges<IntView> xi(x.base());
00408   }
00409 
00410 }}
00411 
00412 
00413 
00414 // STATISTICS: int-prop