Skip to content

[libc++][ranges] Implement ranges::contains_subrange #66963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
647f5fe
[libc++] Implement ranges::contains_subrange
ZijunZhaoCCK Sep 13, 2023
5a1d555
format
ZijunZhaoCCK Sep 21, 2023
7cb4d06
Add a newline
ZijunZhaoCCK Sep 21, 2023
aee12dd
1. call distance(r) directly 2. replace auto with int to avoid potent…
ZijunZhaoCCK Sep 26, 2023
b2c57a2
Reformat
ZijunZhaoCCK Oct 2, 2023
ae96c2f
Correct input type and call ranges::search()
ZijunZhaoCCK Oct 3, 2023
a212ab8
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 2, 2024
67ac338
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 4, 2024
7d11fbc
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 4, 2024
11384e6
1. update function 2. add nondiscard tests
ZijunZhaoCCK Jan 4, 2024
f735342
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 4, 2024
947f87a
Merge branch 'main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 4, 2024
2973c85
update code
ZijunZhaoCCK Jan 5, 2024
3abea8e
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 5, 2024
c4085b3
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
b0f9bac
update code
ZijunZhaoCCK Jan 5, 2024
92005d4
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
72d92de
update code
ZijunZhaoCCK Jan 5, 2024
97b5563
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
65dd43c
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
f68d62d
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
cf639d1
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
f61a18f
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
a5ecb2b
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
e3ec859
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
9d64b67
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
a586862
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 9, 2024
d13b6e1
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
384a487
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Jan 10, 2024
7b66ab9
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Feb 13, 2024
2b1c067
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Feb 13, 2024
16145b2
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
d359566
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Feb 13, 2024
a6fcbb5
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
a3de363
Merge branch 'llvm:main' into libcxx-ranges-contains-subrange
ZijunZhaoCCK Feb 13, 2024
423eeca
1. Call ranges::search() in ranges::contains_subrange()
ZijunZhaoCCK Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libcxx/docs/Status/RangesAlgorithms.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ C++20,all C++20 algorithms,N/A,N/A,✅
C++23,`find_last <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
C++23,`find_last_if <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
C++23,`find_last_if_not <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,
C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao,No patch yet,In Progress
C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,Complete
C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao, `D150831 <https://llvm.org/D150831>`_,Complete
C++23,`shift_left <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`shift_right <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`iota (algorithm) <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
C++23,`fold <https://wg21.link/p2322r5>`_,Unassigned,No patch yet,Not started
C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao,No patch yet,In Progress
C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao, `#65148 <https://github.com/llvm/llvm-project/pull/65148>`_,Complete
C++23,`fold_left_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
C++23,`fold_left <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
C++23,`fold_left_first_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ set(files
__algorithm/ranges_binary_search.h
__algorithm/ranges_clamp.h
__algorithm/ranges_contains.h
__algorithm/ranges_contains_subrange.h
__algorithm/ranges_copy.h
__algorithm/ranges_copy_backward.h
__algorithm/ranges_copy_if.h
Expand Down
99 changes: 99 additions & 0 deletions libcxx/include/__algorithm/ranges_contains_subrange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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_CONTAINS_SUBRANGE_H
#define _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H

#include <__algorithm/ranges_search.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/ranges_operations.h>
#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
#include <__iterator/distance.h>
#include <__iterator/indirectly_comparable.h>
#include <__iterator/projected.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/subrange.h>
#include <__utility/move.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#if _LIBCPP_STD_VER >= 23

_LIBCPP_BEGIN_NAMESPACE_STD

namespace ranges {
namespace __contains_subrange {
struct __fn {
template <forward_iterator _Iter1,
sentinel_for<_Iter1> _Sent1,
forward_iterator _Iter2,
sentinel_for<_Iter2> _Sent2,
class _Pred = ranges::equal_to,
class _Proj1 = identity,
class _Proj2 = identity>
requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static operator()(
_Iter1 __first1,
_Sent1 __last1,
_Iter2 __first2,
_Sent2 __last2,
_Pred __pred = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) {
auto __n2 = ranges::distance(__first2, __last2);
if (__n2 == 0)
return true;

auto __ret = ranges::search(
std::move(__first1), __last1, std::move(__first2), __last2, __pred, std::ref(__proj1), std::ref(__proj2));
return __ret.empty() == false;
}

template <forward_range _Range1,
forward_range _Range2,
class _Pred = ranges::equal_to,
class _Proj1 = identity,
class _Proj2 = identity>
requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static
operator()(_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) {
auto __n2 = 0;
if constexpr (sized_range<_Range2>) {
__n2 = ranges::size(__range2);
} else {
__n2 = std::distance(cbegin(__range2), cend(__range2));
}
if (__n2 == 0)
return true;

auto __ret = ranges::search(__range1, __range2, __pred, std::ref(__proj1), std::ref(__proj2));
return __ret.empty() == false;
}
};
} // namespace __contains_subrange

inline namespace __cpo {
inline constexpr auto contains_subrange = __contains_subrange::__fn{};
} // namespace __cpo
} // namespace ranges

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_STD_VER >= 23

_LIBCPP_POP_MACROS

#endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
14 changes: 14 additions & 0 deletions libcxx/include/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ namespace ranges {
constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
minmax_element(R&& r, Comp comp = {}, Proj proj = {}); // since C++20

template<forward_iterator I1, sentinel_for<I1> S1,
forward_iterator I2, sentinel_for<I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool contains_subrange(I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23

template<forward_range R1, forward_range R2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool contains_subrange(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23

template<class I, class O>
using copy_result = in_out_result<I, O>; // since C++20

Expand Down Expand Up @@ -1875,6 +1888,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_binary_search.h>
#include <__algorithm/ranges_clamp.h>
#include <__algorithm/ranges_contains.h>
#include <__algorithm/ranges_contains_subrange.h>
#include <__algorithm/ranges_copy.h>
#include <__algorithm/ranges_copy_backward.h>
#include <__algorithm/ranges_copy_if.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/libcxx.imp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
{ include: [ "<__algorithm/ranges_binary_search.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_clamp.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_contains.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_contains_subrange.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy_backward.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_copy_if.h>", "private", "<algorithm>", "public" ] },
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ module std_private_algorithm_ranges_clamp [system
export std_private_functional_ranges_operations
}
module std_private_algorithm_ranges_contains [system] { header "__algorithm/ranges_contains.h" }
module std_private_algorithm_ranges_contains_subrange [system] { header "__algorithm/ranges_contains_subrange.h" }
module std_private_algorithm_ranges_copy [system] {
header "__algorithm/ranges_copy.h"
export std_private_algorithm_in_out_result
Expand Down
2 changes: 0 additions & 2 deletions libcxx/modules/std/algorithm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export namespace std {
// [alg.contains], contains
namespace ranges {
using std::ranges::contains;
#if 0
using std::ranges::contains_subrange;
#endif
} // namespace ranges
#endif // _LIBCPP_STD_VER >= 23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ constexpr bool all_the_algorithms()
assert(copies == 0);
(void)std::ranges::contains(a, value, Proj(&copies));
assert(copies == 0);
(void)std::ranges::contains_subrange(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies));
assert(copies == 0);
(void)std::ranges::contains_subrange(a, b, Equal(), Proj(&copies), Proj(&copies));
assert(copies == 0);
#endif
(void)std::ranges::count(first, last, value, Proj(&copies)); assert(copies == 0);
(void)std::ranges::count(a, value, Proj(&copies)); assert(copies == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void test() {
#if TEST_STD_VER >= 23
std::ranges::contains(range, 1);
std::ranges::contains(iter, iter, 1);
std::ranges::contains_subrange(range, range);
std::ranges::contains_subrange(iter, iter, iter, iter);
#endif
std::ranges::count_if(range, pred);
std::ranges::count_if(iter, iter, pred);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ void test() {
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::contains(iter, iter, 1);
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::contains_subrange(range, range);
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::contains_subrange(iter, iter, iter, iter);
// expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::fold_left(range, 0, std::plus());
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
std::ranges::fold_left(iter, iter, 0, std::plus());
Expand Down
Loading