tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
layout_right.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_MDSPAN_LAYOUT_RIGHT_HPP
5#define TETL_MDSPAN_LAYOUT_RIGHT_HPP
6
7#include <etl/_contracts/check.hpp>
8#include <etl/_mdspan/extents.hpp>
9#include <etl/_mdspan/is_extents.hpp>
10#include <etl/_mdspan/layout.hpp>
11#include <etl/_mdspan/submdspan_extents.hpp>
12#include <etl/_mdspan/submdspan_mapping_result.hpp>
13#include <etl/_type_traits/always_false.hpp>
14#include <etl/_type_traits/is_convertible.hpp>
15#include <etl/_type_traits/is_nothrow_constructible.hpp>
16#include <etl/_utility/index_sequence.hpp>
17
18namespace etl {
19
20template <typename Extents>
21struct layout_right::mapping {
22 using extents_type = Extents;
23 using index_type = typename extents_type::index_type;
24 using size_type = typename extents_type::size_type;
25 using rank_type = typename extents_type::rank_type;
26 using layout_type = layout_right;
27
28 // constructors
29 constexpr mapping() noexcept = default;
30 constexpr mapping(mapping const&) noexcept = default;
31
32 constexpr mapping(extents_type const& ext) noexcept
33 : _extents{ext}
34 {
35 }
36
37 template <typename OtherExtents>
38 requires is_constructible_v<extents_type, OtherExtents>
40 mapping<OtherExtents> const& other
41 ) noexcept
42 : _extents{other.extents()}
43 {
44 }
45
46 template <typename OtherExtents>
47 requires(extents_type::rank() <= 1) && is_constructible_v<extents_type, OtherExtents>
49 layout_left::mapping<OtherExtents> const& other
50 ) noexcept
51 : _extents{other.extents()}
52 {
53 }
54
55 template <typename OtherExtents>
56 constexpr explicit(extents_type::rank() > 0) mapping(layout_stride::mapping<OtherExtents> const&) noexcept;
57
58 constexpr auto operator=(mapping const&) noexcept -> mapping& = default;
59
60 // observers
61 [[nodiscard]] constexpr auto extents() const noexcept -> extents_type const&
62 {
63 return _extents;
64 }
65
66 [[nodiscard]] constexpr auto required_span_size() const noexcept -> index_type
67 {
68 return static_cast<index_type>(extents().fwd_prod_of_extents(extents_type::rank()));
69 }
70
71 template <typename... Indices>
72 requires(sizeof...(Indices) == extents_type::rank())
73 and (is_convertible_v<Indices, index_type> and ...)
74 and (is_nothrow_constructible_v<index_type, Indices> and ...)
75 [[nodiscard]] constexpr auto operator()(Indices... indices) const noexcept -> index_type
76 {
77 return [&]<size_t... Is>(index_sequence<Is...> /*seq*/) {
78 return static_cast<index_type>(((static_cast<index_type>(indices) * stride(Is)) + ... + 0));
79 }(index_sequence_for<Indices...>{});
80 }
81
82 [[nodiscard]] constexpr auto stride(rank_type r) const noexcept -> index_type
83 requires(extents_type::rank() > 0)
84 {
85 TETL_PRECONDITION(r < extents_type::rank());
86 return static_cast<index_type>(_extents.rev_prod_of_extents(r));
87 }
88
89 [[nodiscard]] static constexpr auto is_always_unique() noexcept -> bool
90 {
91 return true;
92 }
93 [[nodiscard]] static constexpr auto is_always_exhaustive() noexcept -> bool
94 {
95 return true;
96 }
97 [[nodiscard]] static constexpr auto is_always_strided() noexcept -> bool
98 {
99 return true;
100 }
101
102 [[nodiscard]] static constexpr auto is_unique() noexcept -> bool
103 {
104 return true;
105 }
106 [[nodiscard]] static constexpr auto is_exhaustive() noexcept -> bool
107 {
108 return true;
109 }
110 [[nodiscard]] static constexpr auto is_strided() noexcept -> bool
111 {
112 return true;
113 }
114
115 template <typename OtherExtents>
116 friend constexpr auto operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept -> bool
117 {
118 return lhs.extents() == rhs.extents();
119 }
120
121private:
122 // template <typename... SliceSpecifiers>
123 // [[nodiscard]] constexpr auto submdspan_mapping_impl(SliceSpecifiers... slices) const
124 // {
125 // auto sub_ext = etl::submdspan_extents(extents(), slices...);
126 // using SubExtents = decltype(sub_ext);
127 // static_assert(sizeof(SubExtents) > 0);
128
129 // // auto sub_strides = {};
130
131 // if constexpr (Extents::rank() == 0) {
132 // return etl::submdspan_mapping_result {*this, 0};
133 // } else {
134 // static_assert(etl::always_false<SliceSpecifiers...>);
135 // }
136 // }
137
138 // template <typename... SliceSpecifiers>
139 // friend constexpr auto submdspan_mapping(mapping const& src, SliceSpecifiers... slices)
140 // {
141 // return src.submdspan_mapping_impl(slices...);
142 // }
143
144 TETL_NO_UNIQUE_ADDRESS extents_type _extents{};
145};
146
147} // namespace etl
148
149#endif // TETL_MDSPAN_LAYOUT_RIGHT_HPP
Definition adjacent_find.hpp:9
Definition layout.hpp:9
constexpr mapping(extents_type const &ext) noexcept
Definition layout_right.hpp:32
constexpr mapping() noexcept=default
Indices and constexpr auto operator()(Indices... indices) const noexcept -> index_type
Definition layout_right.hpp:75
static constexpr auto is_exhaustive() noexcept -> bool
Definition layout_right.hpp:106
static constexpr auto is_always_exhaustive() noexcept -> bool
Definition layout_right.hpp:93
friend constexpr auto operator==(mapping const &lhs, mapping< OtherExtents > const &rhs) noexcept -> bool
Definition layout_right.hpp:116
constexpr explicit(!is_convertible_v< OtherExtents, extents_type >) mapping(layout_left
Definition layout_right.hpp:48
static constexpr auto is_always_strided() noexcept -> bool
Definition layout_right.hpp:97
static constexpr auto is_strided() noexcept -> bool
Definition layout_right.hpp:110
static constexpr auto is_always_unique() noexcept -> bool
Definition layout_right.hpp:89
constexpr auto stride(rank_type r) const noexcept -> index_type requires(extents_type::rank() > 0)
Definition layout_right.hpp:82
constexpr explicit(!is_convertible_v< OtherExtents, extents_type >) mapping(mapping< OtherExtents > const &other) noexcept
Definition layout_right.hpp:39
static constexpr auto is_unique() noexcept -> bool
Definition layout_right.hpp:102
constexpr mapping(mapping const &) noexcept=default
constexpr auto extents() const noexcept -> extents_type const &
Definition layout_right.hpp:61
constexpr auto required_span_size() const noexcept -> index_type
Definition layout_right.hpp:66
constexpr explicit(extents_type::rank() > 0) mapping(layout_stride constexpr auto operator=(mapping const &) noexcept -> mapping &=default
Definition layout.hpp:14
Definition layout.hpp:19