exclusive_scan.h 1.7 KB

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