exclusive_scan.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_BEGIN_NAMESPACE_STD
  18. #if _LIBCPP_STD_VER > 14
  19. template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
  20. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
  21. exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOp __b) {
  22. if (__first != __last) {
  23. _Tp __tmp(__b(__init, *__first));
  24. while (true) {
  25. *__result = _VSTD::move(__init);
  26. ++__result;
  27. ++__first;
  28. if (__first == __last)
  29. break;
  30. __init = _VSTD::move(__tmp);
  31. __tmp = __b(__init, *__first);
  32. }
  33. }
  34. return __result;
  35. }
  36. template <class _InputIterator, class _OutputIterator, class _Tp>
  37. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
  38. exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init) {
  39. return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
  40. }
  41. #endif // _LIBCPP_STD_VER > 14
  42. _LIBCPP_END_NAMESPACE_STD
  43. #endif // _LIBCPP___NUMERIC_EXCLUSIVE_SCAN_H