diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -102,6 +102,8 @@ __algorithm/ranges_move.h __algorithm/ranges_move_backward.h __algorithm/ranges_none_of.h + __algorithm/ranges_remove.h + __algorithm/ranges_remove_if.h __algorithm/ranges_replace.h __algorithm/ranges_replace_if.h __algorithm/ranges_reverse.h diff --git a/libcxx/include/__algorithm/ranges_remove.h b/libcxx/include/__algorithm/ranges_remove.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_remove.h @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_RANGES_REMOVE_H +#define _LIBCPP___ALGORITHM_RANGES_REMOVE_H + +#include <__algorithm/ranges_remove_if.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/ranges_operations.h> +#include <__iterator/permutable.h> +#include <__iterator/projected.h> +#include <__ranges/subrange.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __remove { +struct __fn { + + template _Sent, class _Type, class _Proj = identity> + requires indirect_binary_predicate, const _Type*> + _LIBCPP_HIDE_FROM_ABI constexpr + subrange<_Iter> operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) const { + auto __pred = [&](auto&& __other) { return __value == __other; }; + return ranges::__remove_if_impl(__first, __last, __pred, __proj); + } + + template + requires permutable> + && indirect_binary_predicate, _Proj>, const _Type*> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_subrange_t<_Range> operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) const { + auto __pred = [&](auto&& __other) { return __value == __other; }; + return ranges::__remove_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __remove + +inline namespace __cpo { + inline constexpr auto remove = __remove::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_REMOVE_H diff --git a/libcxx/include/__algorithm/ranges_remove_if.h b/libcxx/include/__algorithm/ranges_remove_if.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_remove_if.h @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H +#define _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H + +#include <__algorithm/ranges_find_if.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/iter_move.h> +#include <__iterator/permutable.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/subrange.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { + +template +_LIBCPP_HIDE_FROM_ABI constexpr +subrange<_Iter> __remove_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + auto __end = ranges::__find_if_impl(__first, __last, __pred, __proj); + if (__end == __last) + return {__end, __end}; + + _Iter __i = __end; + while (++__i != __last) { + if (!std::invoke(__pred, std::invoke(__proj, *__i))) { + *__end = ranges::iter_move(__i); + ++__end; + } + } + return {__end, __i}; +} + +namespace __remove_if { +struct __fn { + + template _Sent, + class _Proj = identity, + indirect_unary_predicate> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + subrange<_Iter> operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const { + return ranges::__remove_if_impl(std::move(__first), std::move(__last), __pred, __proj); + } + + template , _Proj>> _Pred> + requires permutable> + _LIBCPP_HIDE_FROM_ABI constexpr + borrowed_subrange_t<_Range> operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { + return ranges::__remove_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } + +}; +} // namespace __remove_if + +inline namespace __cpo { + inline constexpr auto remove_if = __remove_if::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_REMOVE_IF_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -1219,6 +1219,8 @@ #include <__algorithm/ranges_move.h> #include <__algorithm/ranges_move_backward.h> #include <__algorithm/ranges_none_of.h> +#include <__algorithm/ranges_remove.h> +#include <__algorithm/ranges_remove_if.h> #include <__algorithm/ranges_replace.h> #include <__algorithm/ranges_replace_if.h> #include <__algorithm/ranges_reverse.h> diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -189,8 +189,8 @@ //(void)std::ranges::push_heap(a, Less(&copies)); assert(copies == 0); //(void)std::ranges::remove_copy_if(first, last, first2, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::remove_copy_if(a, first2, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::remove_if(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::remove_if(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::remove_if(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::remove_if(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::replace_copy_if(first, last, first2, UnaryTrue(&copies), value); assert(copies == 0); //(void)std::ranges::replace_copy_if(a, first2, UnaryTrue(&copies), value); assert(copies == 0); (void)std::ranges::replace_if(first, last, UnaryTrue(&copies), value); assert(copies == 0); diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp @@ -174,10 +174,10 @@ //(void)std::ranges::remove_copy(a, first2, value, Proj(&copies)); assert(copies == 0); //(void)std::ranges::remove_copy_if(first, last, first2, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::remove_copy_if(a, first2, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::remove(first, last, value, Proj(&copies)); assert(copies == 0); - //(void)std::ranges::remove(a, value, Proj(&copies)); assert(copies == 0); - //(void)std::ranges::remove_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::remove_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::remove(first, last, value, Proj(&copies)); assert(copies == 0); + (void)std::ranges::remove(a, value, Proj(&copies)); assert(copies == 0); + (void)std::ranges::remove_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::remove_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::replace_copy(first, last, first2, value, T(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::replace_copy(a, first2, value, T(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::replace_copy_if(first, last, first2, UnaryTrue(), T(), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove.pass.cpp @@ -0,0 +1,206 @@ +//===----------------------------------------------------------------------===// +// +// 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 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 "boolean_testable.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_prediacte + +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_prediacte + +template +struct Data { + std::array input; + std::array expected; + int val; +}; + +template +constexpr void test(Data d) { + { + 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.begin(), base(ret.begin()), d.expected.begin(), d.expected.end())); + } + { + 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(base(input.begin()), 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)); + } + } + + { + // check that the implicit conversion to bool works + { + StrictComparable a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove(a, a + 4, StrictComparable{2}); + assert(ret.begin() == a + 3); + } + { + StrictComparable a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove(a, StrictComparable{2}); + assert(ret.begin() == a + 3); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove_if.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove_if.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/ranges.remove_if.pass.cpp @@ -0,0 +1,207 @@ +//===----------------------------------------------------------------------===// +// +// 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 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 "boolean_testable.h" +#include "test_iterators.h" + +struct FalsePredicate { + bool operator()(int) { return false; } +}; + +template > +concept HasRemoveIfIt = requires(Iter first, Sent last) { std::ranges::remove_if(first, last, FalsePredicate{}); }; + +static_assert(HasRemoveIfIt); +static_assert(!HasRemoveIfIt); +static_assert(!HasRemoveIfIt); +static_assert(!HasRemoveIfIt); +static_assert(!HasRemoveIfIt); +static_assert(!HasRemoveIfIt); // not indirect_unary_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_unary_predicate + +template +struct Data { + std::array input; + std::array expected; + int val; +}; + +template +constexpr void test(Data d) { + { + auto input = d.input; + + std::same_as> decltype(auto) ret = std::ranges::remove_if( + Iter(input.data()), Sent(Iter(input.data() + input.size())), [&](int i) { return i < d.val; }); + + assert(base(ret.begin()) == input.data() + M); + assert(base(ret.end()) == input.data() + N); + assert(std::ranges::equal(input.begin(), base(ret.begin()), d.expected.begin(), d.expected.end())); + } + { + 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_if(range, [&](int i) { + return i < d.val; + }); + + assert(base(ret.begin()) == input.data() + M); + assert(base(ret.end()) == input.data() + N); + assert(std::ranges::equal(base(input.begin()), base(ret.begin()), d.expected.begin(), d.expected.end())); + } +} + +template +constexpr void tests() { + // simple test + test({.input = {1, 2, 3, 4, 5, 6}, .expected = {5, 6}, .val = 5}); + // empty range + test({}); + // single element range - no match + test({.input = {1}, .expected = {1}, .val = 1}); + // single element range - match + test({.input = {1}, .expected = {}, .val = 2}); + // two element range + test({.input = {1, 2}, .expected = {2}, .val = 2}); + // all elements matche + test({.input = {1, 1, 1, 1, 1}, .expected = {}, .val = 2}); + // the relative order of elements isn't changed + test({.input = {1, 2, 3, 2, 3, 4, 2, 5}, .expected = {2, 3, 2, 3, 4, 2, 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_if(std::array{1, 2, 3, 4}, [](int i) { return i < 0; }); + } + + {// check complexity requirements + {int comp_count = 0; + auto comp = [&](int i) { + ++comp_count; + return i == 0; + }; + int proj_count = 0; + auto proj = [&](int i) { + ++proj_count; + return i; + }; + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove_if(std::begin(a), std::end(a), comp, proj); + assert(ret.begin() == std::end(a) && ret.end() == std::end(a)); + assert(comp_count == 4); + assert(proj_count == 4); +} +{ + int comp_count = 0; + auto comp = [&](int i) { + ++comp_count; + return i == 0; + }; + int proj_count = 0; + auto proj = [&](int i) { + ++proj_count; + return i; + }; + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove_if(a, comp, 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; } + constexpr bool predicate() const { return true; } + }; + { + S a[4] = {}; + auto ret = std::ranges::remove_if(std::begin(a), std::end(a), &S::predicate, &S::identity); + assert(ret.begin() == std::begin(a)); + assert(ret.end() == std::end(a)); + } + { + S a[4] = {}; + auto ret = std::ranges::remove_if(a, &S::predicate, &S::identity); + assert(ret.begin() == std::begin(a)); + assert(ret.end() == std::end(a)); + } +} + +{ + // check that the implicit conversion to bool works + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove_if(a, a + 4, [](const int& i) { return BooleanTestable{i == 3}; }); + assert(ret.begin() == a + 3); + } + { + int a[] = {1, 2, 3, 4}; + auto ret = std::ranges::remove_if(a, [](const int& b) { return BooleanTestable{b == 3}; }); + assert(ret.begin() == a + 3); + } +} + +return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -117,10 +117,10 @@ //static_assert(test(std::ranges::pop_heap, a)); //static_assert(test(std::ranges::prev_permutation, a)); //static_assert(test(std::ranges::push_heap, a)); -//static_assert(test(std::ranges::remove, a, 42)); +static_assert(test(std::ranges::remove, a, 42)); //static_assert(test(std::ranges::remove_copy, a, a, 42)); //static_assert(test(std::ranges::remove_copy_if, a, a, odd)); -//static_assert(test(std::ranges::remove_if, a, odd)); +static_assert(test(std::ranges::remove_if, a, odd)); static_assert(test(std::ranges::replace, a, 42, 43)); //static_assert(test(std::ranges::replace_copy, a, a, 42, 43)); //static_assert(test(std::ranges::replace_copy_if, a, a, odd, 43));