dune-istl 2.12-git
Loading...
Searching...
No Matches
indexrange.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_INDEXRANGE_HH
6#define DUNE_ISTL_EXPERIMENTAL_INDEXRANGE_HH
7
9
10#include <algorithm>
11#include <concepts>
12#include <iterator>
13#include <ranges>
14
15
20namespace Dune::Experimental {
21
22namespace Concepts {
23
30template<class R>
32 std::ranges::random_access_range<R> &&
33 std::totally_ordered<std::ranges::range_value_t<R>>;
34
41template<class R>
42concept BorrowedOrderedIndexRange = OrderedIndexRange<R> && std::ranges::borrowed_range<R>;
43
44} // namespace Concepts
45
53template<Concepts::OrderedIndexRange R>
55{
59 using index_type = std::ranges::range_value_t<index_range_type>;
61 using size_type = std::ranges::range_size_t<index_range_type>;
62
67 constexpr static IntegralRange<index_type> range(const R& index_set)
68 {
69 auto it = std::cbegin(index_set);
70 auto size = std::distance(it, std::cend(index_set));
71 if (size == 0)
72 return {0, 0};
73 return { *it, *(it + (size - 1)) + 1 };
74 }
75
80 constexpr static size_type offset(const R& index_set,
81 const index_type& value)
82 {
83 const auto lb = std::lower_bound(std::cbegin(index_set), std::cend(index_set), value);
84 return static_cast<size_type>(std::distance(std::cbegin(index_set), lb));
85 }
86};
87
88template<class T>
90{
92 using index_type = T;
95
101 const IntegralRange<T>& index_set)
102 {
103 return index_set;
104 }
105
107 constexpr static size_type offset(const IntegralRange<T>& index_set,
108 const index_type& value)
109 {
110 return static_cast<size_type>(value - *index_set.begin());
111 }
112};
113
114} // namespace Dune::Experimental
115
116#endif // DUNE_ISTL_EXPERIMENTAL_INDEXRANGE_HH
Definition experimental/bcrsmatrix.hh:21
int size() const
Traits for sets of indices.
Definition indexrange.hh:55
static constexpr IntegralRange< index_type > range(const R &index_set)
Logical index range covered by index_set.
Definition indexrange.hh:67
R index_range_type
The type of the index set.
Definition indexrange.hh:57
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
static constexpr size_type offset(const IntegralRange< T > &index_set, const index_type &value)
Compute offset of value by subtracting the beginning of the range.
Definition indexrange.hh:107
static constexpr IntegralRange< index_type > range(const IntegralRange< T > &index_set)
Return the logical index range covered by index_set.
Definition indexrange.hh:100
T index_type
The type of indices in the index set.
Definition indexrange.hh:92
typename IntegralRange< T >::size_type size_type
The type used to count elements in the index set.
Definition indexrange.hh:94
constexpr iterator begin() const noexcept
Requirements for an index container used by IndexedArrayView.
Definition indexrange.hh:31
OrderedIndexRange that is safe to use through iterators after moving from temporaries.
Definition indexrange.hh:42
T cbegin(T... args)
T distance(T... args)
T cend(T... args)
T lower_bound(T... args)