Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
inner_product.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::inner_product"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_INNER_PRODUCT_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_INNER_PRODUCT_HPP_
9
10// IWYU pragma: private, include "arene/base/algorithm.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/algorithm/detail/generalized_transform_reduce.hpp"
17#include "arene/base/algorithm/detail/reduce_policy_move_assign.hpp"
18#include "arene/base/algorithm/detail/synthesize_range1_overload.hpp" // IWYU pragma: export
19#include "arene/base/algorithm/detail/traits.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/is_assignable.hpp"
24#include "arene/base/stdlib_choice/is_move_constructible.hpp"
25#include "arene/base/stdlib_choice/move.hpp"
26#include "arene/base/stdlib_choice/multiplies.hpp"
27#include "arene/base/stdlib_choice/plus.hpp"
31// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
32
33// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
34
35// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
36
37namespace arene {
38namespace base {
39namespace algorithm_detail {
40namespace inner_product_detail {
41
42/// @brief function object implementing 'inner_product'
43class inner_product_fn {
44 public:
45 /// @brief computes the inner product of two ranges of elements
46 /// @tparam InputIt1 type of the iterator for the first range
47 /// @tparam InputIt2 type of the iterator for the second range
48 /// @tparam Value type of the accumulated value
49 /// @tparam Plus type of the addition operation
50 /// @tparam Multiplies type of the multiplication operation
51 /// @param first1 iterator to the beginning of the first range
52 /// @param last1 iterator to one past the end of the first range
53 /// @param first2 iterator to the beginning of the second range
54 /// @param init initial value to start with
55 /// @param plus binary function object used to reduce transformed elements
56 /// to a single value
57 /// @param multiplies binary function object applied to each pair of elements
58 /// of the input ranges
59 ///
60 /// @return Initializes the accumulator @c acc (of type @c Value) with the
61 /// initial value @c init and then modifies it with the expression
62 /// <c> acc = plus(std::move(acc), multiplies(*i1, *i2)) </c> for every iterator
63 /// @c i1 in the range <c> [first1, last1) </c> in order and its
64 /// corresponding iterator @c i2 in the range beginning at @c first2.
65 ///
66 /// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
67 /// @pre @c plus does not invalidate subranges, nor modify elements in the
68 /// ranges <c> [first1, last1) </c> and
69 /// <c> [first2, first2 + distance(first1, last1)) </c>
70 /// @pre @c multiplies does not invalidate subranges, nor modify elements in the
71 /// ranges <c> [first1, last1) </c> and
72 /// <c> [first2, first2 + distance(first1, last1)) </c>
73 ///
74 /// @note Constraints <br>
75 /// * @c InputIt1 must satisfy the input iterator requirements
76 /// * @c InputIt2 must satisfy the input iterator requirements
77 /// * @c Value is copy constructible
78 /// * @c Value is copy assignable
79 /// * <c> multiplies(*first1, *first2) </c> is a valid expression
80 /// * <c> plus(init, multiplies(*first1, *first2)) </c> is assignable
81 /// to @c Value
82 ///
83 /// @note Complexity <br>
84 /// O(N) applications of @c plus and @c multiplies, where
85 /// <c> N = distance(first1, last1) </c>.
86 ///
87 /// @note Unlike @c transform_reduce, @c inner_product always performs the
88 /// operations in the order given.
89 ///
90 template <
91 class InputIt1,
92 class InputIt2,
93 class Value,
94 class Plus,
95 class Multiplies,
101 std::is_assignable<
102 Value&,
104 Plus&,
105 Value&&,
107 Multiplies&,
110 >
111 >
112 >::value>> = nullptr>
113 constexpr auto operator()(
114 InputIt1 first1,
115 InputIt1 last1,
116 InputIt2 first2,
117 Value init,
118 Plus plus,
119 Multiplies multiplies
120 ) const noexcept( //
123 generalized_transform_reduce,
124 reduce_policy_move_assign<Value>,
125 InputIt1&,
126 InputIt1&,
127 InputIt2&,
128 Value,
129 Plus&,
130 Multiplies&>
131 )
132 -> Value
133 {
134 using policy = reduce_policy_move_assign<Value>;
135
136 return policy::access( //
137 generalized_transform_reduce{}( //
138 policy{},
139 first1,
140 last1,
141 first2,
142 std::move(init),
143 plus,
144 multiplies
145 )
146 );
147 }
148
149 /// @brief computes the inner product of two ranges of elements
150 /// @tparam InputIt1 type of the iterator for the first range
151 /// @tparam InputIt2 type of the iterator for the second range
152 /// @tparam Value type of the accumulated value
153 /// @param first1 iterator to the beginning of the first range
154 /// @param last1 iterator to one past the end of the first range
155 /// @param first2 iterator to the beginning of the second range
156 /// @param init initial value to start with
157 ///
158 /// @return Initializes the accumulator @c acc (of type @c Value) with the
159 /// initial value @c init and then modifies it with the expression
160 /// <c> acc = std::move(acc) + (*i1 * *i2)) </c> for every iterator
161 /// @c i1 in the range <c> [first1, last1) </c> in order and its
162 /// corresponding iterator @c i2 in the range beginning at @c first2.
163 ///
164 /// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
165 /// @pre binary @c operator+ does not invalidate subranges, nor modify
166 /// elements in the ranges <c> [first1, last1) </c> and
167 /// <c> [first2, first2 + distance(first1, last1)) </c>
168 /// @pre binary @c operator* does not invalidate subranges, nor modify
169 /// elements in the ranges <c> [first1, last1) </c> and
170 /// <c> [first2, first2 + distance(first1, last1)) </c>
171 ///
172 /// @note Constraints <br>
173 /// * @c InputIt1 must satisfy the input iterator requirements
174 /// * @c InputIt2 must satisfy the input iterator requirements
175 /// * @c Value is copy constructible
176 /// * @c Value is copy assignable
177 /// * <c> *first1 * *first2 </c> is a valid expression
178 /// * <c> init + multiplies(*first1, *first2) </c> is assignable
179 /// to @c Value
180 ///
181 /// @note Complexity <br>
182 /// O(N) applications of @c plus and @c multiplies, where
183 /// <c> N = distance(first1, last1) </c>.
184 ///
185 /// @note Unlike @c transform_reduce, @c inner_product always performs the
186 /// operations in the order given.
187 ///
188 template <
189 class InputIt1,
190 class InputIt2,
191 class Value,
196 std::enable_if_t<std::is_assignable<
197 Value&,
199 std::plus<>&,
200 Value&&,
202 std::multiplies<>&,
204 iter_reference_t<InputIt2>>>>::value>> = nullptr>
205 constexpr auto operator()( //
206 InputIt1 first1,
207 InputIt1 last1,
208 InputIt2 first2,
209 Value init
210 ) const
211 noexcept(is_nothrow_invocable_v<
212 inner_product_fn,
213 InputIt1&&,
214 InputIt1&&,
215 InputIt2&&,
216 Value&&,
217 std::plus<>&&,
218 std::multiplies<>&&>) -> Value {
219 return (*this)(
220 std::move(first1),
221 std::move(last1),
222 std::move(first2),
223 std::move(init),
224 std::plus<>{},
225 std::multiplies<>{}
226 );
227 }
228};
229} // namespace inner_product_detail
230} // namespace algorithm_detail
231
232/// @brief computes the inner product of two ranges of elements
233// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
234// object used in multiple TUs."
235// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
236// object used in multiple TUs."
237// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
239 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::inner_product_detail::inner_product_fn>,
240 inner_product
241);
242// parasoft-end-suppress CERT_CPP-DCL56-a
243// parasoft-end-suppress AUTOSAR-M7_3_3-a
244// parasoft-end-suppress CERT_CPP-DCL59-a
245
246} // namespace base
247} // namespace arene
248
249#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_INNER_PRODUCT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename iter_reference_detail::iter_reference_impl< Itr >::type iter_reference_t
Determins the reference type of the given iterator.
Definition iter_reference_t.hpp:47
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
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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