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