dune-istl 2.12-git
Loading...
Searching...
No Matches
indexedarrayview.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_ISTL_EXPERIMENTAL_INDEXEDARRAYVIEW_HH
6#define DUNE_ISTL_EXPERIMENTAL_INDEXEDARRAYVIEW_HH
7
8#include "indexrange.hh"
9#include "../istlexception.hh"
10
13
14#include <cassert>
15#include <cstddef>
16#include <iterator>
17#include <ranges>
18#include <type_traits>
19#include <utility>
20
25namespace Dune::Experimental {
26
34template<std::random_access_iterator T, Concepts::BorrowedOrderedIndexRange R>
36{
37public:
49#if __cpp_lib_ranges_as_const >= 202207L
50 using const_reference = std::iter_const_reference_t<T>;
51#else
54#endif
55
62 {
64#ifdef DUNE_ISTL_WITH_CHECKING
65 if (offset >= size() || index_set_[offset] != i)
66 DUNE_THROW(ISTLError, "index " << i << " not in base array index set");
67#endif
68 return data_[offset];
69 }
70
77 {
79#ifdef DUNE_ISTL_WITH_CHECKING
80 if (offset >= size() || index_set_[offset] != i)
81 DUNE_THROW(ISTLError, "index " << i << " not in base array index set");
82#endif
83 return data_[offset];
84 }
85
86private:
87 class ConstIterator;
88
90 class Iterator
91 : public Dune::IteratorFacade<Iterator,
92 std::random_access_iterator_tag,
93 std::remove_reference_t<reference>,
94 reference>
95 {
96 using Facade = Dune::IteratorFacade<Iterator,
99 reference>;
100
101 public:
103 using offset_type = typename OrderedIndexRangeTraits<index_range_type>::size_type;
104
106 constexpr Iterator() noexcept = default;
108 constexpr Iterator(const Iterator&) = default;
110 constexpr Iterator& operator=(const Iterator&) = default;
111
113 explicit constexpr Iterator(
114 T data,
115 index_range_type index_set_,
116 offset_type
117 offset) noexcept(std::is_nothrow_copy_constructible_v<T> &&
118 std::is_nothrow_copy_constructible_v<index_range_type> &&
119 std::is_nothrow_copy_constructible_v<offset_type>)
120 : data_(data)
121 , index_set_(index_set_)
122 , offset_(offset)
123 {
124 }
125
127 constexpr index_type index() const { return index_set_[offset_]; }
129 constexpr offset_type offset() const { return offset_; }
130
131 // Reference to the component at the current iterator position.
132 constexpr typename Facade::reference operator*() const { return data_[offset_]; }
133
134 private:
136 friend ConstIterator;
137
138 constexpr const offset_type& baseIterator() const { return offset_; }
139 constexpr offset_type& baseIterator() { return offset_; }
140
141 T data_;
142 index_range_type index_set_;
143 offset_type offset_;
144 };
145
147 class ConstIterator
148 : public Dune::IteratorFacade<ConstIterator,
149 std::random_access_iterator_tag,
150 std::remove_reference_t<const_reference>,
151 const_reference>
152 {
153 using Facade =
154 Dune::IteratorFacade<ConstIterator,
158
159 public:
161 using offset_type = typename OrderedIndexRangeTraits<index_range_type>::size_type;
162
164 constexpr ConstIterator() noexcept = default;
166 constexpr ConstIterator(const ConstIterator& other) = default;
168 constexpr ConstIterator& operator=(const ConstIterator& other) = default;
169
171 constexpr ConstIterator(const Iterator& it) noexcept(std::is_nothrow_copy_constructible_v<Iterator>)
172 : it_(it)
173 {}
174
176 constexpr ConstIterator& operator=(const Iterator& it) noexcept(std::is_nothrow_copy_assignable_v<Iterator>)
177 {
178 it_ = it;
179 return *this;
180 }
181
183 constexpr index_type index() const { return it_.index(); }
185 constexpr offset_type offset() const { return it_.offset(); }
186
188 constexpr typename Facade::reference operator*() const { return *it_; }
189
190 private:
192 friend Iterator;
193
194 constexpr const offset_type& baseIterator() const { return it_.baseIterator(); }
195 constexpr offset_type& baseIterator() { return it_.baseIterator(); }
196
197 Iterator it_;
198 };
199
200public:
202 using iterator = Iterator;
204 using const_iterator = ConstIterator;
205
207 constexpr iterator begin() noexcept(
208 noexcept(iterator(data_, index_set_, size_type{})))
209 {
210 return iterator(data_, index_set_, size_type{ 0 });
211 }
212
214 constexpr iterator end() noexcept(
215 noexcept(iterator(data_, index_set_, size_type{})))
216 {
217 return iterator(data_, index_set_, size());
218 }
219
224 {
226 return (ofs == size() || std::cmp_not_equal(index_set_[ofs], i)) ? end() : iterator(data_, index_set_, ofs);
227 }
228
229 bool contains(index_type i) const
230 {
232 return ofs != size() && index_set_[ofs] == i;
233 }
234
236 constexpr const_iterator begin() const
237 noexcept(noexcept(iterator(data_, index_set_, size_type{})))
238 {
239 return iterator(data_, index_set_, size_type{ 0 });
240 }
241
243 constexpr const_iterator end() const
244 noexcept(noexcept(iterator(data_, index_set_, size_type{})))
245 {
246 return iterator(data_, index_set_, size());
247 }
248
253 {
255 return (ofs == size() || index_set_[ofs] != i) ? end() : const_iterator{iterator{data_, index_set_, ofs}};
256 }
257
258 //===== sizes
259
261 constexpr size_type size() const
262 noexcept(noexcept(std::ranges::size(index_set_)))
263 {
264 return std::ranges::size(index_set_);
265 }
266
268 constexpr index_range_type indexSet() const
269 {
270 return index_set_;
271 }
272
279 IndexedArrayView(T data, index_range_type index_set) noexcept(
280 std::is_nothrow_copy_constructible_v<T> &&
281 std::is_nothrow_copy_constructible_v<index_range_type>)
282 : data_(data)
283 , index_set_(index_set) {};
284
285private:
286 T data_;
287 index_range_type index_set_;
288};
289
290} // namespace Dune::Experimental
291
292#endif // DUNE_ISTL_EXPERIMENTAL_INDEXEDARRAYVIEW_HH
Traits for sets of indices.
STL namespace.
Definition experimental/bcrsmatrix.hh:21
SLList< T, A > & operator=(const SLList< T, A > &other)
bigunsignedint< k > operator*(const bigunsignedint< k > &x, std::uintmax_t y)
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
Non-owning indexed view over random-access data.
Definition indexedarrayview.hh:36
constexpr iterator end() noexcept(noexcept(iterator(data_, index_set_, size_type{})))
Iterator to one past the last element in the view.
Definition indexedarrayview.hh:214
constexpr const_iterator begin() const noexcept(noexcept(iterator(data_, index_set_, size_type{})))
Const iterator to the first element in the view.
Definition indexedarrayview.hh:236
ConstIterator const_iterator
Iterator type with const access to the underlying data.
Definition indexedarrayview.hh:204
Iterator iterator
Iterator type with mutable access to the underlying data.
Definition indexedarrayview.hh:202
reference operator[](index_type i)
Access element by an index from the index set.
Definition indexedarrayview.hh:61
constexpr index_range_type indexSet() const
The logical index range covered by the view, as determined by the index set.
Definition indexedarrayview.hh:268
const_reference operator[](index_type i) const
Const access element by an index from the index set.
Definition indexedarrayview.hh:76
constexpr const_iterator end() const noexcept(noexcept(iterator(data_, index_set_, size_type{})))
Const iterator to one past the last element in the view.
Definition indexedarrayview.hh:243
bool contains(index_type i) const
Definition indexedarrayview.hh:229
IndexedArrayView(T data, index_range_type index_set) noexcept(std::is_nothrow_copy_constructible_v< T > &&std::is_nothrow_copy_constructible_v< index_range_type >)
Construct a view from data iterator and index set.
Definition indexedarrayview.hh:279
constexpr iterator begin() noexcept(noexcept(iterator(data_, index_set_, size_type{})))
Iterator to the first element in the view.
Definition indexedarrayview.hh:207
R index_range_type
The type used for the index set.
Definition indexedarrayview.hh:39
constexpr size_type size() const noexcept(noexcept(std::ranges::size(index_set_)))
The number of elements in the view, i.e. the size of the index set.
Definition indexedarrayview.hh:261
typename OrderedIndexRangeTraits< index_range_type >::size_type size_type
The type used to count elements in the index set.
Definition indexedarrayview.hh:45
iterator find(size_type i)
Find element with index i in the index set.
Definition indexedarrayview.hh:223
typename OrderedIndexRangeTraits< index_range_type >::index_type index_type
The type used for the index access.
Definition indexedarrayview.hh:43
const_iterator find(size_type i) const
Find element with index i in the index set.
Definition indexedarrayview.hh:252
std::common_reference_t< const std::iter_value_t< T > &&, std::iter_reference_t< T > > const_reference
The type used for const references to the components.
Definition indexedarrayview.hh:53
static constexpr size_type offset(const R &index_set, const index_type &value)
Offset of value in the index set.
Definition indexrange.hh:80
std::ranges::range_value_t< index_range_type > index_type
The type of indices in the index set.
Definition indexrange.hh:59
std::ranges::range_size_t< index_range_type > size_type
The type used to count elements in the index set.
Definition indexrange.hh:61
derive error class from the base class in common
Definition istlexception.hh:19