Dune Core Modules (2.6.0)

genericiterator.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GENERICITERATOR_HH
4 #define DUNE_GENERICITERATOR_HH
5 
7 #include <cassert>
8 
9 namespace Dune {
10 
83  template<class R>
85  {
86  typedef const R type;
87  };
88 
89  template<class R>
90  struct const_reference<const R>
91  {
92  typedef const R type;
93  };
94 
95  template<class R>
96  struct const_reference<R&>
97  {
98  typedef const R& type;
99  };
100 
101  template<class R>
102  struct const_reference<const R&>
103  {
104  typedef const R& type;
105  };
106 
112  template<class R>
114  {
115  typedef R type;
116  };
117 
118  template<class R>
119  struct mutable_reference<const R>
120  {
121  typedef R type;
122  };
123 
124  template<class R>
125  struct mutable_reference<R&>
126  {
127  typedef R& type;
128  };
129 
130  template<class R>
131  struct mutable_reference<const R&>
132  {
133  typedef R& type;
134  };
135 
147  template<class C, class T, class R=T&, class D = std::ptrdiff_t,
148  template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
150  public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
151  {
152  friend class GenericIterator<typename std::remove_const<C>::type, typename std::remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
153  friend class GenericIterator<const typename std::remove_const<C>::type, const typename std::remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
154 
155  typedef GenericIterator<typename std::remove_const<C>::type, typename std::remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
156  typedef GenericIterator<const typename std::remove_const<C>::type, const typename std::remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
157 
158  public:
159 
168  typedef C Container;
169 
175  typedef T Value;
176 
180  typedef D DifferenceType;
181 
185  typedef R Reference;
186 
187  // Constructors needed by the base iterators
188  GenericIterator() : container_(0), position_(0)
189  {}
190 
199  : container_(&cont), position_(pos)
200  {}
201 
209  GenericIterator(const MutableIterator& other) : container_(other.container_), position_(other.position_)
210  {}
211 
221  GenericIterator(const ConstIterator& other) : container_(other.container_), position_(other.position_)
222  {}
223 
224  // Methods needed by the forward iterator
225  bool equals(const MutableIterator & other) const
226  {
227  return position_ == other.position_ && container_ == other.container_;
228  }
229 
230  bool equals(const ConstIterator & other) const
231  {
232  return position_ == other.position_ && container_ == other.container_;
233  }
234 
235  Reference dereference() const {
236  return container_->operator[](position_);
237  }
238 
239  void increment(){
240  ++position_;
241  }
242 
243  // Additional function needed by BidirectionalIterator
244  void decrement(){
245  --position_;
246  }
247 
248  // Additional function needed by RandomAccessIterator
249  Reference elementAt(DifferenceType i) const {
250  return container_->operator[](position_+i);
251  }
252 
253  void advance(DifferenceType n){
254  position_=position_+n;
255  }
256 
257  DifferenceType distanceTo(const MutableIterator& other) const
258  {
259  assert(other.container_==container_);
260  return other.position_ - position_;
261  }
262 
263  DifferenceType distanceTo(const ConstIterator& other) const
264  {
265  assert(other.container_==container_);
266  return other.position_ - position_;
267  }
268 
269  private:
270  Container *container_;
271  DifferenceType position_;
272  };
273 
276 } // end namespace Dune
277 
278 #endif
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:151
GenericIterator(const MutableIterator &other)
Copy constructor.
Definition: genericiterator.hh:209
GenericIterator(const ConstIterator &other)
Copy constructor.
Definition: genericiterator.hh:221
GenericIterator(Container &cont, DifferenceType pos)
Constructor.
Definition: genericiterator.hh:198
D DifferenceType
The type of the difference between two positions.
Definition: genericiterator.hh:180
R Reference
The type of the reference to the values accessed.
Definition: genericiterator.hh:185
C Container
The type of container we are an iterator for.
Definition: genericiterator.hh:168
T Value
The value type of the iterator.
Definition: genericiterator.hh:175
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:10
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:114
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 1, 22:29, 2024)