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 "False positive: also defines std::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_STDLIB_INCLUDE_STDLIB_DETAIL_INNER_PRODUCT_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INNER_PRODUCT_HPP_
9
10// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
11// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
12// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
13
14// IWYU pragma: private, include <numeric>
15// IWYU pragma: friend "stdlib_detail/.*"
16//
17#include "arene/base/algorithm/detail/generalized_transform_reduce.hpp"
18#include "arene/base/algorithm/detail/reduce_policy_copy_assign.hpp"
19#include "arene/base/algorithm/detail/traits.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/is_assignable.hpp"
23#include "arene/base/stdlib_choice/is_move_constructible.hpp"
24#include "arene/base/stdlib_choice/move.hpp"
25#include "arene/base/stdlib_choice/multiplies.hpp"
26#include "arene/base/stdlib_choice/plus.hpp"
30
31namespace std {
32
33// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
34/// @brief computes the inner product of two ranges of elements
35/// @tparam InputIt1 type of the iterator for the first range
36/// @tparam InputIt2 type of the iterator for the second range
37/// @tparam Value type of the accumulated value
38/// @tparam Plus type of the addition operation
39/// @tparam Multiplies type of the multiplication operation
40/// @param first1 iterator to the beginning of the first range
41/// @param last1 iterator to one past the end of the first range
42/// @param first2 iterator to the beginning of the second range
43/// @param init initial value to start with
44/// @param plus binary function object used to reduce transformed elements
45/// to a single value
46/// @param multiplies binary function object applied to each pair of elements
47/// of the input ranges
48///
49/// @return Initializes the accumulator @c acc (of type @c Value) with the
50/// initial value @c init and then modifies it with the expression
51/// <c> acc = plus(acc, multiplies(*i1, *i2)) </c> for every iterator
52/// @c i1 in the range <c> [first1, last1) </c> in order and its
53/// corresponding iterator @c i2 in the range beginning at @c first2.
54///
55/// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
56/// @pre @c plus does not invalidate subranges, nor modify elements in the
57/// ranges <c> [first1, last1) </c> and
58/// <c> [first2, first2 + distance(first1, last1)) </c>
59/// @pre @c multiplies does not invalidate subranges, nor modify elements in the
60/// ranges <c> [first1, last1) </c> and
61/// <c> [first2, first2 + distance(first1, last1)) </c>
62///
63/// @note Constraints <br>
64/// * @c InputIt1 must satisfy the input iterator requirements
65/// * @c InputIt2 must satisfy the input iterator requirements
66/// * @c Value is copy constructible
67/// * @c Value is copy assignable
68/// * <c> multiplies(*first1, *first2) </c> is a valid expression
69/// * <c> plus(init, multiplies(*first1, *first2)) </c> is assignable
70/// to @c Value
71///
72/// @note Complexity <br>
73/// O(N) applications of @c plus and @c multiplies, where
74/// <c> N = distance(first1, last1) </c>.
75///
76/// @note @c inner_product always performs the operations in the order given.
77///
78template <
79 class InputIt1,
80 class InputIt2,
81 class Value,
82 class Plus,
83 class Multiplies,
89 std::is_assignable<
90 Value&,
92 Plus&,
93 Value&,
95 Multiplies&,
96 arene::base::algorithm_detail::iter_reference_t<InputIt1>,
97 arene::base::algorithm_detail::iter_reference_t<InputIt2>
98 >
99 >
100 >::value>> = nullptr>
102 InputIt1 first1,
103 InputIt1 last1,
104 InputIt2 first2,
105 Value init,
106 Plus plus,
107 Multiplies multiplies
108) noexcept( //
111 arene::base::algorithm_detail::generalized_transform_reduce,
112 arene::base::algorithm_detail::reduce_policy_copy_assign<Value>,
113 InputIt1&,
114 InputIt1&,
115 InputIt2&,
116 Value,
117 Plus&,
118 Multiplies&>
119 )
120 -> Value
121{
122 using arene::base::algorithm_detail::generalized_transform_reduce;
123 using policy = arene::base::algorithm_detail::reduce_policy_copy_assign<Value>;
124
125 return policy::access( //
126 generalized_transform_reduce{}( //
127 policy{},
128 first1,
129 last1,
130 first2,
131 std::move(init),
132 plus,
134 )
135 );
136}
137
138/// @brief computes the inner product of two ranges of elements
139/// @tparam InputIt1 type of the iterator for the first range
140/// @tparam InputIt2 type of the iterator for the second range
141/// @tparam Value type of the accumulated value
142/// @param first1 iterator to the beginning of the first range
143/// @param last1 iterator to one past the end of the first range
144/// @param first2 iterator to the beginning of the second range
145/// @param init initial value to start with
146///
147/// @return Initializes the accumulator @c acc (of type @c Value) with the
148/// initial value @c init and then modifies it with the expression
149/// <c> acc = plus(acc, multiplies(*i1, *i2)) </c> for every iterator
150/// @c i1 in the range <c> [first1, last1) </c> in order and its
151/// corresponding iterator @c i2 in the range beginning at @c first2.
152///
153/// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
154/// @pre binary @c operator+ does not invalidate subranges, nor modify elements
155/// in the ranges <c> [first1, last1) </c> and
156/// <c> [first2, first2 + distance(first1, last1)) </c>
157/// @pre binary @c operator* does not invalidate subranges, nor modify elements
158/// in the ranges <c> [first1, last1) </c> and
159/// <c> [first2, first2 + distance(first1, last1)) </c>
160///
161/// @note Constraints <br>
162/// * @c InputIt1 must satisfy the input iterator requirements
163/// * @c InputIt2 must satisfy the input iterator requirements
164/// * @c Value is copy constructible
165/// * @c Value is copy assignable
166/// * <c> *first1 * *first2 </c> is a valid expression
167/// * <c> init + multiplies(*first1, *first2) </c> is assignable
168/// to @c Value
169///
170/// @note Complexity <br>
171/// O(N) applications of @c plus and @c multiplies, where
172/// <c> N = distance(first1, last1) </c>.
173///
174/// @note @c inner_product always performs the operations in the order given.
175///
176template <
177 class InputIt1,
178 class InputIt2,
179 class Value,
185 Value&,
187 plus<>&,
188 Value&,
190 multiplies<>&,
191 arene::base::algorithm_detail::iter_reference_t<InputIt1>,
192 arene::base::algorithm_detail::iter_reference_t<InputIt2>>>>::value>> = nullptr>
194 InputIt1 first1,
195 InputIt1 last1,
196 InputIt2 first2,
197 Value init
198) noexcept( //
199 noexcept(std::inner_product(
206 ))
207) -> Value {
208 return std::inner_product(
209 std::move(first1),
210 std::move(last1),
211 std::move(first2),
212 std::move(init),
213 std::plus<>{},
215 );
216}
217
218} // namespace std
219
220#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INNER_PRODUCT_HPP_
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if an instance of To is assignable to from arguments of From.
Definition is_assignable.hpp:47
Function object for performing multiplication.
Definition multiplies.hpp:27
Function object for performing addition.
Definition plus.hpp:27
Public export header for the constraints subpackage.
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 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
STL namespace.
auto inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, Value init, Plus plus, Multiplies multiplies) noexcept(//std::is_nothrow_move_constructible< Value >::value &&arene::base::is_nothrow_invocable_v< arene::base::algorithm_detail::generalized_transform_reduce, arene::base::algorithm_detail::reduce_policy_copy_assign< Value >, InputIt1 &, InputIt1 &, InputIt2 &, Value, Plus &, Multiplies & >) -> Value
computes the inner product of two ranges of elements
Definition inner_product.hpp:101
constexpr Type integral_constant< Type, Value >::value
Out-of-line definition for the value constant.
Definition integral_constant.hpp:64
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...