xref: /llvm-project/libcxx/include/__numeric/inner_product.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1a0efb175SChristopher Di Bella // -*- C++ -*-
2a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
3a0efb175SChristopher Di Bella //
4a0efb175SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5a0efb175SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
6a0efb175SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7a0efb175SChristopher Di Bella //
8a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
9a0efb175SChristopher Di Bella 
10a0efb175SChristopher Di Bella #ifndef _LIBCPP___NUMERIC_INNER_PRODUCT_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_INNER_PRODUCT_H
12a0efb175SChristopher Di Bella 
13a0efb175SChristopher Di Bella #include <__config>
14a0efb175SChristopher Di Bella #include <__utility/move.h>
15a0efb175SChristopher Di Bella 
16a0efb175SChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17a0efb175SChristopher Di Bella #  pragma GCC system_header
18a0efb175SChristopher Di Bella #endif
19a0efb175SChristopher Di Bella 
2092e4d679SNicole Rabjohn _LIBCPP_PUSH_MACROS
2192e4d679SNicole Rabjohn #include <__undef_macros>
2292e4d679SNicole Rabjohn 
23a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
24a0efb175SChristopher Di Bella 
25a0efb175SChristopher Di Bella template <class _InputIterator1, class _InputIterator2, class _Tp>
26*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init)27*9783f28cSLouis Dionne inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) {
28a0efb175SChristopher Di Bella   for (; __first1 != __last1; ++__first1, (void)++__first2)
294f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
3077a00c0dSLouis Dionne     __init = std::move(__init) + *__first1 * *__first2;
31a0efb175SChristopher Di Bella #else
32a0efb175SChristopher Di Bella     __init = __init + *__first1 * *__first2;
33a0efb175SChristopher Di Bella #endif
34a0efb175SChristopher Di Bella   return __init;
35a0efb175SChristopher Di Bella }
36a0efb175SChristopher Di Bella 
37a0efb175SChristopher Di Bella template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init,_BinaryOperation1 __binary_op1,_BinaryOperation2 __binary_op2)38*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp inner_product(
39*9783f28cSLouis Dionne     _InputIterator1 __first1,
40*9783f28cSLouis Dionne     _InputIterator1 __last1,
41*9783f28cSLouis Dionne     _InputIterator2 __first2,
42*9783f28cSLouis Dionne     _Tp __init,
43*9783f28cSLouis Dionne     _BinaryOperation1 __binary_op1,
44*9783f28cSLouis Dionne     _BinaryOperation2 __binary_op2) {
45a0efb175SChristopher Di Bella   for (; __first1 != __last1; ++__first1, (void)++__first2)
464f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
4777a00c0dSLouis Dionne     __init = __binary_op1(std::move(__init), __binary_op2(*__first1, *__first2));
48a0efb175SChristopher Di Bella #else
49a0efb175SChristopher Di Bella     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
50a0efb175SChristopher Di Bella #endif
51a0efb175SChristopher Di Bella   return __init;
52a0efb175SChristopher Di Bella }
53a0efb175SChristopher Di Bella 
54a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
55a0efb175SChristopher Di Bella 
5692e4d679SNicole Rabjohn _LIBCPP_POP_MACROS
5792e4d679SNicole Rabjohn 
58a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_INNER_PRODUCT_H
59