xref: /llvm-project/libcxx/include/__numeric/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_REDUCE_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_REDUCE_H
12a0efb175SChristopher Di Bella 
13a0efb175SChristopher Di Bella #include <__config>
14a0efb175SChristopher Di Bella #include <__functional/operations.h>
15a0efb175SChristopher Di Bella #include <__iterator/iterator_traits.h>
162b2e7f6eSNikolas Klauser #include <__utility/move.h>
17a0efb175SChristopher Di Bella 
18a0efb175SChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19a0efb175SChristopher Di Bella #  pragma GCC system_header
20a0efb175SChristopher Di Bella #endif
21a0efb175SChristopher Di Bella 
22*7b462251SLouis Dionne _LIBCPP_PUSH_MACROS
23*7b462251SLouis Dionne #include <__undef_macros>
24*7b462251SLouis Dionne 
25a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
26a0efb175SChristopher Di Bella 
274f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
28a0efb175SChristopher Di Bella template <class _InputIterator, class _Tp, class _BinaryOp>
299783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
reduce(_InputIterator __first,_InputIterator __last,_Tp __init,_BinaryOp __b)309783f28cSLouis Dionne reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) {
31a0efb175SChristopher Di Bella   for (; __first != __last; ++__first)
322b2e7f6eSNikolas Klauser     __init = __b(std::move(__init), *__first);
33a0efb175SChristopher Di Bella   return __init;
34a0efb175SChristopher Di Bella }
35a0efb175SChristopher Di Bella 
36a0efb175SChristopher Di Bella template <class _InputIterator, class _Tp>
379783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
reduce(_InputIterator __first,_InputIterator __last,_Tp __init)389783f28cSLouis Dionne reduce(_InputIterator __first, _InputIterator __last, _Tp __init) {
3977a00c0dSLouis Dionne   return std::reduce(__first, __last, __init, std::plus<>());
40a0efb175SChristopher Di Bella }
41a0efb175SChristopher Di Bella 
42a0efb175SChristopher Di Bella template <class _InputIterator>
434c198542SLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type
reduce(_InputIterator __first,_InputIterator __last)44a0efb175SChristopher Di Bella reduce(_InputIterator __first, _InputIterator __last) {
4577a00c0dSLouis Dionne   return std::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{});
46a0efb175SChristopher Di Bella }
47a0efb175SChristopher Di Bella #endif
48a0efb175SChristopher Di Bella 
49a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
50a0efb175SChristopher Di Bella 
51*7b462251SLouis Dionne _LIBCPP_POP_MACROS
52*7b462251SLouis Dionne 
53a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_REDUCE_H
54