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_EXCLUSIVE_SCAN_H
11#define _LIBCPP___NUMERIC_EXCLUSIVE_SCAN_H
12
13#include <__config>
14#include <__functional/operations.h>
15#include <__utility/move.h>
16
17#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18#  pragma GCC system_header
19#endif
20
21_LIBCPP_PUSH_MACROS
22#include <__undef_macros>
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26#if _LIBCPP_STD_VER >= 17
27
28template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
29_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
30exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOp __b) {
31  if (__first != __last) {
32    _Tp __tmp(__b(__init, *__first));
33    while (true) {
34      *__result = std::move(__init);
35      ++__result;
36      ++__first;
37      if (__first == __last)
38        break;
39      __init = std::move(__tmp);
40      __tmp  = __b(__init, *__first);
41    }
42  }
43  return __result;
44}
45
46template <class _InputIterator, class _OutputIterator, class _Tp>
47_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
48exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init) {
49  return std::exclusive_scan(__first, __last, __result, __init, std::plus<>());
50}
51
52#endif // _LIBCPP_STD_VER >= 17
53
54_LIBCPP_END_NAMESPACE_STD
55
56_LIBCPP_POP_MACROS
57
58#endif // _LIBCPP___NUMERIC_EXCLUSIVE_SCAN_H
59