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_SORT_H
10 #define _LIBCPP___ALGORITHM_RANGES_SORT_H
11 
12 #include <__algorithm/make_projected.h>
13 #include <__algorithm/sort.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/invoke.h>
17 #include <__functional/ranges_operations.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__iterator/next.h>
21 #include <__iterator/projected.h>
22 #include <__iterator/sortable.h>
23 #include <__ranges/access.h>
24 #include <__ranges/concepts.h>
25 #include <__ranges/dangling.h>
26 #include <__utility/forward.h>
27 #include <__utility/move.h>
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #  pragma GCC system_header
31 #endif
32 
33 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 namespace ranges {
38 namespace __sort {
39 
40 struct __fn {
41   template <class _Iter, class _Sent, class _Comp, class _Proj>
42   _LIBCPP_HIDE_FROM_ABI constexpr static
43   _Iter __sort_fn_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
44     auto __last_iter = ranges::next(__first, __last);
45 
46     auto&& __projected_comp = ranges::__make_projected_comp(__comp, __proj);
47     std::__sort_impl(std::move(__first), __last_iter, __projected_comp);
48 
49     return __last_iter;
50   }
51 
52   template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
53     requires sortable<_Iter, _Comp, _Proj>
54   _LIBCPP_HIDE_FROM_ABI constexpr
55   _Iter operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
56     return __sort_fn_impl(std::move(__first), std::move(__last), __comp, __proj);
57   }
58 
59   template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
60     requires sortable<iterator_t<_Range>, _Comp, _Proj>
61   _LIBCPP_HIDE_FROM_ABI constexpr
62   borrowed_iterator_t<_Range> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
63     return __sort_fn_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
64   }
65 };
66 
67 } // namespace __sort
68 
69 inline namespace __cpo {
70   inline constexpr auto sort = __sort::__fn{};
71 } // namespace __cpo
72 } // namespace ranges
73 
74 _LIBCPP_END_NAMESPACE_STD
75 
76 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
77 
78 #endif // _LIBCPP___ALGORITHM_RANGES_SORT_H
79