Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
submdspan.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::submdspan"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_HPP_
9
13#include "arene/base/mdspan/detail/do_submdspan_mapping.hpp"
14#include "arene/base/mdspan/detail/mandate_each_argtype_is_submdspan_slice.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/declval.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/move.hpp"
25
26namespace arene {
27namespace base {
28namespace submdspan_detail {
29
30/// @brief the layout mapping produced by @c submdspan for a source mapping, extents, and slice specifiers
31/// @tparam Mapping the source mdspan's layout mapping type
32/// @tparam Extents the source mdspan's extents type
33/// @tparam SliceSpecifiers pack of slice specifier types, one per rank
34template <class Mapping, class Extents, class... SliceSpecifiers>
35using sub_mapping_t =
36 decltype(apply(
37 bind_front(mdspan_detail::do_submdspan_mapping, std::declval<Mapping const&>()),
38 submdspan_canonicalize_slices(std::declval<Extents const&>(), std::declval<SliceSpecifiers>()...)
39 )
40 .mapping);
41
42// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
43/// @brief unimplemented function whose return type is the return type of <c>submdspan</c>
44/// @tparam ElementType element type of the source mdspan
45/// @tparam Extents extents type of the source mdspan
46/// @tparam LayoutPolicy layout policy of the source mdspan
47/// @tparam AccessorPolicy accessor policy of the source mdspan
48/// @tparam SliceSpecifiers pack of slice specifier types, one per rank
49/// @return The type that should be returned by @c submdspan when called on these arguments
50template <class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy, class... SliceSpecifiers>
51constexpr auto sub_mdspan_impl(priority_tag<1>) -> mdspan<
52 ElementType,
53 typename sub_mapping_t<typename LayoutPolicy::template mapping<Extents>, Extents, SliceSpecifiers...>::extents_type,
54 typename sub_mapping_t<typename LayoutPolicy::template mapping<Extents>, Extents, SliceSpecifiers...>::layout_type,
55 typename AccessorPolicy::offset_policy>;
56
57/// @brief unimplemented function returning <c>void</c>, chosen when the above overload fails to compile
58/// @note Specifying this as a fallback type prevents @c submdspan from having a substitution failure (it will instead
59/// fail to compile when instantiating the template body), which produces more useful errors for users.
60template <class...>
61constexpr auto sub_mdspan_impl(priority_tag<0>) -> void;
62// parasoft-end-suppress CERT_C-EXP37-a
63
64/// @brief the type of the mdspan returned by <c>submdspan</c>, implemented to avoid return-type SFINAE
65/// @tparam ElementType element type of the source mdspan
66/// @tparam Extents extents type of the source mdspan
67/// @tparam LayoutPolicy layout policy of the source mdspan
68/// @tparam AccessorPolicy accessor policy of the source mdspan
69/// @tparam SliceSpecifiers pack of slice specifier types, one per rank
70template <class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy, class... SliceSpecifiers>
71using sub_mdspan_t =
72 decltype(sub_mdspan_impl<ElementType, Extents, LayoutPolicy, AccessorPolicy, SliceSpecifiers...>(priority_tag<1>{})
73 );
74
75/// @brief Creates a view of a subset of an existing @c mdspan.
76class submdspan_fn {
77 public:
78 /// @brief create a submdspan from a source mdspan and slice specifiers
79 /// @tparam ElementType element type of the source mdspan
80 /// @tparam Extents extents type of the source mdspan
81 /// @tparam LayoutPolicy layout policy of the source mdspan
82 /// @tparam AccessorPolicy accessor policy of the source mdspan
83 /// @tparam SliceSpecifiers pack of slice specifier types, one per rank
84 /// @param src the source mdspan to slice
85 /// @param slices one slice specifier per rank of @c src
86 /// @return a new mdspan viewing the subset of @c src described by @c slices
87 ///
88 /// @c submdspan returns an @c mdspan that is a subset of the source @c mdspan. That is, every element of the returned
89 /// @c mdspan is an element of the source @c mdspan.
90 ///
91 /// A slice specifier for each rank controls which elements are included:
92 /// * a single integral value or @c integral-constant-like reduces the rank by one, selecting a single index
93 /// * a pair-like or @c extent_slice or @c range_slice selects a contiguous range of indices
94 /// * @c full_extent_t selects the entire rank
95 ///
96 /// @c submdspan calls @c submdspan_mapping using argument-dependent lookup to obtain the sub-mapping and offset.
97 ///
98 /// @note Constraints <br>
99 /// * <c> sizeof...(SliceSpecifiers) == Extents::rank() </c>
100 /// * <c> is_sliceable_mapping_v<LayoutPolicy::mapping<Extents>> </c> is @c true
101 ///
102 /// @note Mandates <br>
103 /// * each type @c S in @c SliceSpecifiers... is a valid submdspan slice for @c Extents::index_type
104 /// * the canonical form of each @c S in @c SliceSpecifiers... is a valid submdspan slice type for the associated
105 /// static extent of @c Extents
106 ///
107 /// @note Preconditions <br>
108 /// * for each rank index @c k of @c src.extents(), @c the canonical form of each @c s in @c slices... denotes a
109 /// valid submdspan slice for the @c k-th extent of @c src.extents()
110 template <
111 class ElementType,
112 class Extents,
113 class LayoutPolicy,
114 class AccessorPolicy,
115 class... SliceSpecifiers,
117 mdspan_detail::mandate_each_argtype_is_submdspan_slice<typename Extents::index_type, SliceSpecifiers...>(),
119 std::enable_if_t<sizeof...(SliceSpecifiers) == Extents::rank()>,
121 constexpr auto
122 operator()(mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy> const& src, SliceSpecifiers... slices) const
123 noexcept(is_nothrow_invocable_v<decltype(submdspan_canonicalize_slices), Extents const&, SliceSpecifiers&&...>)
124 -> sub_mdspan_t<ElementType, Extents, LayoutPolicy, AccessorPolicy, SliceSpecifiers...> {
125 auto const sub_map_offset = apply(
126 bind_front(mdspan_detail::do_submdspan_mapping, src.mapping()),
127 submdspan_canonicalize_slices(src.extents(), std::move(slices)...)
128 );
129
130 using sub_mapping = decltype(sub_map_offset.mapping);
131
132 return mdspan<
133 ElementType,
134 typename sub_mapping::extents_type,
135 typename sub_mapping::layout_type,
136 typename AccessorPolicy::offset_policy>{
137 src.accessor().offset(src.data_handle(), sub_map_offset.offset),
138 sub_map_offset.mapping,
139 typename AccessorPolicy::offset_policy{src.accessor()}
140 };
141 }
142};
143
144} // namespace submdspan_detail
145
146/// @brief Create a view of an mdspan selected by a slice for each dimension.
147// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
148// object used in multiple TUs."
149// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
150// object used in multiple TUs."
151ARENE_CPP14_INLINE_VARIABLE(submdspan_detail::submdspan_fn, submdspan);
152// parasoft-end-suppress AUTOSAR-M7_3_3-a
153// parasoft-end-suppress CERT_CPP-DCL59-a
154
155} // namespace base
156} // namespace arene
157
158#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typedef nullptr_t
The type of nullptr.
Definition cstddef.hpp:33
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...