Generated on Wed Nov 1 15:04:34 2006 for Gecode by doxygen 1.4.5

occur.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00004  *
00005  *  Copyright:
00006  *     Patrick Pekczynski, 2004
00007  *
00008  *  Last modified: $Date: 2006-08-24 11:25:05 +0200 (Thu, 24 Aug 2006) $ by $Author: schulte $
00009  *  $Revision: 3559 $
00010  *
00011  *  This file is part of Gecode, the generic constrain
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  */
00020 
00021 #include "gecode/iter.hh"
00022 
00023 namespace Gecode { namespace Int { namespace GCC {
00028   class OccurBndsView {
00029   private:
00030     int _min;
00031     int _max;
00032     int c;
00033     int count;
00034   public:
00035     OccurBndsView(void);
00036     int min(void) const;
00037     int max(void) const;
00038     int card(void) const;
00039     int counter(void) const;
00040 
00041     void min(int);
00042     void max(int);
00043     void card(int c);
00044     void counter(int c);
00045 
00046     void init(Space* home, int min, int max, int c);
00047     ModEvent lq(Space* home, int n);
00048     ModEvent gq(Space* home, int n);
00049     ModEvent eq(Space* home, int n);
00050     bool assigned(void) const;
00051     bool modified(void) const;
00052     bool range(void) const;
00053     ModEvent inc(void);
00054 
00055     void cancel(Space* home, Propagator* , PropCond ) {}
00056     void subscribe(Space*, Propagator* , PropCond, bool=true) {}
00057 
00058     void update(Space*, bool, OccurBndsView&);
00059   };
00060 
00061   forceinline
00062   OccurBndsView::OccurBndsView(void) {}
00063 
00064   forceinline int
00065   OccurBndsView::min(void) const {
00066     return _min;
00067   }
00068 
00069   forceinline int
00070   OccurBndsView::max(void) const {
00071     return _max;
00072   }
00073 
00074   forceinline int
00075   OccurBndsView::card(void) const {
00076     return c;
00077   }
00078 
00079   forceinline int
00080   OccurBndsView::counter(void) const {
00081     return count;
00082   }
00083 
00084   forceinline void
00085   OccurBndsView::min(int m) {
00086     _min = m;
00087   }
00088 
00089   forceinline void
00090   OccurBndsView::max(int m) {
00091     _max = m;
00092   }
00093 
00094   forceinline void
00095   OccurBndsView::card(int ca) {
00096     c = ca;
00097   }
00098 
00099   forceinline void
00100   OccurBndsView::counter(int count0) {
00101     count = count0;
00102   }
00103 
00104   forceinline void
00105   OccurBndsView::init(Space* home, int min, int max, int val) {
00106     _min = min; _max=max;
00107     c = val;
00108     count = 0;
00109   }
00110 
00111   forceinline ModEvent
00112   OccurBndsView::inc(void) {
00113     count++;
00114     if (count > _max) {
00115       return ME_GEN_FAILED;
00116     } else {
00117       return ME_GEN_NONE;
00118     }
00119   }
00120 
00121   forceinline bool
00122   OccurBndsView::assigned(void) const {
00123     return _min==_max;
00124   }
00125 
00126   forceinline bool
00127   OccurBndsView::modified(void) const {
00128     return false;
00129   }
00130 
00131   forceinline bool
00132   OccurBndsView::range(void) const {
00133     return true;
00134   }
00135 
00136 
00137   forceinline ModEvent
00138   OccurBndsView::lq(Space*, int i){
00139     // the maximum can be made consistent
00140     if (_min > i) {
00141       return ME_GEN_FAILED;
00142     } else {
00143       return ME_GEN_NONE;
00144     }
00145   }
00146 
00147   forceinline ModEvent
00148   OccurBndsView::gq(Space*, int i){
00149     // this bound is fix
00150     if (_max < i) {
00151       return ME_GEN_FAILED;
00152     }
00153     return ME_GEN_NONE;
00154   }
00155 
00156   forceinline ModEvent
00157   OccurBndsView::eq(Space*, int i){
00158     if (_min > i || _max < i) {
00159       return ME_GEN_FAILED;
00160     } else {
00161       return ME_GEN_NONE;
00162     }
00163   }
00164 
00166   forceinline std::ostream&
00167   operator<<(std::ostream& os, OccurBndsView& xs) {
00168     os << xs.card() << "("<< xs.counter() <<")[";
00169     os << xs.min() << "," << xs.max() << "]";
00170     return os;
00171   }
00172 
00173   forceinline void
00174   OccurBndsView::update(Space*, bool, OccurBndsView& oc) {
00175     _min = oc._min;
00176     _max = oc._max;
00177     c = oc.c;
00178     count = oc.count;
00179   }
00180 
00186   template <class T>
00187   forceinline int
00188   lookupValue(T& a, int v){
00189     int idx = -1;
00190 
00191     int l  = 0;
00192     int r  = a.size() - 1;
00193 
00194     if (r == 0) {
00195       if (a[0].card() == v) {
00196         return 0;
00197       } else {
00198         return -1;
00199       }
00200     }
00201 
00202     while ( l < r ) {
00203       if ( a[l].card() == v) {
00204         idx = l;
00205         break;
00206       }
00207       if ( a[r].card() == v) {
00208         idx = r;
00209         break;
00210       }
00211       int p  = (l + r) / 2;
00212       if ( v == a[p].card()) {
00213         idx = p;
00214         break;
00215       } else {
00216         if ( v < a[p].card()) {
00217           r = p;
00218         } else {
00219           l = p;
00220         }
00221       }
00222       if (l == r - 1) {
00223         break;
00224       }
00225     }
00226 
00227     return idx;
00228   }
00229 
00230 
00235   class CardView : public DerivedViewBase<IntView> {
00236   protected:
00238     int c;
00240     int count;
00241     using DerivedViewBase<IntView>::view;
00242   public:
00243     CardView(void);
00245     CardView(const IntView& x, int c);
00247     void init(const IntView& x, int c);
00248     void init(Space* home, int mi, int ma , int c);
00249 
00251     int card(void) const;
00252     void card(int ca);
00253 
00255     ModEvent inc(void);
00257     void counter(int);
00259     int counter(void);
00260 
00262 
00263     void operator=(const IntView& x);
00264     void operator=(const Gecode::Int::GCC::CardView& x);
00266     int min(void) const;
00268     int max(void) const;
00270     int med(void) const;
00272     int val(void) const;
00274     IntView intview(void);
00276     unsigned int size(void) const;
00278     unsigned int width(void) const;
00280     unsigned int regret_min(void) const;
00282     unsigned int regret_max(void) const;
00284 
00288     bool range(void) const;
00290     bool assigned(void) const;
00291 
00293     bool in(int n) const;
00295     bool in(double n) const;
00297 
00301     ModEvent lq(Space* home, int n);
00303     ModEvent lq(Space* home, double n);
00305     ModEvent le(Space* home, int n);
00307     ModEvent le(Space* home, double n);
00309     ModEvent gq(Space* home, int n);
00311     ModEvent gq(Space* home, double n);
00313     ModEvent gr(Space* home, int n);
00315     ModEvent gr(Space* home, double n);
00317     ModEvent nq(Space* home, int n);
00319     ModEvent nq(Space* home, double n);
00321     ModEvent eq(Space* home, int n);
00323     ModEvent eq(Space* home, double n);
00325 
00330     template <class I> ModEvent inter(Space* home, I& i);
00332     template <class I> ModEvent minus(Space* home, I& i);
00334 
00338     static ModEvent     pme(const Propagator* p);
00340     static PropModEvent pme(ModEvent me);
00342 
00346     void subscribe(Space* home, Propagator* p, PropCond pc, bool process=true);
00348     void cancel(Space* home, Propagator* p, PropCond pc);
00350 
00354     void update(Space* home, bool share, CardView& x);
00356 
00360     bool operator ==(const CardView& x) const;
00362     bool operator !=(const CardView& x) const;
00364     bool operator < (const CardView& x) const;
00366     bool operator > (const CardView& x) const;
00368   };
00369 
00370   /*
00371    * Constructors and initialization
00372    *
00373    */
00374   forceinline
00375   CardView::CardView(void) {}
00376 
00377   forceinline
00378   CardView::CardView(const IntView& x, int d)
00379     : DerivedViewBase<IntView>(x), c(d), count(0) {}
00380 
00381   forceinline void
00382   CardView::init(const IntView& x, int d) {
00383     view  = x;
00384     c     = d;
00385     count = 0;
00386   }
00387 
00388 
00389   forceinline void
00390   CardView::init(Space* home, int mi, int ma, int d) {
00391     IntVar ivar(home, mi, ma);
00392     IntView iview(ivar);
00393     view  = iview;
00394     c     = d;
00395     count = 0;
00396   }
00397 
00398   forceinline void
00399   CardView::card(int ca) {
00400     c = ca;
00401   }
00402 
00403   forceinline int
00404   CardView::card(void) const {
00405     return c;
00406   }
00407 
00408   forceinline ModEvent
00409   CardView::inc(void) {
00410     count++;
00411     if (count > this->max()) {
00412       return ME_GEN_FAILED;
00413     } else {
00414       return ME_GEN_NONE;
00415     }
00416   }
00417 
00418   forceinline void
00419   CardView::counter(int c) {
00420     count = c;
00421   }
00422 
00423   forceinline int
00424   CardView::counter(void) {
00425     return count;
00426   }
00427 
00428   /*
00429    * Value access
00430    *
00431    */
00432 
00433   forceinline void
00434   CardView::operator=(const IntView& x) {
00435     view  = x;
00436     c     = 0;
00437     count = 0;
00438   }
00439 
00440   forceinline void
00441   CardView::operator=(const CardView& x) {
00442     view  = x.view;
00443     c     = x.c;
00444     count = x.count;
00445   }
00446 
00447 
00448   forceinline int
00449   CardView::min(void) const {
00450     return view.min();
00451   }
00452   forceinline int
00453   CardView::max(void) const {
00454     return view.max();
00455   }
00456   forceinline int
00457   CardView::med(void) const {
00458     return view.med();
00459   }
00460 
00461   forceinline int
00462   CardView::val(void) const {
00463     return view.val();
00464   }
00465 
00466   forceinline IntView
00467   CardView::intview(void){
00468     return view;
00469   }
00470 
00471 
00472   forceinline unsigned int
00473   CardView::width(void) const {
00474     return view.width();
00475   }
00476   forceinline unsigned int
00477   CardView::size(void) const {
00478     return view.size();
00479   }
00480   forceinline unsigned int
00481   CardView::regret_min(void) const {
00482     return view.regret_min();
00483   }
00484   forceinline unsigned int
00485   CardView::regret_max(void) const {
00486     return view.regret_max();
00487   }
00488 
00489   /*
00490    * Domain tests
00491    *
00492    */
00493   forceinline bool
00494   CardView::range(void) const {
00495     return view.range();
00496   }
00497   forceinline bool
00498   CardView::assigned(void) const {
00499     return view.assigned();
00500   }
00501 
00502   forceinline bool
00503   CardView::in(int n) const {
00504     return view.in(n);
00505   }
00506   forceinline bool
00507   CardView::in(double n) const {
00508     return view.in(n);
00509   }
00510 
00511 
00512   /*
00513    * Domain update by value
00514    *
00515    */
00516   forceinline ModEvent
00517   CardView::lq(Space* home, int n) {
00518     return view.lq(home,n);
00519   }
00520   forceinline ModEvent
00521   CardView::lq(Space* home, double n) {
00522     return view.lq(home,n);
00523   }
00524   forceinline ModEvent
00525   CardView::le(Space* home, int n) {
00526     return view.le(home,n);
00527   }
00528   forceinline ModEvent
00529   CardView::le(Space* home, double n) {
00530     return view.le(home,n);
00531   }
00532   forceinline ModEvent
00533   CardView::gq(Space* home, int n) {
00534     return view.gq(home,n);
00535   }
00536   forceinline ModEvent
00537   CardView::gq(Space* home, double n) {
00538     return view.gq(home,n);
00539   }
00540   forceinline ModEvent
00541   CardView::gr(Space* home, int n) {
00542     return view.gr(home,n);
00543   }
00544   forceinline ModEvent
00545   CardView::gr(Space* home, double n) {
00546     return view.gr(home,n);
00547   }
00548   forceinline ModEvent
00549   CardView::nq(Space* home, int n) {
00550     return view.nq(home,n);
00551   }
00552   forceinline ModEvent
00553   CardView::nq(Space* home, double n) {
00554     return view.nq(home,n);
00555   }
00556   forceinline ModEvent
00557   CardView::eq(Space* home, int n) {
00558     return view.eq(home,n);
00559   }
00560   forceinline ModEvent
00561   CardView::eq(Space* home, double n) {
00562     return view.eq(home,n);
00563   }
00564 
00565 
00566   /*
00567    * Domain update by range iterator
00568    *
00569    */
00570   template <class I>
00571   ModEvent
00572   CardView::inter(Space* home, I& i) {
00573     return view.inter(home,i);
00574   }
00575   template <class I>
00576   ModEvent
00577   CardView::minus(Space* home, I& i) {
00578     return view.minus(home,i);
00579   }
00580 
00581 
00582 
00583   /*
00584    * Propagator modification events
00585    *
00586    */
00587   forceinline ModEvent
00588   CardView::pme(const Propagator* p) {
00589     return IntView::pme(p);
00590   }
00591   forceinline PropModEvent
00592   CardView::pme(ModEvent me) {
00593     return IntView::pme(me);
00594   }
00595 
00596 
00597   /*
00598    * Dependencies
00599    *
00600    */
00601   forceinline void
00602   CardView::subscribe(Space* home, Propagator* p, PropCond pc, bool process) {
00603     view.subscribe(home, p, pc, process);
00604   }
00605   forceinline void
00606   CardView::cancel(Space* home, Propagator* p, PropCond pc) {
00607     view.cancel(home,p, pc);
00608   }
00609 
00610 
00611   /*
00612    * Cloning
00613    *
00614    */
00615   forceinline void
00616   CardView::update(Space* home, bool share, CardView& x) {
00617     c     = x.c;
00618     count = x.count;
00619     view.update(home,share,x.view);
00620   }
00621 
00622 
00623 
00624 }
00625 
00626 
00630   template <>
00631   class ViewRanges<GCC::CardView>
00632     : public Gecode::Int::ViewRanges<IntView> {
00633   public:
00637     ViewRanges(void);
00639     ViewRanges(const GCC::CardView& x);
00641     void init(const GCC::CardView& x);
00643   };
00644 
00645 
00647   forceinline std::ostream&
00648   operator<<(std::ostream& os, GCC::CardView& v) {
00649     os << "("<<v.card() << ","<< v.counter() <<",";
00650     if (v.min() == v.max()) {
00651       os << v.min() <<" ";
00652     } else {
00653       if (v.range()){
00654         os << "["<<v.min() <<".."<<v.max()<<"] ";
00655       } else {
00656         os << "{";
00657         ViewValues<GCC::CardView> iter(v);
00658         while(iter()){
00659           os << iter.val() <<",";
00660           ++iter;
00661         }
00662         os << "}";
00663       }
00664     }
00665     os << ")";
00666     return os;
00667   }
00668 
00669 
00670   forceinline
00671   ViewRanges<GCC::CardView>::ViewRanges(void) :
00672     Gecode::Int::ViewRanges<IntView>()  {}
00673 
00674   forceinline
00675   ViewRanges<GCC::CardView>::ViewRanges (const GCC::CardView& x)
00676     : Gecode::Int::ViewRanges<IntView>(x.base())  {}
00677 
00678   forceinline void
00679   ViewRanges<GCC::CardView>::init(const GCC::CardView& x) {
00680     Gecode::Int::ViewRanges<IntView> xi(x.base());
00681   }
00682 
00683 }}
00684 
00685 
00686 
00687 // STATISTICS: int-prop