Skip to content

Commit 205557c

Browse files
committed
[libc++][ranges] Implement ranges::max_element
Implement ranges::max_element Reviewed By: Quuxplusone, #libc Spies: libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D117523
1 parent 54dafd3 commit 205557c

File tree

7 files changed

+284
-1
lines changed

7 files changed

+284
-1
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ set(files
6666
__algorithm/pop_heap.h
6767
__algorithm/prev_permutation.h
6868
__algorithm/push_heap.h
69+
__algorithm/ranges_max_element.h
6970
__algorithm/ranges_min_element.h
7071
__algorithm/ranges_swap_ranges.h
7172
__algorithm/remove.h
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_MAX_ELEMENT_H
10+
#define _LIBCPP___ALGORITHM_RANGES_MAX_ELEMENT_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+
22+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23+
# pragma GCC system_header
24+
#endif
25+
26+
#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
27+
28+
_LIBCPP_BEGIN_NAMESPACE_STD
29+
30+
namespace ranges {
31+
namespace __max_element {
32+
struct __fn {
33+
template <class _Ip, class _Sp, class _Proj, class _Comp>
34+
_LIBCPP_HIDE_FROM_ABI static constexpr
35+
_Ip __go(_Ip __first, _Sp __last, _Comp& __comp, _Proj& __proj) {
36+
if (__first == __last)
37+
return __first;
38+
39+
_Ip __i = __first;
40+
while (++__i != __last)
41+
if (std::invoke(__comp, std::invoke(__proj, *__first), std::invoke(__proj, *__i)))
42+
__first = __i;
43+
return __first;
44+
}
45+
46+
template <forward_iterator _Ip, sentinel_for<_Ip> _Sp, class _Proj = identity,
47+
indirect_strict_weak_order<projected<_Ip, _Proj>> _Comp = ranges::less>
48+
_LIBCPP_HIDE_FROM_ABI constexpr
49+
_Ip operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const {
50+
return __go(__first, __last, __comp, __proj);
51+
}
52+
53+
template <forward_range _Rp, class _Proj = identity,
54+
indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
55+
_LIBCPP_HIDE_FROM_ABI constexpr
56+
borrowed_iterator_t<_Rp> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
57+
return __go(ranges::begin(__r), ranges::end(__r), __comp, __proj);
58+
}
59+
};
60+
} // namespace __max_element
61+
62+
inline namespace __cpo {
63+
inline constexpr auto max_element = __max_element::__fn{};
64+
} // namespace __cpo
65+
} // namespace ranges
66+
67+
_LIBCPP_END_NAMESPACE_STD
68+
69+
#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
70+
71+
#endif // _LIBCPP___ALGORITHM_RANGES_MAX_ELEMENT_H

