Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
swap_rows.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::swap_rows"
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_ROWS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SWAP_ROWS_HPP_
7
13#include "arene/base/stdlib_choice/enable_if.hpp"
16
17// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
18// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
19
20namespace arene {
21namespace base {
22namespace linalg {
23
24namespace swap_rows_detail {
25
26/// @brief Class to implement @c swap_rows
27class swap_rows_fn {
28 public:
29 /// @brief Swap two rows of a single linalg matrix with each other
30 /// @tparam Matrix Type of the matrix whose rows should be swapped; should be rank 2 with a swappable value type
31 /// @tparam IndexI Type of the first row index to use; using a @c std::integral_constant skips runtime bounds checks
32 /// @tparam IndexJ Type of the second row index to use; using a @c std::integral_constant skips runtime bounds checks
33 /// @param matrix Matrix whose rows should be swapped
34 /// @param row_index_i Index of the first row to swap
35 /// @param row_index_j Index of the second row to swap
36 /// @note If <c>row_index_i == row_index_j</c> no swap is performed, even if the swap function would have side effects
37 /// @pre both @c row_index_i and @c row_index_j are <c>< matrix.extent(0)</c>, otherwise precondition violation
38 template <
39 typename Matrix,
40 typename IndexI,
41 typename IndexJ,
44 std::enable_if_t<is_invocable_v<decltype(row_view), Matrix, IndexI>>,
45 std::enable_if_t<is_invocable_v<decltype(row_view), Matrix, IndexJ>>,
47 constexpr auto operator()(Matrix matrix, IndexI row_index_i, IndexJ row_index_j) const
49 auto row_i = row_view(matrix, row_index_i);
50 auto row_j = row_view(matrix, row_index_j);
51
52 if (row_index_i != row_index_j) {
53 swap_elements(row_i, row_j);
54 }
55 }
56};
57
58} // namespace swap_rows_detail
59
60/// @brief Swap two rows of a matrix.
61// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
62// object used in multiple TUs."
63// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
64// object used in multiple TUs."
65ARENE_CPP14_INLINE_VARIABLE(swap_rows_detail::swap_rows_fn, swap_rows);
66// parasoft-end-suppress AUTOSAR-M7_3_3-a
67// parasoft-end-suppress CERT_CPP-DCL59-a
68
69} // namespace linalg
70} // namespace base
71} // namespace arene
72
73#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_SWAP_ROWS_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition add.hpp:20
Definition accumulate.hpp:37
constexpr bool is_nothrow_swappable_v
Trait query indicating if a type can be noexcept-swapped.
constexpr bool is_invocable_v
Determine if a functor type is 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
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