Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
submdspan_extents.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines submdspan_extents"
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_EXTENTS_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_EXTENTS_HPP_
9
12#include "arene/base/mdspan/detail/canonical_slice.hpp"
13#include "arene/base/mdspan/detail/mandate_each_argtype_is_submdspan_slice.hpp"
14#include "arene/base/mdspan/detail/map_rank.hpp"
15#include "arene/base/mdspan/detail/to_array.hpp"
21#include "arene/base/stdlib_choice/cstddef.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/forward.hpp"
24#include "arene/base/stdlib_choice/integer_sequence.hpp"
25#include "arene/base/stdlib_choice/integral_constant.hpp"
26#include "arene/base/stdlib_choice/move.hpp"
27#include "arene/base/stdlib_choice/tuple.hpp"
28#include "arene/base/stdlib_choice/tuple_element.hpp"
29#include "arene/base/tuple/detail/get.hpp"
30#include "arene/base/tuple/detail/tuple_transform.hpp"
32
33// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
34// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
35
36namespace arene {
37namespace base {
38namespace submdspan_extents_detail {
39
40// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
41// parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameter 'tuple' is used"
42/// @brief filter tuple elements using a compile-time inverse mapping
43/// @tparam Is index sequence corresponding to the elements of the *filtered* tuple
44/// @tparam Tuple input tuple type
45/// @tparam LiftedInverseMapping compile-time inverse rank mapping
46/// @param tuple input tuple
47///
48/// Constructs a new @c std::tuple by selecting elements from @c tuple at the
49/// indices specified by @c LiftedInverseMapping::value.
50///
51/// @return tuple containing the selected elements
52///
53/// @note @c LiftedInverseMapping an @c inverse_map_rank lifted into type-space.
54/// Lifting the inverse mapping into type-space enables compile-time filtering
55/// of tuple elements by index.
56template <std::size_t... Is, class Tuple, class LiftedInverseMapping>
57constexpr auto filter_impl(std::index_sequence<Is...>, Tuple&& tuple, LiftedInverseMapping) noexcept
58 -> std::tuple<std::tuple_element_t<LiftedInverseMapping::value[Is], Tuple>...> {
59 return std::tuple<std::tuple_element_t<LiftedInverseMapping::value[Is], Tuple>...>{
60 tuple_detail::get<LiftedInverseMapping::value[Is]>(std::forward<Tuple>(tuple))...
61 };
62}
63// parasoft-end-suppress AUTOSAR-A0_1_4-a
64
65/// @brief filter tuple elements using a compile-time inverse mapping
66/// @tparam Tuple input tuple type
67/// @tparam LiftedInverseMapping compile-time inverse rank mapping
68/// @param tuple input tuple
69/// @return tuple containing the selected elements
70///
71/// Creates a new tuple by selecting elements from @c tuple according to the
72/// indices specified by @c LiftedInverseMapping::value.
73///
74/// @note @c LiftedInverseMapping an @c inverse_map_rank lifted into type-space.
75/// Lifting the inverse mapping into type-space enables compile-time filtering
76/// of tuple elements by index.
77template <class Tuple, class LiftedInverseMapping>
78constexpr auto filter(Tuple tuple, LiftedInverseMapping) noexcept //
79 -> decltype(filter_impl(
80 std::make_index_sequence<LiftedInverseMapping::value.size()>{},
81 std::move(tuple),
82 LiftedInverseMapping{}
83 )) {
84 return filter_impl( //
85 std::make_index_sequence<LiftedInverseMapping::value.size()>{},
86 std::move(tuple),
87 LiftedInverseMapping{}
88 );
89}
90// parasoft-end-suppress AUTOSAR-M3_3_2-a
91
92/// @brief compute the extents of a submdspan defined by slice specifiers
93///
94/// Given a source @c extents and a pack of submdspan slice specifiers, computes
95/// the extents of the resulting submdspan after applying the slices.
96///
97/// The slice specifiers are first converted to canonical form using
98/// @c submdspan_canonicalize_slices, and the resulting canonical slices are then
99/// used to determine the rank and extent values of the submdspan.
100class submdspan_extents_fn {
101 /// @brief function object to compute extent values for canonical non-collapsing slices
102 class extent_from_slice {
103 public:
104 /// @brief compute the sliced extent value from an @c extent_slice
105 /// @tparam IndexType index type
106 /// @tparam Offset @c extent_slice offset type
107 /// @tparam Extent @c extent_slice extent type
108 /// @tparam Stride @c extent_slice stride type
109 /// @param slice canonical extent slice
110 /// @return extent value of the slice
111 template <class IndexType, class Offset, class Extent, class Stride>
112 constexpr auto operator()(IndexType const&, extent_slice<Offset, Extent, Stride> slice) const noexcept
113 -> IndexType {
114 return slice.extent;
115 }
116
117 /// @brief propagate the original extent for a full slice
118 /// @tparam IndexType index type
119 /// @param ext original extent
120 /// @return unchanged extent value
121 template <class IndexType>
122 constexpr auto operator()(IndexType const& ext, full_extent_t) const noexcept -> IndexType {
123 return ext;
124 }
125 };
126
127 public:
128 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameters 'src' and 'slices' are used"
129 /// @brief compute submdspan extents from slice specifiers
130 ///
131 /// @tparam IndexType index type of the source extents
132 /// @tparam Extents static extent values of the source extents
133 /// @tparam SliceSpecifiers submdspan slice specifier types
134 ///
135 /// @param src source @c extents to slice
136 /// @param slices submdspan slice specifiers
137 ///
138 /// Canonicalizes @c slices with a call to
139 /// <c> submdspan_canonicalize_slices(src, slices...) </c>,
140 /// and uses the resulting canonical slices to determine the rank and extent
141 /// values of the submdspan.
142 ///
143 /// Returns an @c extents specialization describing the extents of the
144 /// submdspan obtained by applying the canonical slices to @c src.
145 ///
146 /// @return A value @c ext of type
147 /// @c submdspan_subextents_type_t<Extents, SliceSpecifiers...> that, for each
148 /// rank index @c k of @c extents<IndexType, Extents...> where the type of the
149 /// canonical slice @c canonical...[k] is not a collapsing slice type, the
150 /// following holds.
151 /// Let @c sk denote @c canonical...[k], and let @c r denote
152 /// <c> MAP_RANK(canonical, k) </c>:
153 /// * if the type of @c sk is a specialization of @c extent_slice,
154 /// <c> ext.extent(r) </c> equals @c sk.extent;
155 /// * otherwise, <c> ext.extent(r) </c> equals <c> U - L </c>, where
156 /// <c> [L, U) </c> is the submdspan slice range denoted by @c sk for the
157 /// @c k-th extent of @c src.
158 ///
159 /// @note Constraints <br>
160 /// * <c> sizeof...(Extents) == sizeof...(SliceSpecifiers) </c>
161 ///
162 /// @note Mandates <br>
163 /// * for each rank index @c k of @c src, @c SliceSpecifiers...[k] is a valid
164 /// submdspan slice type for @c IndexType
165 /// * the canonical form of each slice specifier is a valid submdspan slice
166 /// type for the associated static extent of @c src
167 ///
168 /// @note Preconditions <br>
169 /// * for each rank index @c k of @c src, the canonical slice
170 /// @c slices...[k] denotes a valid submdspan slice for the @c k-th extent
171 /// of @c src
172 ///
173 /// @note Remarks <br>
174 /// * the rank of the returned extents is equal to
175 /// <c> MAP_RANK(slices, extents::rank()) </c>
176 template <
177 class IndexType,
178 std::size_t... Extents,
179 class... SliceSpecifiers,
180 constraints<std::enable_if_t<sizeof...(Extents) == sizeof...(SliceSpecifiers)>> = nullptr,
181 std::nullptr_t = mdspan_detail::mandate_each_argtype_is_submdspan_slice<IndexType, SliceSpecifiers...>()
182 >
183 constexpr auto operator()(extents<IndexType, Extents...> const& src, SliceSpecifiers... slices) const noexcept( //
185 decltype(submdspan_canonicalize_slices),
186 extents<IndexType, Extents...> const& ,
187 SliceSpecifiers&&...>
188 ) -> submdspan_subextents_type_t<extents<IndexType, Extents...>, SliceSpecifiers...> {
189 // parasoft-begin-suppress AUTOSAR-M0_1_3-a "False positive: 'lifted_inverse_mapping' is used"
190 // parasoft-begin-suppress AUTOSAR-M0_1_3-b "False positive: 'lifted_inverse_mapping' is used"
191 // Lift the inverse mapping into type-space so we can determine which elements of the tuple can be filtered out.
192 constexpr auto lifted_inverse_mapping = std::integral_constant<
193 decltype((mdspan_detail::inverse_map_rank<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>...>)),
194 mdspan_detail::inverse_map_rank<mdspan_detail::canonical_slice_t<IndexType, SliceSpecifiers>...>>{};
195 // parasoft-end-suppress AUTOSAR-M0_1_3-a
196 // parasoft-end-suppress AUTOSAR-M0_1_3-b
197
198 // parasoft-begin-suppress AUTOSAR-A8_4_2-a "False positive: function does have a return"
199 // parasoft-begin-suppress CERT_C-MSC37-a "False positive: function does have a return"
200 // parasoft-begin-suppress CERT_CPP-MSC52-a "False positive: function does have a return"
201 return submdspan_subextents_type_t<extents<IndexType, Extents...>, SliceSpecifiers...>{
202 tuple_detail::tuple_transform_to_array<IndexType>(
203 filter(mdspan_detail::to_array(src), lifted_inverse_mapping),
204 filter(submdspan_canonicalize_slices(src, std::move(slices)...), lifted_inverse_mapping),
205 extent_from_slice{}
206 )
207 };
208 // parasoft-end-suppress AUTOSAR-A8_4_2-a
209 // parasoft-end-suppress CERT_C-MSC37-a
210 // parasoft-end-suppress CERT_CPP-MSC52-a
211 }
212 // parasoft-end-suppress AUTOSAR-A0_1_4-a
213};
214
215} // namespace submdspan_extents_detail
216
217/// @brief Compute the extents of an mdspan after slicing.
218// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
219// object used in multiple TUs."
220// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
221// object used in multiple TUs."
222ARENE_CPP14_INLINE_VARIABLE(submdspan_extents_detail::submdspan_extents_fn, submdspan_extents);
223// parasoft-end-suppress AUTOSAR-M7_3_3-a
224// parasoft-end-suppress CERT_CPP-DCL59-a
225
226} // namespace base
227} // namespace arene
228
229#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_EXTENTS_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
arene::base::type_lists:: flat_map< L0, arene::base::type_lists::filter_detail::apply_filter< Filter >::template filter > filter
Filter a type-list to remove elements that don't match the supplied Filter.
Definition filter.hpp:61
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename submdspan_subextents_type< Extents, SliceSpecifiers... >::type submdspan_subextents_type_t
determines the subextents type from an extents type and slice specifiers
Definition submdspan_subextents_type.hpp:124
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
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
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
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24