//===----------------------------------------------------------------------===// // // 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 // template S, class T, class Proj = identity> // requires indirect_binary_predicate, const T*> // constexpr subrange ranges::remove(I first, S last, const T& value, Proj proj = {}); // template // requires permutable> && // indirect_binary_predicate, Proj>, const T*> // constexpr borrowed_subrange_t // ranges::remove(R&& r, const T& value, Proj proj = {}); #include #include #include #include #include "almost_satisfies_types.h" #include "test_iterators.h" template > concept HasRemoveIt = requires(Iter first, Sent last) { std::ranges::remove(first, last, 0); }; static_assert(HasRemoveIt); static_assert(!HasRemoveIt); static_assert(!HasRemoveIt); static_assert(!HasRemoveIt); static_assert(!HasRemoveIt); static_assert(!HasRemoveIt); // not indirect_binary_predicate template concept HasRemoveR = requires(Range range) { std::ranges::remove(range, 0); }; static_assert(HasRemoveR>); static_assert(!HasRemoveR); static_assert(!HasRemoveR); static_assert(!HasRemoveR); static_assert(!HasRemoveR); static_assert(!HasRemoveR>); // not indirect_binary_predicate template struct Data { std::array input; std::array expected; int val; }; template constexpr void test(Data d) { { // iterator overload auto input = d.input; std::same_as> decltype(auto) ret = std::ranges::remove(Iter(input.data()), Sent(Iter(input.data() + input.size())), d.val); assert(base(ret.begin()) == input.data() + M); assert(base(ret.end()) == input.data() + N); assert(std::ranges::equal(input.data(), base(ret.begin()), d.expected.begin(), d.expected.end())); } { // range overload auto input = d.input; auto range = std::ranges::subrange(Iter(input.data()), Sent(Iter(input.data() + input.size()))); std::same_as> decltype(auto) ret = std::ranges::remove(range, d.val); assert(base(ret.begin()) == input.data() + M); assert(base(ret.end()) == input.data() + N); assert(std::ranges::equal(input.data(), base(ret.begin()), d.expected.begin(), d.expected.end())); } } template constexpr void tests() { // simple test test({.input = {1, 2, 3, 4, 5, 6}, .expected = {1, 2, 3, 4, 6}, .val = 5}); // empty range test({}); // single element range - match test({.input = {1}, .expected = {}, .val = 1}); // single element range - no match test({.input = {1}, .expected = {1}, .val = 2}); // two element range - same order test({.input = {1, 2}, .expected = {1}, .val = 2}); // two element range - reversed order test({.input = {1, 2}, .expected = {2}, .val = 1}); // all elements match test({.input = {1, 1, 1, 1, 1}, .expected = {}, .val = 1}); // the relative order of elements isn't changed test({.input = {1, 2, 3, 2, 3, 4, 2, 5}, .expected = {1, 3, 3, 4, 5}, .val = 2}); } template constexpr void test_sentinels() { tests(); tests>(); tests>(); } constexpr void test_iterators() { test_sentinels>(); test_sentinels>(); test_sentinels>(); test_sentinels>(); test_sentinels(); } constexpr bool test() { test_iterators(); { // check that ranges::dangling is returned [[maybe_unused]] std::same_as decltype(auto) ret = std::ranges::remove(std::array{1, 2, 3, 4}, 1); } { // check complexity requirements struct CompCounter { int* comp_count; constexpr bool operator==(const CompCounter&) const { ++*comp_count; return false; } }; { int proj_count = 0; auto proj = [&](CompCounter i) { ++proj_count; return i; }; int comp_count = 0; CompCounter a[] = {{&comp_count}, {&comp_count}, {&comp_count}, {&comp_count}}; auto ret = std::ranges::remove(std::begin(a), std::end(a), CompCounter{&comp_count}, proj); assert(ret.begin() == std::end(a) && ret.end() == std::end(a)); assert(comp_count == 4); assert(proj_count == 4); } { int proj_count = 0; auto proj = [&](CompCounter i) { ++proj_count; return i; }; int comp_count = 0; CompCounter a[] = {{&comp_count}, {&comp_count}, {&comp_count}, {&comp_count}}; auto ret = std::ranges::remove(a, CompCounter{&comp_count}, proj); assert(ret.begin() == std::end(a) && ret.end() == std::end(a)); assert(comp_count == 4); assert(proj_count == 4); } } { // check that std::invoke is used struct S { constexpr S& identity() { return *this; } bool operator==(const S&) const = default; }; { S a[4] = {}; auto ret = std::ranges::remove(std::begin(a), std::end(a), S{}, &S::identity); assert(ret.begin() == std::begin(a)); assert(ret.end() == std::end(a)); } { S a[4] = {}; auto ret = std::ranges::remove(a, S{}, &S::identity); assert(ret.begin() == std::begin(a)); assert(ret.end() == std::end(a)); } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }