Dune Core Modules (unstable)

indexediterator.hh
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 © 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_COMMON_INDEXEDITERATOR_HH
6#define DUNE_COMMON_INDEXEDITERATOR_HH
7
8#include <iterator>
9#include <type_traits>
10
12
13namespace Dune
14{
28 template <class Iter>
30 : public Iter
31 {
32 using Traits = std::iterator_traits<Iter>;
33 static_assert(std::is_copy_constructible_v<Iter>);
34 static_assert(std::is_base_of_v<std::forward_iterator_tag, typename Traits::iterator_category>);
35
36 public:
38 using size_type = typename Traits::difference_type;
39
41 template <class I = Iter,
42 std::enable_if_t<std::is_default_constructible_v<I>, bool> = true>
43 constexpr IndexedIterator ()
44 noexcept(std::is_nothrow_default_constructible_v<Iter>)
45 {}
46
49 constexpr IndexedIterator (Iter it, size_type index = 0)
50 noexcept(std::is_nothrow_copy_constructible_v<Iter>)
51 : Iter(it)
52 , index_(index)
53 {}
54
56 [[nodiscard]] constexpr size_type index () const noexcept
57 {
58 return index_;
59 }
60
63 {
64 Iter::operator++();
65 ++index_;
66 return *this;
67 }
68
71 {
72 IndexedIterator tmp(*this);
73 this->operator++();
74 return tmp;
75 }
76
78 template <class I = Iter,
79 decltype(--std::declval<I&>(), bool{}) = true>
80 constexpr IndexedIterator& operator-- ()
81 {
82 Iter::operator--();
83 --index_;
84 return *this;
85 }
86
88 template <class I = Iter,
89 decltype(std::declval<I&>()--, bool{}) = true>
90 constexpr IndexedIterator operator-- (int)
91 {
92 IndexedIterator tmp(*this);
93 this->operator--();
94 return tmp;
95 }
96
98 template <class I = Iter,
99 decltype(std::declval<I&>()+=1, bool{}) = true>
100 constexpr IndexedIterator& operator+= (typename Iter::difference_type n)
101 {
102 Iter::operator+=(n);
103 index_ += n;
104 return *this;
105 }
106
108 template <class I = Iter,
109 decltype(std::declval<I&>()-=1, bool{}) = true>
110 constexpr IndexedIterator& operator-= (typename Iter::difference_type n)
111 {
112 Iter::operator-=(n);
113 index_ -= n;
114 return *this;
115 }
116
117 private:
118 size_type index_ = 0;
119 };
120
121
123 template <class T>
125 : public Dune::IteratorFacade<IndexedIterator<T*>, std::random_access_iterator_tag, T>
126 {
127 using Facade = Dune::IteratorFacade<IndexedIterator<T*>, std::random_access_iterator_tag, T>;
128
129 public:
130 using reference = typename Facade::reference;
131 using difference_type = typename Facade::difference_type;
132 using iterator_type = T*;
133
135 constexpr explicit IndexedIterator (T* ptr, difference_type index = 0)
136 : ptr_(ptr)
137 , index_(index)
138 {}
139
141 constexpr reference operator* () const { return *ptr_; }
142
144 constexpr IndexedIterator& operator+= (typename Facade::difference_type d)
145 {
146 ptr_ += d;
147 index_ += d;
148 return *this;
149 }
150
152 friend constexpr difference_type operator- (const IndexedIterator& it1, const IndexedIterator& it2)
153 {
154 return it1.ptr_ - it2.ptr_;
155 }
156
158 friend constexpr bool operator== (const IndexedIterator& it1, const IndexedIterator& it2)
159 {
160 return it1.ptr_ == it2.ptr_;
161 }
162
164 [[nodiscard]] constexpr difference_type index () const noexcept
165 {
166 return index_;
167 }
168
169 private:
170 T* ptr_ = nullptr;
171 difference_type index_ = 0;
172 };
173
174} // end namespace Dune
175
176#endif // DUNE_COMMON_INDEXEDITERATOR_HH
Specialization for pointer types.
Definition: indexediterator.hh:126
constexpr difference_type index() const noexcept
Return the enumeration index.
Definition: indexediterator.hh:164
constexpr IndexedIterator(T *ptr, difference_type index=0)
Construct an IndexedIterator from a pointer and an index.
Definition: indexediterator.hh:135
An iterator mixin that adds an index() method returning an enumeration index for the traversal.
Definition: indexediterator.hh:31
constexpr IndexedIterator & operator++()
Increment the iterator and the index.
Definition: indexediterator.hh:62
constexpr IndexedIterator & operator--()
Decrement the iterator and the index.
Definition: indexediterator.hh:80
constexpr size_type index() const noexcept
Return the enumeration index.
Definition: indexediterator.hh:56
constexpr IndexedIterator() noexcept(std::is_nothrow_default_constructible_v< Iter >)
Default constructor default-constructs the Iter base type and the index by 0.
Definition: indexediterator.hh:43
constexpr IndexedIterator(Iter it, size_type index=0) noexcept(std::is_nothrow_copy_constructible_v< Iter >)
Definition: indexediterator.hh:49
typename Traits::difference_type size_type
Type used for storing the traversal index.
Definition: indexediterator.hh:38
CRTP-Mixing class for stl conformant iterators of given iterator category.
Definition: iteratorfacades.hh:1053
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace
Definition: alignedallocator.hh:13
constexpr bool operator==(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for value equality.
Definition: hybridmultiindex.hh:383
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 2, 22:34, 2026)