-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::search_n #914
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
5 commits
Select commit
Hold shift + click to select a range
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 |
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,135 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <ranges> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // Validate dangling story | ||
| STATIC_ASSERT(same_as<decltype(ranges::search_n(borrowed<false>{}, 13, 42)), ranges::dangling>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::search_n(borrowed<true>{}, 42, 13)), ranges::subrange<int*>>); | ||
|
|
||
| using P = pair<int, int>; | ||
|
|
||
| struct instantiator { | ||
| static constexpr array<P, 11> pairs = { | ||
| P{0, 42}, P{1, 42}, P{1, 42}, P{0, 42}, P{1, 42}, P{1, 42}, P{0, 42}, P{1, 42}, P{1, 42}, P{1, 42}, P{0, 42}}; | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| template <class Fwd> | ||
| static constexpr void call() { | ||
| const Fwd range{pairs}; | ||
|
|
||
| // defaulted predicate and projections | ||
| { | ||
| const auto result = ranges::search_n(range, 2, P{1, 42}); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 1)); | ||
| assert(result.end() == ranges::next(range.begin(), 3)); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), 2, P{1, 42}); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 1)); | ||
| assert(result.end() == ranges::next(range.begin(), 3)); | ||
| } | ||
|
|
||
| // explicit predicate | ||
| { | ||
| const auto result = ranges::search_n(range, 2, P{1, 42}, ranges::equal_to{}); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 1)); | ||
| assert(result.end() == ranges::next(range.begin(), 3)); | ||
| } | ||
| { | ||
| const auto result = | ||
| ranges::search_n(ranges::begin(range), ranges::end(range), 2, P{1, 42}, ranges::equal_to{}); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 1)); | ||
| assert(result.end() == ranges::next(range.begin(), 3)); | ||
| } | ||
|
|
||
| // explicit predicate and projection | ||
| constexpr auto cmp = [](auto&& x, auto&& y) { return x == y + 1; }; | ||
| { | ||
| const auto result = ranges::search_n(range, 3, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 7)); | ||
| assert(result.end() == ranges::next(range.begin(), 10)); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), 3, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == ranges::next(range.begin(), 7)); | ||
| assert(result.end() == ranges::next(range.begin(), 10)); | ||
| } | ||
|
|
||
| // negative case | ||
| { | ||
| const auto result = ranges::search_n(range, 4, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.end()); | ||
| assert(result.end() == range.end()); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), 4, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.end()); | ||
| assert(result.end() == range.end()); | ||
| } | ||
|
|
||
| // trivial case: empty needle | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| const auto result = ranges::search_n(range, 0, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.begin()); | ||
| assert(result.end() == range.begin()); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), 0, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.begin()); | ||
| assert(result.end() == range.begin()); | ||
| } | ||
|
|
||
| // trivial case: range too small | ||
| { | ||
| const auto result = ranges::search_n(range, 99999, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.end()); | ||
| assert(result.end() == range.end()); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), 99999, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.end()); | ||
| assert(result.end() == range.end()); | ||
| } | ||
|
|
||
| // trivial case: negative count | ||
| { | ||
| const auto result = ranges::search_n(range, -42, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.begin()); | ||
| assert(result.end() == range.begin()); | ||
| } | ||
| { | ||
| const auto result = ranges::search_n(ranges::begin(range), ranges::end(range), -42, 0, cmp, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<ranges::iterator_t<Fwd>>>); | ||
| assert(result.begin() == range.begin()); | ||
| assert(result.end() == range.begin()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| using Elem = const P; | ||
| STATIC_ASSERT((test_fwd<instantiator, Elem>(), true)); | ||
| test_fwd<instantiator, Elem>(); | ||
| } | ||
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.