Dune Core Modules (2.10.0)

layout_stride.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_STD_LAYOUT_STRIDE_HH
6#define DUNE_COMMON_STD_LAYOUT_STRIDE_HH
7
8#include <array>
9#include <type_traits>
10
11#include <dune/common/indices.hh>
12#include <dune/common/std/impl/fwd_layouts.hh>
13
14namespace Dune::Std {
15
17template <class Extents>
19{
20 template <class> friend class mapping;
21 static constexpr typename Extents::rank_type rank_ = Extents::rank();
22
23public:
24 using extents_type = Extents;
25 using index_type = typename extents_type::index_type;
26 using size_type = typename extents_type::size_type;
27 using rank_type = typename extents_type::rank_type;
29
30private:
31 using strides_type = std::array<index_type,rank_>;
32
33public:
34
36 constexpr mapping () noexcept
37 : mapping(layout_right::template mapping<extents_type>{})
38 {}
39
41 constexpr mapping (const mapping&) noexcept = default;
42
44 template <class OtherIndexType,
45 std::enable_if_t<std::is_convertible_v<const OtherIndexType&, index_type>, int> = 0,
46 std::enable_if_t<std::is_nothrow_constructible_v<index_type, const OtherIndexType&>, int> = 0>
47 constexpr mapping (const extents_type& e, const std::array<OtherIndexType,rank_>& s) noexcept
48 : extents_(e)
49 , strides_{}
50 {
51 for (rank_type r = 0; r < rank_; ++r)
52 strides_[r] = s[r];
53 }
54
56 template <class OtherIndexType,
57 std::enable_if_t<std::is_convertible_v<const OtherIndexType&, index_type>, int> = 0,
58 std::enable_if_t<std::is_nothrow_constructible_v<index_type, const OtherIndexType&>, int> = 0>
59 constexpr mapping (const extents_type& e, const span<OtherIndexType,rank_>& s) noexcept
60 : extents_(e)
61 , strides_{}
62 {
63 for (rank_type r = 0; r < rank_; ++r)
64 strides_[r] = s[r];
65 }
66
68 template <class M,
69 std::enable_if_t<(M::extents_type::rank() == extents_type::rank()), int> = 0,
70 std::enable_if_t<(M::is_always_unique()), int> = 0,
71 std::enable_if_t<(M::is_always_strided()), int> = 0,
72 decltype(std::declval<M>().extents(), bool{}) = true,
73 decltype(std::declval<M>().stride(std::declval<rank_type>()), bool{}) = true>
74 constexpr mapping (const M& m) noexcept
75 : extents_(m.extents())
76 , strides_{}
77 {
78 for (rank_type r = 0; r < rank_; ++r)
79 strides_[r] = m.stride(r);
80 }
81
83 constexpr mapping& operator= (const mapping&) noexcept = default;
84
85 constexpr const extents_type& extents () const noexcept { return extents_; }
86
88 constexpr index_type required_span_size () const noexcept
89 {
90 return size(extents_,strides_);
91 }
92
94 template <class... Indices,
95 std::enable_if_t<(sizeof...(Indices) == rank_), int> = 0,
96 std::enable_if_t<(std::is_convertible_v<Indices, index_type> && ...), int> = 0,
97 std::enable_if_t<(std::is_nothrow_constructible_v<index_type, Indices> && ...), int> = 0>
98 constexpr index_type operator() (Indices... ii) const noexcept
99 {
100 return unpackIntegerSequence([&](auto... r) {
101 return ((static_cast<index_type>(ii)*strides_[r]) + ... + 0); },
102 std::make_index_sequence<rank_>{});
103 }
104
106 constexpr index_type operator() () const noexcept
107 {
108 return 0;
109 }
110
111 static constexpr bool is_always_unique () noexcept { return true; }
112 static constexpr bool is_always_exhaustive () noexcept { return false; }
113 static constexpr bool is_always_strided () noexcept { return true; }
114
115 static constexpr bool is_unique () noexcept { return true; }
116 static constexpr bool is_strided () noexcept { return true; }
117
118 constexpr bool is_exhaustive () const noexcept
119 {
120 // Actually this could be improved. A strided layout can still be exhaustive.
121 // This test is more complicated to implement, though. See §24.7.3.4.7.4 line (5.2)
122 // in the C++ standard document N4971
123 return extents_type::rank() == 0 || (required_span_size() > 0 && required_span_size() == extents().product());
124 }
125
127 constexpr const strides_type& strides () const noexcept
128 {
129 return strides_;
130 }
131
133 template <class E = extents_type,
134 std::enable_if_t<(E::rank() > 0), int> = 0>
135 constexpr index_type stride (rank_type i) const noexcept
136 {
137 return strides_[i];
138 }
139
140 template <class OtherMapping,
141 std::enable_if_t<(OtherMapping::extents_type::rank() == extents_type::rank()), int> = 0,
142 std::enable_if_t<(OtherMapping::is_always_strided()), int> = 0>
143 friend constexpr bool operator== (const mapping& a, const OtherMapping& b) noexcept
144 {
145 if (offset(b))
146 return false;
147 if constexpr(extents_type::rank() == 0)
148 return true;
149 return a.extents_ == b.extents_ && a.strides_ == b.strides_;
150 }
151
152private:
153 template <class E, class S>
154 static constexpr index_type size (const E& extents, const S& strides) noexcept
155 {
156 if constexpr (E::rank() == 0)
157 return 1;
158 else {
159 if (extents.product() == 0)
160 return 0;
161 else {
162 index_type result = 1;
163 for (rank_type r = 0; r < E::rank(); ++r)
164 result += (extents.extent(r)-1) * strides[r];
165 return result;
166 }
167 }
168 }
169
170 template <class M>
171 static constexpr size_type offset (const M& m) noexcept
172 {
173 if constexpr (M::extents_type::rank() == 0)
174 return m();
175 else {
176 if (m.required_span_size() == 0)
177 return 0;
178 else {
179 return unpackIntegerSequence([&](auto... r) {
180 return m((r,0)...); }, // map the index tuple (0,0...)
181 std::make_index_sequence<M::extents_type::rank()>{});
182 }
183 }
184 }
185
186private:
187 [[no_unique_address]] extents_type extents_;
188 strides_type strides_;
189};
190
191} // end namespace Dune::Std
192
193#endif // DUNE_COMMON_STD_LAYOUT_STRIDE_HH
Multidimensional index space with dynamic and static extents.
Definition: extents.hh:54
constexpr index_type extent(rank_type r) const noexcept
Return the extent of dimension i
Definition: extents.hh:100
A layout mapping where the strides are user-defined.
Definition: layout_stride.hh:19
constexpr mapping(const extents_type &e, const span< OtherIndexType, rank_ > &s) noexcept
Construct the mapping from given extents and strides.
Definition: layout_stride.hh:59
constexpr mapping & operator=(const mapping &) noexcept=default
Copy-assignment for the mapping.
constexpr mapping() noexcept
The default construction initializes the strides from layout_right.
Definition: layout_stride.hh:36
constexpr mapping(const extents_type &e, const std::array< OtherIndexType, rank_ > &s) noexcept
Construct the mapping from given extents and strides.
Definition: layout_stride.hh:47
constexpr index_type stride(rank_type i) const noexcept
Get the single stride i
Definition: layout_stride.hh:135
constexpr index_type required_span_size() const noexcept
Return the sum 1 + (E(0)-1)*S(0) + (E(1)-1)*S(1) + ...
Definition: layout_stride.hh:88
constexpr const strides_type & strides() const noexcept
Get the array of all strides.
Definition: layout_stride.hh:127
constexpr mapping(const mapping &) noexcept=default
Copy constructor for the mapping.
constexpr index_type operator()() const noexcept
The default offset for rank-0 tensors is 0.
Definition: layout_stride.hh:106
A contiguous sequence of elements with static or dynamic extent.
Definition: span.hh:126
decltype(auto) constexpr unpackIntegerSequence(F &&f, std::integer_sequence< I, i... > sequence)
Unpack an std::integer_sequence<I,i...> to std::integral_constant<I,i>...
Definition: indices.hh:124
Namespace with predefined compile time indices for the range [0,19].
Definition: indices.hh:50
Namespace for features backported from new C++ standards.
Definition: default_accessor.hh:10
A layout where the rightmost extent has stride 1, and strides increase right-to-left as the product o...
Definition: fwd_layouts.hh:30
A layout mapping where the strides are user-defined.
Definition: fwd_layouts.hh:40
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)