vtf-logo

ArrayIndexIterator.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__ads_ArrayIndexIterator_h__)
00009 #define __ads_ArrayIndexIterator_h__
00010 
00011 // If we are debugging the whole ads package.
00012 #if defined(DEBUG_ads) && !defined(DEBUG_ads_ArrayIndexIterator)
00013 #define DEBUG_ads_ArrayIndexIterator
00014 #endif
00015 
00016 #include "ArrayIndexingBase.h"
00017 
00018 #include <iterator>
00019 
00020 BEGIN_NAMESPACE_ADS
00021 
00022 
00024 template<int N>
00025 class ArrayIndexIterator :
00026   public std::iterator<std::random_access_iterator_tag,
00027                        typename ArrayIndexingBase<N>::index_type,
00028                        std::ptrdiff_t,
00029                        const typename ArrayIndexingBase<N>::index_type*,
00030                        const typename ArrayIndexingBase<N>::index_type&> {
00031   //
00032   // Private types.
00033   //
00034 
00035 private:
00036   
00037   typedef 
00038   std::iterator<std::random_access_iterator_tag,
00039                 typename ArrayIndexingBase<N>::index_type,
00040                 std::ptrdiff_t,
00041                 const typename ArrayIndexingBase<N>::index_type*,
00042                 const typename ArrayIndexingBase<N>::index_type&> 
00043   Base;
00044   typedef ArrayIndexingBase<N> ArrayType;
00045 
00046   //
00047   // Public types.
00048   //
00049 
00050 public:
00051 
00053   typedef typename Base::iterator_category iterator_category;
00055   typedef typename Base::value_type value_type;
00057   typedef typename Base::difference_type difference_type;
00059   typedef typename Base::pointer pointer;
00061   typedef typename Base::reference reference;
00062 
00063   //
00064   // Member data.
00065   //
00066 
00067 private:
00068 
00069   int _container_index;
00070   mutable value_type _array_index;
00071   const ArrayType& _array;
00072 
00073   //
00074   // Not implemented.
00075   //
00076 
00077 private:
00078 
00079   // Default constructor not implemented.
00080   ArrayIndexIterator();
00081 
00082 public:
00083 
00084   //--------------------------------------------------------------------------
00086 
00087 
00089   ArrayIndexIterator(const ArrayType& x) :
00090     Base(),
00091     _container_index(0),
00092     _array_index(),
00093     _array(x)
00094   {}
00095 
00097   ArrayIndexIterator(const ArrayIndexIterator& x) :
00098     Base(),
00099     _container_index(x._container_index),
00100     _array_index(),
00101     _array(x._array)
00102   {}    
00103 
00105   ArrayIndexIterator&
00106   operator=(const ArrayIndexIterator& x)
00107   {
00108 #ifdef DEBUG_ArrayIndexing
00109     assert(_array == x._array);
00110 #endif
00111     if (&x != this) {
00112       _container_index = x._container_index;
00113     }
00114     return *this;
00115   }
00116 
00118   ~ArrayIndexIterator()
00119   {}
00120 
00122   //--------------------------------------------------------------------------
00124 
00125 
00127   reference
00128   operator*() const 
00129   {
00130     // Update the array index.
00131     _array.index_to_indices(_container_index, _array_index);
00132     // Then return a constant reference to it.
00133     return _array_index;
00134   }
00135 
00137   pointer
00138   operator->() const 
00139   {
00140     // Update the array index.
00141     _array.index_to_indices(_container_index, _array_index);
00142     // Then return a constant pointer to it.
00143     return &_array_index;
00144   }
00145   
00147   ArrayIndexIterator&
00148   operator++() 
00149   { 
00150     ++_container_index; 
00151     return *this; 
00152   }
00153       
00155 
00159   ArrayIndexIterator
00160   operator++(int) 
00161   {
00162     ArrayIndexIterator x(*this); 
00163     ++*this;
00164     return x;
00165   }
00166       
00168   //--------------------------------------------------------------------------
00170 
00171 
00173   ArrayIndexIterator&
00174   operator--() 
00175   { 
00176     --_container_index; 
00177     return *this; 
00178   }
00179       
00181 
00185   ArrayIndexIterator
00186   operator--(int) 
00187   {
00188     ArrayIndexIterator x(*this); 
00189     --*this;
00190     return x;
00191   }
00192       
00194   //--------------------------------------------------------------------------
00196 
00197 
00199   reference
00200   operator[](const difference_type n) const
00201   { 
00202     // Update the array index with the requested offset value.
00203     _array.index_to_indices(int(_container_index + n), _array_index);
00204     // Then return a constant reference to it.
00205     return _array_index;
00206   }
00207   
00209   ArrayIndexIterator&
00210   operator+=(const difference_type n)
00211   { 
00212     _container_index += int(n);
00213     return *this; 
00214   }
00215 
00217 
00220   ArrayIndexIterator
00221   operator+(const difference_type n) const
00222   { 
00223     ArrayIndexIterator x(*this); 
00224     x += n;
00225     return x;
00226   }
00227       
00229   ArrayIndexIterator&
00230   operator-=(const difference_type n)
00231   { 
00232     _container_index -= int(n);
00233     return *this; 
00234   }
00235 
00237 
00240   ArrayIndexIterator
00241   operator-(const difference_type n) const
00242   { 
00243     ArrayIndexIterator x(*this); 
00244     x -= n;
00245     return x;
00246   }
00247 
00249   int
00250   base() const 
00251   {
00252     return _container_index;
00253   }
00254 
00256 };
00257 
00258 
00259 //
00260 // Forward iterator requirements
00261 //
00262 
00264 template<int N>
00265 inline 
00266 bool
00267 operator==(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00268   return x.base() == y.base(); 
00269 }
00270 
00272 template<int N>
00273 inline 
00274 bool
00275 operator!=(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00276   return !(x == y);
00277 }
00278 
00279 //
00280 // Random access iterator requirements
00281 //
00282 
00284 template<int N>
00285 inline 
00286 bool 
00287 operator<(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00288   return x.base() < y.base(); 
00289 }
00290 
00292 template<int N>
00293 inline 
00294 bool
00295 operator>(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00296   return x.base() > y.base(); 
00297 }
00298 
00300 template<int N>
00301 inline 
00302 bool
00303 operator<=(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00304   return x.base() <= y.base(); 
00305 }
00306 
00308 template<int N>
00309 inline bool
00310 operator>=(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00311   return x.base() >= y.base(); 
00312 }
00313 
00315 template<int N>
00316 inline 
00317 typename ArrayIndexIterator<N>::difference_type
00318 operator-(const ArrayIndexIterator<N>& x, const ArrayIndexIterator<N>& y) { 
00319   return x.base() - y.base(); 
00320 }
00321 
00323 template<int N>
00324 inline 
00325 ArrayIndexIterator<N>
00326 operator+(typename ArrayIndexIterator<N>::difference_type n,
00327           const ArrayIndexIterator<N>& i) { 
00328   ArrayIndexIterator<N> x(i); 
00329   x += n;
00330   return x;
00331 }
00332 
00333 
00334 END_NAMESPACE_ADS
00335 
00336 #endif

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