xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/inclusive_scan.h (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
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_INCLUSIVE_SCAN_H
11 #define _LIBCPP___NUMERIC_INCLUSIVE_SCAN_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_BEGIN_NAMESPACE_STD
23 
24 #if _LIBCPP_STD_VER >= 17
25 
26 template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
27 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
28 inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOp __b, _Tp __init) {
29   for (; __first != __last; ++__first, (void)++__result) {
30     __init    = __b(__init, *__first);
31     *__result = __init;
32   }
33   return __result;
34 }
35 
36 template <class _InputIterator, class _OutputIterator, class _BinaryOp>
37 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
38 inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOp __b) {
39   if (__first != __last) {
40     typename iterator_traits<_InputIterator>::value_type __init = *__first;
41     *__result++                                                 = __init;
42     if (++__first != __last)
43       return std::inclusive_scan(__first, __last, __result, __b, __init);
44   }
45 
46   return __result;
47 }
48 
49 template <class _InputIterator, class _OutputIterator>
50 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
51 inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
52   return std::inclusive_scan(__first, __last, __result, std::plus<>());
53 }
54 
55 #endif // _LIBCPP_STD_VER >= 17
56 
57 _LIBCPP_END_NAMESPACE_STD
58 
59 #endif // _LIBCPP___NUMERIC_INCLUSIVE_SCAN_H
60