diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -18,7 +18,7 @@ Search,min,Not assigned,n/a,Not started Search,max,Not assigned,n/a,Not started Search,minmax,Not assigned,n/a,Not started -Search,min_element,Not assigned,n/a,Not started +Search,min_element,Nikolas Klauser,n/a,Complete Search,max_element,Not assigned,n/a,Not started Search,minmax_element,Not assigned,n/a,Not started Search,count,Not assigned,n/a,Not started diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -60,6 +60,7 @@ __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_min_element.h __algorithm/remove.h __algorithm/remove_copy.h __algorithm/remove_copy_if.h diff --git a/libcxx/include/__algorithm/ranges_min_element.h b/libcxx/include/__algorithm/ranges_min_element.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_min_element.h @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// 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_MIN_ELEMENT_H +#define _LIBCPP___ALGORITHM_RANGES_MIN_ELEMENT_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __min_element { +struct __fn { + template _Sp, class _Proj = identity, + indirect_strict_weak_order> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + _Ip operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const { + _Ip __i = __first; + while (++__i != __last) + if (bool(invoke(__comp, invoke(__proj, *__i), invoke(__proj, *__first)))) + __first = __i; + return __first; + } + + template , _Proj>> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_iterator_t<_Rp> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const { + return operator()(ranges::begin(__r), ranges::end(__r), __comp, __proj); + } +}; +} // namespace __min_element + +inline namespace __cpo { + inline constexpr auto min_element = __min_element::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ALGORITHM_RANGES_MIN_ELEMENT_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -18,6 +18,16 @@ namespace std { +namespace ranges { + template S, class Proj = identity, + indirect_strict_weak_order> Comp = ranges::less> // Since C++20 + constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {}); + + template, Proj>> Comp = ranges::less> // Since C++20 + constexpr borrowed_iterator_t min_element(R&& r, Comp comp = {}, Proj proj = {}); +} + template constexpr bool // constexpr in C++20 all_of(InputIterator first, InputIterator last, Predicate pred); @@ -725,6 +735,7 @@ #include <__algorithm/pop_heap.h> #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> +#include <__algorithm/ranges_min_element.h> #include <__algorithm/remove.h> #include <__algorithm/remove_copy.h> #include <__algorithm/remove_copy_if.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -280,6 +280,7 @@ module pop_heap { private header "__algorithm/pop_heap.h" } module prev_permutation { private header "__algorithm/prev_permutation.h" } module push_heap { private header "__algorithm/push_heap.h" } + module ranges_min_elemnt { private header "__algorithm/ranges_min_element.h" } module remove { private header "__algorithm/remove.h" } module remove_copy { private header "__algorithm/remove_copy.h" } module remove_copy_if { private header "__algorithm/remove_copy_if.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min_element.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min_element.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min_element.module.verify.cpp @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min_element.h'}} +#include <__algorithm/ranges_min_element.h> diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min_element.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min_element.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min_element.pass.cpp @@ -0,0 +1,122 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template S, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr I ranges::min_element(I first, S last, Comp comp = {}, Proj proj = {}); +// +// template, Proj>> Comp = ranges::less> +// constexpr borrowed_iterator_t ranges::min_element(R&& r, Comp comp = {}, Proj proj = {}); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" + +std::mt19937 randomness; + +template +constexpr void test_iterators(Iter first, Iter last) { + std::same_as auto it = std::ranges::min_element(first, last); + if (first != last) { + for (Iter j = first; j != last; ++j) + assert(!(*j < *it)); + } else { + assert(it == first); + } +} + +template +constexpr void test_range(Range&& rng, Iter begin, Iter end) { + std::same_as auto it = std::ranges::min_element(std::forward(rng)); + if (begin != end) { + for (Iter j = begin; j != end; ++j) + assert(!(*j < *it)); + } else { + assert(it == begin); + } +} + +template +void test() { + std::array a; + for (size_t i = 0; i < N; ++i) + a[i] = i; + std::shuffle(a.begin(), a.end(), randomness); + ((test_iterators(Iters(a.begin()), Iters(a.end())), + std::shuffle(a.begin(), a.end(), randomness)), ...); + test_range(a, a.begin(), a.end()); +} + +template +bool test() { + test<0, Iters...>(); + test<1, Iters...>(); + test<2, Iters...>(); + test<3, Iters...>(); + test<10, Iters...>(); + test<1000, Iters...>(); + + return true; +} + +constexpr void test_borrowed_range_and_sentinel() { + int a[] = {7, 6, 1, 3, 5, 1, 2, 4}; + + std::ranges::subrange sr {a, sentinel_wrapper(a + 8)}; + int* ret = std::ranges::min_element(sr); + assert(ret == a + 2); +} + +constexpr void test_comparator() { + int a[] = {7, 6, 9, 3, 5, 1, 2, 4}; + + std::ranges::subrange sr {a, sentinel_wrapper(a + 8)}; + int* ret = std::ranges::min_element(sr, std::ranges::greater{}); + assert(ret == a + 2); +} + +constexpr void test_projection() { + int a[] = {7, 6, 9, 3, 5, 1, 2, 4}; + + std::ranges::subrange sr {a, sentinel_wrapper(a + 8)}; + int* ret = std::ranges::min_element(sr, std::ranges::less{}, [](int i){ return i == 5 ? -100 : i; }); + assert(ret == a + 4); +} + +constexpr bool constexpr_test() { + int a[] = {7, 6, 5, 3, 4, 2, 1, 8}; + test_iterators(a, a + 8); + int a2[] = {7, 6, 5, 3, 4, 2, 1, 8}; + test_range(a2, a2, a2 + 8); + + test_borrowed_range_and_sentinel(); + test_comparator(); + test_projection(); + + return true; +} + +int main(int, char**) { + test, + bidirectional_iterator, + random_access_iterator, + const int*>(); + // static_assert(constexpr_test()); + + return 0; +}