xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_remove_if.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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_RANGES_REMOVE_IF_H
10 #define _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H
11 #include <__config>
12 
13 #include <__algorithm/ranges_find_if.h>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__functional/ranges_operations.h>
17 #include <__iterator/concepts.h>
18 #include <__iterator/iter_move.h>
19 #include <__iterator/permutable.h>
20 #include <__iterator/projected.h>
21 #include <__ranges/access.h>
22 #include <__ranges/concepts.h>
23 #include <__ranges/subrange.h>
24 #include <__utility/move.h>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
32 
33 #if _LIBCPP_STD_VER >= 20
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 namespace ranges {
38 
39 template <class _Iter, class _Sent, class _Proj, class _Pred>
40 _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
__remove_if_impl(_Iter __first,_Sent __last,_Pred & __pred,_Proj & __proj)41 __remove_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
42   auto __new_end = ranges::__find_if_impl(__first, __last, __pred, __proj);
43   if (__new_end == __last)
44     return {__new_end, __new_end};
45 
46   _Iter __i = __new_end;
47   while (++__i != __last) {
48     if (!std::invoke(__pred, std::invoke(__proj, *__i))) {
49       *__new_end = ranges::iter_move(__i);
50       ++__new_end;
51     }
52   }
53   return {__new_end, __i};
54 }
55 
56 namespace __remove_if {
57 struct __fn {
58   template <permutable _Iter,
59             sentinel_for<_Iter> _Sent,
60             class _Proj = identity,
61             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
62   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
operator__fn63   operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
64     return ranges::__remove_if_impl(std::move(__first), std::move(__last), __pred, __proj);
65   }
66 
67   template <forward_range _Range,
68             class _Proj = identity,
69             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
70     requires permutable<iterator_t<_Range>>
71   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
operator__fn72   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
73     return ranges::__remove_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
74   }
75 };
76 } // namespace __remove_if
77 
78 inline namespace __cpo {
79 inline constexpr auto remove_if = __remove_if::__fn{};
80 } // namespace __cpo
81 } // namespace ranges
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 #endif // _LIBCPP_STD_VER >= 20
86 
87 _LIBCPP_POP_MACROS
88 
89 #endif // _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H
90