[libc++][ranges] Implement `ranges::nth_element`.

Differential Revision: https://reviews.llvm.org/D128149
This commit is contained in:
Konstantin Varlamov 2022-07-07 20:35:51 -07:00
parent 96063bfa90
commit 23c7328bad
11 changed files with 416 additions and 17 deletions

View File

@ -76,7 +76,7 @@ Permutation,stable_partition,Not assigned,n/a,Not started
Permutation,sort,Konstantin Varlamov,`D127557 <https://llvm.org/D127557>`_,✅
Permutation,stable_sort,Konstantin Varlamov,`D127834 <https://llvm.org/D127834>`_,✅
Permutation,partial_sort,Konstantin Varlamov,n/a,In progress
Permutation,nth_element,Not assigned,n/a,Not started
Permutation,nth_element,Konstantin Varlamov,`D128149 <https://llvm.org/D128149>`_,✅
Permutation,inplace_merge,Not assigned,n/a,Not started
Permutation,make_heap,Not assigned,n/a,Not started
Permutation,push_heap,Not assigned,n/a,Not started

1 Category Algorithm Assignee CL Complete
76 Permutation sort Konstantin Varlamov `D127557 <https://llvm.org/D127557>`_
77 Permutation stable_sort Konstantin Varlamov `D127834 <https://llvm.org/D127834>`_
78 Permutation partial_sort Konstantin Varlamov n/a In progress
79 Permutation nth_element Not assigned Konstantin Varlamov n/a `D128149 <https://llvm.org/D128149>`_ Not started
80 Permutation inplace_merge Not assigned n/a Not started
81 Permutation make_heap Not assigned n/a Not started
82 Permutation push_heap Not assigned n/a Not started

View File

