Generated on Fri Oct 19 11:25:10 2018 for Gecode by doxygen 1.6.3

ranges-diff.hpp

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode { namespace Iter { namespace Ranges {
00035 
00042   template<class I, class J>
00043   class Diff : public MinMax {
00044   protected:
00046     I i;
00048     J j;
00049   public:
00051 
00052 
00053     Diff(void);
00055     Diff(I& i, J& j);
00057     void init(I& i, J& j);
00059 
00061 
00062 
00063     void operator ++(void);
00065   };
00066 
00067 
00068 
00069   template<class I, class J>
00070   forceinline void
00071   Diff<I,J>::operator ++(void) {
00072     // Precondition: mi <= ma
00073     // Task: find next mi greater than ma
00074     while (true) {
00075       if (!i()) break;
00076       mi = ma+1;
00077       ma = i.max();
00078       if (mi > i.max()) {
00079         ++i;
00080         if (!i()) break;
00081         mi = i.min();
00082         ma = i.max();
00083       }
00084       while (j() && (j.max() < mi))
00085         ++j;
00086       if (j() && (j.min() <= ma)) {
00087         // Now the interval [mi ... ma] must be shrunken
00088         // Is [mi ... ma] completely consumed?
00089         if ((mi >= j.min()) && (ma <= j.max()))
00090           continue;
00091         // Does [mi ... ma] overlap on the left?
00092         if (j.min() <= mi) {
00093           mi = j.max()+1;
00094           // Search for max!
00095           ++j;
00096           if (j() && (j.min() <= ma))
00097             ma = j.min()-1;
00098         } else {
00099           ma = j.min()-1;
00100         }
00101       }
00102       return;
00103     }
00104     finish();
00105   }
00106 
00107   template<class I, class J>
00108   forceinline
00109   Diff<I,J>::Diff(void) {}
00110 
00111   template<class I, class J>
00112   forceinline
00113   Diff<I,J>::Diff(I& i0, J& j0)
00114     : i(i0), j(j0) {
00115     if (!i()) {
00116       finish();
00117     } else {
00118       mi = i.min()-1; ma = mi;
00119       operator ++();
00120     }
00121   }
00122 
00123   template<class I, class J>
00124   forceinline void
00125   Diff<I,J>::init(I& i0, J& j0) {
00126     i = i0; j = j0;
00127     if (!i()) {
00128       finish();
00129     } else {
00130       mi = i.min()-1; ma = mi;
00131       operator ++();
00132     }
00133   }
00134 
00135 }}}
00136 
00137 // STATISTICS: iter-any
00138