Skip to content

Commit 11e3ad2

Browse files
committed
[libc++] Implement ranges::is_sorted{, _until}
Reviewed By: Mordante, var-const, #libc Spies: libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D125608
1 parent 3a7a465 commit 11e3ad2

File tree

13 files changed

+574
-12
lines changed

13 files changed

+574
-12
lines changed

libcxx/docs/Status/RangesAlgorithms.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Search,search,Not assigned,n/a,Not started
2727
Search,search_n,Not assigned,n/a,Not started
2828
Search,find_end,Not assigned,n/a,Not started
2929
Read-only,is_partitioned,Nikolas Klauser,`D124440 <https://llvm.org/D124440>`_,✅
30-
Read-only,is_sorted,Not assigned,n/a,Not started
31-
Read-only,is_sorted_unitl,Not assigned,n/a,Not started
30+
Read-only,is_sorted,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
31+
Read-only,is_sorted_unitl,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
3232
Read-only,includes,Not assigned,n/a,Not started
3333
Read-only,is_heap,Not assigned,n/a,Not started
3434
Read-only,is_heap_until,Not assigned,n/a,Not started

libcxx/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ set(files
8383
__algorithm/ranges_for_each.h
8484
__algorithm/ranges_for_each_n.h
8585
__algorithm/ranges_is_partitioned.h
86+
__algorithm/ranges_is_sorted.h
87+
__algorithm/ranges_is_sorted_until.h
8688
__algorithm/ranges_max.h
8789
__algorithm/ranges_max_element.h
8890
__algorithm/ranges_min.h
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_IS_SORTED_H
10+
#define _LIBCPP__ALGORITHM_RANGES_IS_SORTED_H
11+
12+
#include <__algorithm/ranges_is_sorted_until.h>
13+
#include <__config>
14+
#include <__functional/identity.h>
15+
#include <__functional/ranges_operations.h>
16+
#include <__iterator/concepts.h>
17+
#include <__iterator/projected.h>
18+
#include <__ranges/access.h>
19+
#include <__ranges/concepts.h>
20+
#include <__utility/move.h>
21+
22+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23+
# pragma GCC system_header
24+
#endif
25+
26+
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
27+
28+
_LIBCPP_BEGIN_NAMESPACE_STD
29+
30+
namespace ranges {
31+
namespace __is_sorted {
32+
struct __fn {
33+
template <forward_iterator _Iter, sentinel_for<_Iter> _Sent,
34+
class _Proj = identity,
35+
indirect_strict_weak_order<projected<_Iter, _Proj>> _Comp = ranges::less>
36+
_LIBCPP_HIDE_FROM_ABI constexpr
37+
bool operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
38+
return ranges::__is_sorted_until_impl(std::move(__first), __last, __comp, __proj) == __last;
39+
}
40+
41+
template <forward_range _Range,
42+
class _Proj = identity,
43+
indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
44+
_LIBCPP_HIDE_FROM_ABI constexpr
45+
bool operator()(_Range&& __range, _Comp __comp = {}, _Proj __proj = {}) const {
46+
auto __last = ranges::end(__range);
47+
return ranges::__is_sorted_until_impl(ranges::begin(__range), __last, __comp, __proj) == __last;
48+
}
49+
};
50+
} // namespace __is_sorted
51+
52+
inline namespace __cpo {
53+
inline constexpr auto is_sorted = __is_sorted::__fn{};
54+
} // namespace __cpo
55+
} // namespace ranges
56+
57+
_LIBCPP_END_NAMESPACE_STD
58+
59+
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
60+
61+
#endif // _LIBCPP__ALGORITHM_RANGES_IS_SORTED_H
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_IS_SORTED_UNTIL_H
10+
#define _LIBCPP__ALGORITHM_RANGES_IS_SORTED_UNTIL_H
11+
12+
#include <__config>
13+
#include <__functional/identity.h>
14+
#include <__functional/invoke.h>
15+
#include <__functional/ranges_operations.h>
16+
#include <__iterator/concepts.h>
17+
#include <__iterator/projected.h>
18+
#include <__ranges/access.h>
19+
#include <__ranges/concepts.h>
20+
#include <__ranges/dangling.h>
21+
#include <__utility/move.h>
22+
23+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24+
# pragma GCC system_header
25+
#endif
26+
27+
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
28+
29+
_LIBCPP_BEGIN_NAMESPACE_STD
30+
31+
namespace ranges {
32+
33+
template <class _Iter, class _Sent, class _Proj, class _Comp>
34+
_LIBCPP_HIDE_FROM_ABI constexpr
35+
_Iter __is_sorted_until_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
36+
if (__first == __last)
37+
return __first;
38+
auto __i = __first;
39+
while (++__i != __last) {
40+
if (std::invoke(__comp, std::invoke(__proj, *__i), std::invoke(__proj, *__first)))
41+
return __i;
42+
__first = __i;
43+
}
44+
return __i;
45+
}
46+
47+
namespace __is_sorted_until {
48+
struct __fn {
49+
template <forward_iterator _Iter, sentinel_for<_Iter> _Sent,
50+
class _Proj = identity,
51+
indirect_strict_weak_order<projected<_Iter, _Proj>> _Comp = ranges::less>
52+
_LIBCPP_HIDE_FROM_ABI constexpr
53+
_Iter operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
54+
return ranges::__is_sorted_until_impl(std::move(__first), std::move(__last), __comp, __proj);
55+
}
56+
57+
template <forward_range _Range,
58+
class _Proj = identity,
59+
indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
60+
_LIBCPP_HIDE_FROM_ABI constexpr
61+
borrowed_iterator_t<_Range> operator()(_Range&& __range, _Comp __comp = {}, _Proj __proj = {}) const {
62+
return ranges::__is_sorted_until_impl(ranges::begin(__range), ranges::end(__range), __comp, __proj);
63+
}
64+
};
65+
} // namespace __is_sorted_until
66+
67+
inline namespace __cpo {
68+
inline constexpr auto is_sorted_until = __is_sorted_until::__fn{};
69+
} // namespace __cpo
70+
} // namespace ranges
71+
72+
_LIBCPP_END_NAMESPACE_STD
73+
74+
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
75+
76+
#endif // _LIBCPP__ALGORITHM_RANGES_IS_SORTED_UNTIL_H

