00001
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 Dune::RemoveConst<C>::Type, typename Dune::RemoveConst<T>::Type >;
00093 friend class GenericIterator<const typename Dune::RemoveConst<C>::Type, const typename Dune::RemoveConst<T>::Type >;
00094
00095 public:
00096
00105 typedef C Container;
00106
00112 typedef T Value;
00113
00117 typedef D DifferenceType;
00118
00119
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 Dune::RemoveConst<Container>::Type, typename Dune::RemoveConst<T>::Type,D >& other): container_(other.container_), position_(other.position_)
00142 {}
00143
00153 GenericIterator(const GenericIterator<const typename Dune::RemoveConst<Container>::Type, const typename Dune::RemoveConst<T>::Type,D >& other): container_(other.container_), position_(other.position_)
00154 {}
00155
00156
00157 bool equals(const GenericIterator<typename Dune::RemoveConst<Container>::Type,typename Dune::RemoveConst<T>::Type,D>& other) const
00158 {
00159 return position_ == other.position_ && container_ == other.container_;
00160 }
00161
00162
00163 bool equals(const GenericIterator<const typename Dune::RemoveConst<Container>::Type,const typename Dune::RemoveConst<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
00177 void decrement(){
00178 --position_;
00179 }
00180
00181
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 Dune::RemoveConst<Container>::Type,const typename Dune::RemoveConst<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 Dune::RemoveConst<Container>::Type, typename Dune::RemoveConst<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 }
00209
00210 #endif