Dune Core Modules (2.9.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// SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_GENERICITERATOR_HH
6#define DUNE_GENERICITERATOR_HH
7
9#include <cassert>
10
11namespace Dune {
12
85 template<class R>
87 {
88 typedef const R type;
89 };
90
91 template<class R>
92 struct const_reference<const R>
93 {
94 typedef const R type;
95 };
96
97 template<class R>
98 struct const_reference<R&>
99 {
100 typedef const R& type;
101 };
102
103 template<class R>
104 struct const_reference<const R&>
105 {
106 typedef const R& type;
107 };
108
114 template<class R>
116 {
117 typedef R type;
118 };
119
120 template<class R>
121 struct mutable_reference<const R>
122 {
123 typedef R type;
124 };
125
126 template<class R>
127 struct mutable_reference<R&>
128 {
129 typedef R& type;
130 };
131
132 template<class R>
133 struct mutable_reference<const R&>
134 {
135 typedef R& type;
136 };
137
149 template<class C, class T, class R=T&, class D = std::ptrdiff_t,
150 template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
152 public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
153 {
154 friend class GenericIterator<typename std::remove_const<C>::type, typename std::remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
155 friend class GenericIterator<const typename std::remove_const<C>::type, const typename std::remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
156
157 typedef GenericIterator<typename std::remove_const<C>::type, typename std::remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
158 typedef GenericIterator<const typename std::remove_const<C>::type, const typename std::remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
159
160 public:
161
170 typedef C Container;
171
177 typedef T Value;
178
182 typedef D DifferenceType;
183
187 typedef R Reference;
188
189 // Constructors needed by the base iterators
190 GenericIterator() : container_(0), position_(0)
191 {}
192
201 : container_(&cont), position_(pos)
202 {}
203
211 GenericIterator(const MutableIterator& other) : container_(other.container_), position_(other.position_)
212 {}
213
223 GenericIterator(const ConstIterator& other) : container_(other.container_), position_(other.position_)
224 {}
225
226 // Methods needed by the forward iterator
227 bool equals(const MutableIterator & other) const
228 {
229 return position_ == other.position_ && container_ == other.container_;
230 }
231
232 bool equals(const ConstIterator & other) const
233 {
234 return position_ == other.position_ && container_ == other.container_;
235 }
236
237 Reference dereference() const {
238 return container_->operator[](position_);
239 }
240
241 void increment(){
242 ++position_;
243 }
244
245 // Additional function needed by BidirectionalIterator
246 void decrement(){
247 --position_;
248 }
249
250 // Additional function needed by RandomAccessIterator
251 Reference elementAt(DifferenceType i) const {
252 return container_->operator[](position_+i);
253 }
254
255 void advance(DifferenceType n){
256 position_=position_+n;
257 }
258
259 DifferenceType distanceTo(const MutableIterator& other) const
260 {
261 assert(other.container_==container_);
262 return other.position_ - position_;
263 }
264
265 DifferenceType distanceTo(const ConstIterator& other) const
266 {
267 assert(other.container_==container_);
268 return other.position_ - position_;
269 }
270
271 private:
272 Container *container_;
273 DifferenceType position_;
274 };
275
278} // end namespace Dune
279
280#endif
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:153
GenericIterator(const MutableIterator &other)
Copy constructor.
Definition: genericiterator.hh:211
GenericIterator(const ConstIterator &other)
Copy constructor.
Definition: genericiterator.hh:223
GenericIterator(Container &cont, DifferenceType pos)
Constructor.
Definition: genericiterator.hh:200
D DifferenceType
The type of the difference between two positions.
Definition: genericiterator.hh:182
R Reference
The type of the reference to the values accessed.
Definition: genericiterator.hh:187
C Container
The type of container we are an iterator for.
Definition: genericiterator.hh:170
T Value
The value type of the iterator.
Definition: genericiterator.hh:177
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:87
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:116
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)