libcxx/include/algorithm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ namespace ranges {
329329
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
330330
constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); // since C++20
331331
332+
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
333+
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
334+
constexpr bool ranges::is_sorted(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
335+
336+
template<forward_range R, class Proj = identity,
337+
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
338+
constexpr bool ranges::is_sorted(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
339+
340+
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
341+
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
342+
constexpr I ranges::is_sorted_until(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
343+
344+
template<forward_range R, class Proj = identity,
345+
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
346+
constexpr borrowed_iterator_t<R>
347+
ranges::is_sorted_until(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
332348
}
333349
334350
constexpr bool // constexpr in C++20
@@ -1062,6 +1078,8 @@ template <class BidirectionalIterator, class Compare>
10621078
#include <__algorithm/ranges_for_each.h>
10631079
#include <__algorithm/ranges_for_each_n.h>
10641080
#include <__algorithm/ranges_is_partitioned.h>
1081+
#include <__algorithm/ranges_is_sorted.h>
1082+
#include <__algorithm/ranges_is_sorted_until.h>
10651083
#include <__algorithm/ranges_max.h>
10661084
#include <__algorithm/ranges_max_element.h>
10671085
#include <__algorithm/ranges_min.h>

libcxx/include/module.modulemap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ module std [system] {
315315
module ranges_for_each { private header "__algorithm/ranges_for_each.h" }
316316
module ranges_for_each_n { private header "__algorithm/ranges_for_each_n.h" }
317317
module ranges_is_partitioned { private header "__algorithm/ranges_is_partitioned.h" }
318+
module ranges_is_sorted { private header "__algorithm/ranges_is_sorted.h" }
319+
module ranges_is_sorted_until { private header "__algorithm/ranges_is_sorted_until.h" }
318320
module ranges_max { private header "__algorithm/ranges_max.h" }
319321
module ranges_max_element { private header "__algorithm/ranges_max_element.h" }
320322
module ranges_min { private header "__algorithm/ranges_min.h" }

libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ constexpr bool all_the_algorithms()
134134
//(void)std::ranges::is_partitioned(a, UnaryTrue(&copies)); assert(copies == 0);
135135
//(void)std::ranges::is_permutation(first, last, first2, last2, Equal(&copies)); assert(copies == 0);
136136
//(void)std::ranges::is_permutation(a, b, Equal(&copies)); assert(copies == 0);
137-
//(void)std::ranges::is_sorted(first, last, Less(&copies)); assert(copies == 0);
138-
//(void)std::ranges::is_sorted(a, Less(&copies)); assert(copies == 0);
139-
//(void)std::ranges::is_sorted_until(first, last, Less(&copies)); assert(copies == 0);
140-
//(void)std::ranges::is_sorted_until(a, Less(&copies)); assert(copies == 0);
137+
(void)std::ranges::is_sorted(first, last, Less(&copies)); assert(copies == 0);
138+
(void)std::ranges::is_sorted(a, Less(&copies)); assert(copies == 0);
139+
(void)std::ranges::is_sorted_until(first, last, Less(&copies)); assert(copies == 0);
140+
(void)std::ranges::is_sorted_until(a, Less(&copies)); assert(copies == 0);
141141
//if (!std::is_constant_evaluated()) { (void)std::ranges::inplace_merge(first, mid, last, Less(&copies)); assert(copies == 0); }
142142
//if (!std::is_constant_evaluated()) { (void)std::ranges::inplace_merge(a, mid, Less(&copies)); assert(copies == 0); }
143143
//(void)std::ranges::lexicographical_compare(first, last, first2, last2, Less(&copies)); assert(copies == 0);

libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ constexpr bool all_the_algorithms()
117117
//(void)std::ranges::is_partitioned(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
118118
//(void)std::ranges::is_permutation(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0);
119119
//(void)std::ranges::is_permutation(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0);
120-
//(void)std::ranges::is_sorted(first, last, Less(), Proj(&copies)); assert(copies == 0);
121-
//(void)std::ranges::is_sorted(a, Less(), Proj(&copies)); assert(copies == 0);
122-
//(void)std::ranges::is_sorted_until(first, last, Less(), Proj(&copies)); assert(copies == 0);
123-
//(void)std::ranges::is_sorted_until(a, Less(), Proj(&copies)); assert(copies == 0);
120+
(void)std::ranges::is_sorted(first, last, Less(), Proj(&copies)); assert(copies == 0);
121+
(void)std::ranges::is_sorted(a, Less(), Proj(&copies)); assert(copies == 0);
122+
(void)std::ranges::is_sorted_until(first, last, Less(), Proj(&copies)); assert(copies == 0);
123+
(void)std::ranges::is_sorted_until(a, Less(), Proj(&copies)); assert(copies == 0);
124124
//if (!std::is_constant_evaluated()) { (void)std::ranges::inplace_merge(first, mid, last, Less(), Proj(&copies)); assert(copies == 0); }
125125
//if (!std::is_constant_evaluated()) { (void)std::ranges::inplace_merge(a, mid, Less(), Proj(&copies)); assert(copies == 0); }
126126
//(void)std::ranges::lexicographical_compare(first, last, first2, last2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);

libcxx/test/libcxx/private_headers.verify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ END-SCRIPT
120120
#include <__algorithm/ranges_for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each.h'}}
121121
#include <__algorithm/ranges_for_each_n.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each_n.h'}}
122122
#include <__algorithm/ranges_is_partitioned.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_is_partitioned.h'}}
123+
#include <__algorithm/ranges_is_sorted.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_is_sorted.h'}}
124+
#include <__algorithm/ranges_is_sorted_until.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_is_sorted_until.h'}}
123125
#include <__algorithm/ranges_max.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max.h'}}
124126
#include <__algorithm/ranges_max_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max_element.h'}}
125127
#include <__algorithm/ranges_min.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min.h'}}

0 commit comments

Comments
 (0)