DUNE PDELab (git)

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
11namespace Dune
12{
26 template <class Iter>
28 : public Iter
29 {
30 using Traits = std::iterator_traits<Iter>;
31 static_assert(std::is_copy_constructible_v<Iter>);
32 static_assert(std::is_base_of_v<std::forward_iterator_tag, typename Traits::iterator_category>);
33
34 public:
36 using size_type = typename Traits::difference_type;
37
39 template <class I = Iter,
40 std::enable_if_t<std::is_default_constructible_v<I>, bool> = true>
41 constexpr IndexedIterator ()
42 noexcept(std::is_nothrow_default_constructible_v<Iter>)
43 {}
44
47 constexpr IndexedIterator (Iter it, size_type index = 0)
48 noexcept(std::is_nothrow_copy_constructible_v<Iter>)
49 : Iter(it)
50 , index_(index)
51 {}
52
54 [[nodiscard]] constexpr size_type index () const noexcept
55 {
56 return index_;
57 }
58
61 {
62 Iter::operator++();
63 ++index_;
64 return *this;
65 }
66
69 {
70 IndexedIterator tmp(*this);
71 this->operator++();
72 return tmp;
73 }
74
76 template <class I = Iter,
77 decltype(--std::declval<I&>(), bool{}) = true>
78 constexpr IndexedIterator& operator-- ()
79 {
80 Iter::operator--();
81 --index_;
82 return *this;
83 }
84
86 template <class I = Iter,
87 decltype(std::declval<I&>()--, bool{}) = true>
88 constexpr IndexedIterator operator-- (int)
89 {
90 IndexedIterator tmp(*this);
91 this->operator--();
92 return tmp;
93 }
94
96 template <class I = Iter,
97 decltype(std::declval<I&>()+=1, bool{}) = true>
98 constexpr IndexedIterator& operator+= (typename Iter::difference_type n)
99 {
100 Iter::operator+=(n);
101 index_ += n;
102 return *this;
103 }
104
106 template <class I = Iter,
107 decltype(std::declval<I&>()-=1, bool{}) = true>
108 constexpr IndexedIterator& operator-= (typename Iter::difference_type n)
109 {
110 Iter::operator-=(n);
111 index_ -= n;
112 return *this;
113 }
114
115 private:
116 size_type index_ = 0;
117 };
118
119} // end namespace Dune
120
121#endif // DUNE_COMMON_INDEXEDITERATOR_HH
An iterator mixin that adds an index() method returning an enumeration index for the traversal.
Definition: indexediterator.hh:29
constexpr IndexedIterator & operator++()
Increment the iterator and the index.
Definition: indexediterator.hh:60
constexpr IndexedIterator & operator--()
Decrement the iterator and the index.
Definition: indexediterator.hh:78
constexpr size_type index() const noexcept
Return the enumeration index.
Definition: indexediterator.hh:54
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:41
constexpr IndexedIterator(Iter it, size_type index=0) noexcept(std::is_nothrow_copy_constructible_v< Iter >)
Definition: indexediterator.hh:47
typename Traits::difference_type size_type
Type used for storing the traversal index.
Definition: indexediterator.hh:36
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)