master
 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_STABLE_PARTITION_H
10#define _LIBCPP___ALGORITHM_RANGES_STABLE_PARTITION_H
11
12#include <__algorithm/iterator_operations.h>
13#include <__algorithm/make_projected.h>
14#include <__algorithm/ranges_iterator_concept.h>
15#include <__algorithm/stable_partition.h>
16#include <__config>
17#include <__functional/identity.h>
18#include <__functional/invoke.h>
19#include <__functional/ranges_operations.h>
20#include <__iterator/concepts.h>
21#include <__iterator/iterator_traits.h>
22#include <__iterator/next.h>
23#include <__iterator/permutable.h>
24#include <__iterator/projected.h>
25#include <__ranges/access.h>
26#include <__ranges/concepts.h>
27#include <__ranges/dangling.h>
28#include <__ranges/subrange.h>
29#include <__type_traits/remove_cvref.h>
30#include <__utility/forward.h>
31#include <__utility/move.h>
32
33#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34#  pragma GCC system_header
35#endif
36
37_LIBCPP_PUSH_MACROS
38#include <__undef_macros>
39
40#if _LIBCPP_STD_VER >= 20
41
42_LIBCPP_BEGIN_NAMESPACE_STD
43
44namespace ranges {
45struct __stable_partition {
46  template <class _Iter, class _Sent, class _Proj, class _Pred>
47  _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX26 subrange<__remove_cvref_t<_Iter>>
48  __stable_partition_fn_impl(_Iter&& __first, _Sent&& __last, _Pred&& __pred, _Proj&& __proj) {
49    auto __last_iter = ranges::next(__first, __last);
50
51    auto&& __projected_pred = std::__make_projected(__pred, __proj);
52    auto __result           = std::__stable_partition<_RangeAlgPolicy>(
53        std::move(__first), __last_iter, __projected_pred, __iterator_concept<_Iter>());
54
55    return {std::move(__result), std::move(__last_iter)};
56  }
57
58  template <bidirectional_iterator _Iter,
59            sentinel_for<_Iter> _Sent,
60            class _Proj = identity,
61            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
62    requires permutable<_Iter>
63  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 subrange<_Iter>
64  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
65    return __stable_partition_fn_impl(__first, __last, __pred, __proj);
66  }
67
68  template <bidirectional_range _Range,
69            class _Proj = identity,
70            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
71    requires permutable<iterator_t<_Range>>
72  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 borrowed_subrange_t<_Range>
73  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
74    return __stable_partition_fn_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
75  }
76};
77
78inline namespace __cpo {
79inline constexpr auto stable_partition = __stable_partition{};
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_STABLE_PARTITION_H