inclusive_scan.h 2.2 KB

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