Initial commit

This commit is contained in:
Yervant7
2024-01-16 19:28:29 -03:00
commit 8c0524dc0f
9054 changed files with 1081727 additions and 0 deletions
@@ -0,0 +1,200 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I, sentinel_for<I> S, class T1, class T2, class Proj = identity>
// requires indirectly_writable<I, const T2&> &&
// indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
// constexpr I
// ranges::replace(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {});
// template<input_range R, class T1, class T2, class Proj = identity>
// requires indirectly_writable<iterator_t<R>, const T2&> &&
// indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T1*>
// constexpr borrowed_iterator_t<R>
// ranges::replace(R&& r, const T1& old_value, const T2& new_value, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "boolean_testable.h"
#include "test_iterators.h"
template <class Iter, class Sent = sentinel_wrapper<Iter>>
concept HasReplaceIt = requires(Iter iter, Sent sent) { std::ranges::replace(iter, sent, 0, 0); };
static_assert(HasReplaceIt<int*>);
static_assert(!HasReplaceIt<InputIteratorNotDerivedFrom>);
static_assert(!HasReplaceIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasReplaceIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasReplaceIt<int*, SentinelForNotSemiregular>);
static_assert(!HasReplaceIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasReplaceIt<int**>); // not indirectly_writable
static_assert(!HasReplaceIt<IndirectBinaryPredicateNotIndirectlyReadable>);
template <class Range>
concept HasReplaceR = requires(Range range) { std::ranges::replace(range, 0, 0); };
static_assert(HasReplaceR<UncheckedRange<int*>>);
static_assert(!HasReplaceR<InputRangeNotDerivedFrom>);
static_assert(!HasReplaceR<InputRangeNotIndirectlyReadable>);
static_assert(!HasReplaceR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasReplaceR<InputRangeNotSentinelSemiregular>);
static_assert(!HasReplaceR<InputRangeNotSentinelEqualityComparableWith>);
static_assert(!HasReplaceR<UncheckedRange<int**>>); // not indirectly_writable
static_assert(!HasReplaceR<InputRangeIndirectBinaryPredicateNotIndirectlyReadable>);
template <int N>
struct Data {
std::array<int, N> input;
int oldval;
int newval;
std::array<int, N> expected;
};
template <class Iter, class Sent, int N>
constexpr void test(Data<N> d) {
{
auto a = d.input;
std::same_as<Iter> decltype(auto) ret = std::ranges::replace(Iter(a.data()), Sent(Iter(a.data() + N)),
d.oldval,
d.newval);
assert(base(ret) == a.data() + N);
assert(a == d.expected);
}
{
auto a = d.input;
auto range = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data() + N)));
std::same_as<Iter> decltype(auto) ret = std::ranges::replace(range, d.oldval, d.newval);
assert(base(ret) == a.data() + N);
assert(a == d.expected);
}
}
template <class Iter, class Sent = Iter>
constexpr void test_iterators() {
// simple test
test<Iter, Sent, 4>({.input = {1, 2, 3, 4}, .oldval = 2, .newval = 23, .expected = {1, 23, 3, 4}});
// no match
test<Iter, Sent, 4>({.input = {1, 2, 3, 4}, .oldval = 5, .newval = 23, .expected = {1, 2, 3, 4}});
// all match
test<Iter, Sent, 4>({.input = {1, 1, 1, 1}, .oldval = 1, .newval = 23, .expected = {23, 23, 23, 23}});
// empty range
test<Iter, Sent, 0>({.input = {}, .oldval = 1, .newval = 23, .expected = {}});
// single element range
test<Iter, Sent, 1>({.input = {1}, .oldval = 1, .newval = 2, .expected = {2}});
}
constexpr bool test() {
test_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterators<forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<int*>();
{ // check that the projection is used
struct S {
constexpr S(int i_) : i(i_) {}
int i;
};
{
S a[] = {1, 2, 3, 4};
std::ranges::replace(a, a + 4, 3, S{0}, &S::i);
}
{
S a[] = {1, 2, 3, 4};
std::ranges::replace(a, 3, S{0}, &S::i);
}
}
{ // check that std::invoke is used
struct S {
constexpr S(int i_) : i(i_) {}
constexpr bool operator==(const S&) const = default;
constexpr const S& identity() const { return *this; }
int i;
};
{
S a[] = {1, 2, 3, 4};
auto ret = std::ranges::replace(std::begin(a), std::end(a), S{1}, S{2}, &S::identity);
assert(ret == a + 4);
}
{
S a[] = {1, 2, 3, 4};
auto ret = std::ranges::replace(a, S{1}, S{2}, &S::identity);
assert(ret == a + 4);
}
}
{ // check that the implicit conversion to bool works
{
StrictComparable<int> a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace(std::begin(a), std::end(a), 1, 2);
assert(ret == std::end(a));
}
{
StrictComparable<int> a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace(a, 1, 2);
assert(ret == std::end(a));
}
}
{ // check that T1 and T2 can be different types
{
StrictComparable<int> a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace(std::begin(a), std::end(a), '\0', 2ull);
assert(ret == std::end(a));
}
{
StrictComparable<int> a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace(a, '\0', 2ull);
assert(ret == std::end(a));
}
}
{ // check that std::ranges::dangling is returned
[[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
std::ranges::replace(std::array {1, 2, 3, 4}, 1, 1);
}
{ // check that the complexity requirements are met
{
auto projectionCount = 0;
auto proj = [&](int i) { ++projectionCount; return i; };
int a[] = {1, 2, 3, 4, 5};
auto ret = std::ranges::replace(std::begin(a), std::end(a), 1, 2, proj);
assert(ret == a + 5);
assert(projectionCount == 5);
}
{
auto projectionCount = 0;
auto proj = [&](int i) { ++projectionCount; return i; };
int a[] = {1, 2, 3, 4, 5};
auto ret = std::ranges::replace(a, 1, 2, proj);
assert(ret == a + 5);
assert(projectionCount == 5);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,188 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity,
// indirect_unary_predicate<projected<I, Proj>> Pred>
// requires indirectly_writable<I, const T&>
// constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {});
// template<input_range R, class T, class Proj = identity,
// indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
// requires indirectly_writable<iterator_t<R>, const T&>
// constexpr borrowed_iterator_t<R>
// ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "boolean_testable.h"
#include "test_iterators.h"
struct FalsePredicate {
bool operator()(auto&&) { return false; }
};
template <class Iter, class Sent = sentinel_wrapper<Iter>>
concept HasReplaceIt = requires(Iter iter, Sent sent) { std::ranges::replace_if(iter, sent, FalsePredicate{}, 0); };
static_assert(HasReplaceIt<int*>);
static_assert(!HasReplaceIt<InputIteratorNotDerivedFrom>);
static_assert(!HasReplaceIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasReplaceIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasReplaceIt<int*, SentinelForNotSemiregular>);
static_assert(!HasReplaceIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasReplaceIt<int**>); // not indirectly_writable
static_assert(!HasReplaceIt<IndirectBinaryPredicateNotIndirectlyReadable>);
template <class Range>
concept HasReplaceR = requires(Range range) { std::ranges::replace_if(range, FalsePredicate{}, 0); };
static_assert(HasReplaceR<UncheckedRange<int*>>);
static_assert(!HasReplaceR<InputRangeNotDerivedFrom>);
static_assert(!HasReplaceR<InputRangeNotIndirectlyReadable>);
static_assert(!HasReplaceR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasReplaceR<InputRangeNotSentinelSemiregular>);
static_assert(!HasReplaceR<InputRangeNotSentinelEqualityComparableWith>);
static_assert(!HasReplaceR<UncheckedRange<int**>>); // not indirectly_writable
static_assert(!HasReplaceR<InputRangeIndirectBinaryPredicateNotIndirectlyReadable>);
template <class Iter, class Sent, int N, class Pred>
constexpr void test(std::array<int, N> a_, Pred pred, int val, std::array<int, N> expected) {
{
auto a = a_;
std::same_as<Iter> auto ret = std::ranges::replace_if(Iter(a.data()), Sent(Iter(a.data() + N)),
pred,
val);
assert(base(ret) == a.data() + N);
assert(a == expected);
}
{
auto a = a_;
auto range = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data() + N)));
std::same_as<Iter> auto ret = std::ranges::replace_if(range, pred, val);
assert(base(ret) == a.data() + N);
assert(a == expected);
}
}
template <class Iter, class Sent = Iter>
constexpr void test_iterators() {
// simple test
test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 3; }, 23, {23, 23, 3, 4});
// no match
test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 0; }, 23, {1, 2, 3, 4});
// all match
test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i > 0; }, 23, {23, 23, 23, 23});
// empty range
test<Iter, Sent, 0>({}, [](int i) { return i > 0; }, 23, {});
// single element range
test<Iter, Sent, 1>({1}, [](int i) { return i > 0; }, 2, {2});
}
constexpr bool test() {
test_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterators<forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<int*>();
{ // check that the projection is used
struct S {
constexpr S(int i_) : i(i_) {}
int i;
};
{
S a[] = {1, 2, 3, 4};
std::ranges::replace_if(a, a + 4, [](int i) { return i == 3; }, S{0}, &S::i);
}
{
S a[] = {1, 2, 3, 4};
std::ranges::replace_if(a, [](int i) { return i == 3; }, S{0}, &S::i);
}
}
{ // check that std::invoke is used
struct S {
constexpr S(int i_) : i(i_) {}
constexpr bool check() const { return false; }
constexpr const S& identity() const { return *this; }
int i;
};
{
S a[] = {1, 2, 3, 4};
auto ret = std::ranges::replace_if(std::begin(a), std::end(a), &S::check, S{2}, &S::identity);
assert(ret == std::end(a));
}
{
S a[] = {1, 2, 3, 4};
auto ret = std::ranges::replace_if(a, &S::check, S{2}, &S::identity);
assert(ret == std::end(a));
}
}
{ // check that the implicit conversion to bool works
{
int a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace_if(std::begin(a), std::end(a), [](int) { return BooleanTestable{false}; }, 2);
assert(ret == std::end(a));
}
{
int a[] = {1, 2, 2, 4};
auto ret = std::ranges::replace_if(a, [](int) { return BooleanTestable{false}; }, 2);
assert(ret == std::end(a));
}
}
{ // check that std::ranges::dangling is returned
[[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
std::ranges::replace_if(std::array {1, 2, 3, 4}, [](int) { return false; }, 1);
}
{ // check that the complexity requirements are met
{
int predicateCount = 0;
auto pred = [&](int) { ++predicateCount; return false; };
auto projectionCount = 0;
auto proj = [&](int i) { ++projectionCount; return i; };
int a[] = {1, 2, 3, 4, 5};
auto ret = std::ranges::replace_if(a, a + 5, pred, 1, proj);
assert(ret == a + 5);
assert(predicateCount == 5);
assert(projectionCount == 5);
}
{
int predicateCount = 0;
auto pred = [&](int) { ++predicateCount; return false; };
auto projectionCount = 0;
auto proj = [&](int i) { ++projectionCount; return i; };
int a[] = {1, 2, 3, 4, 5};
auto ret = std::ranges::replace_if(a, pred, 1, proj);
assert(ret == a + 5);
assert(predicateCount == 5);
assert(projectionCount == 5);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,255 @@
//===----------------------------------------------------------------------===//
//
// 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
// template<input_iterator I, sentinel_for<I> S, class T1, class T2,
// output_iterator<const T2&> O, class Proj = identity>
// requires indirectly_copyable<I, O> &&
// indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
// constexpr replace_copy_result<I, O>
// replace_copy(I first, S last, O result, const T1& old_value, const T2& new_value,
// Proj proj = {}); // Since C++20
//
// template<input_range R, class T1, class T2, output_iterator<const T2&> O,
// class Proj = identity>
// requires indirectly_copyable<iterator_t<R>, O> &&
// indirect_binary_predicate<ranges::equal_to,
// projected<iterator_t<R>, Proj>, const T1*>
// constexpr replace_copy_result<borrowed_iterator_t<R>, O>
// replace_copy(R&& r, O result, const T1& old_value, const T2& new_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"
template <class Iter, class Sent = sentinel_wrapper<Iter>, class OutIter = int*>
concept HasReplaceCopyIter =
requires(Iter&& first, Sent&& last, OutIter&& result) {
std::ranges::replace_copy(
std::forward<Iter>(first), std::forward<Sent>(last), std::forward<OutIter>(result), 0, 0);
};
static_assert(HasReplaceCopyIter<int*>);
// !input_iterator<I>
static_assert(!HasReplaceCopyIter<InputIteratorNotDerivedFrom>);
static_assert(!HasReplaceCopyIter<InputIteratorNotIndirectlyReadable>);
static_assert(!HasReplaceCopyIter<InputIteratorNotInputOrOutputIterator>);
// !sentinel_for<S, I>
static_assert(!HasReplaceCopyIter<int*, SentinelForNotSemiregular>);
static_assert(!HasReplaceCopyIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
// !output_iterator<O, const T2&>
static_assert(!HasReplaceCopyIter<int*, int*, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasReplaceCopyIter<int*, int*, OutputIteratorNotInputOrOutputIterator>);
// !indirectly_copyable<I, O>
static_assert(!HasReplaceCopyIter<int*, int*, int**>);
// !indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
static_assert(!HasReplaceCopyIter<IndirectBinaryPredicateNotIndirectlyReadable>);
template <class Range, class OutIter = int*>
concept HasReplaceCopyRange = requires(Range&& range, OutIter&& result) {
std::ranges::replace_copy(std::forward<Range>(range), std::forward<OutIter>(result), 0, 0);
};
template <class T>
using R = UncheckedRange<T>;
static_assert(HasReplaceCopyRange<R<int*>>);
// !input_range<R>
static_assert(!HasReplaceCopyRange<InputRangeNotDerivedFrom>);
static_assert(!HasReplaceCopyRange<InputRangeNotIndirectlyReadable>);
static_assert(!HasReplaceCopyRange<InputRangeNotInputOrOutputIterator>);
static_assert(!HasReplaceCopyRange<InputRangeNotSentinelSemiregular>);
static_assert(!HasReplaceCopyRange<InputRangeNotSentinelEqualityComparableWith>);
// !output_iterator<O, const T2&>
static_assert(!HasReplaceCopyRange<R<int*>, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasReplaceCopyRange<R<int*>, OutputIteratorNotInputOrOutputIterator>);
// !indirectly_copyable<iterator_t<R>, O>
static_assert(!HasReplaceCopyRange<R<int*>, R<int**>>);
// !indirect_binary_predicate<ranges::equal_to, projected<iterator_t<T>, Proj>, const T1*>
static_assert(!HasReplaceCopyRange<R<IndirectBinaryPredicateNotIndirectlyReadable>>);
template <int N>
struct Data {
std::array<int, N> input;
int old_value;
int new_value;
std::array<int, N> expected;
};
template <class InIter, class Sent, class OutIter, int N>
constexpr void test(Data<N> d) {
{ // iterator overload
std::array<int, N> output;
auto first = InIter(d.input.data());
auto last = Sent(InIter(d.input.data() + d.input.size()));
auto result = OutIter(output.data());
std::same_as<std::ranges::replace_copy_result<InIter, OutIter>> decltype(auto) ret =
std::ranges::replace_copy(std::move(first), std::move(last), std::move(result), d.old_value, d.new_value);
assert(base(ret.in) == d.input.data() + d.input.size());
assert(base(ret.out) == output.data() + output.size());
assert(d.expected == output);
}
{ // range overload
std::array<int, N> output;
auto range = std::ranges::subrange(InIter(d.input.data()), Sent(InIter(d.input.data() + d.input.size())));
auto result = OutIter(output.data());
std::same_as<std::ranges::replace_copy_result<InIter, OutIter>> decltype(auto) ret =
std::ranges::replace_copy(range, result, d.old_value, d.new_value);
assert(base(ret.in) == d.input.data() + d.input.size());
assert(base(ret.out) == output.data() + output.size());
assert(d.expected == output);
}
}
template <class InIter, class Sent, class OutIter>
constexpr void tests() {
// simple test
test<InIter, Sent, OutIter, 4>({.input = {1, 2, 3, 4}, .old_value = 2, .new_value = 5, .expected = {1, 5, 3, 4}});
// empty range
test<InIter, Sent, OutIter, 0>({.input = {}, .old_value = 2, .new_value = 5, .expected = {}});
// all elements match
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .old_value = 1, .new_value = 2, .expected = {2, 2, 2, 2}});
// no element matches
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .old_value = 2, .new_value = 3, .expected = {1, 1, 1, 1}});
// old_value and new_value are identical - match
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .old_value = 1, .new_value = 1, .expected = {1, 1, 1, 1}});
// old_value and new_value are identical - no match
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .old_value = 2, .new_value = 2, .expected = {1, 1, 1, 1}});
// more elements
test<InIter, Sent, OutIter, 7>(
{.input = {1, 2, 3, 4, 5, 6, 7}, .old_value = 2, .new_value = 3, .expected = {1, 3, 3, 4, 5, 6, 7}});
// single element - match
test<InIter, Sent, OutIter, 1>({.input = {1}, .old_value = 1, .new_value = 5, .expected = {5}});
// single element - no match
test<InIter, Sent, OutIter, 1>({.input = {2}, .old_value = 1, .new_value = 5, .expected = {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 InIter>
constexpr void test_sentinels() {
test_output_iterators<InIter, InIter>();
test_output_iterators<InIter, sentinel_wrapper<InIter>>();
test_output_iterators<InIter, sized_sentinel<InIter>>();
}
constexpr bool test() {
test_output_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_output_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<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*>();
test_sentinels<const int*>();
{ // check that a custom projection works
struct S {
int i;
};
{ // iterator overload
S a[] = {{1}, {2}, {3}, {4}};
S b[4];
auto ret = std::ranges::replace_copy(std::begin(a), std::end(a), std::begin(b), 1, S{2}, &S::i);
assert(ret.in == std::end(a));
assert(ret.out == std::end(b));
}
{ // range overload
S a[] = {{1}, {2}, {3}, {4}};
S b[4];
auto ret = std::ranges::replace_copy(a, std::begin(b), 1, S{2}, &S::i);
assert(ret.in == std::end(a));
assert(ret.out == std::end(b));
}
}
{ // Complexity: exactly `last - first` applications of the corresponding predicate and any projection.
{ // iterator overload
int proj_count = 0;
int a[] = {1, 2, 3, 4};
int b[4];
std::ranges::replace_copy(
std::begin(a), std::end(a), std::begin(b), 0, 0, counting_projection(proj_count));
assert(proj_count == 4);
}
{ // range overload
int proj_count = 0;
int a[] = {1, 2, 3, 4};
int b[4];
std::ranges::replace_copy(a, std::begin(b), 0, 0, counting_projection(proj_count));
assert(proj_count == 4);
}
}
{ // using different types for the old and new values works
struct S {
constexpr operator int() const { return 0; }
constexpr bool operator==(const S&) const = default;
constexpr bool operator==(int i) const { return i == 0; }
};
struct T {
constexpr operator int() const { return 1; }
};
{
int a[] = {0, 1, 2, 3};
int b[4];
std::ranges::replace_copy(std::begin(a), std::end(a), std::begin(b), S{}, T{});
assert(std::ranges::equal(b, std::array{1, 1, 2, 3}));
}
{
int a[] = {0, 1, 2, 3};
int b[4];
std::ranges::replace_copy(a, std::begin(b), S{}, T{});
assert(std::ranges::equal(b, std::array{1, 1, 2, 3}));
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,265 @@
//===----------------------------------------------------------------------===//
//
// 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, class T, output_iterator<const T&> O,
// class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred>
// requires indirectly_copyable<I, O>
// constexpr replace_copy_if_result<I, O>
// replace_copy_if(I first, S last, O result, Pred pred, const T& new_value,
// Proj proj = {}); // Since C++20
//
// template<input_range R, class T, output_iterator<const T&> O, class Proj = identity,
// indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
// requires indirectly_copyable<iterator_t<R>, O>
// constexpr replace_copy_if_result<borrowed_iterator_t<R>, O>
// replace_copy_if(R&& r, O result, Pred pred, const T& new_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_predicates.h"
#include "counting_projection.h"
#include "test_iterators.h"
struct FalsePredicate {
constexpr bool operator()(int) { return false; }
};
template <class Iter, class Sent = sentinel_wrapper<Iter>, class OutIter = int*>
concept HasReplaceCopyIfIter = requires(Iter&& first, Sent&& last, OutIter&& result) {
std::ranges::replace_copy_if(
std::forward<Iter>(first), std::forward<Sent>(last), std::forward<OutIter>(result), FalsePredicate{}, 0);
};
static_assert(HasReplaceCopyIfIter<int*>);
// !input_iterator<I>
static_assert(!HasReplaceCopyIfIter<InputIteratorNotDerivedFrom>);
static_assert(!HasReplaceCopyIfIter<InputIteratorNotIndirectlyReadable>);
static_assert(!HasReplaceCopyIfIter<InputIteratorNotInputOrOutputIterator>);
// !sentinel_for<S, I>
static_assert(!HasReplaceCopyIfIter<int*, SentinelForNotSemiregular>);
static_assert(!HasReplaceCopyIfIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
// !output_iterator<O, const T2&>
static_assert(!HasReplaceCopyIfIter<int*, int*, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasReplaceCopyIfIter<int*, int*, OutputIteratorNotInputOrOutputIterator>);
// !indirect_unary_predicate<Pred, projected<I, Proj>> Pred>
static_assert(!HasReplaceCopyIfIter<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasReplaceCopyIfIter<IndirectUnaryPredicateNotPredicate>);
// !indirectly_copyable<I, O>
static_assert(!HasReplaceCopyIfIter<int*, int*, int**>);
template <class Range, class OutIter = int*>
concept HasReplaceCopyIfRange = requires(Range&& range, OutIter&& result) {
std::ranges::replace_copy_if(std::forward<Range>(range), std::forward<OutIter>(result), FalsePredicate{}, 0);
};
template <class T>
using R = UncheckedRange<T>;
static_assert(HasReplaceCopyIfRange<R<int*>>);
// !input_range<R>
static_assert(!HasReplaceCopyIfRange<InputRangeNotDerivedFrom>);
static_assert(!HasReplaceCopyIfRange<InputRangeNotIndirectlyReadable>);
static_assert(!HasReplaceCopyIfRange<InputRangeNotInputOrOutputIterator>);
static_assert(!HasReplaceCopyIfRange<InputRangeNotSentinelSemiregular>);
static_assert(!HasReplaceCopyIfRange<InputRangeNotSentinelEqualityComparableWith>);
// !output_iterator<O, const T2&>
static_assert(!HasReplaceCopyIfRange<R<int*>, OutputIteratorNotIndirectlyWritable>);
static_assert(!HasReplaceCopyIfRange<R<int*>, OutputIteratorNotInputOrOutputIterator>);
// !indirect_unary_predicate<Pred, projected<iterator_t<R>, Proj>> Pred>
static_assert(!HasReplaceCopyIfRange<R<IndirectUnaryPredicateNotPredicate>>);
// !indirectly_copyable<iterator_t<R>, O>
static_assert(!HasReplaceCopyIfRange<R<int*>, int**>);
template <int N>
struct Data {
std::array<int, N> input;
int cutoff;
int new_value;
std::array<int, N> expected;
};
template <class InIter, class Sent, class OutIter, int N>
constexpr void test(Data<N> d) {
{ // iterator overload
std::array<int, N> output;
auto first = InIter(d.input.data());
auto last = Sent(InIter(d.input.data() + d.input.size()));
auto result = OutIter(output.data());
auto pred = [&](int i) { return i < d.cutoff; };
std::same_as<std::ranges::replace_copy_if_result<InIter, OutIter>> decltype(auto) ret =
std::ranges::replace_copy_if(std::move(first), std::move(last), std::move(result), pred, d.new_value);
assert(base(ret.in) == d.input.data() + d.input.size());
assert(base(ret.out) == output.data() + output.size());
assert(d.expected == output);
}
{ // range overload
std::array<int, N> output;
auto range = std::ranges::subrange(InIter(d.input.data()), Sent(InIter(d.input.data() + d.input.size())));
auto result = OutIter(output.data());
auto pred = [&](int i) { return i < d.cutoff; };
std::same_as<std::ranges::replace_copy_if_result<InIter, OutIter>> decltype(auto) ret =
std::ranges::replace_copy_if(range, result, pred, d.new_value);
assert(base(ret.in) == d.input.data() + d.input.size());
assert(base(ret.out) == output.data() + output.size());
assert(d.expected == output);
}
}
template <class InIter, class Sent, class OutIter>
constexpr void tests() {
// simple test
test<InIter, Sent, OutIter, 4>({.input = {1, 2, 3, 4}, .cutoff = 2, .new_value = 5, .expected = {5, 2, 3, 4}});
// empty range
test<InIter, Sent, OutIter, 0>({.input = {}, .cutoff = 2, .new_value = 5, .expected = {}});
// all elements match
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .cutoff = 2, .new_value = 2, .expected = {2, 2, 2, 2}});
// no element matches
test<InIter, Sent, OutIter, 4>({.input = {1, 1, 1, 1}, .cutoff = 1, .new_value = 3, .expected = {1, 1, 1, 1}});
// more elements
test<InIter, Sent, OutIter, 7>(
{.input = {1, 2, 3, 4, 5, 6, 7}, .cutoff = 3, .new_value = 3, .expected = {3, 3, 3, 4, 5, 6, 7}});
// single element - match
test<InIter, Sent, OutIter, 1>({.input = {1}, .cutoff = 2, .new_value = 5, .expected = {5}});
// single element - no match
test<InIter, Sent, OutIter, 1>({.input = {2}, .cutoff = 2, .new_value = 5, .expected = {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 InIter>
constexpr void test_sentinels() {
test_output_iterators<InIter, InIter>();
test_output_iterators<InIter, sentinel_wrapper<InIter>>();
test_output_iterators<InIter, sized_sentinel<InIter>>();
}
constexpr bool test() {
test_output_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_output_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<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*>();
test_sentinels<const int*>();
{ // check that a custom projection works
struct S {
int i;
};
{ // iterator overload
S a[] = {{1}, {2}, {3}, {4}};
S b[4];
auto ret = std::ranges::replace_copy_if(std::begin(a), std::end(a), std::begin(b), FalsePredicate{}, S{2}, &S::i);
assert(ret.in == std::end(a));
assert(ret.out == std::end(b));
assert(std::ranges::equal(a, b, {}, &S::i, &S::i));
}
{ // range overload
S a[] = {{1}, {2}, {3}, {4}};
S b[4];
auto ret = std::ranges::replace_copy_if(a, std::begin(b), FalsePredicate{}, S{2}, &S::i);
assert(ret.in == std::end(a));
assert(ret.out == std::end(b));
assert(std::ranges::equal(a, b, {}, &S::i, &S::i));
}
}
{ // Complexity: exactly `last - first` applications of the corresponding predicate and any projection.
{ // iterator overload
int pred_count = 0;
int proj_count = 0;
int a[] = {1, 2, 3, 4};
int b[4];
std::ranges::replace_copy_if(
std::begin(a), std::end(a), std::begin(b),
counting_predicate(FalsePredicate{}, pred_count), 0, counting_projection(proj_count));
assert(pred_count == 4);
assert(proj_count == 4);
}
{ // range overload
int pred_count = 0;
int proj_count = 0;
int a[] = {1, 2, 3, 4};
int b[4];
std::ranges::replace_copy_if(a, std::begin(b),
counting_predicate(FalsePredicate{}, pred_count), 0, counting_projection(proj_count));
assert(pred_count == 4);
assert(proj_count == 4);
}
}
{ // using different types for the old and new values works
struct S {
constexpr operator int() const { return 1; }
};
{
int a[] = {0, 0, 2, 3};
int b[4];
std::ranges::replace_copy_if(std::begin(a), std::end(a), std::begin(b), [](int i) { return i < 2; }, S{});
assert(std::ranges::equal(b, std::array{1, 1, 2, 3}));
}
{
int a[] = {0, 0, 2, 3};
int b[4];
std::ranges::replace_copy_if(a, std::begin(b), [](int i) { return i < 2; }, S{});
assert(std::ranges::equal(b, std::array{1, 1, 2, 3}));
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<ForwardIterator Iter, class T>
// requires OutputIterator<Iter, Iter::reference>
// && OutputIterator<Iter, const T&>
// && HasEqualTo<Iter::value_type, T>
// constexpr void // constexpr after C++17
// replace(Iter first, Iter last, const T& old_value, const T& new_value);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3, 4};
const int expected[] = {0, 1, 5, 3, 4};
std::replace(std::begin(ia), std::end(ia), 2, 5);
return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
;
}
#endif
template <class Iter>
void
test()
{
int ia[] = {0, 1, 2, 3, 4};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::replace(Iter(ia), Iter(ia+sa), 2, 5);
assert(ia[0] == 0);
assert(ia[1] == 1);
assert(ia[2] == 5);
assert(ia[3] == 3);
assert(ia[4] == 4);
}
int main(int, char**)
{
test<forward_iterator<int*> >();
test<bidirectional_iterator<int*> >();
test<random_access_iterator<int*> >();
test<int*>();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
return 0;
}
@@ -0,0 +1,94 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<InputIterator InIter, typename OutIter, class T>
// requires OutputIterator<OutIter, InIter::reference>
// && OutputIterator<OutIter, const T&>
// && HasEqualTo<InIter::value_type, T>
// constexpr OutIter // constexpr after C++17
// replace_copy(InIter first, InIter last, OutIter result, const T& old_value,
// const T& new_value);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3, 4};
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
const int expected[] = {0, 1, 5, 3, 4};
auto it = std::replace_copy(std::begin(ia), std::end(ia), std::begin(ib), 2, 5);
return it == (std::begin(ib) + std::size(ia))
&& *it == 0 // don't overwrite the last value in the output array
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
;
}
#endif
template <class InIter, class OutIter>
void
test()
{
int ia[] = {0, 1, 2, 3, 4};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
int ib[sa] = {0};
OutIter r = std::replace_copy(InIter(ia), InIter(ia+sa), OutIter(ib), 2, 5);
assert(base(r) == ib + sa);
assert(ib[0] == 0);
assert(ib[1] == 1);
assert(ib[2] == 5);
assert(ib[3] == 3);
assert(ib[4] == 4);
}
int main(int, char**)
{
test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
test<cpp17_input_iterator<const int*>, int*>();
test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
test<forward_iterator<const int*>, forward_iterator<int*> >();
test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
test<forward_iterator<const int*>, random_access_iterator<int*> >();
test<forward_iterator<const int*>, int*>();
test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
test<bidirectional_iterator<const int*>, int*>();
test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
test<random_access_iterator<const int*>, forward_iterator<int*> >();
test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
test<random_access_iterator<const int*>, random_access_iterator<int*> >();
test<random_access_iterator<const int*>, int*>();
test<const int*, cpp17_output_iterator<int*> >();
test<const int*, forward_iterator<int*> >();
test<const int*, bidirectional_iterator<int*> >();
test<const int*, random_access_iterator<int*> >();
test<const int*, int*>();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
return 0;
}
@@ -0,0 +1,98 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<InputIterator InIter, typename OutIter,
// Predicate<auto, InIter::value_type> Pred, class T>
// requires OutputIterator<OutIter, InIter::reference>
// && OutputIterator<OutIter, const T&>
// && CopyConstructible<Pred>
// constexpr OutIter // constexpr after C++17
// replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);
#include <algorithm>
#include <functional>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3, 4};
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
const int expected[] = {0, 1, 5, 3, 4};
auto it = std::replace_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo, 5);
return it == (std::begin(ib) + std::size(ia))
&& *it == 0 // don't overwrite the last value in the output array
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
;
}
#endif
template <class InIter, class OutIter>
void
test()
{
int ia[] = {0, 1, 2, 3, 4};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
int ib[sa] = {0};
OutIter r = std::replace_copy_if(InIter(ia), InIter(ia+sa),
OutIter(ib), equalToTwo, 5);
assert(base(r) == ib + sa);
assert(ib[0] == 0);
assert(ib[1] == 1);
assert(ib[2] == 5);
assert(ib[3] == 3);
assert(ib[4] == 4);
}
int main(int, char**)
{
test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
test<cpp17_input_iterator<const int*>, int*>();
test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
test<forward_iterator<const int*>, forward_iterator<int*> >();
test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
test<forward_iterator<const int*>, random_access_iterator<int*> >();
test<forward_iterator<const int*>, int*>();
test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
test<bidirectional_iterator<const int*>, int*>();
test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
test<random_access_iterator<const int*>, forward_iterator<int*> >();
test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
test<random_access_iterator<const int*>, random_access_iterator<int*> >();
test<random_access_iterator<const int*>, int*>();
test<const int*, cpp17_output_iterator<int*> >();
test<const int*, forward_iterator<int*> >();
test<const int*, bidirectional_iterator<int*> >();
test<const int*, random_access_iterator<int*> >();
test<const int*, int*>();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
return 0;
}
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred, class T>
// requires OutputIterator<Iter, Iter::reference>
// && OutputIterator<Iter, const T&>
// && CopyConstructible<Pred>
// constexpr void // constexpr after C++17
// replace_if(Iter first, Iter last, Pred pred, const T& new_value);
#include <algorithm>
#include <functional>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3, 4};
const int expected[] = {0, 1, 5, 3, 4};
std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);
return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
;
}
#endif
template <class Iter>
void
test()
{
int ia[] = {0, 1, 2, 3, 4};
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);
assert(ia[0] == 0);
assert(ia[1] == 1);
assert(ia[2] == 5);
assert(ia[3] == 3);
assert(ia[4] == 4);
}
int main(int, char**)
{
test<forward_iterator<int*> >();
test<bidirectional_iterator<int*> >();
test<random_access_iterator<int*> >();
test<int*>();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
return 0;
}