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>
39constexpr subrange<_Iter> __remove_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
40 auto __new_end = std::__find_if(__first, __last, __pred, __proj);
41 if (__new_end == __last)
42 return {__new_end, __new_end};
43
44 _Iter __i = __new_end;
45 while (++__i != __last) {
46 if (!std::invoke(__pred, std::invoke(__proj, *__i))) {
47 *__new_end = ranges::iter_move(__i);
48 ++__new_end;
49 }
50 }
51 return {__new_end, __i};
52}
53
54struct __remove_if {
55 template <permutable _Iter,
56 sentinel_for<_Iter> _Sent,
57 class _Proj = identity,
58 indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
59 [[nodiscard]] 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 [[nodiscard]] 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
74inline namespace __cpo {
75inline constexpr auto remove_if = __remove_if{};
76} // namespace __cpo
77} // namespace ranges
78
79_LIBCPP_END_NAMESPACE_STD
80
81#endif // _LIBCPP_STD_VER >= 20
82
83_LIBCPP_POP_MACROS
84
85#endif // _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H
86