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

cached.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  *  Copyright:
00007  *     Guido Tack, 2011
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-05-02 00:24:42 +0200 (Mon, 02 May 2011) $ by $Author: tack $
00011  *     $Revision: 11973 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode { namespace Int {
00039 
00040   /*
00041    * Constructors and initialization
00042    *
00043    */
00044   template<class View>
00045   forceinline
00046   CachedView<View>::CachedView(void) : _size(0) {}
00047   template<class View>
00048   forceinline
00049   CachedView<View>::CachedView(const View& y)
00050     : DerivedView<View>(y), _firstRange(NULL), _lastRange(NULL), 
00051       _size(0) {}
00052 
00053 
00054   /*
00055    * Value access
00056    *
00057    */
00058   template<class View>
00059   forceinline int
00060   CachedView<View>::min(void) const {
00061     return x.min();
00062   }
00063   template<class View>
00064   forceinline int
00065   CachedView<View>::max(void) const {
00066     return x.max();
00067   }
00068   template<class View>
00069   forceinline int
00070   CachedView<View>::med(void) const {
00071     return x.med();
00072   }
00073   template<class View>
00074   forceinline int
00075   CachedView<View>::val(void) const {
00076     return x.val();
00077   }
00078 
00079   template<class View>
00080   forceinline unsigned int
00081   CachedView<View>::width(void) const {
00082     return x.width();
00083   }
00084   template<class View>
00085   forceinline unsigned int
00086   CachedView<View>::size(void) const {
00087     return x.size();
00088   }
00089   template<class View>
00090   forceinline unsigned int
00091   CachedView<View>::regret_min(void) const {
00092     return x.regret_min();
00093   }
00094   template<class View>
00095   forceinline unsigned int
00096   CachedView<View>::regret_max(void) const {
00097     return x.regret_max();
00098   }
00099 
00100   /*
00101    * Domain tests
00102    *
00103    */
00104   template<class View>
00105   forceinline bool
00106   CachedView<View>::range(void) const {
00107     return x.range();
00108   }
00109   template<class View>
00110   forceinline bool
00111   CachedView<View>::in(int n) const {
00112     return x.in(n);
00113   }
00114   template<class View>
00115   forceinline bool
00116   CachedView<View>::in(double n) const {
00117     return x.in(n);
00118   }
00119 
00120 
00121   /*
00122    * Domain update by value
00123    *
00124    */
00125   template<class View>
00126   forceinline ModEvent
00127   CachedView<View>::lq(Space& home, int n) {
00128     return x.lq(home,n);
00129   }
00130   template<class View>
00131   forceinline ModEvent
00132   CachedView<View>::lq(Space& home, double n) {
00133     return x.lq(home,n);
00134   }
00135   template<class View>
00136   forceinline ModEvent
00137   CachedView<View>::le(Space& home, int n) {
00138     return x.le(home,n);
00139   }
00140   template<class View>
00141   forceinline ModEvent
00142   CachedView<View>::le(Space& home, double n) {
00143     return x.le(home,n);
00144   }
00145   template<class View>
00146   forceinline ModEvent
00147   CachedView<View>::gq(Space& home, int n) {
00148     return x.gq(home,n);
00149   }
00150   template<class View>
00151   forceinline ModEvent
00152   CachedView<View>::gq(Space& home, double n) {
00153     return x.gq(home,n);
00154   }
00155   template<class View>
00156   forceinline ModEvent
00157   CachedView<View>::gr(Space& home, int n) {
00158     return x.gr(home,n);
00159   }
00160   template<class View>
00161   forceinline ModEvent
00162   CachedView<View>::gr(Space& home, double n) {
00163     return x.gr(home,n);
00164   }
00165   template<class View>
00166   forceinline ModEvent
00167   CachedView<View>::nq(Space& home, int n) {
00168     return x.nq(home,n);
00169   }
00170   template<class View>
00171   forceinline ModEvent
00172   CachedView<View>::nq(Space& home, double n) {
00173     return x.nq(home,n);
00174   }
00175   template<class View>
00176   forceinline ModEvent
00177   CachedView<View>::eq(Space& home, int n) {
00178     return x.eq(home,n);
00179   }
00180   template<class View>
00181   forceinline ModEvent
00182   CachedView<View>::eq(Space& home, double n) {
00183     return x.eq(home,n);
00184   }
00185 
00186 
00187   /*
00188    * Iterator-based domain update
00189    *
00190    */
00191   template<class View>
00192   template<class I>
00193   forceinline ModEvent
00194   CachedView<View>::narrow_r(Space& home, I& i, bool depend) {
00195     return x.narrow_r(home,i,depend);
00196   }
00197   template<class View>
00198   template<class I>
00199   forceinline ModEvent
00200   CachedView<View>::inter_r(Space& home, I& i, bool depend) {
00201     return x.inter_r(home,i,depend);
00202   }
00203   template<class View>
00204   template<class I>
00205   forceinline ModEvent
00206   CachedView<View>::minus_r(Space& home, I& i, bool depend) {
00207     return x.minus_r(home,i,depend);
00208   }
00209   template<class View>
00210   template<class I>
00211   forceinline ModEvent
00212   CachedView<View>::narrow_v(Space& home, I& i, bool depend) {
00213     return x.narrow_v(home,i,depend);
00214   }
00215   template<class View>
00216   template<class I>
00217   forceinline ModEvent
00218   CachedView<View>::inter_v(Space& home, I& i, bool depend) {
00219     return x.inter_v(home,i,depend);
00220   }
00221   template<class View>
00222   template<class I>
00223   forceinline ModEvent
00224   CachedView<View>::minus_v(Space& home, I& i, bool depend) {
00225     return x.minus_v(home,i,depend);
00226   }
00227 
00228 
00229 
00230   /*
00231    * Propagator modification events
00232    *
00233    */
00234   template<class View>
00235   forceinline ModEventDelta
00236   CachedView<View>::med(ModEvent me) {
00237     return View::med(me);
00238   }
00239 
00240 
00241   /*
00242    * Delta information for advisors
00243    *
00244    */
00245   template<class View>
00246   forceinline int
00247   CachedView<View>::min(const Delta& d) const {
00248     return x.min(d);
00249   }
00250   template<class View>
00251   forceinline int
00252   CachedView<View>::max(const Delta& d) const {
00253     return x.max(d);
00254   }
00255   template<class View>
00256   forceinline bool
00257   CachedView<View>::any(const Delta& d) const {
00258     return x.any(d);
00259   }
00260 
00261 
00262 
00263   /*
00264    * Cloning
00265    *
00266    */
00267   template<class View>
00268   void
00269   CachedView<View>::update(Space& home, bool share, CachedView<View>& y) {
00270     DerivedView<View>::update(home,share,y);
00271     if (y._firstRange) {
00272       _firstRange = new (home) RangeList(y._firstRange->min(),
00273                                          y._firstRange->max(),NULL);
00274       RangeList* cur = _firstRange;
00275       
00276       for (RangeList* y_cur = y._firstRange->next(); y_cur != NULL;
00277            y_cur = y_cur->next()) {
00278         RangeList* next =
00279           new (home) RangeList(y_cur->min(),y_cur->max(),NULL);
00280         cur->next(next);
00281         cur = next;
00282       }
00283       _lastRange = cur;
00284       _size = y._size;
00285     }
00286   }
00287 
00288   /*
00289    * Cache operations
00290    *
00291    */
00292 
00293   template<class View>
00294   void
00295   CachedView<View>::initCache(Space& home, const IntSet& s) {
00296     _firstRange = NULL;
00297     for (int i=s.ranges(); i--;) {
00298       _firstRange = new (home) RangeList(s.min(i),s.max(i),_firstRange);
00299       if (i==s.ranges()-1)
00300         _lastRange = _firstRange;
00301     }
00302     _size = s.size();
00303   }
00304 
00305   template<class View>
00306   void
00307   CachedView<View>::cache(Space& home) {
00308     _firstRange->dispose(home,_lastRange);
00309     ViewRanges<View> xr(x);
00310     _firstRange = new (home) RangeList(xr.min(),xr.max(),NULL);
00311     ++xr;
00312     RangeList* cur = _firstRange;
00313     for (; xr(); ++xr) {
00314       RangeList* next = new (home) RangeList(xr.min(),xr.max(),NULL);
00315       cur->next(next);
00316       cur = next;
00317     }
00318     _lastRange = cur;
00319     _size = x.size();
00320   }
00321 
00322   template<class View>
00323   forceinline bool
00324   CachedView<View>::modified(void) const {
00325     return x.size() != _size;
00326   }
00327 
00328 
00333   template<class View>
00334   class ViewRanges<CachedView<View> >
00335     : public ViewRanges<View> {
00336   public:
00338 
00339 
00340     ViewRanges(void);
00342     ViewRanges(const CachedView<View>& x);
00344     void init(const CachedView<View>& x);
00346   };
00347 
00348   template<class View>
00349   forceinline
00350   ViewRanges<CachedView<View> >::ViewRanges(void) {}
00351 
00352   template<class View>
00353   forceinline
00354   ViewRanges<CachedView<View> >::ViewRanges(const CachedView<View>& x) {
00355     ViewRanges<IntView>::init(x.base());
00356   }
00357 
00358   template<class View>
00359   forceinline void
00360   ViewRanges<CachedView<View> >::init(const CachedView<View>& x) {
00361     ViewRanges<View>::init(x.base());
00362   }
00363 
00364   template<class View>
00365   forceinline
00366   ViewDiffRanges<View>::ViewDiffRanges(void) {}
00367 
00368   template<class View>
00369   forceinline
00370   ViewDiffRanges<View>::ViewDiffRanges(const CachedView<View>& x)
00371     : cr(x._firstRange), dr(x.base()) {
00372     Super::init(cr,dr);
00373   }
00374 
00375   template<class View>
00376   forceinline void
00377   ViewDiffRanges<View>::init(const CachedView<View>& x) {
00378     cr.init(x._firstRange);
00379     dr.init(x.base());
00380     Super::init(cr,dr);
00381   }
00382 
00383   /*
00384    * View comparison
00385    *
00386    */
00387   template<class View>
00388   forceinline bool
00389   same(const CachedView<View>& x, const CachedView<View>& y) {
00390     return same(x.base(),y.base()) && (x.offset() == y.offset());
00391   }
00392   template<class View>
00393   forceinline bool
00394   before(const CachedView<View>& x, const CachedView<View>& y) {
00395     return before(x.base(),y.base())
00396       || (same(x.base(),y.base()) && (x.offset() < y.offset()));
00397   }
00398 
00399 }}
00400 
00401 // STATISTICS: int-var
00402