@ -103,6 +103,7 @@ set(files
__algorithm/ranges_move.h
__algorithm/ranges_move_backward.h
__algorithm/ranges_none_of.h
__algorithm/ranges_nth_element.h
__algorithm/ranges_remove.h
__algorithm/ranges_remove_if.h
__algorithm/ranges_replace.h

View File

@ -16,6 +16,7 @@
#include <__debug>
#include <__debug_utils/randomize_range.h>
#include <__iterator/iterator_traits.h>
#include <__utility/move.h>
#include <__utility/swap.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@ -220,25 +221,35 @@ __nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _Rando
}
template <class _RandomAccessIterator, class _Compare>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void
nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
{
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
void __nth_element_impl(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last,
_Compare& __comp) {
if (__nth == __last)
return;
std::__debug_randomize_range(__first, __last);
typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
_VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
using _Comp_ref = typename __comp_ref_type<_Compare>::type;
std::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
std::__debug_randomize_range(__first, __nth);
if (__nth != __last) {
std::__debug_randomize_range(++__nth, __last);
}
}
template <class _RandomAccessIterator, class _Compare>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
void nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last,
_Compare __comp) {
std::__nth_element_impl(std::move(__first), std::move(__nth), std::move(__last), __comp);
}
template <class _RandomAccessIterator>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void
nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
{
_VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
void nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last) {
std::nth_element(std::move(__first), std::move(__nth), std::move(__last), __less<typename
iterator_traits<_RandomAccessIterator>::value_type>());
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -0,0 +1,79 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
#define _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H
#include <__algorithm/make_projected.h>
#include <__algorithm/nth_element.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
#include <__functional/ranges_operations.h>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/next.h>
#include <__iterator/projected.h>
#include <__iterator/sortable.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/dangling.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
_LIBCPP_BEGIN_NAMESPACE_STD
namespace ranges {
namespace __nth_element {
struct __fn {
template <class _Iter, class _Sent, class _Comp, class _Proj>
_LIBCPP_HIDE_FROM_ABI constexpr static
_Iter __nth_element_fn_impl(_Iter __first, _Iter __nth, _Sent __last, _Comp& __comp, _Proj& __proj) {
auto __last_iter = ranges::next(__first, __last);
auto&& __projected_comp = ranges::__make_projected_comp(__comp, __proj);
std::__nth_element_impl(std::move(__first), std::move(__nth), __last_iter, __projected_comp);
return __last_iter;
}
template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
requires sortable<_Iter, _Comp, _Proj>
_LIBCPP_HIDE_FROM_ABI constexpr
_Iter operator()(_Iter __first, _Iter __nth, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
return __nth_element_fn_impl(std::move(__first), std::move(__nth), std::move(__last), __comp, __proj);
}
template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
requires sortable<iterator_t<_Range>, _Comp, _Proj>
_LIBCPP_HIDE_FROM_ABI constexpr
borrowed_iterator_t<_Range> operator()(_Range&& __r, iterator_t<_Range> __nth, _Comp __comp = {},
_Proj __proj = {}) const {
return __nth_element_fn_impl(ranges::begin(__r), std::move(__nth), ranges::end(__r), __comp, __proj);
}
};
} // namespace __nth_element
inline namespace __cpo {
inline constexpr auto nth_element = __nth_element::__fn{};
} // namespace __cpo
} // namespace ranges
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
#endif // _LIBCPP___ALGORITHM_RANGES_NTH_ELEMENT_H

View File

@ -379,6 +379,17 @@ namespace ranges {
constexpr borrowed_iterator_t<R>
ranges::is_sorted_until(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
class Proj = identity>
requires sortable<I, Comp, Proj>
constexpr I
ranges::nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {}); // since C++20
template<random_access_range R, class Comp = ranges::less, class Proj = identity>
requires sortable<iterator_t<R>, Comp, Proj>
constexpr borrowed_iterator_t<R>
ranges::nth_element(R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {}); // since C++20
template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity,
indirect_strict_weak_order<const T*, projected<I, Proj>> Comp = ranges::less>
constexpr I upper_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); // since C++20
@ -1313,6 +1324,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_move.h>
#include <__algorithm/ranges_move_backward.h>
#include <__algorithm/ranges_none_of.h>
#include <__algorithm/ranges_nth_element.h>
#include <__algorithm/ranges_remove.h>
#include <__algorithm/ranges_remove_if.h>
#include <__algorithm/ranges_replace.h>

View File

@ -342,6 +342,7 @@ module std [system] {
module ranges_move { private header "__algorithm/ranges_move.h" }
module ranges_move_backward { private header "__algorithm/ranges_move_backward.h" }
module ranges_none_of { private header "__algorithm/ranges_none_of.h" }
module ranges_nth_element { private header "__algorithm/ranges_nth_element.h" }
module ranges_remove { private header "__algorithm/ranges_remove.h" }
module ranges_remove_if { private header "__algorithm/ranges_remove_if.h" }
module ranges_replace { private header "__algorithm/ranges_replace.h" }

View File

@ -169,8 +169,8 @@ constexpr bool all_the_algorithms()
//(void)std::ranges::next_permutation(a, Less(&copies)); assert(copies == 0);
(void)std::ranges::none_of(first, last, UnaryTrue(&copies)); assert(copies == 0);
(void)std::ranges::none_of(a, UnaryTrue(&copies)); assert(copies == 0);
//(void)std::ranges::nth_element(first, mid, last, Less(&copies)); assert(copies == 0);
//(void)std::ranges::nth_element(a, mid, Less(&copies)); assert(copies == 0);
(void)std::ranges::nth_element(first, mid, last, Less(&copies)); assert(copies == 0);
(void)std::ranges::nth_element(a, mid, Less(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort(first, mid, last, Less(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort(a, mid, Less(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort_copy(first, last, first2, mid2, Less(&copies)); assert(copies == 0);

View File

@ -152,8 +152,8 @@ constexpr bool all_the_algorithms()
//(void)std::ranges::next_permutation(a, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::none_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::none_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::nth_element(first, mid, last, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::nth_element(a, mid, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::nth_element(first, mid, last, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::nth_element(a, mid, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort(first, mid, last, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort(a, mid, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::partial_sort_copy(first, last, first2, mid2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);

View File

@ -140,6 +140,7 @@ END-SCRIPT
#include <__algorithm/ranges_move.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_move.h'}}
#include <__algorithm/ranges_move_backward.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_move_backward.h'}}
#include <__algorithm/ranges_none_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_none_of.h'}}
#include <__algorithm/ranges_nth_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_nth_element.h'}}
#include <__algorithm/ranges_remove.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_remove.h'}}
#include <__algorithm/ranges_remove_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_remove_if.h'}}
#include <__algorithm/ranges_replace.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace.h'}}

View File

@ -0,0 +1,294 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// <algorithm>
// template<random_­access_­iterator I, sentinel_­for<I> S, class Comp = ranges::less,
// class Proj = identity>
// requires sortable<I, Comp, Proj>
// constexpr I
// ranges::nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {}); // since C++20
//
// template<random_­access_­range R, class Comp = ranges::less, class Proj = identity>
// requires sortable<iterator_t<R>, Comp, Proj>
// constexpr borrowed_iterator_t<R>
// ranges::nth_element(R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {}); // since C++20
#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <iterator>
#include <optional>
#include <ranges>
#include "almost_satisfies_types.h"
#include "boolean_testable.h"
#include "test_iterators.h"
// SFINAE tests.
using BadComparator = ComparatorNotCopyable<int*>;
static_assert(!std::sortable<int*, BadComparator>);
template <class Iter, class Sent = sentinel_wrapper<Iter>, class Comp = std::ranges::less>
concept HasNthElementIt = requires(Iter first, Iter nth, Sent last, Comp comp) {
std::ranges::nth_element(first, nth, last, comp);
};
static_assert(HasNthElementIt<int*>);
static_assert(!HasNthElementIt<RandomAccessIteratorNotDerivedFrom>);
static_assert(!HasNthElementIt<RandomAccessIteratorBadIndex>);
static_assert(!HasNthElementIt<int*, SentinelForNotSemiregular>);
static_assert(!HasNthElementIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasNthElementIt<int*, int*, BadComparator>);
static_assert(!HasNthElementIt<const int*>); // Doesn't satisfy `sortable`.
template <class Range, class Comp = std::ranges::less>
concept HasNthElementR = requires(Range range, std::ranges::iterator_t<Range> nth, Comp comp) {
std::ranges::nth_element(range, nth, comp);
};
static_assert(HasNthElementR<UncheckedRange<int*>>);
static_assert(!HasNthElementR<RandomAccessRangeNotDerivedFrom>);
static_assert(!HasNthElementR<RandomAccessRangeBadIndex>);
static_assert(!HasNthElementR<UncheckedRange<int*, SentinelForNotSemiregular>>);
static_assert(!HasNthElementR<UncheckedRange<int*, SentinelForNotWeaklyEqualityComparableWith>>);
static_assert(!HasNthElementR<UncheckedRange<int*>, BadComparator>);
static_assert(!HasNthElementR<UncheckedRange<const int*>>); // Doesn't satisfy `sortable`.
template <size_t N, class T, class Iter>
constexpr void verify_nth(const std::array<T, N>& partially_sorted, size_t nth_index, Iter last, T expected_nth) {
// Note that the exact output of `nth_element` is unspecified and may vary between implementations.
assert(base(last) == partially_sorted.end());
auto b = partially_sorted.begin();
auto nth = b + nth_index;
auto e = partially_sorted.end();
if (nth == e)
return;
assert(*nth == expected_nth);
// All elements on the left are <= nth.
assert(std::all_of(b, nth, [&](const auto& v) { return v <= *nth; }));
// All elements on the right are >= nth.
assert(std::all_of(nth, e, [&](const auto& v) { return v >= *nth; }));
{
auto sorted = partially_sorted;
std::ranges::sort(sorted);
// The element at index `n` is the same as if the range were fully sorted.
assert(sorted[nth_index] == *nth);
}
}
template <class Iter, class Sent, size_t N>
constexpr void test_one(std::array<int, N> input, size_t nth_index, std::optional<int> expected_nth = {}) {
assert(expected_nth || nth_index == N);
{ // (iterator, sentinel) overload.
auto partially_sorted = input;
auto b = Iter(partially_sorted.data());
auto nth = b + nth_index;
auto e = Sent(Iter(partially_sorted.data() + partially_sorted.size()));
std::same_as<Iter> decltype(auto) last = std::ranges::nth_element(b, nth, e);
if (nth_index != N) {
verify_nth(partially_sorted, nth_index, last, *expected_nth);
} else {
assert(partially_sorted == input);
}
}
{ // (range) overload.
auto partially_sorted = input;
auto b = Iter(partially_sorted.data());
auto nth = b + nth_index;
auto e = Sent(Iter(partially_sorted.data() + partially_sorted.size()));
auto range = std::ranges::subrange(b, e);
std::same_as<Iter> decltype(auto) last = std::ranges::nth_element(range, nth);
if (nth_index != N) {
verify_nth(partially_sorted, nth_index, last, *expected_nth);
} else {
assert(partially_sorted == input);
}
}
}
template <class Iter, class Sent, size_t N>
constexpr void test_all_cases(std::array<int, N> input) {
auto sorted = input;
std::sort(sorted.begin(), sorted.end());
for (int n = 0; n != N; ++n) {
test_one<Iter, Sent, N>(input, n, sorted[n]);
}
test_one<Iter, Sent, N>(input, N);
}
constexpr void test_iterators() {
auto check = []<class Iter, class Sent> {
// Empty sequence.
test_one<Iter, Sent, 0>({}, 0);
// 1-element sequence.
test_all_cases<Iter, Sent>(std::array{1});
// 2-element sequence.
test_all_cases<Iter, Sent>(std::array{2, 1});
// 3-element sequence.
test_all_cases<Iter, Sent>(std::array{2, 1, 3});
// Longer sequence.
test_all_cases<Iter, Sent>(std::array{2, 1, 3, 6, 8, 4, 11, 5});
// Longer sequence with duplicates.
test_all_cases<Iter, Sent>(std::array{2, 1, 3, 6, 2, 8, 6});
// All elements are the same.
test_all_cases<Iter, Sent>(std::array{1, 1, 1, 1});
{ // nth element is in the right place.
std::array input = {6, 5, 3, 1, 4, 2};
constexpr size_t N = input.size();
test_one<Iter, Sent, N>(input, 2, /*expected_nth=*/3);
}
// Already sorted.
test_all_cases<Iter, Sent>(std::array{1, 2, 3, 4, 5, 6});
// Descending.
test_all_cases<Iter, Sent>(std::array{6, 5, 4, 3, 2, 1});
// Repeating pattern.
test_all_cases<Iter, Sent>(std::array{2, 1, 2, 1, 2, 1});
};
check.operator()<random_access_iterator<int*>, random_access_iterator<int*>>();
check.operator()<random_access_iterator<int*>, sentinel_wrapper<random_access_iterator<int*>>>();
check.operator()<contiguous_iterator<int*>, contiguous_iterator<int*>>();
check.operator()<contiguous_iterator<int*>, sentinel_wrapper<contiguous_iterator<int*>>>();
check.operator()<int*, int*>();
check.operator()<int*, sentinel_wrapper<int*>>();
}
constexpr bool test() {
test_iterators();
{ // A custom comparator works.
const std::array input = {1, 2, 3, 4, 5};
std::ranges::greater comp;
{
auto in = input;
auto last = std::ranges::nth_element(in.begin(), in.begin() + 1, in.end(), comp);
assert(in[1] == 4);
assert(last == in.end());
}
{
auto in = input;
auto last = std::ranges::nth_element(in, in.begin() + 1, comp);
assert(in[1] == 4);
assert(last == in.end());
}
}
{ // A custom projection works.
struct A {
int a;
constexpr bool operator==(const A&) const = default;
};
const std::array input = {A{2}, A{1}, A{3}};
{
auto in = input;
auto last = std::ranges::nth_element(in.begin(), in.begin() + 1, in.end(), {}, &A::a);
assert(in[1] == A{2});
assert(last == in.end());
}
{
auto in = input;
auto last = std::ranges::nth_element(in, in.begin() + 1, {}, &A::a);
assert(in[1] == A{2});
assert(last == in.end());
}
}
{ // `std::invoke` is used in the implementation.
struct S {
int i;
constexpr S(int i_) : i(i_) {}
constexpr bool comparator(const S& rhs) const { return i < rhs.i; }
constexpr const S& projection() const { return *this; }
constexpr bool operator==(const S&) const = default;
};
const std::array input = {S{2}, S{1}, S{3}};
{
auto in = input;
auto last = std::ranges::nth_element(in.begin(), in.begin() + 1, in.end(), &S::comparator, &S::projection);
assert(in[1] == S{2});
assert(last == in.end());
}
{
auto in = input;
auto last = std::ranges::nth_element(in, in.begin() + 1, &S::comparator, &S::projection);
assert(in[1] == S{2});
assert(last == in.end());
}
}
{ // The comparator can return any type that's convertible to `bool`.
const std::array input = {2, 1, 3};
auto pred = [](int i, int j) { return BooleanTestable{i < j}; };
{
std::array in = input;
auto last = std::ranges::nth_element(in.begin(), in.begin() + 1, in.end(), pred);
assert(in[1] == 2);
assert(last == in.end());
}
{
std::array in = input;
auto last = std::ranges::nth_element(in, in.begin() + 1, pred);
assert(in[1] == 2);
assert(last == in.end());
}
}
{ // `std::ranges::dangling` is returned.
std::array in{1, 2, 3};
[[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) result =
std::ranges::nth_element(std::move(in), in.begin());
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -108,7 +108,7 @@ static_assert(test(std::ranges::move, a, a));
static_assert(test(std::ranges::move_backward, a, a));
//static_assert(test(std::ranges::next_permutation, a));
static_assert(test(std::ranges::none_of, a, odd));
//static_assert(test(std::ranges::nth_element, a, a+5));
static_assert(test(std::ranges::nth_element, a, a+5));
//static_assert(test(std::ranges::partial_sort, a, a+5));
//static_assert(test(std::ranges::partial_sort_copy, a, a));
//static_assert(test(std::ranges::partition, a, odd));