-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::search #896
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
11 commits
Select commit
Hold shift + click to select a range
6b2c1d7
Implement ranges::search
CaseyCarter 3a003b0
Silly typo
CaseyCarter 4a54963
Review comment
CaseyCarter f2c61e6
Don't constexpr TEST_EVERYTHING
CaseyCarter c6508ef
Review comments
CaseyCarter 5997b46
Document type requirements of internal functions
CaseyCarter b2b2b4a
Merge branch 'master' into search
CaseyCarter fce2cca
Use _U(begin|end)
CaseyCarter 4d3c780
cleanup unnecessary warning suppressions in _Find_last_iterator
CaseyCarter 31b6d07
Review comment
CaseyCarter b22f726
Merge branch 'master' into search
StephanTLavavej 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
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,175 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #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::search(borrowed<false>{}, borrowed<true>{})), ranges::dangling>); | ||
| STATIC_ASSERT(same_as<decltype(ranges::search(borrowed<true>{}, borrowed<false>{})), ranges::subrange<int*>>); | ||
|
|
||
| struct instantiator { | ||
| static constexpr array<P, 8> pairs = { | ||
| P{0, 42}, P{1, 42}, P{2, 42}, P{3, 42}, P{4, 42}, P{5, 42}, P{6, 42}, P{7, 42}}; | ||
| static constexpr array<int, 3> not_pairs = {2, 3, 4}; | ||
| static constexpr array<int, 3> neg_not_pairs = {-2, -3, -4}; | ||
|
|
||
| template <class Fwd1, class Fwd2> | ||
| static constexpr void call() { | ||
| const Fwd1 range1{pairs}; | ||
| { | ||
| const Fwd2 range2{not_pairs}; | ||
|
|
||
| // defaulted predicate and projections | ||
| { | ||
| auto result = ranges::search(range1, range1); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == range1.begin()); | ||
| assert(result.end() == range1.end()); | ||
| } | ||
| { | ||
| auto result = ranges::search( | ||
| ranges::begin(range2), ranges::end(range2), ranges::begin(range2), ranges::end(range2)); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd2>>>); | ||
| assert(result.begin() == range2.begin()); | ||
| assert(result.end() == range2.end()); | ||
| } | ||
| } | ||
|
|
||
| const Fwd2 range2{neg_not_pairs}; | ||
| const auto pred = [](int x, int y) { return x == -y; }; | ||
|
|
||
| // explicit predicate | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| auto result = ranges::search(range2, range2, ranges::equal_to{}); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd2>>>); | ||
| assert(result.begin() == range2.begin()); | ||
| assert(result.end() == range2.end()); | ||
| } | ||
| { | ||
| auto result = ranges::search(ranges::begin(range1), ranges::end(range1), ranges::begin(range1), | ||
| ranges::end(range1), ranges::equal_to{}); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == range1.begin()); | ||
| assert(result.end() == range1.end()); | ||
| } | ||
|
|
||
| // explicit predicate and one projection | ||
| { | ||
| auto result = ranges::search(range1, range2, pred, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == ranges::next(range1.begin(), 2)); | ||
| assert(result.end() == ranges::next(range1.begin(), 5)); | ||
| } | ||
| { | ||
| auto result = ranges::search(ranges::begin(range1), ranges::end(range1), ranges::begin(range2), | ||
| ranges::end(range2), pred, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == ranges::next(range1.begin(), 2)); | ||
| assert(result.end() == ranges::next(range1.begin(), 5)); | ||
| } | ||
|
|
||
| // explicit predicate and two projections | ||
| constexpr auto minus1 = [](int x) { return x - 1; }; | ||
| { | ||
| auto result = ranges::search(range1, range2, pred, get_first, minus1); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == ranges::next(range1.begin(), 3)); | ||
| assert(result.end() == ranges::next(range1.begin(), 6)); | ||
| } | ||
| { | ||
| auto result = ranges::search(ranges::begin(range1), ranges::end(range1), ranges::begin(range2), | ||
| ranges::end(range2), pred, get_first, minus1); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd1>>>); | ||
| assert(result.begin() == ranges::next(range1.begin(), 3)); | ||
| assert(result.end() == ranges::next(range1.begin(), 6)); | ||
| } | ||
|
|
||
| // negative case | ||
| { | ||
| auto result = ranges::search(range2, range1, pred, minus1, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd2>>>); | ||
| assert(result.empty()); | ||
| } | ||
| { | ||
| auto result = ranges::search(ranges::begin(range2), ranges::end(range2), ranges::begin(range1), | ||
| ranges::end(range1), pred, minus1, get_first); | ||
| STATIC_ASSERT(same_as<decltype(result), ranges::subrange<ranges::iterator_t<Fwd2>>>); | ||
| assert(result.empty()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| using Elem1 = const P; | ||
| using Elem2 = const int; | ||
|
|
||
| #ifdef TEST_EVERYTHING | ||
| int main() { | ||
| // No constexpr test here; the test_fwd_fwd call exceeds the maximum number of steps in a constexpr computation. | ||
| test_fwd_fwd<instantiator, Elem1, Elem2>(); | ||
| } | ||
| #else // ^^^ test all range combinations // test only interesting range combos vvv | ||
| template <class Elem, test::Sized IsSized> | ||
| using fwd_test_range = test::range<forward_iterator_tag, Elem, IsSized, test::CanDifference::no, test::Common::no, | ||
| test::CanCompare::yes, test::ProxyRef::yes>; | ||
| template <class Elem, test::Sized IsSized, test::Common IsCommon> | ||
| using random_test_range = test::range<random_access_iterator_tag, Elem, IsSized, test::CanDifference::no, IsCommon, | ||
| test::CanCompare::yes, test::ProxyRef::no>; | ||
|
|
||
| constexpr bool run_tests() { | ||
| // All (except contiguous) proxy reference types, since the algorithm doesn't really care. Cases with only 1 range | ||
| // sized are not interesting; common is interesting only in that it's necessary to trigger memcmp optimization. | ||
|
|
||
| using test::Common, test::Sized; | ||
|
|
||
| // both forward, non-common, and sized or unsized | ||
| instantiator::call<fwd_test_range<Elem1, Sized::no>, fwd_test_range<Elem2, Sized::no>>(); | ||
| instantiator::call<fwd_test_range<Elem1, Sized::yes>, fwd_test_range<Elem2, Sized::yes>>(); | ||
|
|
||
| // both random-access, and sized or unsized; all permutations of common | ||
| instantiator::call<random_test_range<Elem1, Sized::no, Common::no>, | ||
| random_test_range<Elem2, Sized::no, Common::no>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::no, Common::no>, | ||
| random_test_range<Elem2, Sized::no, Common::yes>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::no, Common::yes>, | ||
| random_test_range<Elem2, Sized::no, Common::no>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::no, Common::yes>, | ||
| random_test_range<Elem2, Sized::no, Common::yes>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::yes, Common::no>, | ||
| random_test_range<Elem2, Sized::yes, Common::no>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::yes, Common::no>, | ||
| random_test_range<Elem2, Sized::yes, Common::yes>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::yes, Common::yes>, | ||
| random_test_range<Elem2, Sized::yes, Common::no>>(); | ||
| instantiator::call<random_test_range<Elem1, Sized::yes, Common::yes>, | ||
| random_test_range<Elem2, Sized::yes, Common::yes>>(); | ||
|
|
||
| { | ||
| // Validate the memcmp optimization | ||
| const int haystack[] = {1, 2, 3, 1, 2, 3, 1, 2, 3}; | ||
| const int needle[] = {1, 2, 3}; | ||
| const auto result = ranges::search(span<const int>{haystack}, needle); | ||
| STATIC_ASSERT(same_as<decltype(result), const ranges::subrange<span<const int>::iterator>>); | ||
| assert(to_address(result.begin()) == haystack + 0); | ||
| assert(to_address(result.end()) == haystack + 3); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| STATIC_ASSERT(run_tests()); | ||
| run_tests(); | ||
| } | ||
| #endif // TEST_EVERYTHING | ||
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.