Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
scaled_accessor.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_LINALG_SCALED_ACCESSOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SCALED_ACCESSOR_HPP_
7
8// IWYU pragma: private, include "arene/base/linalg.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17#include "arene/base/stdlib_choice/cstddef.hpp"
18#include "arene/base/stdlib_choice/declval.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/is_constructible.hpp"
21#include "arene/base/stdlib_choice/is_convertible.hpp"
22#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
23#include "arene/base/stdlib_choice/is_reference.hpp"
24#include "arene/base/stdlib_choice/remove_const.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
26
27// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
28
29namespace arene {
30namespace base {
31namespace linalg {
32
33/// @brief Accessor wrapper to automatically apply a constant scaling factor to all accessed elements
34/// @tparam ScalingFactor Type of the scaling factor to use; must be a scalar in the linalg sense
35/// @tparam NestedAccessor Type of the underlying data accessor to use; must be an mdspan accessor policy
36///
37/// Backport of @c std::linalg::scaled_accessor from C++26.
38/// Provides access to an underlying data array such that the elements of the array are automatically scaled by a
39/// constant factor before being returned.
40///
41/// @note C++26 only requires @c ScalingFactor to be semiregular, but we require it to be a linalg scalar since this
42/// class represents a combination of data access with linalg scalar multiplication.
43template <class ScalingFactor, class NestedAccessor>
44class scaled_accessor : private NestedAccessor {
45 private:
46 /// @brief Specific scaling factor used by an accessor instance
47 ScalingFactor scaling_factor_;
48
49 public:
50 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Identifiers do not hide anything"
51 /// @brief Element type of the element space mapped by the @c access function (no elements of this type are stored)
54 /// @brief Reference type yielded by calls to the @c access function
55 /// @note Despite the name, this type is not actually a reference, because @c access returns temporary values instead.
57 /// @brief Data handle type
58 using data_handle_type = typename NestedAccessor::data_handle_type;
59 /// @brief Offset policy type
61 // parasoft-end-suppress AUTOSAR-A2_10_1-e
62
63 // These first two assertions are equivalent to the ones for a general accessor policy (element_type must be a
64 // complete object type, not an abstract class, and not an array). Due to the decltype-based definition of
65 // element_type and C++'s rules about what can be returned from a function, the only illegal types that can be written
66 // at all are references (which fail the first assertion) and void (which fails the second).
67 static_assert(!std::is_reference<element_type>::value, "Element type must not be a reference");
68 static_assert(std::is_copy_constructible<reference>::value, "Reference type must be copy-constructible");
69 static_assert(arene::base::linalg::is_scalar_v<ScalingFactor>, "ScalingFactor must be a linalg scalar");
70 static_assert(arene::base::is_accessor_policy_v<NestedAccessor>, "NestedAccessor must be a valid accessor policy");
71
72 /// @brief Default constructor
73 constexpr scaled_accessor() = default;
74
75 /// @brief Converting constructor; implicit SFINAE overload for when the nested accessors are implicitly convertible
76 /// @tparam OtherNestedAccessor Accessor type of the other @c scaled_accessor
77 /// @param other A @c scaled_accessor with the same @c ScalingFactor type but possibly a different @c NestedAccessor
78 template <
79 class OtherNestedAccessor,
81 // NOLINTNEXTLINE(hicpp-explicit-conversions) Conditionally explicit when the nested accessor is
88
89 /// @brief Converting constructor; explicit SFINAE overload for when the nested accessors are explicitly constructible
90 /// @tparam OtherNestedAccessor Accessor type of the other @c scaled_accessor
91 /// @param other A @c scaled_accessor with the same @c ScalingFactor type but possibly a different @c NestedAccessor
92 template <
93 class OtherNestedAccessor,
103
104 /// @brief Construct a @c scaled_accessor from a scaling factor and an instance of the nested accessor
105 /// @param scale Specific scaling factor to use for this accessor
106 /// @param inner_accessor Instance of the nested accessor to use
107 constexpr scaled_accessor(ScalingFactor const& scale, NestedAccessor const& inner_accessor) noexcept(
110 )
111 : NestedAccessor(inner_accessor),
112 scaling_factor_{scale} {}
113
116 "-Wimplicit-int-float-conversion",
117 "The noexcept specification is unevaluated so it can't lose information"
118 );
119 ARENE_IGNORE_CLANG("-Wsign-conversion", "The noexcept specification is unevaluated so it can't lose information");
120 /// @brief Obtain the element at the specified index, multiplied by the scaling factor
121 /// @param handle Handle to the underlying data
122 /// @param index Offset from the first element
123 /// @return The <c>index</c>th element of the given data buffer, multiplied by @c scaling_factor()
124 //
125 /// @note Despite the return type name, the data returned by this function is *not* a reference, it's a value.
126 //
127 /// @warning This performs an unchecked multiplication of @c scaling_factor() and the accessed element. Depending on
128 /// their values, this may lead to infinities, NaNs, or undefined behaviour (e.g. from signed integer overflow).
129 constexpr auto access(data_handle_type handle, std::size_t index) const noexcept(noexcept(
131 typename NestedAccessor::element_type(
132 std::declval<decltype(std::declval<NestedAccessor>().access(handle, index))>()
133 )
134 )) -> reference {
136 return scaling_factor() * typename NestedAccessor::element_type(NestedAccessor::access(handle, index));
137 }
138
139 /// @brief apply an offset to a pointer
140 /// @param handle Handle to the underlying data
141 /// @param index offset to apply
142 /// @return data handle after applying the offset
143 constexpr auto offset(data_handle_type handle, std::size_t index) const
144 noexcept(noexcept(std::declval<NestedAccessor>().offset(handle, index))) ->
146 return NestedAccessor::offset(handle, index);
147 }
148
149 /// @brief Get the specific scaling factor used by this accessor
150 /// @return A reference to the scaling factor applied to all accessed data
151 constexpr auto scaling_factor() const noexcept -> ScalingFactor const& { return scaling_factor_; }
152
153 /// @brief Get the specific nested accessor used by this accessor
154 /// @return A reference to the underlying accessor used to get data references
155 constexpr auto nested_accessor() const noexcept -> NestedAccessor const& { return *this; }
156};
157
158namespace scaled_detail {
159
160/// @brief A version of the given @c mdspan type with its accessor replaced by one which scales by @c ScalingFactor
161/// @tparam Mdspan Type of the original @c mdspan type of which this typedef is a scaled version
162/// @tparam ScalingFactor Type of the scaling factor to apply to elements of an @c Mdspan instance
163template <class Mdspan, class ScalingFactor = typename Mdspan::value_type>
164using scaled_mdspan = arene::base::mdspan<
165 typename scaled_accessor<ScalingFactor, typename Mdspan::accessor_type>::element_type,
166 typename Mdspan::extents_type,
167 typename Mdspan::layout_type,
168 scaled_accessor<ScalingFactor, typename Mdspan::accessor_type>>;
169
170} // namespace scaled_detail
171
172/// @brief Return a version of the given mdspan instance that has an accessor which scales by @c scaling_factor
173/// @tparam ScalingFactor Type of the scaling factor to be used; normally deduced from @c scaling_factor
174/// @tparam ElementType Type of the elements of the original <c>mdspan</c>; normally deduced from @c original
175/// @tparam Extents Type of the extents of the original <c>mdspan</c>; normally deduced from @c original
176/// @tparam Layout Type of the layout of the original <c>mdspan</c>; normally deduced from @c original
177/// @tparam Accessor Type of the accessor of the original <c>mdspan</c>; normally deduced from @c original
178/// @param scaling_factor Specific amount by which to scale the elements of @c original
179/// @param original The original @c mdspan instance into whose elements this function will create a scaled view
180/// @return An @c mdspan instance with the same extents and layout as @c original but whose elements appear scaled
181template <class ScalingFactor, class ElementType, class Extents, class Layout, class Accessor>
182constexpr auto scaled(ScalingFactor scaling_factor, mdspan<ElementType, Extents, Layout, Accessor> original) noexcept(
183 noexcept(scaled_detail::scaled_mdspan<mdspan<ElementType, Extents, Layout, Accessor>, ScalingFactor>{
184 original.data_handle(),
185 original.mapping(),
186 {scaling_factor, original.accessor()}
187 })
188) -> scaled_detail::scaled_mdspan<mdspan<ElementType, Extents, Layout, Accessor>, ScalingFactor> {
189 return {original.data_handle(), original.mapping(), {scaling_factor, original.accessor()}};
190}
191
192} // namespace linalg
193} // namespace base
194} // namespace arene
195
196// parasoft-end-suppress AUTOSAR-M2_10_1-a
197
198#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SCALED_ACCESSOR_HPP_
ARENE_IGNORE_CLANG("-Wsign-conversion", "The noexcept specification is unevaluated so it can't lose information")
std::remove_const_t< element_type > reference
Reference type yielded by calls to the access function.
Definition scaled_accessor.hpp:56
constexpr scaled_accessor(scaled_accessor< ScalingFactor, OtherNestedAccessor > other) noexcept(std::is_nothrow_constructible< NestedAccessor, OtherNestedAccessor const & >::value &&std::is_nothrow_copy_constructible< ScalingFactor >::value)
Converting constructor; implicit SFINAE overload for when the nested accessors are implicitly convert...
Definition scaled_accessor.hpp:82
ARENE_IGNORE_CLANG("-Wimplicit-int-float-conversion", "The noexcept specification is unevaluated so it can't lose information")
constexpr scaled_accessor(ScalingFactor const &scale, NestedAccessor const &inner_accessor) noexcept(std::is_nothrow_copy_constructible< NestedAccessor >::value &&std::is_nothrow_copy_constructible< ScalingFactor >::value)
Construct a scaled_accessor from a scaling factor and an instance of the nested accessor.
Definition scaled_accessor.hpp:107
constexpr auto nested_accessor() const noexcept -> NestedAccessor const &
Definition scaled_accessor.hpp:155
constexpr auto offset(data_handle_type handle, std::size_t index) const noexcept(noexcept(std::declval< NestedAccessor >().offset(handle, index))) -> typename offset_policy::data_handle_type
apply an offset to a pointer
Definition scaled_accessor.hpp:143
const element_type
Element type of the element space mapped by the access function (no elements of this type are stored)
Definition scaled_accessor.hpp:52
scaled_accessor< ScalingFactor, typename NestedAccessor::offset_policy > offset_policy
Offset policy type.
Definition scaled_accessor.hpp:60
constexpr auto scaling_factor() const noexcept -> ScalingFactor const &
Definition scaled_accessor.hpp:151
constexpr scaled_accessor()=default
Default constructor.
constexpr auto access(data_handle_type handle, std::size_t index) const noexcept(noexcept(std::declval< ScalingFactor const & >() *typename NestedAccessor::element_type(std::declval< >()))) -> reference
Obtain the element at the specified index, multiplied by the scaling factor.
Definition scaled_accessor.hpp:129
typename NestedAccessor::data_handle_type data_handle_type
Data handle type.
Definition scaled_accessor.hpp:58
A multidimensional view of elements over a contiguous sequence of elements.
Definition mdspan.hpp:158
static constexpr value_type value
Definition integral_constant.hpp:34
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
Definition add.hpp:20
constexpr auto scaled(ScalingFactor scaling_factor, mdspan< ElementType, Extents, Layout, Accessor > original) noexcept(noexcept(scaled_detail::scaled_mdspan< mdspan< ElementType, Extents, Layout, Accessor >, ScalingFactor >{ original.data_handle(), original.mapping(), {scaling_factor, original.accessor()} })) -> scaled_detail::scaled_mdspan< mdspan< ElementType, Extents, Layout, Accessor >, ScalingFactor >
Return a version of the given mdspan instance that has an accessor which scales by scaling_factor.
Definition scaled_accessor.hpp:182
constexpr bool is_scalar_v
Determine if the given type is a scalar for the purposes of linalg algorithms.
Definition accumulate.hpp:37
constexpr bool is_accessor_policy_v
Check if a type is a valid accessor policy.
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
::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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
typename remove_const< Type >::type remove_const_t
remove const from the provided type. An alias for the modified type.
Definition remove_const.hpp:36