diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -16,6 +16,7 @@ __algorithm/equal.h __algorithm/fill_n.h __algorithm/fill.h + __algorithm/find.h __algorithm/find_end.h __algorithm/find_first_of.h __algorithm/find_if_not.h diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h --- a/libcxx/include/__algorithm/find.h +++ b/libcxx/include/__algorithm/find.h @@ -11,6 +11,18 @@ #define _LIBCPP___ALGORITHM_FIND_H #include <__config> +#include <__function_like.h> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/operations.h> +#include <__functional/ranges_operations.h> +#include <__functional/reference_wrapper.h> +#include <__functional_base> +#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 @@ -30,6 +42,39 @@ return __first; } +#if !defined(_LIBCPP_HAS_NO_RANGES) + +namespace ranges { + struct __find_fn final : __function_like { + constexpr explicit __find_fn(__tag __x) noexcept : __function_like(__x) {} + + template _Sp, class _Tp, class _Proj = identity> + requires indirect_binary_predicate, const _Tp*> + _LIBCPP_NODISCARD_EXT constexpr + _Ip operator()(_Ip __first, const _Sp __last, const _Tp& __value, _Proj __proj = {}) const + { + for (; __first != __last; ++__first) { + if (_VSTD::invoke(__proj, *__first) == __value) { + break; + } + } + return __first; + } + + template + requires indirect_binary_predicate, _Proj>, const _Tp*> + _LIBCPP_NODISCARD_EXT constexpr + borrowed_iterator_t<_Rp> operator()(_Rp&& __r, const _Tp& __value, _Proj __proj = {}) const + { + return (*this)(ranges::begin(__r), ranges::end(__r), __value, _VSTD::ref(__proj)); + } + }; + + inline constexpr auto find = __find_fn(__function_like::__tag()); +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS diff --git a/libcxx/include/__functional/identity.h b/libcxx/include/__functional/identity.h --- a/libcxx/include/__functional/identity.h +++ b/libcxx/include/__functional/identity.h @@ -30,6 +30,7 @@ using is_transparent = void; }; + #endif // _LIBCPP_STD_VER > 17 _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -42,6 +42,15 @@ constexpr InputIterator // constexpr in C++20 find(InputIterator first, InputIterator last, const T& value); +template S, class T, class Proj = identity> + requires indirect_binary_predicate, const T*> + constexpr I ranges::find(I first, S last, const T& value, Proj proj = {}); // since C++20 + +template + requires indirect_binary_predicate, Proj>, const T*> + constexpr borrowed_iterator_t + ranges::find(R&& r, const T& value, Proj proj = {}); // since C++20 + template constexpr InputIterator // constexpr in C++20 find_if(InputIterator first, InputIterator last, Predicate pred); diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -236,7 +236,10 @@ module equal_range { header "__algorithm/equal_range.h" } module fill { header "__algorithm/fill.h" } module fill_n { header "__algorithm/fill_n.h" } - module find { header "__algorithm/find.h" } + module find { + header "__algorithm/find.h" + export __function_like + } module find_end { header "__algorithm/find_end.h" } module find_first_of { header "__algorithm/find_first_of.h" } module find_if { header "__algorithm/find_if.h" } diff --git a/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.pass.cpp b/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.pass.cpp @@ -0,0 +1,37 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// Test that entities declared [[nodiscard]] as at extension by libc++, are +// only actually declared such when _LIBCPP_ENABLE_NODISCARD is specified. + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// All entities to which libc++ applies [[nodiscard]] as an extension should +// be tested here and in nodiscard_extensions.fail.cpp. They should also +// be listed in `UsingLibcxx.rst` in the documentation for the extension. + +#include +#include + +#include + +#include "test_macros.h" + + +int main(int, char**) { + auto arr = std::array{0, 1, 2, 3, 4, 5}; + namespace ranges = std::ranges; + + ranges::find(ranges::begin(arr), ranges::end(arr), 1); + ranges::find(arr, 1); + + return 0; +} diff --git a/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.verify.cpp b/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/nodiscard_ranges_extensions.verify.cpp @@ -0,0 +1,38 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// UNSUPPORTED: gcc-10 + +// Test that entities declared [[nodiscard]] as an extension by libc++, are +// only actually declared such when _LIBCPP_ENABLE_NODISCARD is specified. + +// All entities to which libc++ applies [[nodiscard]] as an extension should +// be tested here and in nodiscard_extensions.pass.cpp. They should also +// be listed in `UsingLibcxx.rst` in the documentation for the extension. + +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_NODISCARD + +#include +#include + +#include + +int main() +{ + auto arr = std::array{0, 1, 2, 3, 4, 5}; + namespace ranges = std::ranges; + + // expected-warning-re@+1 {{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}} + ranges::find(ranges::begin(arr), ranges::end(arr), 1); + + // expected-warning-re@+1 {{ignoring return value of function declared with {{'nodiscard'|warn_unused_result}} attribute}} + ranges::find(arr, 1); +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/ranges_find.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/ranges_find.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/ranges_find.pass.cpp @@ -0,0 +1,172 @@ +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// UNSUPPORTED: gcc-10 + +// + +// ranges::find + +#include + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +namespace ranges = std::ranges; + +template class I> +constexpr bool check_find() +{ + + auto const range = std::array{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + auto successor = [](auto const x) { return x + 1; }; + + { + constexpr auto value = 6; + constexpr auto expected_complexity = 7; + auto const expected_result = range.begin() + 6; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + { + constexpr auto value = 3.0; + constexpr auto expected_complexity = 4; + auto const expected_result = range.begin() + 3; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + { + constexpr auto value = 3.7; + constexpr auto expected_complexity = ranges::ssize(range); + auto const expected_result = range.begin() + expected_complexity; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + { + constexpr auto value = 6; + constexpr auto expected_complexity = 6; + auto const expected_result = range.begin() + 5; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + { + constexpr auto value = 3.0; + constexpr auto expected_complexity = 3; + auto const expected_result = range.begin() + 2; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + { + constexpr auto value = 3.7; + constexpr auto expected_complexity = ranges::ssize(range); + auto const expected_result = range.begin() + expected_complexity; + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input.begin(), input.end(), value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + { + auto input = ranges::subrange(complexity_iterator(I(range.begin())), sentinel_wrapper(range.end())); + auto const result = ranges::find(input, value, successor); + assert(result.base().base() == expected_result); + assert(result.count() == expected_complexity); + } + } + + return true; +} + +int main(int, char**) +{ + // static_assert(check_find()); + check_find(); + + // static_assert(check_find()); + check_find(); + + // static_assert(check_find()); + check_find(); + + // static_assert(check_find()); + check_find(); + + // static_assert(check_find()); + check_find(); + + // static_assert(check_find()); + check_find(); + + ASSERT_SAME_TYPE(decltype(ranges::find(std::vector(10), 6)), + ranges::dangling); + + ASSERT_SAME_TYPE(decltype(ranges::find(std::vector(10), 6, {})), + ranges::dangling); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/special_function.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/special_function.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges_find/special_function.compile.pass.cpp @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// UNSUPPORTED: gcc-10 + +// ranges::find + +#include + +#include +#include +#include +#include + +#include "test_range.h" +#include "test_standard_function.h" + +static_assert(is_function_like()); + +namespace ranges = std::ranges; + +namespace std::ranges { + template + class basic_istream_view { + struct iterator; + public: + iterator begin(); + default_sentinel_t end() const; + }; +} // namespace std::ranges + +// The entities defined in the std::ranges namespace in this Clause are not found by argument-dependent +// name lookup ([basic.lookup.argdep]). When found by unqualified ([basic.lookup.unqual]) name lookup for +// the postfix-expression in a function call ([expr.call]), they inhibit argument-dependent name lookup. +template +constexpr bool no_unqualified_lookup() { + auto identity = ranges::basic_istream_view(); + static_assert(!requires(T value) { find(ranges::begin(identity), ranges::end(identity), value); }); + static_assert(!requires(T value) { find(ranges::begin(identity), ranges::end(identity), value.first, &T::first); }); + static_assert(!requires(T value) { find(identity, value); }); + static_assert(!requires(T value) { find(identity, value.first, &T::first); }); + return true; +}; + +static_assert(no_unqualified_lookup>()); + +namespace test { +template +struct iterator { + using value_type = std::pair; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; + + iterator() = default; + + value_type operator*() const; + iterator& operator++(); + iterator operator++(int); + + bool operator==(iterator const&) const = default; +}; + +template +struct range { + std::pair const* begin() const; + std::pair const* end() const; +}; + +template +void find(iterator, iterator, std::pair const&) { + static_assert(std::same_as); +} + +template +void find(iterator, iterator, int, Proj) { + static_assert(std::same_as); +} + +template +void find(R&&, std::pair const&) { + static_assert(std::same_as); +} + +template +void find(R&&, int, Proj) { + static_assert(std::same_as); +} +} // namespace test + +// When found by unqualified ([basic.lookup.unqual]) name lookup for the postfix-expression in a +// function call ([expr.call]), they inhibit argument-dependent name lookup. +void adl_inhibition() { + using std::ranges::find; + + test::iterator x; + (void)find(x, x, std::pair{10, 20}); + (void)find(x, x, 10, &std::pair::first); + + test::range r; + (void)find(r, std::pair{10, 20}); + (void)find(r, 10, &std::pair::first); +} diff --git a/libcxx/test/support/test_iterators.h b/libcxx/test/support/test_iterators.h --- a/libcxx/test/support/test_iterators.h +++ b/libcxx/test/support/test_iterators.h @@ -861,6 +861,13 @@ difference_type stride_displacement_ = 0; }; +template +concept sentinel_for_base = requires(U const& u) { + u.base(); + requires std::input_or_output_iterator>; + requires std::equality_comparable_with; +}; + template class sentinel_wrapper { public: @@ -871,6 +878,11 @@ return base_ == other; } + template + requires sentinel_for_base + constexpr bool operator==(I2 const& other) const { + return base_ == other.base(); + } private: I base_ = I(); };