Generated on Sun Feb 17 15:24:24 2019 for Gecode by doxygen 1.6.3

ranges-scale.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, 2005
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 #include <cmath>
00035 
00036 namespace Gecode { namespace Iter { namespace Ranges {
00037 
00048   template<class Val, class UnsVal, class I>
00049   class ScaleUp {
00050   protected:
00052     I i;
00054     int a;
00056     Val cur;
00058     Val end;
00059   public:
00061 
00062 
00063     ScaleUp(void);
00065     ScaleUp(I& i, int a);
00067     void init(I& i, int a);
00069 
00071 
00072 
00073     bool operator ()(void) const;
00075     void operator ++(void);
00077 
00079 
00080 
00081     Val min(void) const;
00083     Val max(void) const;
00085     UnsVal width(void) const;
00087   };
00088 
00094   template<class I>
00095   class ScaleDown : public MinMax {
00096   protected:
00098     I i;
00100     int a;
00101   public:
00103 
00104 
00105     ScaleDown(void);
00107     ScaleDown(I& i, int a);
00109     void init(I& i, int a);
00111 
00113 
00114 
00115     void operator ++(void);
00117   };
00118 
00119 
00120 
00121   template<class Val, class UnsVal, class I>
00122   forceinline
00123   ScaleUp<Val,UnsVal,I>::ScaleUp(void) {}
00124 
00125   template<class Val, class UnsVal, class I>
00126   inline void
00127   ScaleUp<Val,UnsVal,I>::init(I& i0, int a0) {
00128     i = i0; a = a0;
00129     if (i()) {
00130       cur = a * i.min();
00131       end = a * i.max();
00132     } else {
00133       cur = 1;
00134       end = 0;
00135     }
00136   }
00137 
00138   template<class Val, class UnsVal, class I>
00139   inline
00140   ScaleUp<Val,UnsVal,I>::ScaleUp(I& i0, int a0) : i(i0), a(a0) {
00141     if (i()) {
00142       cur = a * i.min();
00143       end = a * i.max();
00144     } else {
00145       cur = 1;
00146       end = 0;
00147     }
00148   }
00149 
00150   template<class Val, class UnsVal, class I>
00151   forceinline void
00152   ScaleUp<Val,UnsVal,I>::operator ++(void) {
00153     if (a == 1) {
00154       ++i;
00155     } else {
00156       cur += a;
00157       if (cur > end) {
00158         ++i;
00159         if (i()) {
00160           cur = a * i.min();
00161           end = a * i.max();
00162         }
00163       }
00164     }
00165   }
00166   template<class Val, class UnsVal, class I>
00167   forceinline bool
00168   ScaleUp<Val,UnsVal,I>::operator ()(void) const {
00169     return (a == 1) ? i() : (cur <= end);
00170   }
00171 
00172   template<class Val, class UnsVal, class I>
00173   forceinline Val
00174   ScaleUp<Val,UnsVal,I>::min(void) const {
00175     return (a == 1) ? static_cast<Val>(i.min()) : cur;
00176   }
00177   template<class Val, class UnsVal, class I>
00178   forceinline Val
00179   ScaleUp<Val,UnsVal,I>::max(void) const {
00180     return (a == 1) ? static_cast<Val>(i.max()) : cur;
00181   }
00182   template<class Val, class UnsVal, class I>
00183   forceinline UnsVal
00184   ScaleUp<Val,UnsVal,I>::width(void) const {
00185     return (a == 1) ?
00186       static_cast<UnsVal>(i.width()) :
00187       static_cast<UnsVal>(1);
00188   }
00189 
00190 
00191 
00192   template<class I>
00193   forceinline void
00194   ScaleDown<I>::operator ++(void) {
00195     finish();
00196     while ((mi > ma) && i()) {
00197       mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
00198       ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
00199       ++i;
00200     }
00201     while (i()) {
00202       int n_mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
00203       if (n_mi-ma > 1)
00204         break;
00205       int n_ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
00206       if (n_mi <= n_ma) {
00207         ma = n_ma;
00208       }
00209       ++i;
00210     }
00211   }
00212 
00213   template<class I>
00214   forceinline
00215   ScaleDown<I>::ScaleDown(void) {}
00216 
00217   template<class I>
00218   inline void
00219   ScaleDown<I>::init(I& i0, int a0) {
00220     i = i0; a = a0;
00221     operator ++();
00222   }
00223 
00224   template<class I>
00225   inline
00226   ScaleDown<I>::ScaleDown(I& i0, int a0) : i(i0), a(a0) {
00227     i = i0; a = a0;
00228     operator ++();
00229   }
00230 
00231 }}}
00232 
00233 // STATISTICS: iter-any
00234