Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
swap_elements.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::swap_elements"
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_LINALG_SWAP_ELEMENTS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SWAP_ELEMENTS_HPP_
7
12#include "arene/base/detail/index_permutation_range.hpp"
13#include "arene/base/linalg/detail/mandates.hpp"
14#include "arene/base/mdspan/detail/to_array.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
19
20// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
21// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
22
23namespace arene {
24namespace base {
25namespace linalg {
26
27namespace swap_elements_detail {
28
29/// @brief Class to implement @c swap_elements
30class swap_elements_fn {
31 public:
32 /// @brief Swap the elements of two <c>mdspan</c>s with each other
33 /// @tparam LeftSpan The type of the left @c mdspan, must be the same rank as @c RightSpan
34 /// @tparam RightSpan The type of the right @c mdspan, must be the same rank as @c LeftSpan
35 /// @param left The first @c mdspan to swap elements from
36 /// @param right The second @c mdspan to swap elements from
37 /// @note This corresponds to the SSWAP and DSWAP functions from BLAS 1 (xSWAP).
38 /// @pre <c>left.extents() == right.extents()</c>, otherwise precondition violation
39 /// @pre if @c left and @c right overlap, they must alias
40 template <
41 typename LeftSpan,
42 typename RightSpan,
44 linalg_detail::possibly_comparable_constraints<LeftSpan, RightSpan>,
47 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: All parameters are used"
48 constexpr auto operator()(LeftSpan const left, RightSpan const right) const noexcept( //
50 is_nothrow_invocable_v<LeftSpan const&, array<typename LeftSpan::index_type, LeftSpan::rank()>> &&
51 is_nothrow_invocable_v<RightSpan const&, array<typename LeftSpan::index_type, RightSpan::rank()>>
52 ) -> void {
53 // parasoft-end-suppress AUTOSAR-A0_1_4-a
54 ARENE_PRECONDITION(left.extents() == right.extents());
55 if (left.empty()) {
56 return;
57 }
58 auto const extents_array = mdspan_detail::to_array(left.extents());
59 // parasoft-begin-suppress AUTOSAR-M0_1_3 "False positive: All local variables are used"
60 for (auto const& indices : arene::base::detail::make_index_permutation_range(extents_array)) {
61 arene::base::swap(left(indices), right(indices));
62 }
63 // parasoft-end-suppress AUTOSAR-M0_1_3
64 }
65};
66} // namespace swap_elements_detail
67
68/// @brief Swap corresponding elements of two multidimensional views.
69// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
70// object used in multiple TUs."
71// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
72// object used in multiple TUs."
73ARENE_CPP14_INLINE_VARIABLE(swap_elements_detail::swap_elements_fn, swap_elements);
74// parasoft-end-suppress AUTOSAR-M7_3_3-a
75// parasoft-end-suppress CERT_CPP-DCL59-a
76
77} // namespace linalg
78} // namespace base
79} // namespace arene
80
81#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SWAP_ELEMENTS_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr bool is_nothrow_swappable_with_v
Trait query indicating if two types can be noexcept-swapped.
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
constexpr bool is_swappable_with_v
Trait query indicating two types can be swapped.
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
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