Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
equal_elements.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::equal_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_EQUAL_ELEMENTS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_EQUAL_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"
16#include "arene/base/stdlib_choice/equal_to.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 equal_elements_detail {
28
29/// @brief Class to implement @c equal_elements
30class equal_elements_fn {
31 public:
32 /// @brief Check if the elements of two @c mdspan instantations are equal according to the supplied @c Comparator
33 /// @tparam LhsSpan The type of the first @c mdspan
34 /// @tparam RhsSpan The type of the second @c mdspan
35 /// @tparam Comparator The type of the comparator
36 /// @param lhs_span The first @c mdspan
37 /// @param rhs_span The second @c mdspan
38 /// @param comp The instance of the comparator to use
39 /// @pre @c lhs_span and @c rhs_span are both instantiations of @c mdspan with matching extents
40 /// @return @c true if @c lhs_span[indices] is equal to @c rhs_span[indices] for each valid @c indices array given the
41 /// @c extents, @c false otherwise
42 template <
43 typename LhsSpan,
44 typename RhsSpan,
45 typename Comparator = std::equal_to<>,
47 linalg_detail::possibly_comparable_constraints<LhsSpan, RhsSpan>,
49 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: All parameters are used"
50 constexpr auto operator()(LhsSpan const lhs_span, RhsSpan const rhs_span, Comparator comp = {}) const noexcept( //
52 &&
53 is_nothrow_invocable_v<LhsSpan const&, array<typename LhsSpan::index_type, LhsSpan::rank()>> &&
54 is_nothrow_invocable_v<RhsSpan const&, array<typename RhsSpan::index_type, RhsSpan::rank()>>
55) -> bool {
56 // parasoft-end-suppress AUTOSAR-A0_1_4-a
57 ARENE_PRECONDITION(lhs_span.extents() == rhs_span.extents());
58 if (lhs_span.empty()) {
59 return true;
60 }
61 auto const extents_array = mdspan_detail::to_array(lhs_span.extents());
62 // parasoft-begin-suppress AUTOSAR-M0_1_3 "False positive: All local variables are used"
63 for (auto& indices : arene::base::detail::make_index_permutation_range(extents_array)) {
64 // parasoft-end-suppress AUTOSAR-M0_1_3
65 if (!comp(lhs_span(indices), rhs_span(indices))) {
66 return false;
67 }
68 }
69 return true;
70 }
71};
72} // namespace equal_elements_detail
73
74/// @brief Compare the corresponding elements of two multidimensional views for equality.
75// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
76// object used in multiple TUs."
77// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
78// object used in multiple TUs."
79ARENE_CPP14_INLINE_VARIABLE(equal_elements_detail::equal_elements_fn, equal_elements);
80// parasoft-end-suppress AUTOSAR-M7_3_3-a
81// parasoft-end-suppress CERT_CPP-DCL59-a
82
83} // namespace linalg
84} // namespace base
85} // namespace arene
86
87#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_EQUAL_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
Definition accumulate.hpp:37
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
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