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

virtual-ranges-inter.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:05:50 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3515 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
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 
00022 #include <algorithm>
00023 
00024 namespace Gecode { namespace Iter { namespace Ranges { namespace Virt {
00025 
00032   class Inter : public MinMax, public Iterator {
00033   private:
00034     Inter(const Inter&);
00035   protected:
00037     Iterator* i;
00039     Iterator* j;
00040   public:
00042 
00043 
00044     Inter(void);
00046     Inter(Iterator* i, Iterator* j);
00048     ~Inter(void);
00050 
00052 
00053 
00054     virtual int min(void) const;
00056     virtual int max(void) const;
00058     virtual unsigned int width(void) const;
00060 
00062 
00063 
00064     virtual void operator++(void);
00066     virtual bool operator()(void);
00068   };
00069 
00070 
00077   class NaryInter : public MinMax, public Iterator {
00078   private:
00079     NaryInter(const NaryInter&);
00080   protected:
00082     Iterator** is;
00084     int n;
00085   public:
00087 
00088 
00089     NaryInter(void);
00091     NaryInter(Iterator** i, int n);
00093 
00095 
00096 
00097     virtual int min(void) const;
00099     virtual int max(void) const;
00101     virtual unsigned int width(void) const;
00103 
00105 
00106 
00107     void operator++(void);
00109     virtual bool operator()(void);
00111   };
00112 
00113 
00114   /*
00115    * Binary intersection
00116    *
00117    */
00118 
00119   inline void
00120   Inter::operator++(void) {
00121     if (!(*i)() || !(*j)()) goto done;
00122     do {
00123       while ((*i)() && (i->max() < j->min())) ++(*i);
00124       if (!(*i)()) goto done;
00125       while ((*j)() && (j->max() < i->min())) ++(*j);
00126       if (!(*j)()) goto done;
00127     } while (i->max() < j->min());
00128     // Now the intervals overlap: consume the smaller interval
00129     ma = std::min(i->max(),j->max());
00130     mi = std::max(i->min(),j->min());
00131     if (i->max() < j->max()) ++(*i); else ++(*j);
00132     return;
00133   done:
00134     finish();
00135   }
00136 
00137   forceinline
00138   Inter::Inter(void) {}
00139 
00140   forceinline
00141   Inter::Inter(Iterator* i0, Iterator* j0)
00142     : i(i0), j(j0) {
00143     operator++();
00144   }
00145 
00146   forceinline
00147   Inter::~Inter(void) { delete i; delete j; }
00148 
00149   forceinline bool
00150   Inter::operator()(void) { return MinMax::operator()(); }
00151 
00152   forceinline int
00153   Inter::min(void) const { return MinMax::min(); }
00154 
00155   forceinline int
00156   Inter::max(void) const { return MinMax::max(); }
00157 
00158   forceinline unsigned int
00159   Inter::width(void) const { return MinMax::width(); }
00160 
00161   /*
00162    * Nary intersection
00163    *
00164    */
00165 
00166   inline void
00167   NaryInter::operator++(void) {
00168     // The next interval to be returned must have a hole
00169     // between it and the previously returned range.
00170     mi = ma+2;
00171     ma = is[0]->max();
00172     // Intersect with all other intervals
00173   restart:
00174     for (int i = n; i--;) {
00175       // Skip intervals that are too small
00176       while ((*(is[i]))() && (is[i]->max() < mi))
00177         ++(*(is[i]));
00178       if (!(*(is[i]))())
00179         goto done;
00180 
00181       if (is[i]->min() > ma) {
00182         mi=is[i]->min();
00183         ma=is[i]->max();
00184         goto restart;
00185       }
00186       // Now the intervals overlap
00187       if (mi < is[i]->min())
00188         mi = is[i]->min();
00189       if (ma > is[i]->max()) {
00190         ma = is[i]->max();
00191       }
00192     }
00193     return;
00194   done:
00195     finish();
00196   }
00197 
00198   forceinline
00199   NaryInter::NaryInter(void) {}
00200 
00201   inline
00202   NaryInter::NaryInter(Iterator** is0, int n0)
00203     : is(is0), n(n0) {
00204     if (!(*(is[0]))()) {
00205       finish();
00206     } else {
00207       ma=is[0]->min()-2;
00208       operator++();
00209     }
00210   }
00211 
00212   forceinline bool
00213   NaryInter::operator()(void) { return MinMax::operator()(); }
00214 
00215   forceinline int
00216   NaryInter::min(void) const { return MinMax::min(); }
00217 
00218   forceinline int
00219   NaryInter::max(void) const { return MinMax::max(); }
00220 
00221   forceinline unsigned int
00222   NaryInter::width(void) const { return MinMax::width(); }
00223 
00224 }}}}
00225 
00226 // STATISTICS: iter-any
00227