xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/reduce.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 #if _LIBCPP_STD_VER > 14
24 template <class _InputIterator, class _Tp, class _BinaryOp>
25 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp reduce(_InputIterator __first, _InputIterator __last,
26                                                                    _Tp __init, _BinaryOp __b) {
27   for (; __first != __last; ++__first)
28     __init = __b(__init, *__first);
29   return __init;
30 }
31 
32 template <class _InputIterator, class _Tp>
33 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _Tp reduce(_InputIterator __first, _InputIterator __last,
34                                                                    _Tp __init) {
35   return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
36 }
37 
38 template <class _InputIterator>
39 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 typename iterator_traits<_InputIterator>::value_type
40 reduce(_InputIterator __first, _InputIterator __last) {
41   return _VSTD::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{});
42 }
43 #endif
44 
45 _LIBCPP_END_NAMESPACE_STD
46 
47 #endif // _LIBCPP___NUMERIC_REDUCE_H
48