Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
is_sliceable_mapping.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_IS_SLICEABLE_MAPPING_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_IS_SLICEABLE_MAPPING_HPP_
7
10#include "arene/base/mdspan/detail/count.hpp"
11#include "arene/base/mdspan/detail/do_submdspan_mapping.hpp"
12#include "arene/base/mdspan/detail/non_collapsing_slice_type.hpp"
17#include "arene/base/stdlib_choice/cstddef.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/is_same.hpp"
24
25namespace arene {
26namespace base {
27namespace is_sliceable_mapping_detail {
28
29/// @brief Invalid slice type
30struct invalid_slice {};
31
32/// @brief Primary template (false by default) to check if the provided submdspan_mapping_result specialization
33/// satisfies the sliceable layout mapping requirements.
34/// @tparam SubmdspanMappingResult A specialization of submdspan_mapping_result
35/// @tparam ExpectedIndexType The expected layout mapping extents index type
36/// @tparam ExpectedRank The expected rank of the result mapping (i.e. the number of non-collapsing slices)
37template <typename SubmdspanMappingResult, class ExpectedIndexType, std::size_t ExpectedRank>
38extern constexpr bool is_valid_submdspan_mapping_result = false;
39
40/// @brief Array of @c bool with @c true values at non-collapsing slices
41/// @tparam Slices Parameter pack of slices
42template <class... Slices>
43constexpr auto non_collapsing_slice_mask =
44 arene::base::array<bool, sizeof...(Slices)>{arene::base::mdspan_detail::non_collapsing_slice_type_v<Slices>...};
45
46/// @brief Number of non-collapsing slices in a @c type_list of slice types
47/// @tparam SlicesTypeList A specialization of @c arene::base::type_list holding the slice types
48template <class SlicesTypeList>
49extern constexpr std::size_t non_collapsing_slice_count = 0U;
50
51/// @brief Number of non-collapsing slices in a @c type_list of slice types
52/// @tparam Slices The pack of slice types held by the @c type_list
53template <class... Slices>
54extern constexpr std::size_t non_collapsing_slice_count<arene::base::type_list<Slices...>> = static_cast<std::size_t>(
55 mdspan_detail::count(non_collapsing_slice_mask<Slices...>.begin(), non_collapsing_slice_mask<Slices...>.end(), true)
56);
57
58/// @brief Specialization template to check if the provided submdspan_mapping_result specialization satisfies the
59/// sliceable layout mapping requirements.
60/// @tparam LayoutMapping The type used to specialize @c submdspan_mapping_result
61/// @tparam ExpectedIndexType The expected layout mapping extents index type
62/// @tparam ExpectedRank The expected rank of the result mapping (i.e. the number of non-collapsing slices)
63///
64/// @see https://eel.is/c++draft/views#mdspan.sub.map.sliceable-4
65template <typename LayoutMapping, class ExpectedIndexType, std::size_t ExpectedRank>
66extern constexpr bool is_valid_submdspan_mapping_result<
67 submdspan_mapping_result<LayoutMapping>,
68 ExpectedIndexType,
69 ExpectedRank> = is_layout_mapping_v<LayoutMapping> && //
71 (LayoutMapping::extents_type::rank() == ExpectedRank) && //
73
74/// @brief Template to check if a type meets the sliceable layout mapping requirements
75/// @tparam M The class to check
76///
77/// Primary template is false by default.
78template <class M, class = constraints<>>
79extern constexpr bool is_sliceable_mapping_v = false;
80
81/// @brief Template to check if a type meets the sliceable layout mapping requirements
82/// @tparam M The class to check
83///
84/// Specialization template is SFINAE constrained and conditionalized upon @c is_valid_submdspan_mapping_result.
85///
86/// @see https://eel.is/c++draft/views#mdspan.sub.map.sliceable-3
87template <class M>
88extern constexpr bool is_sliceable_mapping_v<
89 M,
93 (M::extents_type::rank() == 0) ==
95 is_invocable<decltype(mdspan_detail::do_submdspan_mapping), M>,
96 repeat_type_t<M::extents_type::rank(), type_list, invalid_slice>>::value>,
98 is_invocable<decltype(mdspan_detail::do_submdspan_mapping), M>,
99 repeat_type_t<M::extents_type::rank(), type_list, full_extent_t>>::value>,
101 is_invocable<decltype(mdspan_detail::do_submdspan_mapping), M>,
102 repeat_type_t<M::extents_type::rank(), type_list, typename M::extents_type::index_type>>::value>>> =
103 is_valid_submdspan_mapping_result<
104 typename type_lists::concat_t<
105 invoke_result<decltype(mdspan_detail::do_submdspan_mapping), M>,
106 repeat_type_t<M::extents_type::rank(), type_list, full_extent_t>>::type,
107 typename M::extents_type::index_type,
108 non_collapsing_slice_count<repeat_type_t<M::extents_type::rank(), type_list, full_extent_t>>> &&
109 is_valid_submdspan_mapping_result<
110 typename type_lists::concat_t<
111 invoke_result<decltype(mdspan_detail::do_submdspan_mapping), M>,
112 repeat_type_t<M::extents_type::rank(), type_list, typename M::extents_type::index_type>>::type,
113 typename M::extents_type::index_type,
114 non_collapsing_slice_count<
115 repeat_type_t<M::extents_type::rank(), type_list, typename M::extents_type::index_type>>>;
116
117} // namespace is_sliceable_mapping_detail
118
119// parasoft-begin-suppress AUTOSAR-A2_7_2-a-2 "False positive: no commented-out code"
120/// @brief Check if a type meets the sliceable layout mapping requirements
121/// @tparam M The class to check
122/// @tparam Enable SFINAE hook used by specializations that select on a property of @c M (e.g.
123/// @c layout_transpose::mapping, whose nested layout is not deducible via an exact-type partial specialization)
124///
125/// Let:
126/// - @c M denote a layout mapping class;
127/// - @c IT denote @c M::extents_type::index_type;
128/// - @c m denote a value of type (possibly @c const) @c M;
129/// - @c M_rank be equal to @c M::extents_type::rank();
130/// - @c valid_slices denote a pack of (possibly @c const) objects for which <c> sizeof...(valid_slices) == M_rank </c>
131/// is @c true and, for each rank index @c i of @c m.extents(), @c valid_slices...[i] is a valid submdspan slice for
132/// the @c i <sup>th</sup> extent of @c m.extents();
133/// - @c invalid_slices denote a pack of objects for which <c> sizeof...(invalid_slices) == M_rank </c> is @c true and
134/// there exists an integer @c k such that the cv-unqualified type of @c invalid_slices...[k] is none of the
135/// following:
136/// - @c IT,
137/// - @c full_extent_t,
138/// - a specialization of @c constant_wrapper, or
139/// - a specialization of @c extent_slice.
140///
141/// A type @c M meets the sliceable layout mapping requirements if
142/// - @c M meets the layout mapping requirements
143/// - the expression <c> submdspan_mapping(m, invalid_slices...) </c> is ill-formed, and
144/// - the following expression is well-formed and has the specified semantics:
145/// <c> submdspan_mapping(m, valid_slices...) </c>
146/// *Result*: A type @c SMR that is a specialization of type @c submdspan_mapping_result<SM> for some type @c SM such
147/// that
148/// - @c SM meets the layout mapping requirements,
149/// - <c> SM::extents_type </c> is a specialization of extents,
150/// - <c> SM::extents_type::rank() </c> equals <c> MAP_RANK(valid_slices, M_rank) </c>, and
151/// - <c> SM::extents_type::index_type </c> denotes @c IT.
152/// *Returns*: An object @c smr of type @c SMR such that
153/// - <c> smr.mapping.extents() == subextents(m.extents(), valid_slices...) </c> is @c true; and
154/// - for each integer pack @c i which is a multidimensional index in <c> smr.mapping.extents() </c>, <c>
155/// smr.mapping(i...) + smr.offset == m(j) </c> is @c true, where @c j is an integer pack such that
156/// - @c sizeof...(j) is equal to @c M_rank; and
157/// - for each rank index @c p of @c m.extents(), @c j...[p] is equal to the sum of
158/// - the lower bound of the submdspan slice range of <c> valid_slices...[p] </c> for extent @c p of <c>
159/// m.extents() </c>, and
160/// - zero if the type of <c> valid_slices...[p] </c> is a collapsing slice type, <c>
161/// i...[MAP_RANK(valid_slices, p)] </c> otherwise.
162template <class M, class Enable = void>
163extern constexpr bool is_sliceable_mapping_v = is_sliceable_mapping_detail::is_sliceable_mapping_v<M>;
164// parasoft-begin-suppress AUTOSAR-A2_7_2-a-2 "False positive: no commented-out code"
165
166} // namespace base
167} // namespace arene
168#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_IS_SLICEABLE_MAPPING_HPP_
static constexpr value_type value
Definition integral_constant.hpp:34
typename concat< Ln... >::type concat_t
collapse L0<T...>, .. Ln<U...> into TypeList<T..., .., U...>
Definition concat.hpp:107
Definition accumulate.hpp:37
constexpr bool is_layout_mapping_v
Helper trait to indicate if the given type has all the basic properties required of a layout mapping:
typename repeat_type< N, T, Arg >::type repeat_type_t
Repeat a template argument a specified number of times.
Definition repeat_type.hpp:48
constexpr bool is_extents_v
Helper variable to check if a provided type is an instantiation of extents.
constexpr bool is_sliceable_mapping_v
Check if a type meets the sliceable layout mapping requirements.
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
bool
Check if element Idx and all subsequent elements of the two tuples are equal.
Definition tuple.hpp:649
::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 end(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the one-past-the-end element of the array.
Definition end.hpp:28
constexpr auto begin(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the first element of the array.
Definition begin.hpp:28