Skip to content
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

Implement ranges::remove family #1005

Merged
merged 8 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 281 additions & 21 deletions stl/inc/algorithm

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions tests/std/include/range_algorithm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,11 @@ constexpr void test_in_write() {
with_input_ranges<with_writable_iterators<Instantiator, Element2>, Element1>::call();
}

template <class Instantiator, class Element1, class Element2>
constexpr void test_fwd_write() {
with_forward_ranges<with_writable_iterators<Instantiator, Element2>, Element1>::call();
}

template <class Instantiator, class Element>
constexpr void test_read() {
with_input_iterators<Instantiator, Element>::call();
Expand Down
4 changes: 4 additions & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ tests\P0896R4_ranges_alg_none_of
tests\P0896R4_ranges_alg_partition
tests\P0896R4_ranges_alg_partition_copy
tests\P0896R4_ranges_alg_partition_point
tests\P0896R4_ranges_alg_remove
tests\P0896R4_ranges_alg_remove_copy
tests\P0896R4_ranges_alg_remove_copy_if
tests\P0896R4_ranges_alg_remove_if
tests\P0896R4_ranges_alg_replace
tests\P0896R4_ranges_alg_replace_copy
tests\P0896R4_ranges_alg_replace_copy_if
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove/env.lst
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
63 changes: 63 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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() {
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>();
}
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove_copy/env.lst
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
76 changes: 76 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove_copy/test.cpp
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>();
}
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove_copy_if/env.lst
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 tests/std/tests/P0896R4_ranges_alg_remove_copy_if/test.cpp
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>();
}
4 changes: 4 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove_if/env.lst
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
65 changes: 65 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_remove_if/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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() {
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
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>();
}