Dune Core Modules (2.9.1)

iteratorfacades.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_ITERATORFACADES_HH
6#define DUNE_ITERATORFACADES_HH
7
8#include <iterator>
9#include <type_traits>
10
11#include "typetraits.hh"
12
13namespace Dune
14{
139 template<class T, class V, class R = V&, class D = std::ptrdiff_t>
141 {
142
143 public:
144 /* type aliases required by C++ for iterators */
145 using iterator_category = std::forward_iterator_tag;
146 using value_type = typename std::remove_const<V>::type;
147 using difference_type = D;
148 using pointer = V*;
149 using reference = R;
150
175 typedef T DerivedType;
176
180 typedef V Value;
181
185 typedef V* Pointer;
186
190 typedef D DifferenceType;
191
195 typedef R Reference;
196
199 {
200 return static_cast<DerivedType const*>(this)->dereference();
201 }
202
203 Pointer operator->() const
204 {
205 return &(static_cast<const DerivedType *>(this)->dereference());
206 }
207
210 {
211 static_cast<DerivedType *>(this)->increment();
212 return *static_cast<DerivedType *>(this);
213 }
214
217 {
218 DerivedType tmp(static_cast<DerivedType const&>(*this));
219 this->operator++();
220 return tmp;
221 }
222 };
223
234 template<class T1, class V1, class R1, class D,
235 class T2, class V2, class R2>
236 inline typename EnableIfInterOperable<T1,T2,bool>::type
239 {
240 if(std::is_convertible<T2,T1>::value)
241 return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
242 else
243 return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
244 }
245
256 template<class T1, class V1, class R1, class D,
257 class T2, class V2, class R2>
258 inline typename EnableIfInterOperable<T1,T2,bool>::type
261 {
262 if(std::is_convertible<T2,T1>::value)
263 return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
264 else
265 return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
266 }
267
272 template<class T, class V, class R = V&, class D = std::ptrdiff_t>
274 {
275
276 public:
277 /* type aliases required by C++ for iterators */
278 using iterator_category = std::bidirectional_iterator_tag;
279 using value_type = typename std::remove_const<V>::type;
280 using difference_type = D;
281 using pointer = V*;
282 using reference = R;
283
309 typedef T DerivedType;
310
314 typedef V Value;
315
319 typedef V* Pointer;
320
324 typedef D DifferenceType;
325
329 typedef R Reference;
330
333 {
334 return static_cast<DerivedType const*>(this)->dereference();
335 }
336
337 Pointer operator->() const
338 {
339 return &(static_cast<const DerivedType *>(this)->dereference());
340 }
341
344 {
345 static_cast<DerivedType *>(this)->increment();
346 return *static_cast<DerivedType *>(this);
347 }
348
351 {
352 DerivedType tmp(static_cast<DerivedType const&>(*this));
353 this->operator++();
354 return tmp;
355 }
356
357
360 {
361 static_cast<DerivedType *>(this)->decrement();
362 return *static_cast<DerivedType *>(this);
363 }
364
367 {
368 DerivedType tmp(static_cast<DerivedType const&>(*this));
369 this->operator--();
370 return tmp;
371 }
372 };
373
381 template<class T1, class V1, class R1, class D,
382 class T2, class V2, class R2>
383 inline typename std::enable_if<std::is_convertible<T2,T1>::value,bool>::type
386 {
387 return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
388 }
389
398 template<class T1, class V1, class R1, class D,
399 class T2, class V2, class R2>
400 inline
401 typename std::enable_if<std::is_convertible<T1,T2>::value && !std::is_convertible<T2,T1>::value,
402 bool>::type
405 {
406 return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
407 }
408
419 template<class T1, class V1, class R1, class D,
420 class T2, class V2, class R2>
421 inline typename EnableIfInterOperable<T1,T2,bool>::type
424 {
425 return !(lhs == rhs);
426 }
427
432 template<class T, class V, class R = V&, class D = std::ptrdiff_t>
434 {
435
436 public:
437 /* type aliases required by C++ for iterators */
438 using iterator_category = std::random_access_iterator_tag;
439 using value_type = typename std::remove_const<V>::type;
440 using difference_type = D;
441 using pointer = V*;
442 using reference = R;
443
477 typedef T DerivedType;
478
482 typedef V Value;
483
487 typedef V* Pointer;
488
492 typedef D DifferenceType;
493
497 typedef R Reference;
498
501 {
502 return static_cast<DerivedType const*>(this)->dereference();
503 }
504
505 Pointer operator->() const
506 {
507 return &(static_cast<const DerivedType *>(this)->dereference());
508 }
509
516 {
517 return static_cast<const DerivedType *>(this)->elementAt(n);
518 }
519
522 {
523 static_cast<DerivedType *>(this)->increment();
524 return *static_cast<DerivedType *>(this);
525 }
526
529 {
530 DerivedType tmp(static_cast<DerivedType const&>(*this));
531 this->operator++();
532 return tmp;
533 }
534
535 DerivedType& operator+=(DifferenceType n)
536 {
537 static_cast<DerivedType *>(this)->advance(n);
538 return *static_cast<DerivedType *>(this);
539 }
540
541 DerivedType operator+(DifferenceType n) const
542 {
543 DerivedType tmp(static_cast<DerivedType const&>(*this));
544 tmp.advance(n);
545 return tmp;
546 }
547
548
551 {
552 static_cast<DerivedType *>(this)->decrement();
553 return *static_cast<DerivedType *>(this);
554 }
555
558 {
559 DerivedType tmp(static_cast<DerivedType const&>(*this));
560 this->operator--();
561 return tmp;
562 }
563
564 DerivedType& operator-=(DifferenceType n)
565 {
566 static_cast<DerivedType *>(this)->advance(-n);
567 return *static_cast<DerivedType *>(this);
568 }
569
570 DerivedType operator-(DifferenceType n) const
571 {
572 DerivedType tmp(static_cast<DerivedType const&>(*this));
573 tmp.advance(-n);
574 return tmp;
575 }
576
577
578 };
579
590 template<class T1, class V1, class R1, class D,
591 class T2, class V2, class R2>
592 inline typename EnableIfInterOperable<T1,T2,bool>::type
595 {
596 if(std::is_convertible<T2,T1>::value)
597 return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
598 else
599 return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
600 }
601
612 template<class T1, class V1, class R1, class D,
613 class T2, class V2, class R2>
614 inline typename EnableIfInterOperable<T1,T2,bool>::type
617 {
618 if(std::is_convertible<T2,T1>::value)
619 return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
620 else
621 return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
622 }
623
634 template<class T1, class V1, class R1, class D,
635 class T2, class V2, class R2>
639 {
640 if(std::is_convertible<T2,T1>::value)
641 return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>0;
642 else
643 return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<0;
644 }
645
646
657 template<class T1, class V1, class R1, class D,
658 class T2, class V2, class R2>
662 {
663 if(std::is_convertible<T2,T1>::value)
664 return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>=0;
665 else
666 return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<=0;
667 }
668
669
680 template<class T1, class V1, class R1, class D,
681 class T2, class V2, class R2>
682 inline typename EnableIfInterOperable<T1,T2,bool>::type
685 {
686 if(std::is_convertible<T2,T1>::value)
687 return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<0;
688 else
689 return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>0;
690 }
691
702 template<class T1, class V1, class R1, class D,
703 class T2, class V2, class R2>
704 inline typename EnableIfInterOperable<T1,T2,bool>::type
707 {
708 if(std::is_convertible<T2,T1>::value)
709 return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<=0;
710 else
711 return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>=0;
712 }
713
724 template<class T1, class V1, class R1, class D,
725 class T2, class V2, class R2>
726 inline typename EnableIfInterOperable<T1,T2,D>::type
729 {
730 if(std::is_convertible<T2,T1>::value)
731 return -static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs));
732 else
733 return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs));
734 }
735
737}
738#endif
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:274
Reference operator*() const
Dereferencing operator.
Definition: iteratorfacades.hh:332
T DerivedType
The type of derived iterator.
Definition: iteratorfacades.hh:309
DerivedType & operator--()
Preincrement operator.
Definition: iteratorfacades.hh:359
DerivedType & operator++()
Preincrement operator.
Definition: iteratorfacades.hh:343
V * Pointer
The pointer to the Value.
Definition: iteratorfacades.hh:319
R Reference
The type of the reference to the values accessed.
Definition: iteratorfacades.hh:329
DerivedType operator++(int)
Postincrement operator.
Definition: iteratorfacades.hh:350
D DifferenceType
The type of the difference between two positions.
Definition: iteratorfacades.hh:324
DerivedType operator--(int)
Postincrement operator.
Definition: iteratorfacades.hh:366
V Value
The type of value accessed through the iterator.
Definition: iteratorfacades.hh:314
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:141
DerivedType & operator++()
Preincrement operator.
Definition: iteratorfacades.hh:209
D DifferenceType
The type of the difference between two positions.
Definition: iteratorfacades.hh:190
R Reference
The type of the reference to the values accessed.
Definition: iteratorfacades.hh:195
T DerivedType
The type of derived iterator.
Definition: iteratorfacades.hh:175
V Value
The type of value accessed through the iterator.
Definition: iteratorfacades.hh:180
DerivedType operator++(int)
Postincrement operator.
Definition: iteratorfacades.hh:216
V * Pointer
The pointer to the Value.
Definition: iteratorfacades.hh:185
Reference operator*() const
Dereferencing operator.
Definition: iteratorfacades.hh:198
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:434
DerivedType operator--(int)
Postdecrement operator.
Definition: iteratorfacades.hh:557
Reference operator*() const
Dereferencing operator.
Definition: iteratorfacades.hh:500
DerivedType & operator++()
Preincrement operator.
Definition: iteratorfacades.hh:521
D DifferenceType
The type of the difference between two positions.
Definition: iteratorfacades.hh:492
DerivedType & operator--()
Predecrement operator.
Definition: iteratorfacades.hh:550
Reference operator[](DifferenceType n) const
Get the element n positions from the current one.
Definition: iteratorfacades.hh:515
T DerivedType
The type of derived iterator.
Definition: iteratorfacades.hh:477
V * Pointer
The pointer to the Value.
Definition: iteratorfacades.hh:487
DerivedType operator++(int)
Postincrement operator.
Definition: iteratorfacades.hh:528
V Value
The type of value accessed through the iterator.
Definition: iteratorfacades.hh:482
R Reference
The type of the reference to the values accessed.
Definition: iteratorfacades.hh:497
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:402
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:135
EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:637
EnableIfInterOperable< T1, T2, bool >::type operator>(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:683
EnableIfInterOperable< T1, T2, bool >::type operator<=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:660
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:237
EnableIfInterOperable< T1, T2, bool >::type operator>=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:705
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:259
Dune namespace.
Definition: alignedallocator.hh:13
Enable typedef if two types are interoperable.
Definition: typetraits.hh:81
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)