xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/adjacent_difference.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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_BEGIN_NAMESPACE_STD
22 
23 template <class _InputIterator, class _OutputIterator>
24 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
25 _OutputIterator
26 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
27 {
28     if (__first != __last)
29     {
30         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
31         *__result = __acc;
32         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
33         {
34             typename iterator_traits<_InputIterator>::value_type __val(*__first);
35 #if _LIBCPP_STD_VER > 17
36             *__result = __val - _VSTD::move(__acc);
37 #else
38             *__result = __val - __acc;
39 #endif
40             __acc = _VSTD::move(__val);
41         }
42     }
43     return __result;
44 }
45 
46 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
47 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
48 _OutputIterator
49 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
50                       _BinaryOperation __binary_op)
51 {
52     if (__first != __last)
53     {
54         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
55         *__result = __acc;
56         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
57         {
58             typename iterator_traits<_InputIterator>::value_type __val(*__first);
59 #if _LIBCPP_STD_VER > 17
60             *__result = __binary_op(__val, _VSTD::move(__acc));
61 #else
62             *__result = __binary_op(__val, __acc);
63 #endif
64             __acc = _VSTD::move(__val);
65         }
66     }
67     return __result;
68 }
69 
70 _LIBCPP_END_NAMESPACE_STD
71 
72 #endif // _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
73