tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
layout_stride.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_MDSPAN_LAYOUT_STRIDE_HPP
5#define TETL_MDSPAN_LAYOUT_STRIDE_HPP
6
7#include <etl/_array/array.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_mdspan/extents.hpp>
10#include <etl/_mdspan/is_extents.hpp>
11#include <etl/_mdspan/layout.hpp>
12#include <etl/_span/span.hpp>
13#include <etl/_type_traits/is_convertible.hpp>
14#include <etl/_type_traits/is_nothrow_constructible.hpp>
15#include <etl/_utility/as_const.hpp>
16#include <etl/_utility/index_sequence.hpp>
17
18namespace etl {
19
20template <typename Extents>
21struct layout_stride::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_stride;
27
28private:
29 static constexpr auto rank = extents_type::rank();
30
31public:
32 constexpr mapping() noexcept = default;
33 constexpr mapping(mapping const&) noexcept = default;
34
35 template <typename OtherIndexType>
36 requires(is_convertible_v<OtherIndexType const&, index_type>
37 and is_nothrow_constructible_v<index_type, OtherIndexType const&>)
38 constexpr mapping(extents_type const& ext, span<OtherIndexType, rank> s) noexcept
39 : _extents(ext)
40 , _strides([&]<size_t... Is>(index_sequence<Is...> /*seq*/) {
41 return array{static_cast<index_type>(etl::as_const(s[Is]))...};
42 }(make_index_sequence<rank>()))
43 {
44 }
45
46 template <typename OtherIndexType>
47 requires(
48 is_convertible_v<OtherIndexType const&, index_type>
49 and is_nothrow_constructible_v<index_type, OtherIndexType const&>
50 )
51 constexpr mapping(extents_type const& ext, array<OtherIndexType, rank> const& s) noexcept
52 : mapping(ext, span(s))
53 {
54 }
55
56 template <typename StridedLayoutMapping>
57 constexpr explicit(false /* see description */) mapping(StridedLayoutMapping const&) noexcept;
58
59 constexpr auto operator=(mapping const&) noexcept -> mapping& = default;
60
61 [[nodiscard]] constexpr auto required_span_size() const noexcept -> index_type;
62 [[nodiscard]] constexpr auto extents() const noexcept -> extents_type const&
63 {
64 return _extents;
65 }
66 [[nodiscard]] constexpr auto strides() const noexcept -> array<index_type, rank>
67 {
68 return _strides;
69 }
70 [[nodiscard]] constexpr auto stride(rank_type i) const noexcept -> index_type
71 {
72 TETL_PRECONDITION(i < extents_type::rank());
73 return _strides[i];
74 }
75
76 template <typename... Indices>
77 requires(
78 (sizeof...(Indices) == rank) //
79 and (is_convertible_v<Indices, index_type> and ...) //
80 and (is_nothrow_constructible_v<index_type, Indices> and ...)
81 )
82 [[nodiscard]] constexpr auto operator()(Indices... is) const noexcept -> index_type
83 {
84 return [&]<size_t... Is>(index_sequence<Is...> /*seq*/) {
85 return static_cast<index_type>(((static_cast<index_type>(is) * _strides[Is]) + ... + index_type(0)));
86 }(index_sequence_for<Indices...>{});
87 }
88
89 [[nodiscard]] static constexpr auto is_always_unique() noexcept -> bool
90 {
91 return true;
92 }
93 [[nodiscard]] static constexpr auto is_always_strided() noexcept -> bool
94 {
95 return true;
96 }
97 [[nodiscard]] static constexpr auto is_always_exhaustive() noexcept -> bool
98 {
99 return false;
100 }
101
102 [[nodiscard]] static constexpr auto is_unique() noexcept -> bool
103 {
104 return true;
105 }
106 [[nodiscard]] static constexpr auto is_strided() noexcept -> bool
107 {
108 return true;
109 }
110 [[nodiscard]] constexpr auto is_exhaustive() const noexcept -> bool;
111
112 template <typename OtherMapping>
113 friend constexpr auto operator==(mapping const&, OtherMapping const&) noexcept -> bool;
114
115private:
116 TETL_NO_UNIQUE_ADDRESS extents_type _extents{};
117 TETL_NO_UNIQUE_ADDRESS array<index_type, rank> _strides{};
118};
119
120} // namespace etl
121
122#endif // TETL_MDSPAN_LAYOUT_STRIDE_HPP
Definition adjacent_find.hpp:9
A container that encapsulates fixed size arrays.
Definition array.hpp:49
constexpr mapping() noexcept=default
constexpr auto strides() const noexcept -> array< index_type, rank >
Definition layout_stride.hpp:66
static constexpr auto is_always_exhaustive() noexcept -> bool
Definition layout_stride.hpp:97
constexpr auto is_exhaustive() const noexcept -> bool
constexpr auto operator=(mapping const &) noexcept -> mapping &=default
constexpr auto operator()(Indices... is) const noexcept -> index_type
Definition layout_stride.hpp:82
static constexpr auto is_always_strided() noexcept -> bool
Definition layout_stride.hpp:93
static constexpr auto is_strided() noexcept -> bool
Definition layout_stride.hpp:106
friend constexpr auto operator==(mapping const &, OtherMapping const &) noexcept -> bool
constexpr auto stride(rank_type i) const noexcept -> index_type
Definition layout_stride.hpp:70
static constexpr auto is_always_unique() noexcept -> bool
Definition layout_stride.hpp:89
constexpr mapping(extents_type const &ext, span< OtherIndexType, rank > s) noexcept
Definition layout_stride.hpp:38
constexpr mapping(extents_type const &ext, array< OtherIndexType, rank > const &s) noexcept
Definition layout_stride.hpp:51
static constexpr auto is_unique() noexcept -> bool
Definition layout_stride.hpp:102
constexpr explicit(false) mapping(StridedLayoutMapping const &) noexcept
constexpr mapping(mapping const &) noexcept=default
constexpr auto extents() const noexcept -> extents_type const &
Definition layout_stride.hpp:62
constexpr auto required_span_size() const noexcept -> index_type
Definition layout.hpp:19