xref: /llvm-project/libcxx/include/__numeric/transform_reduce.h (revision 7b4622514d232ce5f7110dd8b20d90e81127c467)
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_TRANSFORM_REDUCE_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_TRANSFORM_REDUCE_H
12a0efb175SChristopher Di Bella 
13a0efb175SChristopher Di Bella #include <__config>
14a0efb175SChristopher Di Bella #include <__functional/operations.h>
15a0efb175SChristopher Di Bella #include <__utility/move.h>
16a0efb175SChristopher Di Bella 
17a0efb175SChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18a0efb175SChristopher Di Bella #  pragma GCC system_header
19a0efb175SChristopher Di Bella #endif
20a0efb175SChristopher Di Bella 
21*7b462251SLouis Dionne _LIBCPP_PUSH_MACROS
22*7b462251SLouis Dionne #include <__undef_macros>
23*7b462251SLouis Dionne 
24a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
25a0efb175SChristopher Di Bella 
264f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
27a0efb175SChristopher Di Bella template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
289783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
transform_reduce(_InputIterator __first,_InputIterator __last,_Tp __init,_BinaryOp __b,_UnaryOp __u)299783f28cSLouis Dionne transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b, _UnaryOp __u) {
30a0efb175SChristopher Di Bella   for (; __first != __last; ++__first)
31ee6ec2c5SNikolas Klauser     __init = __b(std::move(__init), __u(*__first));
32a0efb175SChristopher Di Bella   return __init;
33a0efb175SChristopher Di Bella }
34a0efb175SChristopher Di Bella 
35a0efb175SChristopher Di Bella template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOp1, class _BinaryOp2>
transform_reduce(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init,_BinaryOp1 __b1,_BinaryOp2 __b2)369783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp transform_reduce(
379783f28cSLouis Dionne     _InputIterator1 __first1,
38a0efb175SChristopher Di Bella     _InputIterator1 __last1,
399783f28cSLouis Dionne     _InputIterator2 __first2,
409783f28cSLouis Dionne     _Tp __init,
419783f28cSLouis Dionne     _BinaryOp1 __b1,
429783f28cSLouis Dionne     _BinaryOp2 __b2) {
43a0efb175SChristopher Di Bella   for (; __first1 != __last1; ++__first1, (void)++__first2)
44ee6ec2c5SNikolas Klauser     __init = __b1(std::move(__init), __b2(*__first1, *__first2));
45a0efb175SChristopher Di Bella   return __init;
46a0efb175SChristopher Di Bella }
47a0efb175SChristopher Di Bella 
48a0efb175SChristopher Di Bella template <class _InputIterator1, class _InputIterator2, class _Tp>
499783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
transform_reduce(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init)509783f28cSLouis Dionne transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) {
519783f28cSLouis Dionne   return std::transform_reduce(__first1, __last1, __first2, std::move(__init), std::plus<>(), std::multiplies<>());
52a0efb175SChristopher Di Bella }
53a0efb175SChristopher Di Bella #endif
54a0efb175SChristopher Di Bella 
55a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
56a0efb175SChristopher Di Bella 
57*7b462251SLouis Dionne _LIBCPP_POP_MACROS
58*7b462251SLouis Dionne 
59a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_TRANSFORM_REDUCE_H
60