xref: /freebsd-src/contrib/llvm-project/libcxx/include/__numeric/adjacent_difference.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
14824e7fdSDimitry Andric // -*- C++ -*-
24824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
34824e7fdSDimitry Andric //
44824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
64824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
74824e7fdSDimitry Andric //
84824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
94824e7fdSDimitry Andric 
104824e7fdSDimitry Andric #ifndef _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
114824e7fdSDimitry Andric #define _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
124824e7fdSDimitry Andric 
134824e7fdSDimitry Andric #include <__config>
144824e7fdSDimitry Andric #include <__iterator/iterator_traits.h>
154824e7fdSDimitry Andric #include <__utility/move.h>
164824e7fdSDimitry Andric 
174824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
184824e7fdSDimitry Andric #  pragma GCC system_header
194824e7fdSDimitry Andric #endif
204824e7fdSDimitry Andric 
21*06c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
22*06c3fb27SDimitry Andric #include <__undef_macros>
23*06c3fb27SDimitry Andric 
244824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
254824e7fdSDimitry Andric 
264824e7fdSDimitry Andric template <class _InputIterator, class _OutputIterator>
27bdd1243dSDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
284824e7fdSDimitry Andric _OutputIterator
294824e7fdSDimitry Andric adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
304824e7fdSDimitry Andric {
314824e7fdSDimitry Andric     if (__first != __last)
324824e7fdSDimitry Andric     {
334824e7fdSDimitry Andric         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
344824e7fdSDimitry Andric         *__result = __acc;
354824e7fdSDimitry Andric         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
364824e7fdSDimitry Andric         {
374824e7fdSDimitry Andric             typename iterator_traits<_InputIterator>::value_type __val(*__first);
38*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
394824e7fdSDimitry Andric             *__result = __val - _VSTD::move(__acc);
404824e7fdSDimitry Andric #else
414824e7fdSDimitry Andric             *__result = __val - __acc;
424824e7fdSDimitry Andric #endif
434824e7fdSDimitry Andric             __acc = _VSTD::move(__val);
444824e7fdSDimitry Andric         }
454824e7fdSDimitry Andric     }
464824e7fdSDimitry Andric     return __result;
474824e7fdSDimitry Andric }
484824e7fdSDimitry Andric 
494824e7fdSDimitry Andric template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
50bdd1243dSDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
514824e7fdSDimitry Andric _OutputIterator
524824e7fdSDimitry Andric adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
534824e7fdSDimitry Andric                       _BinaryOperation __binary_op)
544824e7fdSDimitry Andric {
554824e7fdSDimitry Andric     if (__first != __last)
564824e7fdSDimitry Andric     {
574824e7fdSDimitry Andric         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
584824e7fdSDimitry Andric         *__result = __acc;
594824e7fdSDimitry Andric         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
604824e7fdSDimitry Andric         {
614824e7fdSDimitry Andric             typename iterator_traits<_InputIterator>::value_type __val(*__first);
62*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
634824e7fdSDimitry Andric             *__result = __binary_op(__val, _VSTD::move(__acc));
644824e7fdSDimitry Andric #else
654824e7fdSDimitry Andric             *__result = __binary_op(__val, __acc);
664824e7fdSDimitry Andric #endif
674824e7fdSDimitry Andric             __acc = _VSTD::move(__val);
684824e7fdSDimitry Andric         }
694824e7fdSDimitry Andric     }
704824e7fdSDimitry Andric     return __result;
714824e7fdSDimitry Andric }
724824e7fdSDimitry Andric 
734824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
744824e7fdSDimitry Andric 
75*06c3fb27SDimitry Andric _LIBCPP_POP_MACROS
76*06c3fb27SDimitry Andric 
774824e7fdSDimitry Andric #endif // _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
78