1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// <algorithm>
// template<input_iterator I, sentinel_for<I> S, weakly_incrementable O, class T,
// class Proj = identity>
// requires indirectly_copyable<I, O> &&
// indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
// constexpr remove_copy_result<I, O>
// remove_copy(I first, S last, O result, const T& value, Proj proj = {}); // Since C++20
//
// template<input_range R, weakly_incrementable O, class T, class Proj = identity>
// requires indirectly_copyable<iterator_t<R>, O> &&
// indirect_binary_predicate<ranges::equal_to,
// projected<iterator_t<R>, Proj>, const T*>
// constexpr remove_copy_result<borrowed_iterator_t<R>, O>
// remove_copy(R&& r, O result, const T& value, Proj proj = {}); // Since C++20
#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <ranges>
#include <utility>
#include "almost_satisfies_types.h"
#include "counting_projection.h"
#include "test_iterators.h"
struct ToPtr {
int* operator()(int) const;
};
template <class Iter = int*, class Sent = int*, class OutIter = int*, class Proj = std::identity>
concept HasRemoveCopyIter =
requires(Iter&& iter, Sent&& sent, OutIter&& out, Proj&& proj) {
std::ranges::remove_copy(
std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<OutIter>(out), 0, std::forward<Proj>(proj));
};
static_assert(HasRemoveCopyIter<int*>);
// !input_iterator<I>
static_assert(!HasRemoveCopyIter<InputIteratorNotDerivedFrom>);
static_assert(!HasRemoveCopyIter<cpp20_output_iterator<int*>>);
// !sentinel_for<S, I>
static_assert(!HasRemoveCopyIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasRemoveCopyIter<int*, SentinelForNotSemiregular>);
// !weakly_incrementable<O>
static_assert(!HasRemoveCopyIter<int*, int*, WeaklyIncrementableNotMovable>);
// !indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
static_assert(!HasRemoveCopyIter<int*, int*, int*, ToPtr>);
// !indirectly_copyable<I, O>
static_assert(!HasRemoveCopyIter<int*, int*, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasRemoveCopyIter<const int*, const int*, const int*>);
template <class Range, class OutIter = int*, class Proj = std::identity>
concept HasRemoveCopyRange =
requires(Range&& range, OutIter&& out, Proj&& proj) {
std::ranges::remove_copy(
std::forward<Range>(range), std::forward<OutIter>(out), 0, std::forward<Proj>(proj));
};
template <class T>
using R = UncheckedRange<T>;
static_assert(HasRemoveCopyRange<R<int*>>);
// !input_range<R>
static_assert(!HasRemoveCopyRange<InputRangeNotDerivedFrom>);
static_assert(!HasRemoveCopyRange<InputRangeNotIndirectlyReadable>);
static_assert(!HasRemoveCopyRange<InputRangeNotInputOrOutputIterator>);
static_assert(!HasRemoveCopyRange<InputRangeNotSentinelSemiregular>);
static_assert(!HasRemoveCopyRange<InputRangeNotSentinelEqualityComparableWith>);
// !weakly_incrementable<O>
static_assert(!HasRemoveCopyRange<R<int*>, WeaklyIncrementableNotMovable>);
// !indirect_binary_predicate<ranges::equal_to, projected<iterator_t<I>, Proj>, const T*>
static_assert(!HasRemoveCopyRange<R<int*>, int*, ToPtr>);
// !indirectly_copyable<I, O>
static_assert(!HasRemoveCopyRange<R<int*>, int*, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasRemoveCopyRange<const int*, const int*, const int*>);
template <int N, int M>
struct Data {
std::array<int, N> input;
std::array<int, M> expected;
int val;
};
template <class InIter, class Sent, class OutIter, int N, int M>
constexpr void test(Data<N, M> d) {
using Result = std::ranges::remove_copy_result<InIter, OutIter>;
{ // iterator overload
std::array<int, M> output;
std::same_as<Result> decltype(auto) ret = std::ranges::remove_copy(
InIter(d.input.data()), Sent(InIter(d.input.data() + d.input.size())), OutIter(output.data()), d.val);
assert(base(ret.in) == d.input.data() + N);
assert(base(ret.out) == output.data() + M);
assert(d.expected == output);
}
{ // range overload
std::array<int, M> output;
auto range = std::ranges::subrange(InIter(d.input.data()), Sent(InIter(d.input.data() + d.input.size())));
std::same_as<Result> decltype(auto) ret =
std::ranges::remove_copy(range, OutIter(output.data()), d.val);
assert(base(ret.in) == d.input.data() + N);
assert(base(ret.out) == output.data() + M);
assert(d.expected == output);
}
}
template <class Iter, class Sent, class OutIter>
constexpr void tests() {
// simple test
test<Iter, Sent, OutIter, 6, 5>({.input = {1, 2, 3, 4, 5, 6}, .expected = {1, 2, 3, 4, 6}, .val = 5});
// empty range
test<Iter, Sent, OutIter, 0, 0>({});
// single element range - match
test<Iter, Sent, OutIter, 1, 0>({.input = {1}, .expected = {}, .val = 1});
// single element range - no match
test<Iter, Sent, OutIter, 1, 1>({.input = {1}, .expected = {1}, .val = 2});
// two element range - same order
test<Iter, Sent, OutIter, 2, 1>({.input = {1, 2}, .expected = {1}, .val = 2});
// two element range - reversed order
test<Iter, Sent, OutIter, 2, 1>({.input = {1, 2}, .expected = {2}, .val = 1});
// all elements match
test<Iter, Sent, OutIter, 5, 0>({.input = {1, 1, 1, 1, 1}, .expected = {}, .val = 1});
// the relative order of elements isn't changed
test<Iter, Sent, OutIter, 8, 5>({.input = {1, 2, 3, 2, 3, 4, 2, 5}, .expected = {1, 3, 3, 4, 5}, .val = 2});
}
template <class InIter, class Sent>
constexpr void test_output_iterators() {
tests<InIter, Sent, cpp17_output_iterator<int*>>();
tests<InIter, Sent, forward_iterator<int*>>();
tests<InIter, Sent, bidirectional_iterator<int*>>();
tests<InIter, Sent, random_access_iterator<int*>>();
tests<InIter, Sent, contiguous_iterator<int*>>();
tests<InIter, Sent, int*>();
}
template <class Iter>
constexpr void test_sentinels() {
test_output_iterators<Iter, Iter>();
test_output_iterators<Iter, sentinel_wrapper<Iter>>();
test_output_iterators<Iter, sized_sentinel<Iter>>();
}
constexpr bool test() {
test_output_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_output_iterators<cpp17_input_iterator<int*>, sized_sentinel<cpp17_input_iterator<int*>>>();
test_output_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_output_iterators<cpp20_input_iterator<int*>, sized_sentinel<cpp20_input_iterator<int*>>>();
test_sentinels<forward_iterator<int*>>();
test_sentinels<bidirectional_iterator<int*>>();
test_sentinels<random_access_iterator<int*>>();
test_sentinels<contiguous_iterator<int*>>();
test_sentinels<int*>();
{ // check that passing a different type works
struct S {
constexpr operator int() const { return 3; }
};
{ // iterator overload
int a[] = {1, 2, 3, 4};
int b[3];
std::ranges::remove_copy(std::begin(a), std::end(a), std::begin(b), S{});
}
{ // range overload
int a[] = {1, 2, 3, 4};
int b[3];
std::ranges::remove_copy(a, std::begin(b), S{});
}
}
{ // check that a custom projection works
struct S {
constexpr operator int() const { return 3; }
};
{ // iterator overload
int a[] = {1, 2, 3, 4};
int b[3];
std::ranges::remove_copy(std::begin(a), std::end(a), std::begin(b), S{});
}
{ // range overload
int a[] = {1, 2, 3, 4};
int b[3];
std::ranges::remove_copy(a, std::begin(b), S{});
}
}
// Complexity: Exactly last - first applications of the corresponding predicate and any projection.
{
std::array in{4, 4, 5, 6};
std::array expected{5, 6};
// iterator overload
{
int numberOfProj = 0;
std::array<int, 2> out;
std::ranges::remove_copy(
in.begin(),
in.end(),
out.begin(),
4,
counting_projection(numberOfProj));
assert(numberOfProj == static_cast<int>(in.size()));
assert(std::ranges::equal(out, expected));
}
// range overload
{
int numberOfProj = 0;
std::array<int, 2> out;
std::ranges::remove_copy(
in, out.begin(), 4, counting_projection(numberOfProj));
assert(numberOfProj == static_cast<int>(in.size()));
assert(std::ranges::equal(out, expected));
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|