5#ifndef DUNE_COMMON_RANGE_UTILITIES_HH
6#define DUNE_COMMON_RANGE_UTILITIES_HH
37 typename std::enable_if<IsIterable<T>::value,
int>::type = 0>
38 typename T::value_type
40 using std::max_element;
41 return *max_element(v.begin(), v.end());
45 typename std::enable_if<!IsIterable<T>::value,
int>::type = 0>
54 typename std::enable_if<IsIterable<T>::value,
int>::type = 0>
55 typename T::value_type
57 using std::min_element;
58 return *min_element(v.begin(), v.end());
62 typename std::enable_if<!IsIterable<T>::value,
int>::type = 0>
71 typename std::enable_if<IsIterable<T>::value,
int>::type = 0>
74 for (
const auto & e : v)
80 typename std::enable_if<!IsIterable<T>::value,
int>::type = 0>
81 bool any_true(
const T & v) {
return v; }
83 template<std::
size_t N>
95 typename std::enable_if<IsIterable<T>::value,
int>::type = 0>
98 for (
const auto & e : v)
103 template <
typename T,
104 typename std::enable_if<!IsIterable<T>::value,
int>::type = 0>
105 bool all_true(
const T & v) {
return v; }
107 template<std::
size_t N>
119 class IntegralRangeIterator
122 typedef std::random_access_iterator_tag iterator_category;
123 typedef T value_type;
124 typedef std::make_signed_t<T> difference_type;
125 typedef const T *pointer;
128 constexpr IntegralRangeIterator() noexcept : value_(0) {}
129 constexpr explicit IntegralRangeIterator(value_type value) noexcept : value_(value) {}
131 pointer operator->() const noexcept {
return &value_; }
132 constexpr reference
operator*() const noexcept {
return value_; }
134 constexpr reference operator[]( difference_type n )
const noexcept {
return (value_ + n); }
136 constexpr bool operator==(
const IntegralRangeIterator & other)
const noexcept {
return (value_ == other.value_); }
137 constexpr bool operator!=(
const IntegralRangeIterator & other)
const noexcept {
return (value_ != other.value_); }
139 constexpr bool operator<(
const IntegralRangeIterator & other)
const noexcept {
return (value_ <= other.value_); }
140 constexpr bool operator<=(
const IntegralRangeIterator & other)
const noexcept {
return (value_ <= other.value_); }
141 constexpr bool operator>(
const IntegralRangeIterator & other)
const noexcept {
return (value_ >= other.value_); }
142 constexpr bool operator>=(
const IntegralRangeIterator & other)
const noexcept {
return (value_ >= other.value_); }
144 IntegralRangeIterator& operator++() noexcept { ++value_;
return *
this; }
145 IntegralRangeIterator operator++(
int)
noexcept { IntegralRangeIterator copy( *
this ); ++(*this);
return copy; }
147 IntegralRangeIterator& operator--() noexcept { --value_;
return *
this; }
148 IntegralRangeIterator operator--(
int)
noexcept { IntegralRangeIterator copy( *
this ); --(*this);
return copy; }
150 IntegralRangeIterator& operator+=(difference_type n)
noexcept { value_ += n;
return *
this; }
151 IntegralRangeIterator& operator-=(difference_type n)
noexcept { value_ -= n;
return *
this; }
153 friend constexpr IntegralRangeIterator
operator+(
const IntegralRangeIterator &a, difference_type n)
noexcept {
return IntegralRangeIterator(a.value_ + n); }
154 friend constexpr IntegralRangeIterator
operator+(difference_type n,
const IntegralRangeIterator &a)
noexcept {
return IntegralRangeIterator(a.value_ + n); }
155 friend constexpr IntegralRangeIterator
operator-(
const IntegralRangeIterator &a, difference_type n)
noexcept {
return IntegralRangeIterator(a.value_ - n); }
157 constexpr difference_type
operator-(
const IntegralRangeIterator &other)
const noexcept {
return (
static_cast<difference_type
>(value_) -
static_cast<difference_type
>(other.value_)); }
202 constexpr bool empty() const noexcept {
return (from_ == to_); }
228 template <
class T, T to, T from = 0>
231 template <T ofs, T... i>
232 static std::integer_sequence<T, (i+ofs)...> shift_integer_sequence(std::integer_sequence<T, i...>);
243 typedef decltype(shift_integer_sequence<from>(std::make_integer_sequence<T, to-from>()))
integer_sequence;
262 template <
class U, U i>
263 constexpr auto operator[](
const std::integral_constant<U, i> &)
const noexcept
264 -> std::integral_constant<value_type, from + static_cast<value_type>(i)>
273 static constexpr std::integral_constant<bool, from == to>
empty() noexcept {
return {}; }
275 static constexpr std::integral_constant<size_type, static_cast<size_type>(to) -
static_cast<size_type>(from) >
size()
noexcept {
return {}; }
291 template<
class T,
class U,
292 std::enable_if_t<std::is_same<std::decay_t<T>, std::decay_t<U>>::value,
int> = 0,
293 std::enable_if_t<std::is_integral<std::decay_t<T>>::value,
int> = 0>
299 template<
class T, std::enable_if_t<std::is_
integral<std::decay_t<T>>::value,
int> = 0>
305 template<
class T, std::enable_if_t<std::is_enum<std::decay_t<T>>::value,
int> = 0>
311 template<
class T, T from, T to>
317 template<
class T, T to>
348 template <class I, class F, class TT, class C = typename std::iterator_traits<I>::iterator_category>
349 class TransformedRangeIterator;
351 template<
class I,
class F,
class TT,
class C>
352 struct TransformationRangeIteratorTraits
355 static decltype(
auto) transform(FF&& f,
const I& it) {
356 if constexpr (std::is_same_v<TT,IteratorTransformationTag>)
372 using reference =
decltype(transform(std::declval<F>(), std::declval<I>()));
374 using pointer = std::conditional_t<std::is_lvalue_reference_v<reference>, value_type*, ProxyArrowResult<reference>>;
375 using difference_type =
typename std::iterator_traits<I>::difference_type;
380 template <
class I,
class F,
class TT,
class C>
381 class TransformedRangeIterator :
382 public TransformationRangeIteratorTraits<I,F, TT, C>::Facade
384 using Traits = TransformationRangeIteratorTraits<I,F, TT, C>;
385 using Facade =
typename Traits::Facade;
387 static constexpr bool isBidirectional = std::is_convertible_v<C, std::bidirectional_iterator_tag>;
388 static constexpr bool isRandomAccess = std::is_convertible_v<C, std::random_access_iterator_tag>;
393 using reference =
typename Facade::reference;
394 using difference_type =
typename Facade::difference_type;
396 template<
class II,
class FF>
397 constexpr TransformedRangeIterator(II&& it, FF&& f) noexcept :
398 it_(std::forward<II>(it)),
399 f_(std::forward<FF>(f))
404 std::enable_if_t<std::is_convertible_v<II, I> and std::is_default_constructible_v<F>,
int> =0>
405 constexpr TransformedRangeIterator(II&& it) noexcept :
406 it_(std::forward<II>(it)),
412 std::enable_if_t<std::is_convertible_v<FF, F> and std::is_default_constructible_v<I>,
int> =0>
413 constexpr TransformedRangeIterator(FF&& f) noexcept :
415 f_(std::forward<FF>(f))
430 constexpr TransformedRangeIterator() noexcept :
436 constexpr reference
operator*() const noexcept {
437 return Traits::transform(f_, it_);
447 const I& baseIterator() const noexcept {
451 I& baseIterator() noexcept {
499 template <
class R,
class F,
class T=ValueTransformationTag>
502 using RawConstIterator = std::decay_t<decltype(std::declval<const R>().
begin())>;
503 using RawIterator = std::decay_t<decltype(std::declval<R>().begin())>;
513 using const_iterator = Impl::TransformedRangeIterator<RawConstIterator, const F*, T>;
521 using iterator = Impl::TransformedRangeIterator<RawIterator, F*, T>;
534 template<
class RR,
class FF>
536 rawRange_(std::forward<RR>(
rawRange)),
537 f_(std::forward<FF>(f))
539 static_assert(std::is_same_v<T, ValueTransformationTag> or std::is_same_v<T, IteratorTransformationTag>,
540 "The TransformationType passed to TransformedRangeView has to be either ValueTransformationTag or IteratorTransformationTag.");
556 return iterator(rawRange_.begin(), &f_);
572 return iterator(rawRange_.end(), &f_);
579 std::enable_if_t<std::is_same_v<typename It::iterator_category,std::random_access_iterator_tag>,
int> = 0>
580 constexpr decltype(
auto)
operator[](std::size_t i)
const noexcept
582 return this->
begin()[i];
589 std::enable_if_t<std::is_same_v<typename It::iterator_category,std::random_access_iterator_tag>,
int> = 0>
590 constexpr decltype(
auto)
operator[](std::size_t i)
noexcept
592 return this->
begin()[i];
605 template<
class Range=R,
606 class = std::void_t<decltype(std::declval<const Range>().size())>>
609 return rawRange_.size();
615 constexpr bool empty() const noexcept
617 return rawRange_.begin() == rawRange_.end();
669 template <
class R,
class F>
702 template <
class R,
class F>
721 template<
class Range>
724 return std::tuple<
decltype(*it),
decltype(it.index())>(*it, it.index());
Traits for type conversions and type information.
This file implements iterator facade classes for writing stl conformant iterators.
auto iteratorTransformedRangeView(R &&range, F &&f)
Create a TransformedRangeView using an iterator transformation.
Definition rangeutilities.hh:703
auto transformedRangeView(R &&range, F &&f)
Create a TransformedRangeView.
Definition rangeutilities.hh:670
auto sparseRange(Range &&range)
Allow structured-binding for-loops for sparse iterators.
Definition rangeutilities.hh:722
static StaticIntegralRange< T, to, from > range(std::integral_constant< T, from >, std::integral_constant< T, to >) noexcept
Definition rangeutilities.hh:312
std::enable_if_t< not Impl::disableCopyMoveHelper< This, T... >::value, int > disableCopyMove
Helper to disable constructor as copy and move constructor.
Definition typeutilities.hh:45
typename AutonomousValueType< T >::type AutonomousValue
Type free of internal references that T can be converted to.
Definition typetraits.hh:588
bigunsignedint< k > operator*(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:549
bigunsignedint< k > operator-(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:542
bigunsignedint< k > operator+(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:535
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:638
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:684
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:661
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:238
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:706
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:260
Dune namespace.
Definition alignedallocator.hh:13
bool any_true(const AlignedNumber< bool, align > &val)
Definition debugalign.hh:506
bool all_true(const AlignedNumber< bool, align > &val)
Definition debugalign.hh:512
T max_value(const AlignedNumber< T, align > &val)
Definition debugalign.hh:494
T min_value(const AlignedNumber< T, align > &val)
Definition debugalign.hh:500
This class encapsulates access of IteratorFacade.
Definition iteratorfacades.hh:786
CRTP-Mixing class for stl conformant iterators of given iterator category.
Definition iteratorfacades.hh:1053
constexpr decltype(auto) operator*() const
Dereferencing operator.
Definition iteratorfacades.hh:1119
dynamic integer range for use in range-based for loops
Definition rangeutilities.hh:177
constexpr iterator begin() const noexcept
obtain a random-access iterator to the first element
Definition rangeutilities.hh:194
constexpr iterator end() const noexcept
obtain a random-access iterator past the last element
Definition rangeutilities.hh:196
constexpr value_type operator[](const value_type &i) const noexcept
access specified element
Definition rangeutilities.hh:199
constexpr bool contains(value_type index) const noexcept
check whether given index is within range [from, to)
Definition rangeutilities.hh:207
constexpr bool empty() const noexcept
check whether the range is empty
Definition rangeutilities.hh:202
constexpr IntegralRange(std::pair< value_type, value_type > range) noexcept
construct integer range std::pair
Definition rangeutilities.hh:191
T value_type
type of integers contained in the range
Definition rangeutilities.hh:180
constexpr IntegralRange(value_type from, value_type to) noexcept
construct integer range [from, to)
Definition rangeutilities.hh:187
constexpr size_type size() const noexcept
obtain number of elements in the range
Definition rangeutilities.hh:204
std::make_unsigned_t< T > size_type
unsigned integer type corresponding to value_type
Definition rangeutilities.hh:184
constexpr IntegralRange(value_type to) noexcept
construct integer range [0, to)
Definition rangeutilities.hh:189
Impl::IntegralRangeIterator< T > iterator
type of iterator
Definition rangeutilities.hh:182
static integer range for use in range-based for loops
Definition rangeutilities.hh:230
static constexpr iterator end() noexcept
obtain a random-access iterator past the last element
Definition rangeutilities.hh:259
std::make_unsigned_t< T > size_type
unsigned integer type corresponding to value_type
Definition rangeutilities.hh:240
static constexpr bool contains(value_type index) noexcept
check whether given index is within range [from, to)
Definition rangeutilities.hh:278
constexpr StaticIntegralRange() noexcept=default
default constructor
constexpr auto operator[](const std::integral_constant< U, i > &) const noexcept -> std::integral_constant< value_type, from+static_cast< value_type >(i)>
access specified element (static version)
Definition rangeutilities.hh:263
static constexpr std::integral_constant< bool, from==to > empty() noexcept
check whether the range is empty
Definition rangeutilities.hh:273
Impl::IntegralRangeIterator< T > iterator
type of iterator
Definition rangeutilities.hh:238
static constexpr iterator begin() noexcept
obtain a random-access iterator to the first element
Definition rangeutilities.hh:257
static constexpr integer_sequence to_integer_sequence() noexcept
return corresponding std::integer_sequence
Definition rangeutilities.hh:254
T value_type
type of integers contained in the range
Definition rangeutilities.hh:236
decltype(shift_integer_sequence< from >(std::make_integer_sequence< T, to-from >())) integer_sequence
type of corresponding std::integer_sequence
Definition rangeutilities.hh:243
constexpr value_type operator[](const size_type &i) const noexcept
access specified element (dynamic version)
Definition rangeutilities.hh:270
static constexpr std::integral_constant< size_type, static_cast< size_type >(to) - static_cast< size_type >(from) > size() noexcept
obtain number of elements in the range
Definition rangeutilities.hh:275
Tag to enable value based transformations in TransformedRangeView.
Definition rangeutilities.hh:328
Tag to enable iterator based transformations in TransformedRangeView.
Definition rangeutilities.hh:333
A range transforming the values of another range on-the-fly.
Definition rangeutilities.hh:501
Impl::TransformedRangeIterator< RawIterator, F *, T > iterator
Iterator type.
Definition rangeutilities.hh:521
constexpr TransformedRangeView(RR &&rawRange, FF &&f) noexcept
Construct from range and function.
Definition rangeutilities.hh:535
constexpr iterator end() noexcept
Definition rangeutilities.hh:571
Impl::TransformedRangeIterator< RawConstIterator, const F *, T > const_iterator
Const iterator type.
Definition rangeutilities.hh:513
std::remove_reference_t< R > RawRange
Export type of the wrapped untransformed range.
Definition rangeutilities.hh:529
constexpr iterator begin() noexcept
Definition rangeutilities.hh:555
RawRange & rawRange() noexcept
Export the wrapped untransformed range.
Definition rangeutilities.hh:631
const RawRange & rawRange() const noexcept
Export the wrapped untransformed range.
Definition rangeutilities.hh:623
constexpr const_iterator begin() const noexcept
Obtain a iterator to the first element.
Definition rangeutilities.hh:551
auto size() const noexcept
Obtain the size of the range.
Definition rangeutilities.hh:607
constexpr const_iterator end() const noexcept
Obtain a iterator past the last element.
Definition rangeutilities.hh:567
constexpr bool empty() const noexcept
Checks whether the range is empty.
Definition rangeutilities.hh:615
Check if a type is callable with ()-operator and given arguments.
Definition typetraits.hh:162