genericiterator.hh

Go to the documentation of this file.
00001 // $Id: genericiterator.hh 4914 2007-04-20 12:36:05Z mblatt $
00002 #ifndef DUNE_GENERICITERATOR_HH
00003 #define DUNE_GENERICITERATOR_HH
00004 
00005 #include <dune/common/iteratorfacades.hh>
00006 #include <cassert>
00007 
00008 namespace Dune {
00009 
00088 template<class C, class T, class D = std::ptrdiff_t>
00089 class GenericIterator : 
00090 public Dune::RandomAccessIteratorFacade<GenericIterator<C,T>,T, T&, D>
00091 {
00092   friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type >;
00093   friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type >;
00094 
00095 public:
00096 
00105   typedef C Container;
00106   
00112   typedef T Value;
00113   
00117   typedef D DifferenceType;
00118   
00119   // Constructors needed by the base iterators.
00120   GenericIterator(): container_(0), position_(0)
00121   {}
00122 
00130   GenericIterator(Container& cont, DifferenceType pos)
00131     : container_(&cont), position_(pos)
00132   {} 
00133 
00141   GenericIterator(const GenericIterator<typename remove_const<Container>::type, typename remove_const<T>::type,D >& other): container_(other.container_), position_(other.position_)
00142   {}
00143 
00153   GenericIterator(const GenericIterator<const typename remove_const<Container>::type, const typename remove_const<T>::type, D >& other): container_(other.container_), position_(other.position_)
00154   {}
00155 
00156   // Methods needed by the forward iterator
00157   bool equals(const GenericIterator<typename remove_const<Container>::type,typename remove_const<T>::type,D>& other) const
00158   {
00159     return position_ == other.position_ && container_ == other.container_;
00160   }
00161 
00162   
00163   bool equals(const GenericIterator<const typename remove_const<Container>::type,const typename remove_const<T>::type,D>& other) const
00164   {
00165     return position_ == other.position_ && container_ == other.container_;
00166   }
00167   
00168   T& dereference() const{
00169     return container_->operator[](position_);
00170   }
00171 
00172   void increment(){
00173     ++position_;
00174   }
00175  
00176   // Additional function needed by BidirectionalIterator
00177   void decrement(){
00178     --position_;
00179   }
00180 
00181   // Additional function needed by RandomAccessIterator
00182   T& elementAt(DifferenceType i)const{
00183     return container_->operator[](position_+i);
00184   }
00185 
00186   void advance(DifferenceType n){
00187     position_=position_+n;
00188   }
00189 
00190   std::ptrdiff_t distanceTo(GenericIterator<const typename remove_const<Container>::type,const typename remove_const<T>::type,D> other)const
00191   {
00192     assert(other.container_==container_);
00193     return other.position_ - position_;
00194   }
00195 
00196   std::ptrdiff_t distanceTo(GenericIterator<typename remove_const<Container>::type, typename remove_const<T>::type,D> other)const
00197   {
00198     assert(other.container_==container_);
00199     return other.position_ - position_;
00200   }
00201 private:
00202   Container *container_;
00203   DifferenceType position_;
00204 };
00205 
00208 } // end namespace Dune
00209 
00210 #endif

Generated on 6 Nov 2008 with Doxygen (ver 1.5.6) [logfile].