xref: /llvm-project/libcxx/include/__numeric/inclusive_scan.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
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_INCLUSIVE_SCAN_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_INCLUSIVE_SCAN_H
12a0efb175SChristopher Di Bella 
13a0efb175SChristopher Di Bella #include <__config>
14a0efb175SChristopher Di Bella #include <__functional/operations.h>
15a0efb175SChristopher Di Bella #include <__iterator/iterator_traits.h>
16a0efb175SChristopher Di Bella #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 
22a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
23a0efb175SChristopher Di Bella 
244f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
25a0efb175SChristopher Di Bella 
26a0efb175SChristopher Di Bella template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
274c198542SLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
inclusive_scan(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOp __b,_Tp __init)28a0efb175SChristopher Di Bella inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOp __b, _Tp __init) {
29a0efb175SChristopher Di Bella   for (; __first != __last; ++__first, (void)++__result) {
30a0efb175SChristopher Di Bella     __init    = __b(__init, *__first);
31a0efb175SChristopher Di Bella     *__result = __init;
32a0efb175SChristopher Di Bella   }
33a0efb175SChristopher Di Bella   return __result;
34a0efb175SChristopher Di Bella }
35a0efb175SChristopher Di Bella 
36a0efb175SChristopher Di Bella template <class _InputIterator, class _OutputIterator, class _BinaryOp>
374c198542SLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
inclusive_scan(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOp __b)38a0efb175SChristopher Di Bella inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOp __b) {
39a0efb175SChristopher Di Bella   if (__first != __last) {
40a0efb175SChristopher Di Bella     typename iterator_traits<_InputIterator>::value_type __init = *__first;
41a0efb175SChristopher Di Bella     *__result++                                                 = __init;
42a0efb175SChristopher Di Bella     if (++__first != __last)
4377a00c0dSLouis Dionne       return std::inclusive_scan(__first, __last, __result, __b, __init);
44a0efb175SChristopher Di Bella   }
45a0efb175SChristopher Di Bella 
46a0efb175SChristopher Di Bella   return __result;
47a0efb175SChristopher Di Bella }
48a0efb175SChristopher Di Bella 
49a0efb175SChristopher Di Bella template <class _InputIterator, class _OutputIterator>
50*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
inclusive_scan(_InputIterator __first,_InputIterator __last,_OutputIterator __result)51*9783f28cSLouis Dionne inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
5277a00c0dSLouis Dionne   return std::inclusive_scan(__first, __last, __result, std::plus<>());
53a0efb175SChristopher Di Bella }
54a0efb175SChristopher Di Bella 
554f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 17
56a0efb175SChristopher Di Bella 
57a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
58a0efb175SChristopher Di Bella 
59a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_INCLUSIVE_SCAN_H
60