diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -44,7 +44,7 @@ Write,move_backward,Not assigned,n/a,Not started Write,fill,Not assigned,n/a,Not started Write,fill_n,Not assigned,n/a,Not started -Write,transform,Not assigned,n/a,Not started +Write,transform,Nikolas Klauser,n/a,✅ Write,generate,Not assigned,n/a,Not started Write,generate_n,Not assigned,n/a,Not started Write,remove_copy,Not assigned,n/a,Not started diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -74,6 +74,7 @@ __algorithm/ranges_min_element.h __algorithm/ranges_mismatch.h __algorithm/ranges_swap_ranges.h + __algorithm/ranges_transform.h __algorithm/remove.h __algorithm/remove_copy.h __algorithm/remove_copy_if.h diff --git a/libcxx/include/__algorithm/ranges_transform.h b/libcxx/include/__algorithm/ranges_transform.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_transform.h @@ -0,0 +1,172 @@ +//===----------------------------------------------------------------------===// +// +// 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_TRANSFORM_H +#define _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H + +#include <__algorithm/in_in_out_result.h> +#include <__algorithm/in_out_result.h> +#include <__concepts/constructible.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.h> +#include <__utility/move.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 +using unary_transform_result = in_out_result<_Ip, _Op>; + +template +using binary_transform_result = in_in_out_result<_I1, _I2, _O1>; + +namespace __transform { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI static constexpr + unary_transform_result<_InputIterator, _OutputIterator> __unary(_InputIterator __first, _Sentinel __last, + _OutputIterator __result, + _Functor& __operation, + _Projection& __projection) { + while (__first != __last) { + *__result = std::invoke(__operation, std::invoke(__projection, *__first)); + ++__first; + ++__result; + } + + return {std::move(__first), std::move(__result)}; + } + + template _Sentinel, + weakly_incrementable _OutputIterator, + copy_constructible _Functor, + class _Projection = identity> + requires indirectly_writable<_OutputIterator, indirect_result_t<_Functor&, projected<_InputIterator, _Projection>>> + _LIBCPP_HIDE_FROM_ABI constexpr + unary_transform_result<_InputIterator, _OutputIterator> operator()(_InputIterator __first, _Sentinel __last, + _OutputIterator __result, + _Functor __operation, + _Projection __projection = {}) const { + return __unary(std::move(__first), std::move(__last), std::move(__result), __operation, __projection); + } + + template + requires indirectly_writable<_OutputIterator, + indirect_result_t<_Functor, projected, _Projection>>> + _LIBCPP_HIDE_FROM_ABI constexpr + unary_transform_result, _OutputIterator> operator()(_Range&& __range, + _OutputIterator __result, + _Functor __operation, + _Projection __projection = {}) const { + return __unary(ranges::begin(__range), ranges::end(__range), std::move(__result), __operation, __projection); + } + + template + _LIBCPP_HIDE_FROM_ABI static constexpr binary_transform_result<_InputIterator1, _InputIterator2, _OutputIterator> + __binary(_InputIterator1 __first1, _Sentinel1 __last1, + _InputIterator2 __first2, _Sentinel2 __last2, + _OutputIterator __result, + _Functor& __binary_operation, + _Projection1& __projection1, + _Projection2& __projection2) { + while (__first1 != __last1 && __first2 != __last2) { + *__result = std::invoke(__binary_operation, std::invoke(__projection1, *__first1), + std::invoke(__projection2, *__first2)); + ++__first1; + ++__first2; + ++__result; + } + return {std::move(__first1), std::move(__first2), std::move(__result)}; + } + + template _Sentinel1, + input_iterator _InputIterator2, sentinel_for<_InputIterator2> _Sentinel2, + weakly_incrementable _OutputIterator, copy_constructible _Functor, + class _Projection1 = identity, class _Projection2 = identity> + requires indirectly_writable<_OutputIterator, + indirect_result_t<_Functor&, projected<_InputIterator1, _Projection1>, + projected<_InputIterator2, _Projection2>>> + _LIBCPP_HIDE_FROM_ABI constexpr + binary_transform_result<_InputIterator1, _InputIterator2, _OutputIterator> + operator()(_InputIterator1 __first1, _Sentinel1 __last1, + _InputIterator2 __first2, _Sentinel2 __last2, + _OutputIterator __result, + _Functor __binary_operation, + _Projection1 __projection1 = {}, + _Projection2 __projection2 = {}) const { + return __binary(std::move(__first1), std::move(__last1), + std::move(__first2), std::move(__last2), + std::move(__result), + __binary_operation, + __projection1, + __projection2); + } + + template + requires indirectly_writable<_OutputIterator, + indirect_result_t<_Functor&, projected, _Projection1>, + projected, _Projection2>>> + _LIBCPP_HIDE_FROM_ABI constexpr + binary_transform_result, borrowed_iterator_t<_Range2>, _OutputIterator> + operator()(_Range1&& __range1, + _Range2&& __range2, + _OutputIterator __result, + _Functor __binary_operation, + _Projection1 __projection1 = {}, + _Projection2 __projection2 = {}) const { + return __binary(ranges::begin(__range1), ranges::end(__range1), + ranges::begin(__range2), ranges::end(__range2), + std::move(__result), + __binary_operation, + __projection1, + __projection2); + } + +}; +} // namespace __transform + +inline namespace __cpo { + inline constexpr auto transform = __transform::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_TRANSFORM_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -820,6 +820,7 @@ #include <__algorithm/ranges_min_element.h> #include <__algorithm/ranges_mismatch.h> #include <__algorithm/ranges_swap_ranges.h> +#include <__algorithm/ranges_transform.h> #include <__algorithm/remove.h> #include <__algorithm/remove_copy.h> #include <__algorithm/remove_copy_if.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -302,6 +302,7 @@ module ranges_min_element { private header "__algorithm/ranges_min_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } + module ranges_transform { private header "__algorithm/ranges_transform.h" } module remove { private header "__algorithm/remove.h" } module remove_copy { private header "__algorithm/remove_copy.h" } module remove_copy_if { private header "__algorithm/remove_copy_if.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_transform.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_transform.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_transform.module.verify.cpp @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}} +#include <__algorithm/ranges_transform.h> diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp @@ -0,0 +1,390 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// + +#include +#include +#include +#include + +#include "test_iterators.h" +#include "almost_satisfies_types.h" + +template +concept HasTranformR = requires(Range r, int* out) { + std::ranges::transform(r, out, [](int) { return 0; }); + std::ranges::transform(r, r, out, [](int, int) { return 0; }); +}; +static_assert(HasTranformR>); +static_assert(!HasTranformR); +static_assert(!HasTranformR); +static_assert(!HasTranformR); +static_assert(!HasTranformR); +static_assert(!HasTranformR); +static_assert(!HasTranformR); + +template +concept HasTransformIt = requires(It it, Sent sent, int* out) { + std::ranges::transform(it, sent, out, [](int) { return 0; }); + std::ranges::transform(it, sent, it, sent, out, [](int, int) { return 0; }); +}; +static_assert(HasTransformIt); +static_assert(!HasTransformIt); +static_assert(!HasTransformIt); +static_assert(!HasTransformIt); +static_assert(!HasTransformIt, SentinelForNotSemiregular>); +static_assert(!HasTransformIt, InputRangeNotSentinelEqualityComparableWith>); + +template +constexpr void test_iterators() { + { // simple + { // unary + { + int a[] = {1, 2, 3, 4, 5}; + int b[5]; + std::same_as> decltype(auto) ret = + std::ranges::transform(In1(a), Sent1(In1(a + 5)), Out(b), [](int i) { return i * 2; }); + assert((std::to_array(b) == std::array{2, 4, 6, 8, 10})); + assert(base(ret.in) == a + 5); + assert(base(ret.out) == b + 5); + } + { + int a[] = {1, 2, 3, 4, 5}; + int b[5]; + auto range = std::ranges::subrange(In1(a), Sent1(In1(a + 5))); + std::same_as> decltype(auto) ret = + std::ranges::transform(range, Out(b), [](int i) { return i * 2; }); + assert((std::to_array(b) == std::array{2, 4, 6, 8, 10})); + assert(base(ret.in) == a + 5); + assert(base(ret.out) == b + 5); + } + } + { // binary + { + int a[] = {1, 2, 3, 4, 5}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + std::same_as> decltype(auto) ret = std::ranges::transform( + In1(a), Sent1(In1(a + 5)), In2(b), Sent2(In2(b + 5)), Out(c), [](int i, int j) { return i + j; }); + + assert((std::to_array(c) == std::array{6, 6, 6, 6, 6})); + assert(base(ret.in1) == a + 5); + assert(base(ret.in2) == b + 5); + assert(base(ret.out) == c + 5); + } + { + int a[] = {1, 2, 3, 4, 5}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5))); + + std::same_as> decltype(auto) ret = std::ranges::transform( + range1, range2, Out(c), [](int i, int j) { return i + j; }); + + assert((std::to_array(c) == std::array{6, 6, 6, 6, 6})); + assert(base(ret.in1) == a + 5); + assert(base(ret.in2) == b + 5); + assert(base(ret.out) == c + 5); + } + } + } + + { // first range empty + { // unary + { + int a[] = {}; + int b[5]; + std::same_as> decltype(auto) ret = + std::ranges::transform(In1(a), Sent1(In1(a)), Out(b), [](int i) { return i * 2; }); + assert(base(ret.in) == a); + assert(base(ret.out) == b); + } + { + int a[] = {}; + int b[5]; + auto range = std::ranges::subrange(In1(a), Sent1(In1(a))); + std::same_as> decltype(auto) ret = + std::ranges::transform(range, Out(b), [](int i) { return i * 2; }); + assert(base(ret.in) == a); + assert(base(ret.out) == b); + } + } + { // binary + { + int a[] = {}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + std::same_as> decltype(auto) ret = std::ranges::transform( + In1(a), Sent1(In1(a)), In2(b), Sent2(In2(b + 5)), Out(c), [](int i, int j) { return i + j; }); + + assert(base(ret.in1) == a); + assert(base(ret.in2) == b); + assert(base(ret.out) == c); + } + { + int a[] = {}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5))); + + std::same_as> decltype(auto) ret = std::ranges::transform( + range1, range2, Out(c), [](int i, int j) { return i + j; }); + + assert(base(ret.in1) == a); + assert(base(ret.in2) == b); + assert(base(ret.out) == c); + } + } + } + + { // second range empty (binary) + { + int a[] = {5, 4, 3, 2, 1}; + int b[] = {}; + int c[5]; + + std::same_as> decltype(auto) ret = std::ranges::transform( + In1(a), Sent1(In1(a + 5)), In2(b), Sent2(In2(b)), Out(c), [](int i, int j) { return i + j; }); + + assert(base(ret.in1) == a); + assert(base(ret.in2) == b); + assert(base(ret.out) == c); + } + { + int a[] = {5, 4, 3, 2, 1}; + int b[] = {}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b))); + + std::same_as> decltype(auto) ret = std::ranges::transform( + range1, range2, Out(c), [](int i, int j) { return i + j; }); + + assert(base(ret.in1) == a); + assert(base(ret.in2) == b); + assert(base(ret.out) == c); + } + } + + + { // first range one element + { // unary + { + int a[] = {2}; + int b[5]; + std::same_as> decltype(auto) ret = + std::ranges::transform(In1(a), Sent1(In1(a + 1)), Out(b), [](int i) { return i * 2; }); + assert(b[0] == 4); + assert(base(ret.in) == a + 1); + assert(base(ret.out) == b + 1); + } + { + int a[] = {2}; + int b[5]; + auto range = std::ranges::subrange(In1(a), Sent1(In1(a + 1))); + std::same_as> decltype(auto) ret = + std::ranges::transform(range, Out(b), [](int i) { return i * 2; }); + assert(b[0] == 4); + assert(base(ret.in) == a + 1); + assert(base(ret.out) == b + 1); + } + } + { // binary + { + int a[] = {2}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + std::same_as> decltype(auto) ret = std::ranges::transform( + In1(a), Sent1(In1(a + 1)), In2(b), Sent2(In2(b + 5)), Out(c), [](int i, int j) { return i + j; }); + + assert(c[0] == 7); + assert(base(ret.in1) == a + 1); + assert(base(ret.in2) == b + 1); + assert(base(ret.out) == c + 1); + } + { + int a[] = {2}; + int b[] = {5, 4, 3, 2, 1}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 1))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5))); + + std::same_as> decltype(auto) ret = std::ranges::transform( + range1, range2, Out(c), [](int i, int j) { return i + j; }); + + assert(base(ret.in1) == a + 1); + assert(base(ret.in2) == b + 1); + assert(base(ret.out) == c + 1); + } + } + } + + { // second range contains one element (binary) + { + int a[] = {5, 4, 3, 2, 1}; + int b[] = {4}; + int c[5]; + + std::same_as> decltype(auto) ret = std::ranges::transform( + In1(a), Sent1(In1(a + 5)), In2(b), Sent2(In2(b + 1)), Out(c), [](int i, int j) { return i + j; }); + + assert(c[0] == 9); + assert(base(ret.in1) == a + 1); + assert(base(ret.in2) == b + 1); + assert(base(ret.out) == c + 1); + } + { + int a[] = {5, 4, 3, 2, 1}; + int b[] = {4}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 1))); + + std::same_as> decltype(auto) ret = std::ranges::transform( + range1, range2, Out(c), [](int i, int j) { return i + j; }); + + assert(c[0] == 9); + assert(base(ret.in1) == a + 1); + assert(base(ret.in2) == b + 1); + assert(base(ret.out) == c + 1); + } + } +} + +template +constexpr void test_iterator_in1() { + test_iterators, In2, Out, sentinel_wrapper>, Sent2>(); + test_iterators, In2, Out, forward_iterator, Sent2>(); + test_iterators, In2, Out, bidirectional_iterator, Sent2>(); + test_iterators, In2, Out, random_access_iterator, Sent2>(); + test_iterators, In2, Out, contiguous_iterator, Sent2>(); + test_iterators(); +} + +template +constexpr void test_iterators_in1_in2() { + test_iterator_in1, Out, sentinel_wrapper>>(); + test_iterator_in1, Out>(); + test_iterator_in1, Out>(); + test_iterator_in1, Out>(); + test_iterator_in1, Out>(); + test_iterator_in1(); +} + +constexpr bool test() { + test_iterators_in1_in2>(); + test_iterators_in1_in2>(); + test_iterators_in1_in2>(); + test_iterators_in1_in2>(); + test_iterators_in1_in2>(); + test_iterators_in1_in2(); + + { // check that std::ranges::dangling is returned properly + { // unary + std::array b; + std::same_as> auto ret = + std::ranges::transform(std::array{1, 2, 3, 5, 4}, b.data(), [](int i) { return i * i; }); + assert((b == std::array{1, 4, 9, 25, 16})); + assert(ret.out == b.data() + b.size()); + } + // binary + { + int b[] = {2, 5, 4, 3, 1}; + std::array c; + std::same_as> auto ret = + std::ranges::transform(std::array{1, 2, 3, 5, 4}, b, c.data(), [](int i, int j) { return i * j; }); + assert((c == std::array{2, 10, 12, 15, 4})); + assert(ret.in2 == b + 5); + assert(ret.out == c.data() + c.size()); + } + { + int a[] = {2, 5, 4, 3, 1, 4, 5, 6}; + std::array c; + std::same_as> auto ret = + std::ranges::transform(a, std::array{1, 2, 3, 5, 4, 5, 6, 7}, c.data(), [](int i, int j) { return i * j; }); + assert((c == std::array{2, 10, 12, 15, 4, 20, 30, 42})); + assert(ret.in1 == a + 8); + assert(ret.out == c.data() + c.size()); + } + { + std::array c; + std::same_as> auto ret = + std::ranges::transform(std::array{4, 4, 4}, std::array{4, 4, 4}, c.data(), [](int i, int j) { return i * j; }); + assert((c == std::array{16, 16, 16})); + assert(ret.out == c.data() + c.size()); + } + } + + { // count predicate and projection call counts + { // unary + int predCount = 0; + int projCount = 0; + auto pred = [&](int) { ++predCount; return 1; }; + auto proj = [&](int) { ++projCount; return 0; }; + std::array c; + std::ranges::transform(std::array{1, 2, 3, 4}, c.data(), pred, proj); + assert(predCount == 4); + assert(projCount == 4); + assert((c == std::array{1, 1, 1, 1})); + } + { // binary + int predCount = 0; + int proj1Count = 0; + int proj2Count = 0; + auto pred = [&](int, int) { ++predCount; return 1; }; + auto proj1 = [&](int) { ++proj1Count; return 0; }; + auto proj2 = [&](int) { ++proj2Count; return 0; }; + std::array c; + std::ranges::transform(std::array{1, 2, 3, 4}, std::array{1, 2, 3, 4}, c.data(), pred, proj1, proj2); + assert(predCount == 4); + assert(proj1Count == 4); + assert(proj2Count == 4); + assert((c == std::array{1, 1, 1, 1})); + } + } + + { // check that std::invoke is used + { // unary + struct S { int i; }; + S a[] = { S{1}, S{3}, S{2} }; + std::array b; + auto ret = std::ranges::transform(a, b.data(), [](int i) { return i; }, &S::i); + assert((b == std::array{1, 3, 2})); + assert(ret.out == b.data() + 3); + } + { // binary + struct S { int i; }; + S a[] = { S{1}, S{3}, S{2} }; + S b[] = { S{2}, S{5}, S{3} }; + std::array c; + auto ret = std::ranges::transform(a, b, c.data(), [](int i, int j) { return i + j + 2; }, &S::i, &S::i); + assert((c == std::array{5, 10, 7})); + assert(ret.out == c.data() + 3); + } + } + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); +}