vtf-logo

IntIterator.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__IntIterator_h__)
00009 #define __IntIterator_h__
00010 
00011 // If we are debugging the whole ads package.
00012 #if defined(DEBUG_ads) && !defined(DEBUG_IntIterator)
00013 #define DEBUG_IntIterator
00014 #endif
00015 
00016 #include "../defs.h"
00017 
00018 #include <iterator>
00019 
00020 BEGIN_NAMESPACE_ADS
00021 
00023 
00052 template<typename T = int>
00053 class IntIterator :
00054   public std::iterator<std::random_access_iterator_tag,
00055                        T,
00056                        std::ptrdiff_t,
00057                        const T*,
00058                        const T&> {
00059   //
00060   // Private types.
00061   //
00062 
00063 private:
00064   
00065   typedef std::iterator<std::random_access_iterator_tag,
00066                         T,
00067                         std::ptrdiff_t,
00068                         const T*,
00069                         const T&> Base;
00070 
00071   //
00072   // Public types.
00073   //
00074 
00075 public:
00076 
00078   typedef typename Base::iterator_category iterator_category;
00080   typedef typename Base::value_type value_type;
00082   typedef typename Base::difference_type difference_type;
00084   typedef typename Base::pointer pointer;
00086   typedef typename Base::reference reference;
00087 
00088   //
00089   // Member data.
00090   //
00091 
00092 private:
00093 
00094   value_type _value;
00095 
00096 public:
00097 
00098   //--------------------------------------------------------------------------
00100 
00101 
00102   // Default constructor.  The value is initialized to zero.
00103   IntIterator() :
00104     _value(0)
00105   {}
00106 
00108   IntIterator(const value_type x) :
00109     _value(x)
00110   {}
00111 
00113   IntIterator(const IntIterator& x) :
00114     _value(x._value)
00115   {}
00116 
00118   IntIterator&
00119   operator=(const IntIterator& other) {
00120     if (&other != this) {
00121       _value = other._value;
00122     }
00123     return *this;
00124   }
00125 
00127   ~IntIterator()
00128   {}
00129 
00131   //--------------------------------------------------------------------------
00133 
00134 
00136   reference
00137   operator*() const {
00138     // Return a reference to the value.
00139     return _value;
00140   }
00141 
00143   pointer
00144   operator->() const {
00145     // Return a pointer to the value.
00146     return &_value;
00147   }
00148   
00150   IntIterator&
00151   operator++() { 
00152     ++_value; 
00153     return *this; 
00154   }
00155       
00157 
00161   IntIterator
00162   operator++(int) {
00163     IntIterator x(*this); 
00164     ++*this;
00165     return x;
00166   }
00167       
00169   //--------------------------------------------------------------------------
00171 
00172 
00174   IntIterator&
00175   operator--() { 
00176     --_value; 
00177     return *this; 
00178   }
00179       
00181 
00185   IntIterator
00186   operator--(int) {
00187     IntIterator x(*this); 
00188     --*this;
00189     return x;
00190   }
00191       
00193   //--------------------------------------------------------------------------
00195 
00196 
00198   value_type
00199   operator[](const difference_type n) { 
00200     // Return the value offset by n.
00201     return value_type(_value + n);
00202   }
00203   
00205   IntIterator&
00206   operator+=(const difference_type n) { 
00207     _value += value_type(n);
00208     return *this; 
00209   }
00210 
00212 
00215   IntIterator
00216   operator+(const difference_type n) const { 
00217     IntIterator x(*this); 
00218     x += n;
00219     return x;
00220   }
00221       
00223   IntIterator&
00224   operator-=(const difference_type n) { 
00225     _value -= value_type(n);
00226     return *this; 
00227   }
00228 
00230 
00233   IntIterator
00234   operator-(const difference_type n) const { 
00235     IntIterator x(*this); 
00236     x -= n;
00237     return x;
00238   }
00239 
00241   value_type
00242   base() const {
00243     return _value;
00244   }
00245 
00247 };
00248 
00249 //
00250 // Convenience functions.
00251 //
00252 
00254 
00255 template<typename T>
00256 IntIterator<T>
00257 constructIntIterator(const T x) {
00258   IntIterator<T> i(x);
00259   return i;
00260 }
00261 
00262 //
00263 // Forward iterator requirements
00264 //
00265 
00267 
00268 template<typename T>
00269 inline 
00270 bool
00271 operator==(const IntIterator<T>& x, const IntIterator<T>& y) { 
00272   return x.base() == y.base(); 
00273 }
00274 
00276 
00277 template<typename T>
00278 inline 
00279 bool
00280 operator!=(const IntIterator<T>& x, const IntIterator<T>& y) { 
00281   return !(x == y);
00282 }
00283 
00284 //
00285 // Random access iterator requirements
00286 //
00287 
00289 
00290 template<typename T>
00291 inline 
00292 bool 
00293 operator<(const IntIterator<T>& x, const IntIterator<T>& y) { 
00294   return x.base() < y.base(); 
00295 }
00296 
00298 
00299 template<typename T>
00300 inline 
00301 bool
00302 operator>(const IntIterator<T>& x, const IntIterator<T>& y) { 
00303   return x.base() > y.base(); 
00304 }
00305 
00307 
00308 template<typename T>
00309 inline 
00310 bool
00311 operator<=(const IntIterator<T>& x, const IntIterator<T>& y) { 
00312   return x.base() <= y.base(); 
00313 }
00314 
00316 template<typename T>
00317 inline bool
00318 operator>=(const IntIterator<T>& x, const IntIterator<T>& y) { 
00319   return x.base() >= y.base(); 
00320 }
00321 
00323 
00324 template<typename T>
00325 inline 
00326 typename IntIterator<T>::difference_type
00327 operator-(const IntIterator<T>& x, const IntIterator<T>& y) { 
00328   return x.base() - y.base(); 
00329 }
00330 
00332 
00333 template<typename T>
00334 inline 
00335 IntIterator<T>
00336 operator+(typename IntIterator<T>::difference_type n, 
00337           const IntIterator<T>& i) { 
00338   IntIterator<T> x(i); 
00339   x += n;
00340   return x;
00341 }
00342 
00343 END_NAMESPACE_ADS
00344 
00345 #endif

Generated on Fri Aug 24 12:55:25 2007 for Algorithms and Data Structures Package by  doxygen 1.4.7