-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::starts_with and ranges::ends_with #1997
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
StephanTLavavej
merged 27 commits into
microsoft:main
from
miscco:P1659-ranges-starts_with
Oct 20, 2021
Merged
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ed0af15
Implement ranges::starts_with and ranges::ends_with
miscco a4d81a2
Address review feedback
miscco 08ac0ed
Accept any integral for _Equal_count
miscco c884756
Merge branch 'main' into P1659-ranges-starts_with
miscco 75b4380
Add feature test macro
miscco 39bd70d
Apply optimizations for infinite ranges
miscco 51c859d
Merge branch 'main' into P1659-ranges-starts_with
StephanTLavavej 3817faf
Fix failing tests
miscco e2ac556
Rework so that we pass unwrapped iterators around
miscco e0a9b89
Merge branch 'main' into P1659-ranges-starts_with
miscco 063f583
Fix formatting
miscco 81e49e5
Merge branch 'main' into P1659-ranges-starts_with
miscco 1e26004
Merge branch 'main' into P1659-ranges-starts_with
miscco b929362
Merge branch 'main' into P1659-ranges-starts_with
StephanTLavavej 37a18ba
Sort `tests/std/test.lst`.
StephanTLavavej 69141f7
Merge branch 'main' into P1659-ranges-starts_with
miscco 5849ddb
Address review comments
miscco 624f273
Fix incorret feature test macro placement
miscco 5f22bb6
Fix infinite ranges and prepare LWG issue
miscco 9164868
Begone infinite ranges
miscco 2bf6d93
Merge branch 'main' into P1659-ranges-starts_with
miscco 8c9bf6a
We cannot handle infinite input ranges at all in starts_with, as `_Eq…
miscco 507f85b
Reenable infinite sizes optimization
miscco 58da2b3
optimize ends_with; fix borked tests
CaseyCarter cd7733d
Factor out large middle section of _Ends_with_impl
CaseyCarter 2df4fa1
Review comments
CaseyCarter 42c9471
[[nonodiscard]]
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
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_latest_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,96 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <functional> | ||
#include <ranges> | ||
#include <utility> | ||
|
||
#include <range_algorithm_support.hpp> | ||
|
||
using namespace std; | ||
|
||
// clang-format off | ||
template <class T> | ||
concept testable_range = ranges::input_range<T> && (ranges::forward_range<T> || ranges::sized_range<T>); | ||
|
||
template <class T> | ||
concept testable_sentinel = ranges::input_range<T> | ||
&& (ranges::forward_range<T> || sized_sentinel_for<ranges::sentinel_t<T>, ranges::iterator_t<T>>); | ||
// clang-format on | ||
|
||
struct instantiator { | ||
static constexpr pair<int, int> haystack[] = {{0, 42}, {2, 42}, {4, 42}}; | ||
static constexpr pair<int, int> short_haystack[] = {{4, 42}}; | ||
static constexpr pair<long, long> needle[] = {{13, 2}, {13, 4}}; | ||
static constexpr pair<long, long> wrong_needle[] = {{13, 2}, {13, 3}}; | ||
|
||
template <testable_range In1, testable_range In2> | ||
static constexpr void test_range() { | ||
using ranges::ends_with, ranges::equal_to; | ||
|
||
{ | ||
const same_as<bool> auto match = ends_with(In1{haystack}, In2{needle}, equal_to{}, get_first, get_second); | ||
assert(match); | ||
} | ||
{ | ||
const same_as<bool> auto match = | ||
ends_with(In1{haystack}, In2{wrong_needle}, equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
{ | ||
const same_as<bool> auto match = | ||
ends_with(In1{short_haystack}, In2{needle}, equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
} | ||
|
||
template <testable_sentinel In1, testable_sentinel In2> | ||
static constexpr void test_iterator_sentinel() { | ||
using ranges::begin, ranges::end, ranges::ends_with, ranges::equal_to; | ||
|
||
{ | ||
In1 h{haystack}; | ||
In2 n{needle}; | ||
const same_as<bool> auto match = | ||
ends_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(match); | ||
} | ||
|
||
{ | ||
In1 h{haystack}; | ||
In2 n{wrong_needle}; | ||
const same_as<bool> auto match = | ||
ends_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
|
||
{ | ||
In1 h{short_haystack}; | ||
In2 n{needle}; | ||
const same_as<bool> auto match = | ||
ends_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
} | ||
|
||
template <ranges::input_range In1, ranges::input_range In2> | ||
static void call() { | ||
if constexpr (testable_range<In1> && testable_range<In2>) { | ||
test_range<In1, In2>(); | ||
STATIC_ASSERT((test_range<In1, In2>(), true)); | ||
} | ||
|
||
if constexpr (testable_sentinel<In1> && testable_sentinel<In2>) { | ||
test_iterator_sentinel<In1, In2>(); | ||
STATIC_ASSERT((test_iterator_sentinel<In1, In2>(), true)); | ||
} | ||
} | ||
}; | ||
|
||
int main() { | ||
#ifndef _PREFAST_ // TRANSITION, GH-1030 | ||
test_in_in<instantiator, const pair<int, int>, const pair<long, long>>(); | ||
#endif // TRANSITION, GH-1030 | ||
} |
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_latest_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,84 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <functional> | ||
#include <ranges> | ||
#include <utility> | ||
|
||
#include <range_algorithm_support.hpp> | ||
|
||
using namespace std; | ||
|
||
struct instantiator { | ||
static constexpr pair<int, int> haystack[] = {{0, 42}, {2, 42}, {4, 42}}; | ||
static constexpr pair<int, int> short_haystack[] = {{0, 42}}; | ||
static constexpr pair<long, long> needle[] = {{13, 0}, {13, 2}}; | ||
static constexpr pair<long, long> wrong_needle[] = {{13, 0}, {13, 3}}; | ||
|
||
template <ranges::input_range In1, ranges::input_range In2> | ||
static constexpr void test() { | ||
using ranges::begin, ranges::end, ranges::starts_with; | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Validate range overload | ||
{ | ||
const same_as<bool> auto match = starts_with(In1{haystack}, In2{needle}, equal_to{}, get_first, get_second); | ||
assert(match); | ||
} | ||
{ | ||
const same_as<bool> auto match = | ||
starts_with(In1{haystack}, In2{wrong_needle}, equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
{ | ||
const same_as<bool> auto match = | ||
starts_with(In1{short_haystack}, In2{needle}, equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
|
||
// Validate iterator + sentinel overload | ||
{ | ||
In1 h{haystack}; | ||
In2 n{needle}; | ||
const same_as<bool> auto match = | ||
starts_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(match); | ||
} | ||
{ | ||
In1 h{haystack}; | ||
In2 n{wrong_needle}; | ||
const same_as<bool> auto match = | ||
starts_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
{ | ||
In1 h{short_haystack}; | ||
In2 n{needle}; | ||
const same_as<bool> auto match = | ||
starts_with(begin(h), end(h), begin(n), end(n), equal_to{}, get_first, get_second); | ||
assert(!match); | ||
} | ||
} | ||
|
||
template <ranges::input_range In1, ranges::input_range In2> | ||
static void call() { | ||
test<In1, In2>(); | ||
STATIC_ASSERT((test<In1, In2>(), true)); | ||
} | ||
}; | ||
|
||
int main() { | ||
{ // infinite haystack | ||
const same_as<bool> auto match = ranges::starts_with(views::iota(0), views::iota(0, 5)); | ||
assert(match); | ||
} | ||
{ // infinite needle | ||
const same_as<bool> auto match = ranges::starts_with(views::iota(0, 5), views::iota(0)); | ||
assert(!match); | ||
} | ||
|
||
#ifndef _PREFAST_ // TRANSITION, GH-1030 | ||
test_in_in<instantiator, const pair<int, int>, const pair<long, long>>(); | ||
#endif // TRANSITION, GH-1030 | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.