-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++][ranges] implement ranges::elements_of
#91414
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
Open
xiaoyang-sde
wants to merge
34
commits into
llvm:main
Choose a base branch
from
xiaoyang-sde:elements_of
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
1807a42
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde e14469b
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 85ca069
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde b031c38
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 32b68f2
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 8e35609
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 162ce8a
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 9dd983d
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 8291917
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 30a1e6e
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde b741665
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 1c4122f
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 07a62fe
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 74bd185
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 5e37e03
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde cd3b4cd
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde bfb0e25
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde aacbc6b
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde ffc9700
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 4c72a3a
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde c2df436
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 5884764
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 752d590
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 93c65aa
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde b564478
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 94b9c2c
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 06f28a1
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 89a36ef
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 68a3e3f
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde ea82cce
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 03ccc54
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 5ca6002
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde 2e11e43
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde f717f55
[libc++][ranges] implement 'ranges::elements_of'
xiaoyang-sde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // -*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBCPP___RANGES_ELEMENTS_OF_H | ||
| #define _LIBCPP___RANGES_ELEMENTS_OF_H | ||
|
|
||
| #include <__config> | ||
| #include <__memory/allocator.h> | ||
| #include <__ranges/concepts.h> | ||
| #include <__utility/forward.h> | ||
| #include <cstddef> | ||
|
|
||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
| # pragma GCC system_header | ||
| #endif | ||
|
|
||
| _LIBCPP_PUSH_MACROS | ||
| #include <__undef_macros> | ||
|
|
||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||
|
|
||
| #if _LIBCPP_STD_VER >= 23 | ||
|
|
||
| namespace ranges { | ||
|
|
||
| template <range _Range, class _Allocator = allocator<byte>> | ||
| struct elements_of { | ||
| _LIBCPP_NO_UNIQUE_ADDRESS _Range range; | ||
| _LIBCPP_NO_UNIQUE_ADDRESS _Allocator allocator = _Allocator(); | ||
|
|
||
| # if defined(_LIBCPP_COMPILER_CLANG_BASED) && defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER < 1600 | ||
| // This explicit constructor is required because AppleClang 15 hasn't | ||
| // implemented P0960R3. | ||
| template <std::ranges::range _Range2, class _Allocator2 = std::allocator<std::byte>> | ||
| _LIBCPP_HIDE_FROM_ABI explicit constexpr elements_of(_Range2&& Range, _Allocator2&& Alloc = _Allocator()) | ||
| : range(std::forward<_Range2>(Range)), allocator(std::forward<_Allocator2>(Alloc)) {} | ||
| # endif | ||
| }; | ||
|
|
||
| template <class _Range, class _Allocator = allocator<byte>> | ||
| # if defined(_LIBCPP_COMPILER_CLANG_BASED) && defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER < 1600 | ||
| // This explicit constraint is required because AppleClang 15 might not deduce | ||
| // the correct type for `_Range` without it. | ||
| requires range<_Range&&> | ||
xiaoyang-sde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # endif | ||
| elements_of(_Range&&, _Allocator = _Allocator()) -> elements_of<_Range&&, _Allocator>; | ||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace ranges | ||
|
|
||
| #endif // _LIBCPP_STD_VER >= 23 | ||
|
|
||
| _LIBCPP_END_NAMESPACE_STD | ||
|
|
||
| _LIBCPP_POP_MACROS | ||
|
|
||
| #endif // _LIBCPP___RANGES_ELEMENTS_OF_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
libcxx/test/std/ranges/range.utility/range.elementsof/ctad.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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, c++20 | ||
|
|
||
| // std::ranges::elements_of; | ||
|
|
||
| #include <ranges> | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "min_allocator.h" | ||
| #include "test_allocator.h" | ||
| #include "test_iterators.h" | ||
|
|
||
| template <class Iterator> | ||
| struct Range { | ||
| Iterator begin() const; | ||
| sentinel_wrapper<Iterator> end() const; | ||
| }; | ||
|
|
||
| constexpr bool test() { | ||
| types::for_each( | ||
| types::type_list<std::allocator<std::byte>, min_allocator<std::byte>, test_allocator<std::byte>>{}, | ||
| []<class Allocator> { | ||
| types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iterator> { | ||
| Range<Iterator> r; | ||
| static_assert(std::same_as<decltype(std::ranges::elements_of(r)), | ||
| std::ranges::elements_of<Range<Iterator>&, std::allocator<std::byte>>>); | ||
| static_assert(std::same_as<decltype(std::ranges::elements_of(Range<Iterator>())), | ||
| std::ranges::elements_of<Range<Iterator>&&, std::allocator<std::byte>>>); | ||
|
|
||
| Allocator a; | ||
| static_assert(std::same_as<decltype(std::ranges::elements_of(r, a)), | ||
| std::ranges::elements_of<Range<Iterator>&, Allocator>>); | ||
| static_assert(std::same_as<decltype(std::ranges::elements_of(Range<Iterator>(), Allocator())), | ||
| std::ranges::elements_of<Range<Iterator>&&, Allocator>>); | ||
| }); | ||
| }); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static_assert(test()); |
112 changes: 112 additions & 0 deletions
112
libcxx/test/std/ranges/range.utility/range.elementsof/elements_of.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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, c++20 | ||
|
|
||
| // std::ranges::elements_of; | ||
|
|
||
| #include <ranges> | ||
|
|
||
| #include <memory> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| #include "min_allocator.h" | ||
| #include "test_allocator.h" | ||
| #include "test_iterators.h" | ||
|
|
||
| template <class Iterator> | ||
| struct Range { | ||
| using Sentinel = sentinel_wrapper<Iterator>; | ||
|
|
||
| Iterator begin() { return Iterator(data_.data()); } | ||
|
|
||
| sentinel_wrapper<Iterator> end() { return Sentinel(Iterator(data_.data() + data_.size())); } | ||
xiaoyang-sde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| std::vector<int> data_ = {0, 1, 2, 3}; | ||
| }; | ||
|
|
||
| template <class Range, class Allocator> | ||
| constexpr bool test_range() { | ||
| Range r; | ||
|
|
||
| using elements_of_t = std::ranges::elements_of<Range&, Allocator>; | ||
| { | ||
| // constructor | ||
| std::same_as<elements_of_t> decltype(auto) elements_of = std::ranges::elements_of(r, Allocator()); | ||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_range = elements_of.range; | ||
| if (!std::is_constant_evaluated()) { | ||
| assert(std::ranges::distance(elements_of_range) == 4); | ||
| } | ||
| [[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_allocator = elements_of.allocator; | ||
| } | ||
| { | ||
| // designated initializer | ||
|
|
||
| // AppleClang 15 hasn't implemented P0960R3, so `std::ranges::elements_of` requires a | ||
| // user-defined constructor, making it non-aggregate and therefore incompatible with designated | ||
| // initializers. | ||
| if constexpr (std::is_aggregate_v<elements_of_t>) { | ||
| std::same_as<elements_of_t> decltype(auto) elements_of = std::ranges::elements_of{ | ||
| .range = r, | ||
| .allocator = Allocator(), | ||
| }; | ||
| [[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_range = elements_of.range; | ||
| if (!std::is_constant_evaluated()) { | ||
| assert(std::ranges::distance(elements_of_range) == 4); | ||
| } | ||
| [[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_allocator = elements_of.allocator; | ||
| } | ||
| } | ||
| { | ||
| // copy constructor | ||
| std::same_as<elements_of_t> decltype(auto) elements_of_1 = std::ranges::elements_of(r, Allocator()); | ||
| std::same_as<elements_of_t> auto elements_of_2 = elements_of_1; | ||
| [[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_1_range = elements_of_1.range; | ||
| [[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_2_range = elements_of_2.range; | ||
| if (!std::is_constant_evaluated()) { | ||
| assert(std::ranges::distance(elements_of_1_range) == 4); | ||
| assert(std::ranges::distance(elements_of_2_range) == 4); | ||
| } | ||
| [[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_2_allocator = elements_of_2.allocator; | ||
| } | ||
|
|
||
| using elements_of_r_t = std::ranges::elements_of<Range&&, Allocator>; | ||
| { | ||
| // move constructor | ||
| std::same_as<elements_of_r_t> decltype(auto) elements_of_1 = std::ranges::elements_of(std::move(r), Allocator()); | ||
| std::same_as<elements_of_r_t> auto elements_of_2 = std::move(elements_of_1); | ||
| [[maybe_unused]] std::same_as<Range&&> decltype(auto) elements_of_1_range = std::move(elements_of_1.range); | ||
| [[maybe_unused]] std::same_as<Range&&> decltype(auto) elements_of_2_range = std::move(elements_of_2.range); | ||
| if (!std::is_constant_evaluated()) { | ||
| assert(std::ranges::distance(elements_of_1_range) == 4); | ||
| assert(std::ranges::distance(elements_of_2_range) == 4); | ||
| } | ||
| [[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_2_allocator = elements_of_2.allocator; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| constexpr bool test() { | ||
| types::for_each(types::type_list<std::allocator<std::byte>, min_allocator<std::byte>, test_allocator<std::byte>>{}, | ||
| []<class Allocator> { | ||
| types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iterator> { | ||
| test_range<Range<Iterator>, Allocator>(); | ||
| }); | ||
| }); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int main(int, char**) { | ||
| test(); | ||
| static_assert(test()); | ||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.