Dune Core Modules (2.3.1)

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// $Id$
4#ifndef DUNE_GENERICITERATOR_HH
5#define DUNE_GENERICITERATOR_HH
6
8#include <cassert>
9
10namespace Dune {
11
84 template<class R>
86 {
87 typedef const R type;
88 };
89
90 template<class R>
91 struct const_reference<const R>
92 {
93 typedef const R type;
94 };
95
96 template<class R>
97 struct const_reference<R&>
98 {
99 typedef const R& type;
100 };
101
102 template<class R>
103 struct const_reference<const R&>
104 {
105 typedef const R& type;
106 };
107
113 template<class R>
115 {
116 typedef R type;
117 };
118
119 template<class R>
120 struct mutable_reference<const R>
121 {
122 typedef R type;
123 };
124
125 template<class R>
126 struct mutable_reference<R&>
127 {
128 typedef R& type;
129 };
130
131 template<class R>
132 struct mutable_reference<const R&>
133 {
134 typedef R& type;
135 };
136
148 template<class C, class T, class R=T&, class D = std::ptrdiff_t,
149 template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
151 public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
152 {
153 friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
154 friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
155
156 typedef GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
157 typedef GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
158
159 public:
160
169 typedef C Container;
170
176 typedef T Value;
177
181 typedef D DifferenceType;
182
186 typedef R Reference;
187
188 // Constructors needed by the base iterators
189 GenericIterator() : container_(0), position_(0)
190 {}
191
200 : container_(&cont), position_(pos)
201 {}
202
210 GenericIterator(const MutableIterator& other) : container_(other.container_), position_(other.position_)
211 {}
212
222 GenericIterator(const ConstIterator& other) : container_(other.container_), position_(other.position_)
223 {}
224
225 // Methods needed by the forward iterator
226 bool equals(const MutableIterator & other) const
227 {
228 return position_ == other.position_ && container_ == other.container_;
229 }
230
231 bool equals(const ConstIterator & other) const
232 {
233 return position_ == other.position_ && container_ == other.container_;
234 }
235
236 Reference dereference() const {
237 return container_->operator[](position_);
238 }
239
240 void increment(){
241 ++position_;
242 }
243
244 // Additional function needed by BidirectionalIterator
245 void decrement(){
246 --position_;
247 }
248
249 // Additional function needed by RandomAccessIterator
250 Reference elementAt(DifferenceType i) const {
251 return container_->operator[](position_+i);
252 }
253
254 void advance(DifferenceType n){
255 position_=position_+n;
256 }
257
258 DifferenceType distanceTo(const MutableIterator& other) const
259 {
260 assert(other.container_==container_);
261 return other.position_ - position_;
262 }
263
264 DifferenceType distanceTo(const ConstIterator& other) const
265 {
266 assert(other.container_==container_);
267 return other.position_ - position_;
268 }
269
270 private:
271 Container *container_;
272 DifferenceType position_;
273 };
274
277} // end namespace Dune
278
279#endif
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:152
GenericIterator(const MutableIterator &other)
Copy constructor.
Definition: genericiterator.hh:210
GenericIterator(const ConstIterator &other)
Copy constructor.
Definition: genericiterator.hh:222
GenericIterator(Container &cont, DifferenceType pos)
Constructor.
Definition: genericiterator.hh:199
D DifferenceType
The type of the difference between two positions.
Definition: genericiterator.hh:181
R Reference
The type of the reference to the values accessed.
Definition: genericiterator.hh:186
C Container
The type of container we are an iterator for.
Definition: genericiterator.hh:169
T Value
The value type of the iterator.
Definition: genericiterator.hh:176
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignment.hh:14
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:86
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:115
Removes a const qualifier while preserving others.
Definition: typetraits.hh:176
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)