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_ADJACENT_DIFFERENCE_H |
11 | #define _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H |
12 | |
13 | #include <__config> |
14 | #include <__iterator/iterator_traits.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 | template <class _InputIterator, class _OutputIterator> |
27 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator |
28 | adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) { |
29 | if (__first != __last) { |
30 | typename iterator_traits<_InputIterator>::value_type __acc(*__first); |
31 | *__result = __acc; |
32 | for (++__first, (void)++__result; __first != __last; ++__first, (void)++__result) { |
33 | typename iterator_traits<_InputIterator>::value_type __val(*__first); |
34 | #if _LIBCPP_STD_VER >= 20 |
35 | *__result = __val - std::move(__acc); |
36 | #else |
37 | *__result = __val - __acc; |
38 | #endif |
39 | __acc = std::move(__val); |
40 | } |
41 | } |
42 | return __result; |
43 | } |
44 | |
45 | template <class _InputIterator, class _OutputIterator, class _BinaryOperation> |
46 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator adjacent_difference( |
47 | _InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op) { |
48 | if (__first != __last) { |
49 | typename iterator_traits<_InputIterator>::value_type __acc(*__first); |
50 | *__result = __acc; |
51 | for (++__first, (void)++__result; __first != __last; ++__first, (void)++__result) { |
52 | typename iterator_traits<_InputIterator>::value_type __val(*__first); |
53 | #if _LIBCPP_STD_VER >= 20 |
54 | *__result = __binary_op(__val, std::move(__acc)); |
55 | #else |
56 | *__result = __binary_op(__val, __acc); |
57 | #endif |
58 | __acc = std::move(__val); |
59 | } |
60 | } |
61 | return __result; |
62 | } |
63 | |
64 | _LIBCPP_END_NAMESPACE_STD |
65 | |
66 | _LIBCPP_POP_MACROS |
67 | |
68 | #endif // _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H |
69 | |