1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_UNIQUE_H 10 #define _LIBCPP___ALGORITHM_UNIQUE_H 11 12 #include <__config> 13 #include <__algorithm/comp.h> 14 #include <__algorithm/adjacent_find.h> 15 #include <__iterator/iterator_traits.h> 16 #include <__utility/move.h> 17 #include <type_traits> 18 19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20 #pragma GCC system_header 21 #endif 22 23 _LIBCPP_PUSH_MACROS 24 #include <__undef_macros> 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 // unique 29 30 template <class _ForwardIterator, class _BinaryPredicate> 31 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator 32 unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) 33 { 34 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type> 35 (__first, __last, __pred); 36 if (__first != __last) 37 { 38 // ... a a ? ... 39 // f i 40 _ForwardIterator __i = __first; 41 for (++__i; ++__i != __last;) 42 if (!__pred(*__first, *__i)) 43 *++__first = _VSTD::move(*__i); 44 ++__first; 45 } 46 return __first; 47 } 48 49 template <class _ForwardIterator> 50 _LIBCPP_NODISCARD_EXT inline 51 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 52 _ForwardIterator 53 unique(_ForwardIterator __first, _ForwardIterator __last) 54 { 55 typedef typename iterator_traits<_ForwardIterator>::value_type __v; 56 return _VSTD::unique(__first, __last, __equal_to<__v>()); 57 } 58 59 _LIBCPP_END_NAMESPACE_STD 60 61 _LIBCPP_POP_MACROS 62 63 #endif // _LIBCPP___ALGORITHM_UNIQUE_H 64