Dune Core Modules (unstable)

layout_right.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_RIGHT_HH
6#define DUNE_COMMON_STD_LAYOUT_RIGHT_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
22public:
23 using extents_type = Extents;
24 using size_type = typename extents_type::size_type;
25 using rank_type = typename extents_type::rank_type;
26 using index_type = typename extents_type::index_type;
28
30 constexpr mapping () noexcept = default;
31
33 constexpr mapping (const mapping&) noexcept = default;
34
36 constexpr mapping (const extents_type& e) noexcept
37 : extents_(e)
38 {}
39
41 template <class OtherExtents,
42 std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
43 #if __cpp_conditional_explicit >= 201806L
44 explicit(!std::is_convertible_v<OtherExtents, extents_type>)
45 #endif
46 constexpr mapping (const mapping<OtherExtents>& m) noexcept
47 : extents_(m.extents())
48 {}
49
51 template <class OtherExtents, class E = extents_type,
52 std::enable_if_t<(E::rank() <= 1), int> = 0,
53 std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
54 #if __cpp_conditional_explicit >= 201806L
55 explicit(!std::is_convertible_v<OtherExtents, extents_type>)
56 #endif
57 constexpr mapping (const layout_left::mapping<OtherExtents>& m) noexcept
58 : extents_(m.extents())
59 {}
60
62 template <class OtherExtents,
63 std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
64 #if __cpp_conditional_explicit >= 201806L
65 explicit(extents_type::rank() > 0)
66 #endif
68 : extents_(m.extents())
69 {
70#ifndef NDEBUG
71 if constexpr(extents_type::rank() > 0) {
72 index_type prod = 1;
73 for (rank_type r = extents_type::rank()-1; r > 0; --r) {
74 assert(m.strides(r) == prod);
75 prod *= m.extents().extent(r);
76 }
77 assert(m.strides(0) == prod);
78 }
79#endif
80 }
81
83 constexpr mapping& operator= (const mapping&) noexcept = default;
84
85 constexpr const extents_type& extents () const noexcept { return extents_; }
86 constexpr index_type required_span_size () const noexcept { return extents_.product(); }
87
89 template <class... Indices,
90 std::enable_if_t<(sizeof...(Indices) == extents_type::rank()), int> = 0,
91 std::enable_if_t<(std::is_convertible_v<Indices, index_type> && ...), int> = 0,
92 std::enable_if_t<(std::is_nothrow_constructible_v<Indices, index_type> && ...), int> = 0>
93 constexpr index_type operator() (Indices... ii) const noexcept
94 {
95 const std::array indices{index_type(std::move(ii))...};
96 index_type value = indices.front();
97 for (rank_type j = 0; j < extents_type::rank()-1; ++j) {
98 value = indices[j+1] + extents_.extent(j+1) * value;
99 }
100 return value;
101 }
102
104 constexpr index_type operator() () const noexcept
105 {
106 return 0;
107 }
108
109 static constexpr bool is_always_unique () noexcept { return true; }
110 static constexpr bool is_always_exhaustive () noexcept { return true; }
111 static constexpr bool is_always_strided () noexcept { return true; }
112
113 static constexpr bool is_unique () noexcept { return true; }
114 static constexpr bool is_exhaustive () noexcept { return true; }
115 static constexpr bool is_strided () noexcept { return true; }
116
118 template <class E = extents_type,
119 std::enable_if_t<(E::rank() > 0), int> = 0>
120 constexpr index_type stride (rank_type i) const noexcept
121 {
122 assert(i < extents_type::rank());
123 index_type prod = 1;
124 for (rank_type r = i+1; r < extents_type::rank(); ++r)
125 prod *= extents().extent(r);
126 return prod;
127 }
128
129 template <class OtherExtents>
130 friend constexpr bool operator== (const mapping& a, const mapping<OtherExtents>& b) noexcept
131 {
132 return a.extents_ == b.extents_;
133 }
134
135private:
136 [[no_unique_address]] extents_type extents_;
137};
138
139} // end namespace Dune::Std
140
141#endif // DUNE_COMMON_STD_LAYOUT_RIGHT_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 leftmost extent has stride 1.
Definition: layout_left.hh:19
A layout mapping where the rightmost extent has stride 1.
Definition: layout_right.hh:19
constexpr index_type operator()() const noexcept
The default offset for rank-0 tensors is 0.
Definition: layout_right.hh:104
constexpr mapping & operator=(const mapping &) noexcept=default
Copy-assignment for the mapping.
constexpr mapping() noexcept=default
The default construction is possible for default constructible extents.
constexpr index_type stride(rank_type i) const noexcept
The stride is the product of the extents E(n)*E(n-1)*...*E(i+1)
Definition: layout_right.hh:120
constexpr mapping(const layout_left::mapping< OtherExtents > &m) noexcept
Construct the mapping from a layout_left.
Definition: layout_right.hh:57
A layout mapping where the strides are user-defined.
Definition: layout_stride.hh:19
constexpr const strides_type & strides() const noexcept
Get the array of all strides.
Definition: layout_stride.hh:127
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
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)