libcxx/include/algorithm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ template <class BidirectionalIterator, class Compare>
763763
#include <__algorithm/pop_heap.h>
764764
#include <__algorithm/prev_permutation.h>
765765
#include <__algorithm/push_heap.h>
766+
#include <__algorithm/ranges_max_element.h>
766767
#include <__algorithm/ranges_min_element.h>
767768
#include <__algorithm/ranges_swap_ranges.h>
768769
#include <__algorithm/remove.h>

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ module std [system] {
294294
module pop_heap { private header "__algorithm/pop_heap.h" }
295295
module prev_permutation { private header "__algorithm/prev_permutation.h" }
296296
module push_heap { private header "__algorithm/push_heap.h" }
297+
module ranges_max_element { private header "__algorithm/ranges_max_element.h" }
297298
module ranges_min_element { private header "__algorithm/ranges_min_element.h" }
298299
module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" }
299300
module remove { private header "__algorithm/remove.h" }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
// REQUIRES: modules-build
10+
11+
// WARNING: This test was generated by 'generate_private_header_tests.py'
12+
// and should not be edited manually.
13+
14+
// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max_element.h'}}
15+
#include <__algorithm/ranges_max_element.h>
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
// <algorithm>
10+
11+
// UNSUPPORTED: c++03, c++11, c++14, c++17
12+
// UNSUPPORTED: libcpp-no-concepts
13+
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
14+
15+
// template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
16+
// indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
17+
// constexpr I ranges::max_element(I first, S last, Comp comp = {}, Proj proj = {});
18+
//
19+
// template<forward_range R, class Proj = identity,
20+
// indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
21+
// constexpr borrowed_iterator_t<R> ranges::max_element(R&& r, Comp comp = {}, Proj proj = {});
22+
23+
#include <algorithm>
24+
#include <array>
25+
#include <cassert>
26+
#include <random>
27+
#include <ranges>
28+
29+
#include "test_macros.h"
30+
#include "test_iterators.h"
31+
32+
template <class T>
33+
concept HasMaxElement = requires (T t) { std::ranges::max_element(t); };
34+
35+
struct NoLessThanOp {};
36+
struct NotTotallyOrdered {
37+
int i;
38+
bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
39+
};
40+
41+
static_assert(HasMaxElement<std::array<int, 0>>);
42+
static_assert(!HasMaxElement<int>);
43+
static_assert(!HasMaxElement<NoLessThanOp>);
44+
static_assert(!HasMaxElement<NotTotallyOrdered>);
45+
46+
template <class Iter>
47+
constexpr void test_iterators(Iter first, Iter last) {
48+
std::same_as<Iter> auto it = std::ranges::max_element(first, last);
49+
if (first != last) {
50+
for (Iter j = first; j != last; ++j)
51+
assert(!(*j > *it));
52+
} else {
53+
assert(it == first);
54+
}
55+
}
56+
57+
template <class Range, class Iter>
58+
constexpr void test_range(Range&& rng, Iter begin, Iter end) {
59+
std::same_as<Iter> auto it = std::ranges::max_element(std::forward<Range>(rng));
60+
if (begin != end) {
61+
for (Iter j = begin; j != end; ++j)
62+
assert(!(*j > *it));
63+
} else {
64+
assert(it == begin);
65+
}
66+
}
67+
68+
template <class It>
69+
constexpr void test(std::initializer_list<int> a, int expected) {
70+
const int* first = a.begin();
71+
const int* last = a.end();
72+
{
73+
std::same_as<It> auto it = std::ranges::max_element(It(first), It(last));
74+
assert(base(it) == first + expected);
75+
}
76+
{
77+
using Sent = sentinel_wrapper<It>;
78+
std::same_as<It> auto it = std::ranges::max_element(It(first), Sent(It(last)));
79+
assert(base(it) == first + expected);
80+
}
81+
{
82+
auto range = std::ranges::subrange(It(first), It(last));
83+
std::same_as<It> auto it = std::ranges::max_element(range);
84+
assert(base(it) == first + expected);
85+
}
86+
{
87+
using Sent = sentinel_wrapper<It>;
88+
auto range = std::ranges::subrange(It(first), Sent(It(last)));
89+
std::same_as<It> auto it = std::ranges::max_element(range);
90+
assert(base(it) == first + expected);
91+
}
92+
}
93+
94+
template <class It>
95+
constexpr bool test() {
96+
test<It>({}, 0);
97+
test<It>({1}, 0);
98+
test<It>({1, 2}, 1);
99+
test<It>({2, 1}, 0);
100+
test<It>({2, 1, 2}, 0);
101+
test<It>({2, 1, 1}, 0);
102+
103+
return true;
104+
}
105+
106+
constexpr void test_borrowed_range_and_sentinel() {
107+
int a[] = {7, 6, 1, 3, 5, 1, 2, 4};
108+
109+
int* ret = std::ranges::max_element(std::views::all(a));
110+
assert(ret == a + 0);
111+
assert(*ret == 7);
112+
}
113+
114+
constexpr void test_comparator() {
115+
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
116+
int* ret = std::ranges::max_element(a, std::ranges::greater{});
117+
assert(ret == a + 5);
118+
assert(*ret == 1);
119+
}
120+
121+
constexpr void test_projection() {
122+
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
123+
{
124+
int* ret = std::ranges::max_element(a, std::ranges::less{}, [](int i) { return i == 5 ? 100 : i; });
125+
assert(ret == a + 4);
126+
assert(*ret == 5);
127+
}
128+
{
129+
int* ret = std::ranges::max_element(a, std::less<int*>{}, [](int& i) { return &i; });
130+
assert(ret == a + 7);
131+
assert(*ret == 4);
132+
}
133+
}
134+
135+
struct Immobile {
136+
int i;
137+
138+
constexpr Immobile(int i_) : i(i_) {}
139+
Immobile(const Immobile&) = delete;
140+
Immobile(Immobile&&) = delete;
141+
142+
auto operator<=>(const Immobile&) const = default;
143+
};
144+
145+
constexpr void test_immobile() {
146+
147+
Immobile arr[] {1, 2, 3};
148+
assert(std::ranges::max_element(arr) == arr);
149+
assert(std::ranges::max_element(arr, arr + 3) == arr);
150+
}
151+
152+
constexpr void test_dangling() {
153+
int compares = 0;
154+
int projections = 0;
155+
auto comparator = [&](int a, int b) {
156+
++compares;
157+
return a < b;
158+
};
159+
auto projection = [&](int a) {
160+
++projections;
161+
return a;
162+
};
163+
[[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
164+
std::ranges::max_element(std::array{1, 2, 3}, comparator, projection);
165+
assert(compares == 2);
166+
assert(projections == 4);
167+
}
168+
169+
constexpr bool test() {
170+
171+
test<forward_iterator<const int*>>();
172+
test<bidirectional_iterator<const int*>>();
173+
test<random_access_iterator<const int*>>();
174+
test<const int*>();
175+
176+
int a[] = {7, 6, 5, 3, 4, 2, 1, 8};
177+
test_iterators(a, a + 8);
178+
int a2[] = {7, 6, 5, 3, 4, 2, 1, 8};
179+
test_range(a2, a2, a2 + 8);
180+
181+
test_borrowed_range_and_sentinel();
182+
test_comparator();
183+
test_projection();
184+
test_dangling();
185+
186+
return true;
187+
}
188+
189+
int main(int, char**) {
190+
test();
191+
static_assert(test());
192+
193+
return 0;
194+
}

libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int a[10];
9898
//static_assert(test(std::ranges::lower_bound, a, 42));
9999
//static_assert(test(std::ranges::make_heap, a));
100100
//static_assert(test(std::ranges::max, a));
101-
//static_assert(test(std::ranges::max_element, a));
101+
static_assert(test(std::ranges::max_element, a));
102102
//static_assert(test(std::ranges::merge, a, a, a));
103103
//static_assert(test(std::ranges::min, a));
104104
static_assert(test(std::ranges::min_element, a));

0 commit comments

Comments
 (0)