xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_partition.h (revision d54a7d337331d991e039e4f42f6b4dc64aedce08)
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_PARTITION_H
10  #define _LIBCPP___ALGORITHM_RANGES_PARTITION_H
11  
12  #include <__algorithm/iterator_operations.h>
13  #include <__algorithm/make_projected.h>
14  #include <__algorithm/partition.h>
15  #include <__algorithm/ranges_iterator_concept.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/permutable.h>
23  #include <__iterator/projected.h>
24  #include <__ranges/access.h>
25  #include <__ranges/concepts.h>
26  #include <__ranges/subrange.h>
27  #include <__utility/forward.h>
28  #include <__utility/move.h>
29  #include <__utility/pair.h>
30  #include <type_traits>
31  
32  #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33  #  pragma GCC system_header
34  #endif
35  
36  #if _LIBCPP_STD_VER > 17
37  
38  _LIBCPP_BEGIN_NAMESPACE_STD
39  
40  namespace ranges {
41  namespace __partition {
42  
43  struct __fn {
44  
45    template <class _Iter, class _Sent, class _Proj, class _Pred>
46    _LIBCPP_HIDE_FROM_ABI static constexpr
47    subrange<__remove_cvref_t<_Iter>> __partition_fn_impl(_Iter&& __first, _Sent&& __last, _Pred&& __pred, _Proj&& __proj) {
48      auto&& __projected_pred = std::__make_projected(__pred, __proj);
49      auto __result = std::__partition<_RangeAlgPolicy>(
50          std::move(__first), std::move(__last), __projected_pred, __iterator_concept<_Iter>());
51  
52      return {std::move(__result.first), std::move(__result.second)};
53    }
54  
55    template <permutable _Iter, sentinel_for<_Iter> _Sent, class _Proj = identity,
56              indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
57    _LIBCPP_HIDE_FROM_ABI constexpr
58    subrange<_Iter> operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
59      return __partition_fn_impl(__first, __last, __pred, __proj);
60    }
61  
62    template <forward_range _Range, class _Proj = identity,
63              indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
64    requires permutable<iterator_t<_Range>>
65    _LIBCPP_HIDE_FROM_ABI constexpr
66    borrowed_subrange_t<_Range> operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
67      return __partition_fn_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
68    }
69  
70  };
71  
72  } // namespace __partition
73  
74  inline namespace __cpo {
75    inline constexpr auto partition = __partition::__fn{};
76  } // namespace __cpo
77  } // namespace ranges
78  
79  _LIBCPP_END_NAMESPACE_STD
80  
81  #endif // _LIBCPP_STD_VER > 17
82  
83  #endif // _LIBCPP___ALGORITHM_RANGES_PARTITION_H
84