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 @@ -11,10 +11,10 @@ Search,equal,Not assigned,n/a,Not started Search,lexicographical_compare,Not assigned,n/a,Not started Search,partition_point,Christopher Di Bella,`D105794 `_,Under review -Search,lower_bound,Christopher Di Bella,`D105795 `_,Under review -Search,upper_bound,Christopher Di Bella,`D105795 `_,Under review +Search,lower_bound,Nikolas Klauser,,✅ +Search,upper_bound,Nikolas Klauser,,✅ Search,equal_range,Christopher Di Bella,n/a,Not started -Search,binary_search,Christopher Di Bella,n/a,Not started +Search,binary_search,Nikolas Klauser,,✅ Search,min,Not assigned,n/a,Not started Search,max,Not assigned,n/a,Not started Search,minmax,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 @@ -66,13 +66,16 @@ __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_binary_search.h __algorithm/ranges_find.h __algorithm/ranges_find_if.h __algorithm/ranges_find_if_not.h + __algorithm/ranges_lower_bound.h __algorithm/ranges_max_element.h __algorithm/ranges_min_element.h __algorithm/ranges_mismatch.h __algorithm/ranges_swap_ranges.h + __algorithm/ranges_upper_bound.h __algorithm/remove.h __algorithm/remove_copy.h __algorithm/remove_copy_if.h diff --git a/libcxx/include/__algorithm/ranges_binary_search.h b/libcxx/include/__algorithm/ranges_binary_search.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_binary_search.h @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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_BINARY_SEARCH_H +#define _LIBCPP___ALGORITHM_RANGES_BINARY_SEARCH_H + +#include <__algorithm/ranges_lower_bound.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __binary_search { +struct __fn { + template _Sp, class _Tp, class _Proj = identity, + indirect_strict_weak_order> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Ip __first, _Sp __last, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + auto __ret = ranges::__lower_bound_impl(__first, __last, __value, __comp, __proj); + return __ret != __last && !std::invoke(__comp, __value, std::invoke(__proj, *__first)); + } + + template , _Proj>> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Rp&& __r, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + auto __first = ranges::begin(__r); + auto __last = ranges::end(__r); + auto __ret = ranges::__lower_bound_impl(__first, __last, __value, __comp, __proj); + return __ret != __last && !std::invoke(__comp, __value, std::invoke(__proj, *__first)); + } +}; +} // namespace __binary_search + +inline namespace __cpo { + inline constexpr auto binary_search = __binary_search::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_BINARY_SEARCH_H diff --git a/libcxx/include/__algorithm/ranges_lower_bound.h b/libcxx/include/__algorithm/ranges_lower_bound.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_lower_bound.h @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// 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_LOWER_BOUND_H +#define _LIBCPP___ALGORITHM_RANGES_LOWER_BOUND_H + +#include <__algorithm/half_positive.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/advance.h> +#include <__iterator/concepts.h> +#include <__iterator/distance.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.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 { + +template +_LIBCPP_HIDE_FROM_ABI constexpr +_Ip __lower_bound_impl(_Ip __first, _Sp __last, const _Tp& __value, _Comp& __comp, _Proj& __proj) { + using __diff_t = typename iterator_traits<_Ip>::difference_type; + __diff_t __len = std::distance(__first, __last); + while (__len != 0) { + __diff_t __len2 = std::__half_positive(__len); + _Ip __m = __first; + std::advance(__m, __len2); + if (std::invoke(__comp, std::invoke(__proj, *__m), __value)) { + __first = ++__m; + __len -= __len2 + 1; + } else { + __len = __len2; + } + } + return __first; +} + +namespace __lower_bound { +struct __fn { + template _Sp, class _Tp, class _Proj = identity, + indirect_strict_weak_order> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + _Ip operator()(_Ip __first, _Sp __last, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + return __lower_bound_impl(__first, __last, __value, __comp, __proj); + } + + template , _Proj>> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_iterator_t<_Rp> operator()(_Rp&& __r, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + return __lower_bound_impl(ranges::begin(__r), ranges::end(__r), __value, __comp, __proj); + } +}; +} // namespace __lower_bound + +inline namespace __cpo { + inline constexpr auto lower_bound = __lower_bound::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_LOWER_BOUND_H diff --git a/libcxx/include/__algorithm/ranges_upper_bound.h b/libcxx/include/__algorithm/ranges_upper_bound.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_upper_bound.h @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// 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_UPPER_BOUND_H +#define _LIBCPP___ALGORITHM_RANGES_UPPER_BOUND_H + +#include <__algorithm/ranges_lower_bound.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.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 __upper_bound { +struct __fn { + template _Sp, class _Tp, class _Proj = identity, + indirect_strict_weak_order> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + _Ip operator()(_Ip __first, _Sp __last, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + auto __comp2 = [&](auto&& __lhs, auto&& __rhs) { return !std::invoke(__comp, __rhs, __lhs); }; + return __lower_bound_impl(__first, __last, __value, __comp2, __proj); + } + + template , _Proj>> _Comp = ranges::less> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_iterator_t<_Rp> operator()(_Rp&& __r, const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const { + auto __comp2 = [&](auto&& __lhs, auto&& __rhs) { return !std::invoke(__comp, __rhs, __lhs); }; + return __lower_bound_impl(ranges::begin(__r), ranges::end(__r), __value, __comp2, __proj); + } +}; +} // namespace __upper_bound + +inline namespace __cpo { + inline constexpr auto upper_bound = __upper_bound::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_UPPER_BOUND_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -83,6 +83,37 @@ indirect_unary_predicate, Proj>> Pred> constexpr borrowed_iterator_t find_if_not(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S, class T, class Proj = identity, + indirect_strict_weak_order> Comp = ranges::less> + constexpr I upper_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); // since C++20 + + template, Proj>> Comp = + ranges::less> + constexpr borrowed_iterator_t + upper_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); // since C++20 + + template S, class T, class Proj = identity, + indirect_strict_weak_order> Comp = ranges::less> + constexpr I lower_bound(I first, S last, const T& value, Comp comp = {}, + Proj proj = {}); // since C++20 + template, Proj>> Comp = + ranges::less> + constexpr borrowed_iterator_t + lower_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); // since C++20 + + template S, class T, class Proj = identity, + indirect_strict_weak_order> Comp = ranges::less> + constexpr bool binary_search(I first, S last, const T& value, Comp comp = {}, + Proj proj = {}); // since C++20 + template, Proj>> Comp = + ranges::less> + constexpr bool binary_search(R&& r, const T& value, Comp comp = {}, + Proj proj = {}); // since C++20 + } template @@ -800,13 +831,16 @@ #include <__algorithm/pop_heap.h> #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> +#include <__algorithm/ranges_binary_search.h> #include <__algorithm/ranges_find.h> #include <__algorithm/ranges_find_if.h> #include <__algorithm/ranges_find_if_not.h> +#include <__algorithm/ranges_lower_bound.h> #include <__algorithm/ranges_max_element.h> #include <__algorithm/ranges_min_element.h> #include <__algorithm/ranges_mismatch.h> #include <__algorithm/ranges_swap_ranges.h> +#include <__algorithm/ranges_upper_bound.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 @@ -294,13 +294,16 @@ 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_binary_search { private header "__algorithm/ranges_binary_search.h" } module ranges_find { private header "__algorithm/ranges_find.h" } module ranges_find_if { private header "__algorithm/ranges_find_if.h" } module ranges_find_if_not { private header "__algorithm/ranges_find_if_not.h" } + module ranges_lower_bound { private header "__algorithm/ranges_lower_bound.h" } module ranges_max_element { private header "__algorithm/ranges_max_element.h" } module ranges_min_element { private header "__algorithm/ranges_min_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } + module ranges_upper_bound { private header "__algorithm/ranges_upper_bound.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/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -86,7 +86,7 @@ void **first2 = b; //void **mid2 = b+5; void **last2 = b+10; - //void *value = nullptr; + void *value = nullptr; //int count = 1; int copies = 0; @@ -96,8 +96,8 @@ //(void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::any_of(a, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::binary_search(first, last, value, Less(&copies)); assert(copies == 0); - //(void)std::ranges::binary_search(a, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::binary_search(first, last, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::binary_search(a, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::clamp(value, value, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::count_if(first, last, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::count_if(a, UnaryTrue(&copies)); assert(copies == 0); @@ -142,8 +142,8 @@ //if (!std::is_constant_evaluated()) { (void)std::ranges::inplace_merge(a, mid, Less(&copies)); assert(copies == 0); } //(void)std::ranges::lexicographical_compare(first, last, first2, last2, Less(&copies)); assert(copies == 0); //(void)std::ranges::lexicographical_compare(a, b, Less(&copies)); assert(copies == 0); - //(void)std::ranges::lower_bound(first, last, value, Less(&copies)); assert(copies == 0); - //(void)std::ranges::lower_bound(a, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::lower_bound(first, last, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::lower_bound(a, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::make_heap(first, last, Less(&copies)); assert(copies == 0); //(void)std::ranges::make_heap(a, Less(&copies)); assert(copies == 0); //(void)std::ranges::max(value, value, Less(&copies)); assert(copies == 0); @@ -226,8 +226,8 @@ //(void)std::ranges::unique(a, Equal(&copies)); assert(copies == 0); //(void)std::ranges::unique_copy(first, last, first2, Equal(&copies)); assert(copies == 0); //(void)std::ranges::unique_copy(a, first2, Equal(&copies)); assert(copies == 0); - //(void)std::ranges::upper_bound(first, last, value, Less(&copies)); assert(copies == 0); - //(void)std::ranges::upper_bound(a, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::upper_bound(first, last, value, Less(&copies)); assert(copies == 0); + (void)std::ranges::upper_bound(a, value, Less(&copies)); assert(copies == 0); return true; } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_binary_search.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_binary_search.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_binary_search.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_binary_search.h'}} +#include <__algorithm/ranges_binary_search.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_lower_bound.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_lower_bound.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_lower_bound.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_lower_bound.h'}} +#include <__algorithm/ranges_lower_bound.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_upper_bound.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_upper_bound.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_upper_bound.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_upper_bound.h'}} +#include <__algorithm/ranges_upper_bound.h> diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/binary.search/ranges.binary_search.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/binary.search/ranges.binary_search.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/binary.search/ranges.binary_search.pass.cpp @@ -0,0 +1,177 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template S, class T, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr bool ranges::binary_search(I first, S last, const T& value, Comp comp = {}, +// Proj proj = {}); +// template, Proj>> Comp = +// ranges::less> +// constexpr bool ranges::binary_search(R&& r, const T& value, Comp comp = {}, +// Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct NotLessThanComparable {}; + +template +concept HasLowerBoundIt = requires(It it, Sent sent) { std::ranges::binary_search(it, sent, 1); }; + +static_assert(HasLowerBoundIt); +static_assert(!HasLowerBoundIt, sentinel_wrapper>>); +static_assert(!HasLowerBoundIt); +static_assert(!HasLowerBoundIt); +static_assert(!HasLowerBoundIt); + +template +concept HasLowerBoundR = requires(Range range) { std::ranges::binary_search(range, 1); }; + +static_assert(HasLowerBoundR>); +static_assert(!HasLowerBoundR); +static_assert(!HasLowerBoundR); +static_assert(!HasLowerBoundR>); + +template +concept HasLowerBoundPred = requires(int* it, Pred pred) {std::ranges::binary_search(it, it, 1, pred); }; + +static_assert(HasLowerBoundPred); +static_assert(!HasLowerBoundPred); +static_assert(!HasLowerBoundPred); + +template +constexpr void test_iterators() { + { + int a[] = {1, 2, 3, 4, 5, 6}; + std::same_as auto ret = std::ranges::binary_search(It(a), It(a + 6), 3); + assert(base(ret)); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto range = std::ranges::subrange(It(a), It(a + 6)); + auto ret = std::ranges::binary_search(range, 3); + assert(base(ret)); + } +} + +constexpr bool test() { + test_iterators(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + + { // check that the predicate is used + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::binary_search(a, a + 6, 2, std::ranges::greater{}); + assert(ret); + } + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::binary_search(a, 2, std::ranges::greater{}); + assert(ret); + } + } + + { // check that the projection is used + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::binary_search(a, a + 6, 0, {}, [](int i) { return i - 3; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::binary_search(a, 0, {}, [](int i) { return i - 3; }); + assert(ret); + } + } + + { // check that std::invoke is used + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::binary_search(a, a + 6, 4, {}, &S::check); + assert(ret); + } + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::binary_search(a, 4, {}, &S::check); + assert(ret); + } + } + + { // check that true is returned with multiple matches + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::binary_search(a, a + 5, 2); + assert(ret); + } + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::binary_search(a, 2); + assert(ret); + } + } + + { // check that an empty range works + { + std::array a; + auto ret = std::ranges::binary_search(a.begin(), a.end(), 1); + assert(!ret); + } + { + std::array a; + auto ret = std::ranges::binary_search(a, 1); + assert(!ret); + } + } + + { // check that false is returned if all elements compare less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::binary_search(a, a + 4, 5); + assert(!ret); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::binary_search(a, 5); + assert(!ret); + } + } + + { // check that false is returned if no element compares less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::binary_search(a, a + 4, 0); + assert(!ret); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::binary_search(a, 0); + assert(!ret); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); +} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/lower.bound/ranges.lower_bound.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/lower.bound/ranges.lower_bound.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/lower.bound/ranges.lower_bound.pass.cpp @@ -0,0 +1,187 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template S, class T, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr I ranges::lower_bound(I first, S last, const T& value, Comp comp = {}, +// Proj proj = {}); +// template, Proj>> Comp = +// ranges::less> +// constexpr borrowed_iterator_t +// ranges::lower_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct NotLessThanComparable {}; + +template +concept HasLowerBoundIt = requires(It it, Sent sent) { std::ranges::lower_bound(it, sent, 1); }; + +static_assert(HasLowerBoundIt); +static_assert(!HasLowerBoundIt, sentinel_wrapper>>); +static_assert(!HasLowerBoundIt); +static_assert(!HasLowerBoundIt); +static_assert(!HasLowerBoundIt); + +template +concept HasLowerBoundR = requires(Range range) { std::ranges::lower_bound(range, 1); }; + +static_assert(HasLowerBoundR>); +static_assert(!HasLowerBoundR); +static_assert(!HasLowerBoundR); +static_assert(!HasLowerBoundR>); + +template +concept HasLowerBoundPred = requires(int* it, Pred pred) {std::ranges::lower_bound(it, it, 1, pred); }; + +static_assert(HasLowerBoundPred); +static_assert(!HasLowerBoundPred); +static_assert(!HasLowerBoundPred); + +template +constexpr void test_iterators() { + { + int a[] = {1, 2, 3, 4, 5, 6}; + std::same_as auto ret = std::ranges::lower_bound(It(a), It(a + 6), 3); + assert(base(ret) == a + 2); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto range = std::ranges::subrange(It(a), It(a + 6)); + auto ret = std::ranges::lower_bound(range, 3); + assert(base(ret) == a + 2); + } +} + +constexpr bool test() { + test_iterators(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + + { // check that the predicate is used + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::lower_bound(a, a + 6, 2, std::ranges::greater{}); + assert(ret == a + 4); + } + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::lower_bound(a, 2, std::ranges::greater{}); + assert(ret == a + 4); + } + } + + { // check that the projection is used + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::lower_bound(a, a + 6, 0, {}, [](int i) { return i - 3; }); + assert(ret == a + 2); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::lower_bound(a, 0, {}, [](int i) { return i - 3; }); + assert(ret == a + 2); + } + } + + { // check that std::invoke is used + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::lower_bound(a, a + 6, 4, {}, &S::check); + assert(ret == a + 3); + } + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::lower_bound(a, 4, {}, &S::check); + assert(ret == a + 3); + } + } + + { // check that the first lower bound is returned + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::lower_bound(a, a + 5, 2); + assert(ret == a + 1); + } + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::lower_bound(a, 2); + assert(ret == a + 1); + } + } + + { // check that an empty range works + { + std::array a; + auto ret = std::ranges::lower_bound(a.begin(), a.end(), 1); + assert(ret == a.end()); + } + { + std::array a; + auto ret = std::ranges::lower_bound(a, 1); + assert(ret == a.end()); + } + } + + { // check that end is returned if all elements compare less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::lower_bound(a, a + 4, 5); + assert(ret == a + 4); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::lower_bound(a, 5); + assert(ret == a + 4); + } + } + + { // check that the first element is returned if no element compares less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::lower_bound(a, a + 4, 0); + assert(ret == a); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::lower_bound(a, 0); + assert(ret == a); + } + } + + { // check that ranges::dangling is returned + [[maybe_unused]] std::same_as auto ret = std::ranges::lower_bound(std::array{1, 2}, 1); + } + + { // check that an iterator is returned with a borrowing range + int a[] = {1, 2, 3}; + std::same_as auto ret = std::ranges::lower_bound(std::views::all(a), 1); + assert(ret == a + 0); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); +} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/upper.bound/ranges.upper_bound.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/upper.bound/ranges.upper_bound.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.binary.search/upper.bound/ranges.upper_bound.pass.cpp @@ -0,0 +1,186 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template S, class T, class Proj = identity, +// indirect_strict_weak_order> Comp = ranges::less> +// constexpr I ranges::upper_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); +// template, Proj>> Comp = +// ranges::less> +// constexpr borrowed_iterator_t +// ranges::upper_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct NotLessThanComparable {}; + +template +concept HasUpperBoundIt = requires(It it, Sent sent) { std::ranges::upper_bound(it, sent, 1); }; + +static_assert(HasUpperBoundIt); +static_assert(!HasUpperBoundIt, sentinel_wrapper>>); +static_assert(!HasUpperBoundIt); +static_assert(!HasUpperBoundIt); +static_assert(!HasUpperBoundIt); + +template +concept HasUpperBoundR = requires(Range range) { std::ranges::upper_bound(range, 1); }; + +static_assert(HasUpperBoundR>); +static_assert(!HasUpperBoundR); +static_assert(!HasUpperBoundR); +static_assert(!HasUpperBoundR>); + +template +concept HasUpperBoundPred = requires(int* it, Pred pred) {std::ranges::upper_bound(it, it, 1, pred); }; + +static_assert(HasUpperBoundPred); +static_assert(!HasUpperBoundPred); +static_assert(!HasUpperBoundPred); + +template +constexpr void test_iterators() { + { + int a[] = {1, 2, 3, 4, 5, 6}; + std::same_as auto ret = std::ranges::upper_bound(It(a), It(a + 6), 3); + assert(base(ret) == a + 3); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto range = std::ranges::subrange(It(a), It(a + 6)); + auto ret = std::ranges::upper_bound(range, 3); + assert(base(ret) == a + 3); + } +} + +constexpr bool test() { + test_iterators(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + + { // check that the predicate is used + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::upper_bound(a, a + 6, 2, std::ranges::greater{}); + assert(ret == a + 5); + } + { + int a[] = {6, 5, 4, 3, 2, 1}; + auto ret = std::ranges::upper_bound(a, 2, std::ranges::greater{}); + assert(ret == a + 5); + } + } + + { // check that the projection is used + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::upper_bound(a, a + 6, 0, {}, [](int i) { return i - 3; }); + assert(ret == a + 3); + } + { + int a[] = {1, 2, 3, 4, 5, 6}; + auto ret = std::ranges::upper_bound(a, 0, {}, [](int i) { return i - 3; }); + assert(ret == a + 3); + } + } + + { // check that std::invoke is used + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::upper_bound(a, a + 6, 4, {}, &S::check); + assert(ret == a + 4); + } + { + struct S { int check; int other; }; + S a[] = {{1, 6}, {2, 5}, {3, 4}, {4, 3}, {5, 2}, {6, 1}}; + auto ret = std::ranges::upper_bound(a, 4, {}, &S::check); + assert(ret == a + 4); + } + } + + { // check that the last upper bound is returned + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::upper_bound(a, a + 5, 2); + assert(ret == a + 4); + } + { + int a[] = {1, 2, 2, 2, 3}; + auto ret = std::ranges::upper_bound(a, 2); + assert(ret == a + 4); + } + } + + { // check that an empty range works + { + std::array a; + auto ret = std::ranges::upper_bound(a.begin(), a.end(), 1); + assert(ret == a.end()); + } + { + std::array a; + auto ret = std::ranges::upper_bound(a, 1); + assert(ret == a.end()); + } + } + + { // check that end is returned if all elements compare less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::upper_bound(a, a + 4, 5); + assert(ret == a + 4); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::upper_bound(a, 5); + assert(ret == a + 4); + } + } + + { // check that the first element is returned if no element compares less than + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::upper_bound(a, a + 4, 0); + assert(ret == a); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::upper_bound(a, 0); + assert(ret == a); + } + } + + { // check that ranges::dangling is returned + [[maybe_unused]] std::same_as auto ret = std::ranges::upper_bound(std::array{1, 2}, 1); + } + + { // check that an iterator is returned with a borrowing range + int a[] = {1, 2, 3}; + std::same_as auto ret = std::ranges::upper_bound(std::views::all(a), 1); + assert(ret == a + 1); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); +} diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -63,7 +63,7 @@ //static_assert(test(std::ranges::adjacent_find, a)); //static_assert(test(std::ranges::all_of, a, odd)); //static_assert(test(std::ranges::any_of, a, odd)); -//static_assert(test(std::ranges::binary_search, a, 42)); +static_assert(test(std::ranges::binary_search, a, 42)); //static_assert(test(std::ranges::clamp, 42, 42, 42)); //static_assert(test(std::ranges::copy, a, a)); //static_assert(test(std::ranges::copy_backward, a, a)); @@ -94,7 +94,7 @@ //static_assert(test(std::ranges::is_sorted, a)); //static_assert(test(std::ranges::is_sorted_until, a)); //static_assert(test(std::ranges::lexicographical_compare, a, a)); -//static_assert(test(std::ranges::lower_bound, a, 42)); +static_assert(test(std::ranges::lower_bound, a, 42)); //static_assert(test(std::ranges::make_heap, a)); //static_assert(test(std::ranges::max, a)); static_assert(test(std::ranges::max_element, a)); @@ -146,7 +146,7 @@ //static_assert(test(std::ranges::transform, a, a, triple)); //static_assert(test(std::ranges::unique, a)); //static_assert(test(std::ranges::unique_copy, a, a)); -//static_assert(test(std::ranges::upper_bound, a, 42)); +static_assert(test(std::ranges::upper_bound, a, 42)); // [memory.syn] diff --git a/libcxx/test/support/almost_satisfies_types.h b/libcxx/test/support/almost_satisfies_types.h --- a/libcxx/test/support/almost_satisfies_types.h +++ b/libcxx/test/support/almost_satisfies_types.h @@ -128,4 +128,43 @@ static_assert(std::semiregular); static_assert(!std::sentinel_for>); +// almost a forward_iterator +class ForwardIteratorNotDerivedFrom { +public: + using difference_type = long; + using value_type = int; + using iterator_category = std::input_iterator_tag; + + ForwardIteratorNotDerivedFrom& operator++(); + ForwardIteratorNotDerivedFrom operator++(int); + const int& operator*() const; + bool operator==(const ForwardIteratorNotDerivedFrom&) const = default; +}; + +using ForwardRangeNotDerivedFrom = UncheckedRange; + +static_assert(std::input_iterator); +static_assert(std::incrementable); +static_assert(std::sentinel_for); +static_assert(!std::forward_iterator); + +class ForwardIteratorNotIncrementable { +public: + using difference_type = long; + using value_type = int; + using iterator_category = std::forward_iterator_tag; + + ForwardIteratorNotIncrementable& operator++(); + int operator++(int); + const int& operator*() const; + bool operator==(const ForwardIteratorNotIncrementable&) const = default; +}; + +using ForwardRangeNotIncrementable = UncheckedRange; + +static_assert(std::input_iterator); +static_assert(!std::incrementable); +static_assert(std::sentinel_for); +static_assert(!std::forward_iterator); + #endif // ALMOST_SATISFIES_TYPES_H