Skip to content

Commit 3b470d1

Browse files
committed
[libc++][ranges] Implement ranges::min_element
Implement ranges::min_element Reviewed By: Quuxplusone, Mordante, #libc Spies: miscco, libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D117025
1 parent 0498f92 commit 3b470d1

File tree

7 files changed

+292
-3
lines changed

7 files changed

+292
-3
lines changed

libcxx/docs/Status/RangesAlgorithms.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Search,binary_search,Christopher Di Bella,n/a,Not started
1818
Search,min,Not assigned,n/a,Not started
1919
Search,max,Not assigned,n/a,Not started
2020
Search,minmax,Not assigned,n/a,Not started
21-
Search,min_element,Not assigned,n/a,Not started
21+
Search,min_element,Nikolas Klauser,n/a,Complete
2222
Search,max_element,Not assigned,n/a,Not started
2323
Search,minmax_element,Not assigned,n/a,Not started
2424
Search,count,Not assigned,n/a,Not started

libcxx/include/CMakeLists.txt

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

libcxx/include/algorithm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ namespace ranges {
3131
template <class I, class O1, class O2>
3232
struct in_out_out_result; // since C++20
3333
34-
template<class I, class O>
35-
struct in_out_result; // since C++20
34+
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
35+
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> // since C++20
36+
constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
37+
38+
template<forward_range R, class Proj = identity,
39+
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less> // since C++20
40+
constexpr borrowed_iterator_t<R> min_element(R&& r, Comp comp = {}, Proj proj = {});
3641
}
3742
3843
template <class InputIterator, class Predicate>
@@ -749,6 +754,7 @@ template <class BidirectionalIterator, class Compare>
749754
#include <__algorithm/pop_heap.h>
750755
#include <__algorithm/prev_permutation.h>
751756
#include <__algorithm/push_heap.h>
757+
#include <__algorithm/ranges_min_element.h>
752758
#include <__algorithm/ranges_swap_ranges.h>
753759
#include <__algorithm/remove.h>
754760
#include <__algorithm/remove_copy.h>

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ module std [system] {
285285
module pop_heap { private header "__algorithm/pop_heap.h" }
286286
module prev_permutation { private header "__algorithm/prev_permutation.h" }
287287
module push_heap { private header "__algorithm/push_heap.h" }
288+
module ranges_min_element { private header "__algorithm/ranges_min_element.h" }
288289
module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" }
289290
module remove { private header "__algorithm/remove.h" }
290291
module remove_copy { private header "__algorithm/remove_copy.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_min_element.h'}}
15+
#include <__algorithm/ranges_min_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::min_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::min_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 HasMinElement = requires (T t) { std::ranges::min_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(HasMinElement<std::array<int, 0>>);
42+
static_assert(!HasMinElement<int>);
43+
static_assert(!HasMinElement<NoLessThanOp>);
44+
static_assert(!HasMinElement<NotTotallyOrdered>);
45+
46+
template <class Iter>
47+
constexpr void test_iterators(Iter first, Iter last) {
48+
std::same_as<Iter> auto it = std::ranges::min_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::min_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::min_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::min_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::min_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::min_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}, 0);
99+
test<It>({2, 1}, 1);
100+
test<It>({2, 1, 2}, 1);
101+
test<It>({2, 1, 1}, 1);
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::min_element(std::views::all(a));
110+
assert(ret == a + 2);
111+
assert(*ret == 1);
112+
}
113+
114+
constexpr void test_comparator() {
115+
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
116+
int* ret = std::ranges::min_element(a, std::ranges::greater{});
117+
assert(ret == a + 2);
118+
assert(*ret == 9);
119+
}
120+
121+
constexpr void test_projection() {
122+
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
123+
{
124+
int* ret = std::ranges::min_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::min_element(a, std::less<int*>{}, [](int& i) { return &i; });
130+
assert(ret == a);
131+
assert(*ret == 7);
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::min_element(arr) == arr);
149+
assert(std::ranges::min_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::min_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+
}

0 commit comments

Comments
 (0)