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_REMOVE_H 10 #define _LIBCPP___ALGORITHM_REMOVE_H 11 12 #include <__config> 13 #include <__algorithm/find.h> 14 #include <__algorithm/find_if.h> 15 #include <type_traits> 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 // remove 27 28 template <class _ForwardIterator, class _Tp> 29 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator 30 remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 31 { 32 __first = _VSTD::find(__first, __last, __value_); 33 if (__first != __last) 34 { 35 _ForwardIterator __i = __first; 36 while (++__i != __last) 37 { 38 if (!(*__i == __value_)) 39 { 40 *__first = _VSTD::move(*__i); 41 ++__first; 42 } 43 } 44 } 45 return __first; 46 } 47 48 // remove_if 49 50 template <class _ForwardIterator, class _Predicate> 51 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator 52 remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 53 { 54 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> 55 (__first, __last, __pred); 56 if (__first != __last) 57 { 58 _ForwardIterator __i = __first; 59 while (++__i != __last) 60 { 61 if (!__pred(*__i)) 62 { 63 *__first = _VSTD::move(*__i); 64 ++__first; 65 } 66 } 67 } 68 return __first; 69 } 70 71 // remove_copy 72 73 template <class _InputIterator, class _OutputIterator, class _Tp> 74 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 75 _OutputIterator 76 remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_) 77 { 78 for (; __first != __last; ++__first) 79 { 80 if (!(*__first == __value_)) 81 { 82 *__result = *__first; 83 ++__result; 84 } 85 } 86 return __result; 87 } 88 89 // remove_copy_if 90 91 template <class _InputIterator, class _OutputIterator, class _Predicate> 92 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 93 _OutputIterator 94 remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) 95 { 96 for (; __first != __last; ++__first) 97 { 98 if (!__pred(*__first)) 99 { 100 *__result = *__first; 101 ++__result; 102 } 103 } 104 return __result; 105 } 106 107 _LIBCPP_END_NAMESPACE_STD 108 109 _LIBCPP_POP_MACROS 110 111 #endif // _LIBCPP___ALGORITHM_REMOVE_H 112