1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef _LIBCPP___NUMERIC_REDUCE_H |
11 | #define _LIBCPP___NUMERIC_REDUCE_H |
12 | |
13 | #include <__config> |
14 | #include <__functional/operations.h> |
15 | #include <__iterator/iterator_traits.h> |
16 | #include <__utility/move.h> |
17 | |
18 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
19 | # pragma GCC system_header |
20 | #endif |
21 | |
22 | _LIBCPP_PUSH_MACROS |
23 | #include <__undef_macros> |
24 | |
25 | _LIBCPP_BEGIN_NAMESPACE_STD |
26 | |
27 | #if _LIBCPP_STD_VER >= 17 |
28 | template <class _InputIterator, class _Tp, class _BinaryOp> |
29 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp |
30 | reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) { |
31 | for (; __first != __last; ++__first) |
32 | __init = __b(std::move(__init), *__first); |
33 | return __init; |
34 | } |
35 | |
36 | template <class _InputIterator, class _Tp> |
37 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp |
38 | reduce(_InputIterator __first, _InputIterator __last, _Tp __init) { |
39 | return std::reduce(__first, __last, __init, std::plus<>()); |
40 | } |
41 | |
42 | template <class _InputIterator> |
43 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type |
44 | reduce(_InputIterator __first, _InputIterator __last) { |
45 | return std::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{}); |
46 | } |
47 | #endif |
48 | |
49 | _LIBCPP_END_NAMESPACE_STD |
50 | |
51 | _LIBCPP_POP_MACROS |
52 | |
53 | #endif // _LIBCPP___NUMERIC_REDUCE_H |
54 | |