-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::move_backward #1053
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2b9f6d
Implement ranges::move_backward
CaseyCarter 0af3e4e
Review comments
CaseyCarter 6abb1a0
miscco review comments
CaseyCarter 4c82da1
Maybe it's better to run the tests than comment them out
CaseyCarter a75ccf9
Promote _Move_backward_common to namespace scope (for at least inplac…
CaseyCarter 30a4859
Apply suggestions from code review
CaseyCarter 23d68f2
Update tests/std/tests/P0896R4_ranges_alg_move_backward/test.cpp
CaseyCarter 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\concepts_matrix.lst |
141 changes: 141 additions & 0 deletions
141
tests/std/tests/P0896R4_ranges_alg_move_backward/test.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,141 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <span> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct int_wrapper { | ||
| int val = 10; | ||
|
|
||
| constexpr int_wrapper() = default; | ||
| constexpr int_wrapper(int x) : val{x} {} | ||
| constexpr int_wrapper(int_wrapper&& that) : val{exchange(that.val, -1)} {} | ||
| constexpr int_wrapper& operator=(int_wrapper&& that) { | ||
| val = exchange(that.val, -1); | ||
| return *this; | ||
| } | ||
| auto operator<=>(const int_wrapper&) const = default; | ||
| }; | ||
|
|
||
| // Validate that move_backward_result aliases in_out_result | ||
| STATIC_ASSERT(same_as<ranges::move_backward_result<int, double>, ranges::in_out_result<int, double>>); | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::move_backward(borrowed<false>{}, nullptr_to<int>)), | ||
| ranges::move_backward_result<ranges::dangling, int*>>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::move_backward(borrowed<true>{}, nullptr_to<int>)), | ||
| ranges::move_backward_result<int*, int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr int expected_output[] = {13, 42, 1729}; | ||
| static constexpr int expected_input[] = {-1, -1, -1}; | ||
| static constexpr int expected_overlapping[] = {-1, 0, 1, 2}; | ||
|
|
||
| template <ranges::bidirectional_range R1, ranges::bidirectional_range R2> | ||
| static constexpr void call() { | ||
| #if !defined(__clang__) && !defined(__EDG__) // TRANSITION, VSO-938163 | ||
| #pragma warning(suppress : 4127) // conditional expression is constant | ||
| if (!ranges::contiguous_range<R1> || !is_constant_evaluated()) | ||
| #endif // TRANSITION, VSO-938163 | ||
| { | ||
| // For the second range, we need an iterator to the end; it's expedient to simply ignore ranges with | ||
| // differing iterator and sentinel types (i.e., ranges that don't satisfy common_range). | ||
| if constexpr (ranges::common_range<R2>) { | ||
| using ranges::move_backward, ranges::move_backward_result, ranges::equal, ranges::iterator_t; | ||
|
|
||
| { // Validate range overload | ||
| int_wrapper input[] = {13, 42, 1729}; | ||
| int_wrapper output[] = {-2, -2, -2}; | ||
| R1 wrapped_input{input}; | ||
| R2 wrapped_output{output}; | ||
| same_as<move_backward_result<iterator_t<R1>, iterator_t<R2>>> auto result = | ||
| move_backward(wrapped_input, wrapped_output.end()); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out == wrapped_output.begin()); | ||
| assert(equal(output, expected_output, ranges::equal_to{}, &int_wrapper::val)); | ||
| assert(equal(input, expected_input, ranges::equal_to{}, &int_wrapper::val)); | ||
| } | ||
| { // Validate iterator + sentinel overload | ||
| int_wrapper input[] = {13, 42, 1729}; | ||
| int_wrapper output[] = {-2, -2, -2}; | ||
| R1 wrapped_input{input}; | ||
| R2 wrapped_output{output}; | ||
| same_as<move_backward_result<iterator_t<R1>, iterator_t<R2>>> auto result = | ||
| move_backward(wrapped_input.begin(), wrapped_input.end(), wrapped_output.end()); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out == wrapped_output.begin()); | ||
| assert(equal(output, expected_output, ranges::equal_to{}, &int_wrapper::val)); | ||
| assert(equal(input, expected_input, ranges::equal_to{}, &int_wrapper::val)); | ||
| } | ||
| { // Validate overlapping ranges | ||
| int_wrapper io[] = {0, 1, 2, 42}; | ||
| R1 wrapped_input{span{io}.first<3>()}; | ||
| R2 wrapped_output{span{io}.last<3>()}; | ||
| same_as<move_backward_result<iterator_t<R1>, iterator_t<R2>>> auto result = | ||
| move_backward(wrapped_input, wrapped_output.end()); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out == wrapped_output.begin()); | ||
| assert(equal(io, expected_overlapping, ranges::equal_to{}, &int_wrapper::val)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| constexpr void test_memmove() { | ||
| // Get some coverage for the memmove optimization, which we would not otherwise have since we do not currently | ||
| // unwrap output iterators. TRANSITION, GH-893 | ||
| using ranges::move_backward, ranges::move_backward_result, ranges::begin, ranges::end, ranges::equal; | ||
|
|
||
| struct S { // move-only and trivially copyable | ||
| int val = 10; | ||
|
|
||
| constexpr S() = default; | ||
| constexpr S(int x) : val{x} {} | ||
| constexpr S(S&&) = default; | ||
| constexpr S& operator=(S&&) = default; | ||
| auto operator<=>(const S&) const = default; | ||
| }; | ||
|
|
||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { // Validate range overload | ||
| S input[] = {13, 42, 1729}; | ||
| S output[] = {-2, -2, -2}; | ||
| const same_as<move_backward_result<S*, S*>> auto result = move_backward(input, end(output)); | ||
| assert(result.in == end(input)); | ||
| assert(result.out == begin(output)); | ||
| assert(equal(output, input)); | ||
| } | ||
| { // Validate iterator + sentinel overload | ||
| S input[] = {13, 42, 1729}; | ||
| S output[] = {-2, -2, -2}; | ||
| const same_as<move_backward_result<S*, S*>> auto result = move_backward(begin(input), end(input), end(output)); | ||
| assert(result.in == end(input)); | ||
| assert(result.out == begin(output)); | ||
| assert(equal(output, input)); | ||
| } | ||
| { // Validate overlapping ranges | ||
| S io[] = {0, 1, 2, 42}; | ||
| const same_as<move_backward_result<S*, S*>> auto result = move_backward(io + 0, io + 3, io + 4); | ||
| assert(result.in == io + 3); | ||
| assert(result.out == io + 1); | ||
| constexpr int expected[] = {0, 0, 1, 2}; | ||
| assert(equal(io, expected, ranges::equal_to{}, &S::val)); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| STATIC_ASSERT((test_bidi_bidi<instantiator, int_wrapper, int_wrapper>(), true)); | ||
| test_bidi_bidi<instantiator, int_wrapper, int_wrapper>(); | ||
|
|
||
| STATIC_ASSERT((test_memmove(), true)); | ||
| test_memmove(); | ||
| } | ||
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.