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 #if _LIBCPP_STD_VER >= 20
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 namespace ranges {
35 
36 template <class _Iter, class _Sent, class _Proj, class _Pred>
37 _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
38 __remove_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
39   auto __new_end = ranges::__find_if_impl(__first, __last, __pred, __proj);
40   if (__new_end == __last)
41     return {__new_end, __new_end};
42 
43   _Iter __i = __new_end;
44   while (++__i != __last) {
45     if (!std::invoke(__pred, std::invoke(__proj, *__i))) {
46       *__new_end = ranges::iter_move(__i);
47       ++__new_end;
48     }
49   }
50   return {__new_end, __i};
51 }
52 
53 namespace __remove_if {
54 struct __fn {
55   template <permutable _Iter,
56             sentinel_for<_Iter> _Sent,
57             class _Proj = identity,
58             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
59   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
60   operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
61     return ranges::__remove_if_impl(std::move(__first), std::move(__last), __pred, __proj);
62   }
63 
64   template <forward_range _Range,
65             class _Proj = identity,
66             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
67     requires permutable<iterator_t<_Range>>
68   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
69   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
70     return ranges::__remove_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
71   }
72 };
73 } // namespace __remove_if
74 
75 inline namespace __cpo {
76 inline constexpr auto remove_if = __remove_if::__fn{};
77 } // namespace __cpo
78 } // namespace ranges
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 #endif // _LIBCPP_STD_VER >= 20
83 
84 #endif // _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H
85