Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
axis_view.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::axis_view"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_AXIS_VIEW_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_AXIS_VIEW_HPP_
7
8// IWYU pragma: private, include "arene/base/mdspan.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
12#include "arene/base/detail/make_range_from_indexable.hpp"
17#include "arene/base/stdlib_choice/cstddef.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/integer_sequence.hpp"
20#include "arene/base/stdlib_choice/integral_constant.hpp"
22
23// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
24
25namespace arene {
26namespace base {
27
28namespace axis_view_detail {
29
30/// @brief select the submdspan slice specifier for a non-iterated axis
31/// @tparam IndexType the mdspan index type of the running index
32///
33/// Every axis other than the one held fixed keeps its whole extent (@c full_extent).
34/// @return @c arene::base::full_extent
35template <class IndexType>
36constexpr auto select_slice(IndexType /*index*/, std::false_type /*is_fixed_axis*/) noexcept
37 -> arene::base::full_extent_t {
38 return arene::base::full_extent;
39}
40
41/// @brief select the single running index for the iterated axis
42/// @tparam IndexType the mdspan index type of the running index
43///
44/// The axis held fixed is reduced to the single running @c index.
45/// @param index the index of the slice being read
46/// @return @c index
47template <class IndexType>
48constexpr auto select_slice(IndexType index, std::true_type /*is_fixed_axis*/) noexcept -> IndexType {
49 return index;
50}
51
52/// @brief form the submdspan that fixes axis @c Axis of @c mds to @c index
53/// @tparam Axis the axis held fixed at @c index; every other axis is kept in full
54/// @tparam InSpan an mdspan type
55/// @tparam IndexType the mdspan index type of the running index
56/// @tparam Positions the source axes @c 0..rank-1
57/// @param mds the mdspan to slice
58/// @param index the index at which axis @c Axis is fixed
59/// @return <tt> submdspan(mds, s0, ..., s_{rank-1}) </tt> where @c s_Axis is @c index and every other @c s_i is @c
60/// full_extent (a rank-(N-1) view)
61template <std::size_t Axis, class InSpan, class IndexType, std::size_t... Positions>
62constexpr auto submdspan_at_axis(InSpan const mds, IndexType index, std::index_sequence<Positions...>) noexcept(
63 noexcept(arene::base::submdspan(mds, select_slice(index, std::integral_constant<bool, (Positions == Axis)>{})...))
64) -> decltype(arene::base::submdspan(mds, select_slice(index, std::integral_constant<bool, (Positions == Axis)>{})...)
65) {
66 return arene::base::submdspan(mds, select_slice(index, std::integral_constant<bool, (Positions == Axis)>{})...);
67}
68
69/// @brief end policy reporting the size of axis @c Axis of an mdspan
70/// @tparam Axis the axis being iterated over
71///
72/// Supplies the one-past-last index for @c axis_view: the number of slices
73/// along axis @c Axis, which is that axis's extent.
74template <std::size_t Axis>
75class axis_extent_fn {
76 public:
77 /// @brief report the extent of axis @c Axis of @c mds
78 /// @tparam InSpan an mdspan type
79 /// @param mds the mdspan whose slices are counted
80 /// @return the extent of axis @c Axis of @c mds
81 template <class InSpan>
82 constexpr auto operator()(InSpan const mds) const
83 noexcept(noexcept(mds.extent(static_cast<typename InSpan::rank_type>(Axis))))
84 -> decltype(mds.extent(static_cast<typename InSpan::rank_type>(Axis))) {
85 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert"
86 static_assert(Axis < InSpan::rank(), "axis_view: Axis must be less than the mdspan rank");
87 // parasoft-end-suppress CERT_C-PRE31-c-3
88 return mds.extent(static_cast<typename InSpan::rank_type>(Axis));
89 }
90};
91
92/// @brief index policy reading the @c index -th slice along axis @c Axis of an mdspan
93/// @tparam Axis the axis held fixed
94///
95/// Supplies element access for @c axis_view: the @c index -th slice of the
96/// viewed mdspan, obtained by fixing axis @c Axis to @c index and keeping
97/// every other axis in full (a rank-(N-1) view).
98template <std::size_t Axis>
99class submdspan_axis_fn {
100 public:
101 /// @brief read the @c index -th slice along axis @c Axis of @c mds
102 /// @tparam InSpan an mdspan type whose layout mapping is sliceable
103 /// @tparam Index the offset type supplied by @c indexable_iterator (a signed distance)
104 /// @param mds the mdspan to read a slice from
105 /// @param index the slice to read
106 /// @return the rank-(N-1) view fixing axis @c Axis of @c mds at @c index
107 template <
108 class InSpan,
109 class Index,
111 nullptr>
112 constexpr auto operator()(InSpan const mds, Index index) const
113 noexcept(noexcept(axis_view_detail::submdspan_at_axis<Axis>(
114 mds,
115 static_cast<typename InSpan::index_type>(index),
116 std::make_index_sequence<InSpan::rank()>{}
117 )))
118 -> decltype(axis_view_detail::submdspan_at_axis<Axis>(
119 mds,
120 static_cast<typename InSpan::index_type>(index),
121 std::make_index_sequence<InSpan::rank()>{}
122 )) {
123 // The index is a non-negative slice offset, so the cast to the mdspan index_type is well-defined.
124 return axis_view_detail::submdspan_at_axis<Axis>(
125 mds,
126 static_cast<typename InSpan::index_type>(index),
127 std::make_index_sequence<InSpan::rank()>{}
128 );
129 }
130};
131
132/// @brief A function object which creates an iterable range along one axis of a passed-in mdspan
133/// @tparam Axis the axis over which the range iterates
134template <std::size_t Axis>
135constexpr auto range_factory =
136 bind_back(detail::make_range_from_indexable, axis_extent_fn<Axis>{}, submdspan_axis_fn<Axis>{});
137
138/// @brief function object implementing <c>axis_view</c>, called to get a view into one or more elements of an axis
139/// @tparam Axis the axis held fixed
140template <std::size_t Axis>
141class axis_view_fn {
142 public:
143 /// @brief Overload without index: create a single-index range which, when incremented, advances along @c Axis
144 /// @tparam InSpan an mdspan type whose layout mapping is sliceable
145 /// @param mds the mdspan to iterate over slices of
146 /// @return a range with begin and end iterators, where iteration advances along slices of @c Axis
147 template <
148 class InSpan,
149 constraints<std::enable_if_t<is_invocable_v<decltype(range_factory<Axis>), InSpan>>> = nullptr>
150 constexpr auto operator()(InSpan mds) const noexcept(is_nothrow_invocable_v<decltype(range_factory<Axis>), InSpan>) {
151 return range_factory<Axis>(mds);
152 }
153
154 /// @brief Overload with index: return the slice at index @c index of the range created by the non-indexed version
155 /// @tparam InSpan an mdspan type whose layout mapping is sliceable
156 /// @tparam Index any mdspan index type (probably a @c std::integral_constant or a built-in integral type)
157 /// @param mds the mdspan to return a slice of
158 /// @param index index of the slice to read
159 /// @return the rank-(N-1) view fixing axis @c Axis of @c mds at @c index
160 template <
161 class InSpan,
162 class Index,
163 constraints<std::enable_if_t<is_invocable_v<decltype(range_factory<Axis>), InSpan>>> = nullptr>
164 constexpr auto operator()(InSpan mds, Index index) const
165 noexcept(noexcept(submdspan_at_axis<Axis>(mds, index, std::make_index_sequence<InSpan::rank()>{}))) {
166 return submdspan_at_axis<Axis>(mds, index, std::make_index_sequence<InSpan::rank()>{});
167 }
168};
169
170} // namespace axis_view_detail
171
172/// @brief Create a range of slices along an mdspan axis.
173///
174/// Returns a range containing the slices of an mdspan taken along axis
175/// @c Axis. The @c i -th element of the returned range is equivalent to
176/// <tt> submdspan(A, s0, ..., s_{rank-1}) </tt> where the specifier for
177/// axis @c Axis is @c i and every other specifier is @c full_extent, i.e. a
178/// rank-(N-1) view of @c A with axis @c Axis fixed at @c i.
179///
180/// The returned value is a range: it defines @c begin and @c end members and may
181/// be used in a range-based for loop and with functions that operate on ranges.
182///
183/// Since every slice is formed with @c submdspan, the layout mapping of @c A must
184/// be sliceable, i.e. <tt> is_sliceable_mapping_v<decltype(A)::mapping_type> </tt>
185/// must be @c true. Note: this restriction could be lifted in the future, if an
186/// alternative implementation is provided for non-sliceable layouts.
187///
188/// @snippet docs/examples/axis_view_examples.cpp axis_view_usage_example
189///
190/// @tparam Axis the axis iterated over, must be less than the rank of the mdspan
191template <std::size_t Axis>
192extern constexpr auto axis_view = axis_view_detail::axis_view_fn<Axis>{};
193
194} // namespace base
195} // namespace arene
196
197// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
198
199#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_AXIS_VIEW_HPP_
Definition accumulate.hpp:37
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
constexpr auto axis_view
Create a range of slices along an mdspan axis.
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
bool_constant< false > false_type
An alias for bool_constant<false>
Definition integral_constant.hpp:79
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
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76