-
Notifications
You must be signed in to change notification settings - Fork 14.4k
WIP [libc++][ranges] P3060R3: Add std::views::indices(n) #146823
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
Draft
H-G-Hristov
wants to merge
10
commits into
llvm:main
Choose a base branch
from
H-G-Hristov:hgh/libcxx/P3060R3-Add-std_views_indices
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.
+196
−0
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
251a813
[libc++][ranges] P3060R3: Add `std::views::indices(n)`
H-G-Hristov 5e9293a
Implement and cleanup
H-G-Hristov e2c569c
Update version
H-G-Hristov aacc52c
Add release notes
H-G-Hristov 207fb0c
Cleanup
H-G-Hristov 0f2b6cd
House keeping
H-G-Hristov cc3f700
Addressed review comments
H-G-Hristov c1981bd
Update libcxx/modules/std/ranges.inc
Zingam 8e48a6c
Add CPO test
H-G-Hristov 0c947c4
Made the lambda `static`
H-G-Hristov 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
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
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
110 changes: 110 additions & 0 deletions
110
libcxx/test/std/ranges/range.factories/range.iota.view/indices.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,110 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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: std-at-least-c++26 | ||
|
||
// ranges | ||
|
||
// inline constexpr unspecified indices = unspecified; | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include "test_macros.h" | ||
#define TEST_HAS_NO_INT128 // Size cannot be larger than 64 bits | ||
#include "type_algorithms.h" | ||
|
||
#include "types.h" | ||
|
||
// Test SFINAE. | ||
|
||
template <typename SizeType> | ||
concept HasIndices = requires(SizeType s) { std::ranges::views::indices(s); }; | ||
|
||
struct IntegerTypesTest { | ||
template <class T> | ||
constexpr void operator()() { | ||
static_assert(HasIndices<T>); | ||
} | ||
}; | ||
|
||
struct NotIntegerLike {}; | ||
|
||
void test_SFIANE() { | ||
static_assert(HasIndices<std::size_t>); | ||
types::for_each(types::integer_types(), IntegerTypesTest{}); | ||
|
||
// Not integer-like types should not satisfy HasIndices | ||
static_assert(!HasIndices<bool>); | ||
static_assert(!HasIndices<float>); | ||
static_assert(!HasIndices<void>); | ||
static_assert(!HasIndices<SomeInt>); // Does satisfy is_integer_like, but not the conversion to std::size_t | ||
static_assert(!HasIndices<NotIntegerLike>); | ||
} | ||
|
||
constexpr bool test() { | ||
{ | ||
auto indices_view = std::ranges::views::indices(5); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
{ | ||
std::vector v(5, 0); | ||
|
||
auto indices_view = std::ranges::views::indices(std::ranges::size(v)); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
{ | ||
std::vector v(5, SomeInt{}); | ||
|
||
// Check that the indices view works as expected | ||
auto indices_view = std::ranges::views::indices(std::ranges::size(v)); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
test_SFIANE(); | ||
|
||
test(); | ||
static_assert(test()); | ||
|
||
return 0; | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libcxx/modules/std/ranges.inc
should also be changed.Also, is there any particular reason not to make this lambda
static
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about that. Why would we want to mark a CPO lambda
static
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, I'm inclined to make the
operator()
static, see also #86052. I think this will improve performance in certain circumstances, but also accept non-portable (although harmless) calls on volatile-qualified values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!