-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::remove family #1005
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
8 commits
Select commit
Hold shift + click to select a range
623e18e
Implement ranges::remove family
miscco 0d1f9ff
Address review comments
miscco fe5e859
Ensure that we only do N comparisons
miscco cc64c08
Apply fix against VSO-938163
miscco aaccfc1
Review comments and test failures:
CaseyCarter dc551d1
Review comments from STL
miscco f4c82ba
Apply suggestions from code review
CaseyCarter ea92006
Resolve some more VSO-938163 issues triggered by merging with the cop…
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
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,69 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <span> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| using namespace std; | ||
| using P = pair<int, int>; | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove(borrowed<false>{}, 42)), ranges::dangling>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove(borrowed<true>{}, 42)), ranges::subrange<int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr P expected[3] = {{0, 99}, {2, 99}, {4, 99}}; | ||
|
|
||
| template <ranges::forward_range Read> | ||
| static constexpr void call() { | ||
| #if !defined(__clang__) && !defined(__EDG__) // TRANSITION, VSO-938163 | ||
| #pragma warning(suppress : 4127) // conditional expression is constant | ||
| if (!ranges::contiguous_range<Read> || !is_constant_evaluated()) | ||
| #endif // TRANSITION, VSO-938163 | ||
| { | ||
| using ranges::remove, ranges::subrange, ranges::equal, ranges::iterator_t; | ||
|
|
||
| size_t projectionCounter = 0; | ||
| auto projection = [&projectionCounter](const P& val) { | ||
| ++projectionCounter; | ||
| return val.second; | ||
| }; | ||
|
|
||
| { // Validate iterator + sentinel overload | ||
| P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove(wrapped_input.begin(), wrapped_input.end(), 47, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), subrange<iterator_t<Read>>>); | ||
| assert(result.begin() == next(wrapped_input.begin(), 3)); | ||
| assert(result.end() == wrapped_input.end()); | ||
| assert(equal(expected, span{input}.first<3>())); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
|
|
||
| projectionCounter = 0; | ||
|
|
||
| { // Validate range overload | ||
| P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove(wrapped_input, 47, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), subrange<iterator_t<Read>>>); | ||
| assert(result.begin() == next(wrapped_input.begin(), 3)); | ||
| assert(result.end() == wrapped_input.end()); | ||
| assert(equal(expected, span{input}.first<3>())); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| STATIC_ASSERT((test_fwd<instantiator, P>(), true)); | ||
| test_fwd<instantiator, P>(); | ||
| } |
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 |
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,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| using namespace std; | ||
| using P = pair<int, int>; | ||
|
|
||
| // Validate that remove_copy_result aliases in_out_result | ||
| STATIC_ASSERT(same_as<ranges::remove_copy_result<int, double>, ranges::in_out_result<int, double>>); | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_copy(borrowed<false>{}, static_cast<int*>(nullptr), 42)), | ||
| ranges::remove_copy_result<ranges::dangling, int*>>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_copy(borrowed<true>{}, static_cast<int*>(nullptr), 42)), | ||
| ranges::remove_copy_result<int*, int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| static constexpr P expected[3] = {{0, 99}, {2, 99}, {4, 99}}; | ||
|
|
||
| template <ranges::input_range Read, indirectly_writable<ranges::range_reference_t<Read>> Write> | ||
| static constexpr void call() { | ||
| // Fails checking the indirect_binary_predicate requirement in C1XX's permissive mode with proxy iterators | ||
| // (probably related to VSO-566808) | ||
| constexpr bool non_proxy = | ||
| is_reference_v<ranges::range_reference_t<Read>> && is_reference_v<iter_reference_t<Write>>; | ||
| if constexpr (non_proxy || !is_permissive) { | ||
| using ranges::remove_copy, ranges::remove_copy_result, ranges::equal, ranges::iterator_t; | ||
|
|
||
| size_t projectionCounter = 0; | ||
| auto projection = [&projectionCounter](const P& val) { | ||
| ++projectionCounter; | ||
| return val.second; | ||
| }; | ||
|
|
||
| { // Validate iterator + sentinel overload | ||
| P output[3] = {{-1, -1}, {-1, -1}, {-1, -1}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove_copy(wrapped_input.begin(), wrapped_input.end(), Write{output}, 47, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), remove_copy_result<iterator_t<Read>, Write>>); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out.peek() == output + 3); | ||
| assert(equal(output, expected)); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
|
|
||
| projectionCounter = 0; | ||
|
|
||
| { // Validate range overload | ||
| P output[3] = {{-1, -1}, {-1, -1}, {-1, -1}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove_copy(wrapped_input, Write{output}, 47, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), remove_copy_result<iterator_t<Read>, Write>>); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out.peek() == output + 3); | ||
| assert(equal(output, expected)); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| #ifndef _PREFAST_ // TRANSITION, GH-1030 | ||
| STATIC_ASSERT((test_in_write<instantiator, const P, P>(), true)); | ||
| #endif // TRANSITION, GH-1030 | ||
| test_in_write<instantiator, const P, P>(); | ||
| } |
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 |
77 changes: 77 additions & 0 deletions
77
tests/std/tests/P0896R4_ranges_alg_remove_copy_if/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,77 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| using namespace std; | ||
| using P = pair<int, int>; | ||
|
|
||
| constexpr auto matches = [](const int val) { return val == 47; }; | ||
|
|
||
| // Validate that remove_copy_if_result aliases in_out_result | ||
| STATIC_ASSERT(same_as<ranges::remove_copy_if_result<int, double>, ranges::in_out_result<int, double>>); | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_copy_if(borrowed<false>{}, static_cast<int*>(nullptr), matches)), | ||
| ranges::remove_copy_if_result<ranges::dangling, int*>>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_copy_if(borrowed<true>{}, static_cast<int*>(nullptr), matches)), | ||
| ranges::remove_copy_if_result<int*, int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| static constexpr P expected[3] = {{0, 99}, {2, 99}, {4, 99}}; | ||
|
|
||
| template <ranges::input_range Read, indirectly_writable<ranges::range_reference_t<Read>> Write> | ||
| static constexpr void call() { | ||
| // Fails checking the indirect_binary_predicate requirement in C1XX's permissive mode with proxy iterators | ||
| // (probably related to VSO-566808) | ||
| constexpr bool non_proxy = | ||
| is_reference_v<ranges::range_reference_t<Read>> && is_reference_v<iter_reference_t<Write>>; | ||
| if constexpr (non_proxy || !is_permissive) { | ||
| using ranges::remove_copy_if, ranges::remove_copy_if_result, ranges::equal, ranges::iterator_t; | ||
|
|
||
| size_t projectionCounter = 0; | ||
| auto projection = [&projectionCounter](const P& val) { | ||
| ++projectionCounter; | ||
| return val.second; | ||
| }; | ||
|
|
||
| { // Validate iterator + sentinel overload | ||
| P output[3] = {{-1, -1}, {-1, -1}, {-1, -1}}; | ||
| Read wrapped_input{input}; | ||
| auto result = | ||
| remove_copy_if(wrapped_input.begin(), wrapped_input.end(), Write{output}, matches, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), remove_copy_if_result<iterator_t<Read>, Write>>); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out.peek() == output + 3); | ||
| assert(equal(output, expected)); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
|
|
||
| projectionCounter = 0; | ||
|
|
||
| { // Validate range overload | ||
| P output[3] = {{-1, -1}, {-1, -1}, {-1, -1}}; | ||
| Read wrapped_input{input}; | ||
| auto result = remove_copy_if(wrapped_input, Write{output}, matches, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), remove_copy_if_result<iterator_t<Read>, Write>>); | ||
| assert(result.in == wrapped_input.end()); | ||
| assert(result.out.peek() == output + 3); | ||
| assert(equal(output, expected)); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| #ifndef _PREFAST_ // TRANSITION, GH-1030 | ||
| STATIC_ASSERT((test_in_write<instantiator, const P, P>(), true)); | ||
| #endif // TRANSITION, GH-1030 | ||
| test_in_write<instantiator, const P, P>(); | ||
| } |
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 |
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,71 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <span> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| using namespace std; | ||
| using P = pair<int, int>; | ||
|
|
||
| constexpr auto matches = [](const int val) { return val == 47; }; | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_if(borrowed<false>{}, matches)), ranges::dangling>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::remove_if(borrowed<true>{}, matches)), ranges::subrange<int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr P expected[3] = {{0, 99}, {2, 99}, {4, 99}}; | ||
|
|
||
| template <ranges::forward_range Read> | ||
| static constexpr void call() { | ||
| #if !defined(__clang__) && !defined(__EDG__) // TRANSITION, VSO-938163 | ||
| #pragma warning(suppress : 4127) // conditional expression is constant | ||
| if (!ranges::contiguous_range<Read> || !is_constant_evaluated()) | ||
| #endif // TRANSITION, VSO-938163 | ||
| { | ||
| using ranges::remove_if, ranges::subrange, ranges::equal, ranges::iterator_t; | ||
|
|
||
| size_t projectionCounter = 0; | ||
| auto projection = [&projectionCounter](const P& val) { | ||
| ++projectionCounter; | ||
| return val.second; | ||
| }; | ||
|
|
||
| { // Validate iterator + sentinel overload | ||
| P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove_if(wrapped_input.begin(), wrapped_input.end(), matches, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), subrange<iterator_t<Read>>>); | ||
| assert(result.begin() == next(wrapped_input.begin(), 3)); | ||
| assert(result.end() == wrapped_input.end()); | ||
| assert(equal(expected, span{input}.first<3>())); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
|
|
||
| projectionCounter = 0; | ||
|
|
||
| { // Validate range overload | ||
| P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}; | ||
| Read wrapped_input{input}; | ||
|
|
||
| auto result = remove_if(wrapped_input, matches, projection); | ||
| STATIC_ASSERT(same_as<decltype(result), subrange<iterator_t<Read>>>); | ||
| assert(result.begin() == next(wrapped_input.begin(), 3)); | ||
| assert(result.end() == wrapped_input.end()); | ||
| assert(equal(expected, span{input}.first<3>())); | ||
| assert(projectionCounter == ranges::size(input)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| STATIC_ASSERT((test_fwd<instantiator, P>(), true)); | ||
| test_fwd<instantiator, P>(); | ||
| } | ||
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.