Generated on Mon Aug 25 11:35:39 2008 for Gecode by doxygen 1.5.6

virtual-ranges-inter.icc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-09-11 15:58:22 +0200 (Tue, 11 Sep 2007) $ by $Author: schulte $
00011  *     $Revision: 4973 $
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 #include <algorithm>
00039 
00040 namespace Gecode { namespace Iter { namespace Ranges { namespace Virt {
00041 
00047   class Inter : public MinMax, public Iterator {
00048   private:
00049     Inter(const Inter&);
00050   protected:
00052     Iterator* i;
00054     Iterator* j;
00055   public:
00057 
00058 
00059     Inter(void);
00061     Inter(Iterator* i, Iterator* j);
00063     ~Inter(void);
00065 
00067 
00068 
00069     virtual int min(void) const;
00071     virtual int max(void) const;
00073     virtual unsigned int width(void) const;
00075 
00077 
00078 
00079     virtual void operator++(void);
00081     virtual bool operator()(void);
00083   };
00084 
00085 
00091   class NaryInter : public MinMax, public Iterator {
00092   private:
00093     NaryInter(const NaryInter&);
00094   protected:
00096     Iterator** is;
00098     int n;
00099   public:
00101 
00102 
00103     NaryInter(void);
00105     NaryInter(Iterator** i, int n);
00107 
00109 
00110 
00111     virtual int min(void) const;
00113     virtual int max(void) const;
00115     virtual unsigned int width(void) const;
00117 
00119 
00120 
00121     void operator++(void);
00123     virtual bool operator()(void);
00125   };
00126 
00127 
00128   /*
00129    * Binary intersection
00130    *
00131    */
00132 
00133   inline void
00134   Inter::operator++(void) {
00135     if (!(*i)() || !(*j)()) goto done;
00136     do {
00137       while ((*i)() && (i->max() < j->min())) ++(*i);
00138       if (!(*i)()) goto done;
00139       while ((*j)() && (j->max() < i->min())) ++(*j);
00140       if (!(*j)()) goto done;
00141     } while (i->max() < j->min());
00142     // Now the intervals overlap: consume the smaller interval
00143     ma = std::min(i->max(),j->max());
00144     mi = std::max(i->min(),j->min());
00145     if (i->max() < j->max()) ++(*i); else ++(*j);
00146     return;
00147   done:
00148     finish();
00149   }
00150 
00151   forceinline
00152   Inter::Inter(void) {}
00153 
00154   forceinline
00155   Inter::Inter(Iterator* i0, Iterator* j0)
00156     : i(i0), j(j0) {
00157     operator++();
00158   }
00159 
00160   forceinline
00161   Inter::~Inter(void) { delete i; delete j; }
00162 
00163   forceinline bool
00164   Inter::operator()(void) { return MinMax::operator()(); }
00165 
00166   forceinline int
00167   Inter::min(void) const { return MinMax::min(); }
00168 
00169   forceinline int
00170   Inter::max(void) const { return MinMax::max(); }
00171 
00172   forceinline unsigned int
00173   Inter::width(void) const { return MinMax::width(); }
00174 
00175   /*
00176    * Nary intersection
00177    *
00178    */
00179 
00180   inline void
00181   NaryInter::operator++(void) {
00182     // The next interval to be returned must have a hole
00183     // between it and the previously returned range.
00184     mi = ma+2;
00185     ma = is[0]->max();
00186     // Intersect with all other intervals
00187   restart:
00188     for (int i = n; i--;) {
00189       // Skip intervals that are too small
00190       while ((*(is[i]))() && (is[i]->max() < mi))
00191         ++(*(is[i]));
00192       if (!(*(is[i]))())
00193         goto done;
00194 
00195       if (is[i]->min() > ma) {
00196         mi=is[i]->min();
00197         ma=is[i]->max();
00198         goto restart;
00199       }
00200       // Now the intervals overlap
00201       if (mi < is[i]->min())
00202         mi = is[i]->min();
00203       if (ma > is[i]->max()) {
00204         ma = is[i]->max();
00205       }
00206     }
00207     return;
00208   done:
00209     finish();
00210   }
00211 
00212   forceinline
00213   NaryInter::NaryInter(void) {}
00214 
00215   inline
00216   NaryInter::NaryInter(Iterator** is0, int n0)
00217     : is(is0), n(n0) {
00218     if (!(*(is[0]))()) {
00219       finish();
00220     } else {
00221       ma=is[0]->min()-2;
00222       operator++();
00223     }
00224   }
00225 
00226   forceinline bool
00227   NaryInter::operator()(void) { return MinMax::operator()(); }
00228 
00229   forceinline int
00230   NaryInter::min(void) const { return MinMax::min(); }
00231 
00232   forceinline int
00233   NaryInter::max(void) const { return MinMax::max(); }
00234 
00235   forceinline unsigned int
00236   NaryInter::width(void) const { return MinMax::width(); }
00237 
00238 }}}}
00239 
00240 // STATISTICS: iter-any
00241