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
10#include "arene/base/compiler_support/cpp14_inline.hpp"
11#include "arene/base/constraints/constraints.hpp"
12#include "arene/base/functional/bind_front.hpp"
13#include "arene/base/mdspan/detail/do_submdspan_mapping.hpp"
14#include "arene/base/mdspan/detail/mandate_each_argtype_is_submdspan_slice.hpp"
15#include "arene/base/mdspan/is_sliceable_mapping.hpp"
16#include "arene/base/mdspan/mdspan.hpp"
17#include "arene/base/mdspan/submdspan_canonicalize_slices.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/move.hpp"
21#include "arene/base/tuple/apply.hpp"
22#include "arene/base/type_traits/is_invocable.hpp"
23
24namespace arene {
25namespace base {
26namespace submdspan_detail {
27
28/// @brief Creates a view of a subset of an existing @c mdspan.
29class submdspan_fn {
30 public:
31 /// @brief create a submdspan from a source mdspan and slice specifiers
32 /// @tparam ElementType element type of the source mdspan
33 /// @tparam Extents extents type of the source mdspan
34 /// @tparam LayoutPolicy layout policy of the source mdspan
35 /// @tparam AccessorPolicy accessor policy of the source mdspan
36 /// @tparam SliceSpecifiers pack of slice specifier types, one per dimension
37 /// @param src the source mdspan to slice
38 /// @param slices one slice specifier per dimension of @c src
39 /// @return a new mdspan viewing the subset of @c src described by @c slices
40 ///
41 /// @c submdspan returns an @c mdspan that is a subset of the source @c mdspan. That is, every element of the returned
42 /// @c mdspan is an element of the source @c mdspan.
43 ///
44 /// A slice specifier for each dimension controls which elements are included:
45 /// * a single integral value or @c integral-constant-like reduces the rank by one, selecting a single index
46 /// * a pair-like or @c extent_slice or @c range_slice selects a contiguous range of indices
47 /// * @c full_extent_t selects the entire dimension
48 ///
49 /// @c submdspan calls @c submdspan_mapping using argument-dependent lookup to obtain the sub-mapping and offset.
50 ///
51 /// @note Constraints <br>
52 /// * <c> sizeof...(SliceSpecifiers) == Extents::rank() </c>
53 /// * <c> is_sliceable_mapping_v<LayoutPolicy::mapping<Extents>> </c> is @c true
54 ///
55 /// @note Mandates <br>
56 /// * each type @c S in @c SliceSpecifiers... is a valid submdspan slice for @c Extents::index_type
57 /// * the canonical form of each @c S in @c SliceSpecifiers... is a valid submdspan slice type for the associated
58 /// static extent of @c Extents
59 ///
60 /// @note Preconditions <br>
61 /// * for each rank index @c k of @c src.extents(), @c the canonical form of each @c s in @c slices... denotes a
62 /// valid submdspan slice for the @c k-th extent of @c src.extents()
63 template <
64 class ElementType,
65 class Extents,
66 class LayoutPolicy,
67 class AccessorPolicy,
68 class... SliceSpecifiers,
69 std::nullptr_t =
70 mdspan_detail::mandate_each_argtype_is_submdspan_slice<typename Extents::index_type, SliceSpecifiers...>(),
71 constraints<
72 std::enable_if_t<sizeof...(SliceSpecifiers) == Extents::rank()>,
73 std::enable_if_t<
74 submdspan_detail::is_sliceable_mapping_v<typename LayoutPolicy::template mapping<Extents>>>> = nullptr>
75 constexpr auto
76 operator()(mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy> const& src, SliceSpecifiers... slices) const
77 noexcept(is_nothrow_invocable_v<decltype(submdspan_canonicalize_slices), Extents const&, SliceSpecifiers&&...>) {
78 auto const sub_map_offset = apply(
79 bind_front(mdspan_detail::do_submdspan_mapping, src.mapping()),
80 submdspan_canonicalize_slices(src.extents(), std::move(slices)...)
81 );
82
83 using sub_mapping = decltype(sub_map_offset.mapping);
84
85 return mdspan<
86 ElementType,
87 typename sub_mapping::extents_type,
88 typename sub_mapping::layout_type,
89 typename AccessorPolicy::offset_policy>{
90 src.accessor().offset(src.data_handle(), sub_map_offset.offset),
91 sub_map_offset.mapping,
92 typename AccessorPolicy::offset_policy{src.accessor()}
93 };
94 }
95};
96
97} // namespace submdspan_detail
98
99/// @def arene::base::submdspan
100/// @copydoc arene::base::submdspan_detail::submdspan_fn::operator()
101// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
102// object used in multiple TUs."
103// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
104// object used in multiple TUs."
105ARENE_CPP14_INLINE_VARIABLE(submdspan_detail::submdspan_fn, submdspan);
106// parasoft-end-suppress AUTOSAR-M7_3_3-a
107// parasoft-end-suppress CERT_CPP-DCL59-a
108
109} // namespace base
110} // namespace arene
111
112#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_SUBMDSPAN_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(submdspan_detail::submdspan_fn, submdspan)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10