This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. philnik marked an inline comment as done. Closed by commit rG3ba8548c8e04: [libc++][ranges] Implement ranges::transform (authored by philnik). Herald added a project: clang. Herald added a subscriber: cfe-commits.
Changed prior to commit: https://reviews.llvm.org/D122173?vs=419955&id=420414#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D122173/new/ https://reviews.llvm.org/D122173 Files: clang/lib/ExtractAPI/Serialization/ranges_transform.module.verify.cpp libcxx/docs/Status/RangesAlgorithms.csv libcxx/include/CMakeLists.txt libcxx/include/__algorithm/ranges_transform.h libcxx/include/algorithm libcxx/include/module.modulemap libcxx/test/libcxx/private_headers.verify.cpp libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp libcxx/test/support/almost_satisfies_types.h
Index: libcxx/test/support/almost_satisfies_types.h =================================================================== --- libcxx/test/support/almost_satisfies_types.h +++ libcxx/test/support/almost_satisfies_types.h @@ -128,4 +128,15 @@ static_assert(std::semiregular<SentinelForNotWeaklyEqualityComparableWith>); static_assert(!std::sentinel_for<SentinelForNotWeaklyEqualityComparableWith, cpp20_input_iterator<int*>>); +class WeaklyIncrementableNotMovable { +public: + using difference_type = long; + WeaklyIncrementableNotMovable& operator++(); + void operator++(int); + WeaklyIncrementableNotMovable(const WeaklyIncrementableNotMovable&) = delete; +}; + +static_assert(!std::movable<WeaklyIncrementableNotMovable>); +static_assert(!std::weakly_incrementable<WeaklyIncrementableNotMovable>); + #endif // ALMOST_SATISFIES_TYPES_H Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/ranges.transform.pass.cpp @@ -0,0 +1,583 @@ +//===----------------------------------------------------------------------===// +// +// 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, +// copy_constructible F, class Proj = identity> +// requires indirectly_writable<O, indirect_result_t<F&, projected<I, Proj>>> +// constexpr ranges::unary_transform_result<I, O> +// ranges::transform(I first1, S last1, O result, F op, Proj proj = {}); +// template<input_range R, weakly_incrementable O, copy_constructible F, +// class Proj = identity> +// requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R>, Proj>>> +// constexpr ranges::unary_transform_result<borrowed_iterator_t<R>, O> +// ranges::transform(R&& r, O result, F op, Proj proj = {}); +// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2, +// weakly_incrementable O, copy_constructible F, class Proj1 = identity, +// class Proj2 = identity> +// requires indirectly_writable<O, indirect_result_t<F&, projected<I1, Proj1>, +// projected<I2, Proj2>>> +// constexpr ranges::binary_transform_result<I1, I2, O> +// ranges::transform(I1 first1, S1 last1, I2 first2, S2 last2, O result, +// F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); +// template<input_range R1, input_range R2, weakly_incrementable O, +// copy_constructible F, class Proj1 = identity, class Proj2 = identity> +// requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>, +// projected<iterator_t<R2>, Proj2>>> +// constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O> +// ranges::transform(R1&& r1, R2&& r2, O result, +// F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); + +#include <algorithm> +#include <array> +#include <cassert> +#include <ranges> + +#include "test_iterators.h" +#include "almost_satisfies_types.h" + +struct BinaryFunc { + int operator()(int, int); +}; + +template <class Range> +concept HasTranformR = requires(Range r, int* out) { + std::ranges::transform(r, out, std::identity{}); + std::ranges::transform(r, r, out, BinaryFunc{}); +}; +static_assert(HasTranformR<std::array<int, 1>>); +static_assert(!HasTranformR<int>); +static_assert(!HasTranformR<InputRangeNotDerivedFrom>); +static_assert(!HasTranformR<InputRangeNotIndirectlyReadable>); +static_assert(!HasTranformR<InputRangeNotInputOrOutputIterator>); +static_assert(!HasTranformR<InputRangeNotSentinelSemiregular>); +static_assert(!HasTranformR<InputRangeNotSentinelEqualityComparableWith>); + +template <class It, class Sent = It> +concept HasTransformIt = requires(It it, Sent sent, int* out) { + std::ranges::transform(it, sent, out, std::identity{}); + std::ranges::transform(it, sent, it, sent, out, BinaryFunc{}); +}; +static_assert(HasTransformIt<int*>); +static_assert(!HasTransformIt<InputIteratorNotDerivedFrom>); +static_assert(!HasTransformIt<InputIteratorNotIndirectlyReadable>); +static_assert(!HasTransformIt<InputIteratorNotInputOrOutputIterator>); +static_assert(!HasTransformIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>); +static_assert(!HasTransformIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>); + +template <class It> +concept HasTransformOut = requires(int* it, int* sent, It out, std::array<int, 2> range) { + std::ranges::transform(it, sent, out, std::identity{}); + std::ranges::transform(it, sent, it, sent, out, BinaryFunc{}); + std::ranges::transform(range, out, std::identity{}); + std::ranges::transform(range, range, out, BinaryFunc{}); +}; +static_assert(HasTransformOut<int*>); +static_assert(!HasTransformOut<WeaklyIncrementableNotMovable>); + +// check indirectly_readable +static_assert(HasTransformOut<char*>); +static_assert(!HasTransformOut<int**>); + +struct MoveOnlyFunctor { + MoveOnlyFunctor(const MoveOnlyFunctor&) = delete; + MoveOnlyFunctor(MoveOnlyFunctor&&) = default; + int operator()(int); + int operator()(int, int); +}; + +template <class Func> +concept HasTransformFuncUnary = requires(int* it, int* sent, int* out, std::array<int, 2> range, Func func) { + std::ranges::transform(it, sent, out, func); + std::ranges::transform(range, out, func); +}; +static_assert(HasTransformFuncUnary<std::identity>); +static_assert(!HasTransformFuncUnary<MoveOnlyFunctor>); + +template <class Func> +concept HasTransformFuncBinary = requires(int* it, int* sent, int* out, std::array<int, 2> range, Func func) { + std::ranges::transform(it, sent, it, sent, out, func); + std::ranges::transform(range, range, out, func); +}; +static_assert(HasTransformFuncBinary<BinaryFunc>); +static_assert(!HasTransformFuncBinary<MoveOnlyFunctor>); + +static_assert(std::is_same_v<std::ranges::unary_transform_result<int, long>, std::ranges::in_out_result<int, long>>); +static_assert(std::is_same_v<std::ranges::binary_transform_result<int, long, char>, + std::ranges::in_in_out_result<int, long, char>>); + +template <class In1, class In2, class Out, class Sent1, class Sent2> +constexpr bool test_iterators() { + { // simple + { // unary + { + int a[] = {1, 2, 3, 4, 5}; + int b[5]; + std::same_as<std::ranges::in_out_result<In1, Out>> 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<std::ranges::in_out_result<In1, Out>> 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<std::ranges::in_in_out_result<In1, In2, Out>> 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<std::ranges::in_in_out_result<In1, In2, Out>> 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]; + 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))); + 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]; + + 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))); + + 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]; + + 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))); + + 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); + } + } + + { // both ranges empty (binary) + { + int a[] = {}; + int b[] = {}; + int c[5]; + + auto ret = std::ranges::transform( + In1(a), Sent1(In1(a)), 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[] = {}; + int b[] = {}; + int c[5]; + + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b))); + + 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]; + 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))); + 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]; + + 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))); + + auto ret = std::ranges::transform(range1, range2, 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); + } + } + } + + { // second range contains one element (binary) + { + int a[] = {5, 4, 3, 2, 1}; + int b[] = {4}; + int c[5]; + + 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))); + + 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); + } + } + + { // check that the transform function and projection call counts are correct + { // unary + { + int predCount = 0; + int projCount = 0; + auto pred = [&](int) { ++predCount; return 1; }; + auto proj = [&](int) { ++projCount; return 0; }; + int a[] = {1, 2, 3, 4}; + std::array<int, 4> c; + std::ranges::transform(In1(a), Sent1(In1(a + 4)), Out(c.data()), pred, proj); + assert(predCount == 4); + assert(projCount == 4); + assert((c == std::array{1, 1, 1, 1})); + } + { + int predCount = 0; + int projCount = 0; + auto pred = [&](int) { ++predCount; return 1; }; + auto proj = [&](int) { ++projCount; return 0; }; + int a[] = {1, 2, 3, 4}; + std::array<int, 4> c; + auto range = std::ranges::subrange(In1(a), Sent1(In1(a + 4))); + std::ranges::transform(range, Out(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; }; + int a[] = {1, 2, 3, 4}; + int b[] = {1, 2, 3, 4}; + std::array<int, 4> c; + std::ranges::transform(In1(a), Sent1(In1(a + 4)), In2(b), Sent2(In2(b + 4)), Out(c.data()), pred, proj1, proj2); + assert(predCount == 4); + assert(proj1Count == 4); + assert(proj2Count == 4); + assert((c == std::array{1, 1, 1, 1})); + } + { + 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; }; + int a[] = {1, 2, 3, 4}; + int b[] = {1, 2, 3, 4}; + std::array<int, 4> c; + auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 4))); + auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 4))); + std::ranges::transform(range1, range2, Out(c.data()), pred, proj1, proj2); + assert(predCount == 4); + assert(proj1Count == 4); + assert(proj2Count == 4); + assert((c == std::array{1, 1, 1, 1})); + } + } + } + + return true; +} + +template <class In2, class Out, class Sent2 = In2> +constexpr void test_iterator_in1() { + test_iterators<cpp17_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp17_input_iterator<int*>>, Sent2>(); + test_iterators<cpp20_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp20_input_iterator<int*>>, Sent2>(); + test_iterators<forward_iterator<int*>, In2, Out, forward_iterator<int*>, Sent2>(); + test_iterators<bidirectional_iterator<int*>, In2, Out, bidirectional_iterator<int*>, Sent2>(); + test_iterators<random_access_iterator<int*>, In2, Out, random_access_iterator<int*>, Sent2>(); + test_iterators<contiguous_iterator<int*>, In2, Out, contiguous_iterator<int*>, Sent2>(); + test_iterators<int*, In2, Out, int*, Sent2>(); + // static_asserting here to avoid hitting the constant evaluation step limit + static_assert(test_iterators<cpp17_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp17_input_iterator<int*>>, Sent2>()); + static_assert(test_iterators<cpp20_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp20_input_iterator<int*>>, Sent2>()); + static_assert(test_iterators<forward_iterator<int*>, In2, Out, forward_iterator<int*>, Sent2>()); + static_assert(test_iterators<bidirectional_iterator<int*>, In2, Out, bidirectional_iterator<int*>, Sent2>()); + static_assert(test_iterators<random_access_iterator<int*>, In2, Out, random_access_iterator<int*>, Sent2>()); + static_assert(test_iterators<contiguous_iterator<int*>, In2, Out, contiguous_iterator<int*>, Sent2>()); + static_assert(test_iterators<int*, In2, Out, int*, Sent2>()); +} + +template <class Out> +void test_iterators_in1_in2() { + test_iterator_in1<cpp17_input_iterator<int*>, Out, sentinel_wrapper<cpp17_input_iterator<int*>>>(); + test_iterator_in1<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>(); + test_iterator_in1<forward_iterator<int*>, Out>(); + test_iterator_in1<bidirectional_iterator<int*>, Out>(); + test_iterator_in1<random_access_iterator<int*>, Out>(); + test_iterator_in1<contiguous_iterator<int*>, Out>(); + test_iterator_in1<int*, Out>(); +} + +constexpr bool test() { + { // check that std::ranges::dangling is returned properly + { // unary + std::array<int, 5> b; + std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> 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<int, 5> c; + std::same_as<std::ranges::in_in_out_result<std::ranges::dangling, int*, int*>> 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<int, 8> c; + std::same_as<std::ranges::in_in_out_result<int*, std::ranges::dangling, int*>> 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<int, 3> c; + std::same_as<std::ranges::in_in_out_result<std::ranges::dangling, std::ranges::dangling, int*>> 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()); + } + } + + { // check that returning another type from the projection works + { // unary + { + struct S { int i; int other; }; + S a[] = { S{0, 0}, S{1, 0}, S{3, 0}, S{10, 0} }; + std::array<int, 4> b; + std::ranges::transform(a, a + 4, b.begin(), [](S s) { return s.i; }); + assert((b == std::array{0, 1, 3, 10})); + } + { + struct S { int i; int other; }; + S a[] = { S{0, 0}, S{1, 0}, S{3, 0}, S{10, 0} }; + std::array<int, 4> b; + std::ranges::transform(a, b.begin(), [](S s) { return s.i; }); + assert((b == std::array{0, 1, 3, 10})); + } + } + { // binary + { + struct S { int i; int other; }; + S a[] = { S{0, 0}, S{1, 0}, S{3, 0}, S{10, 0} }; + S b[] = { S{0, 10}, S{1, 20}, S{3, 30}, S{10, 40} }; + std::array<int, 4> c; + std::ranges::transform(a, a + 4, b, b + 4, c.begin(), [](S s1, S s2) { return s1.i + s2.other; }); + assert((c == std::array{10, 21, 33, 50})); + } + { + struct S { int i; int other; }; + S a[] = { S{0, 0}, S{1, 0}, S{3, 0}, S{10, 0} }; + S b[] = { S{0, 10}, S{1, 20}, S{3, 30}, S{10, 40} }; + std::array<int, 4> c; + std::ranges::transform(a, b, c.begin(), [](S s1, S s2) { return s1.i + s2.other; }); + assert((c == std::array{10, 21, 33, 50})); + } + } + } + + { // check that std::invoke is used + { // unary + struct S { int i; }; + S a[] = { S{1}, S{3}, S{2} }; + std::array<int, 3> 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<int, 3> 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_iterators_in1_in2<cpp17_output_iterator<int*>>(); + test_iterators_in1_in2<forward_iterator<int*>>(); + test_iterators_in1_in2<bidirectional_iterator<int*>>(); + test_iterators_in1_in2<random_access_iterator<int*>>(); + test_iterators_in1_in2<contiguous_iterator<int*>>(); + test_iterators_in1_in2<int*>(); + test(); + static_assert(test()); + + return 0; +} Index: libcxx/test/libcxx/private_headers.verify.cpp =================================================================== --- libcxx/test/libcxx/private_headers.verify.cpp +++ libcxx/test/libcxx/private_headers.verify.cpp @@ -112,6 +112,7 @@ #include <__algorithm/ranges_min_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min_element.h'}} #include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}} #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}} +#include <__algorithm/ranges_transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}} #include <__algorithm/remove.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/remove.h'}} #include <__algorithm/remove_copy.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/remove_copy.h'}} #include <__algorithm/remove_copy_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/remove_copy_if.h'}} Index: libcxx/include/module.modulemap =================================================================== --- libcxx/include/module.modulemap +++ libcxx/include/module.modulemap @@ -303,6 +303,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" } Index: libcxx/include/algorithm =================================================================== --- libcxx/include/algorithm +++ libcxx/include/algorithm @@ -110,6 +110,41 @@ requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*> constexpr range_value_t<R> max(R&& r, Comp comp = {}, Proj proj = {}); // since C++20 + + template<class I, class O> + using unary_transform_result = in_out_result<I, O>; // since C++20 + + template<class I1, class I2, class O> + using binary_transform_result = in_in_out_result<I1, I2, O>; // since C++20 + + template<input_iterator I, sentinel_for<I> S, weakly_incrementable O, + copy_constructible F, class Proj = identity> + requires indirectly_writable<O, indirect_result_t<F&, projected<I, Proj>>> + constexpr ranges::unary_transform_result<I, O> + transform(I first1, S last1, O result, F op, Proj proj = {}); // since C++20 + + template<input_range R, weakly_incrementable O, copy_constructible F, + class Proj = identity> + requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R>, Proj>>> + constexpr ranges::unary_transform_result<borrowed_iterator_t<R>, O> + transform(R&& r, O result, F op, Proj proj = {}); // since C++20 + + template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2, + weakly_incrementable O, copy_constructible F, class Proj1 = identity, + class Proj2 = identity> + requires indirectly_writable<O, indirect_result_t<F&, projected<I1, Proj1>, + projected<I2, Proj2>>> + constexpr ranges::binary_transform_result<I1, I2, O> + transform(I1 first1, S1 last1, I2 first2, S2 last2, O result, + F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + + template<input_range R1, input_range R2, weakly_incrementable O, + copy_constructible F, class Proj1 = identity, class Proj2 = identity> + requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>, + projected<iterator_t<R2>, Proj2>>> + constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O> + transform(R1&& r1, R2&& r2, O result, + F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 } constexpr bool // constexpr in C++20 @@ -836,6 +871,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> Index: libcxx/include/__algorithm/ranges_transform.h =================================================================== --- /dev/null +++ libcxx/include/__algorithm/ranges_transform.h @@ -0,0 +1,170 @@ +//===----------------------------------------------------------------------===// +// +// 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 <class _Ip, class _Op> +using unary_transform_result = in_out_result<_Ip, _Op>; + +template <class _I1, class _I2, class _O1> +using binary_transform_result = in_in_out_result<_I1, _I2, _O1>; + +namespace __transform { +struct __fn { +private: + template <class _InIter, class _Sent, + class _OutIter, + class _Func, + class _Proj> + _LIBCPP_HIDE_FROM_ABI static constexpr + unary_transform_result<_InIter, _OutIter> __unary(_InIter __first, _Sent __last, + _OutIter __result, + _Func& __operation, + _Proj& __projection) { + while (__first != __last) { + *__result = std::invoke(__operation, std::invoke(__projection, *__first)); + ++__first; + ++__result; + } + + return {std::move(__first), std::move(__result)}; + } + + template <class _InIter1, class _Sent1, + class _InIter2, class _Sent2, + class _OutIter, + class _Func, + class _Proj1, + class _Proj2> + _LIBCPP_HIDE_FROM_ABI static constexpr binary_transform_result<_InIter1, _InIter2, _OutIter> + __binary(_InIter1 __first1, _Sent1 __last1, + _InIter2 __first2, _Sent2 __last2, + _OutIter __result, + _Func& __binary_operation, + _Proj1& __projection1, + _Proj2& __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)}; + } +public: + template <input_iterator _InIter, sentinel_for<_InIter> _Sent, + weakly_incrementable _OutIter, + copy_constructible _Func, + class _Proj = identity> + requires indirectly_writable<_OutIter, indirect_result_t<_Func&, projected<_InIter, _Proj>>> + _LIBCPP_HIDE_FROM_ABI constexpr + unary_transform_result<_InIter, _OutIter> operator()(_InIter __first, _Sent __last, + _OutIter __result, + _Func __operation, + _Proj __proj = {}) const { + return __unary(std::move(__first), std::move(__last), std::move(__result), __operation, __proj); + } + + template <input_range _Range, + weakly_incrementable _OutIter, + copy_constructible _Func, + class _Proj = identity> + requires indirectly_writable<_OutIter, indirect_result_t<_Func, projected<iterator_t<_Range>, _Proj>>> + _LIBCPP_HIDE_FROM_ABI constexpr + unary_transform_result<borrowed_iterator_t<_Range>, _OutIter> operator()(_Range&& __range, + _OutIter __result, + _Func __operation, + _Proj __projection = {}) const { + return __unary(ranges::begin(__range), ranges::end(__range), std::move(__result), __operation, __projection); + } + + template <input_iterator _InIter1, sentinel_for<_InIter1> _Sent1, + input_iterator _InIter2, sentinel_for<_InIter2> _Sent2, + weakly_incrementable _OutIter, + copy_constructible _Func, + class _Proj1 = identity, + class _Proj2 = identity> + requires indirectly_writable<_OutIter, indirect_result_t<_Func&, projected<_InIter1, _Proj1>, + projected<_InIter2, _Proj2>>> + _LIBCPP_HIDE_FROM_ABI constexpr + binary_transform_result<_InIter1, _InIter2, _OutIter> operator()(_InIter1 __first1, _Sent1 __last1, + _InIter2 __first2, _Sent2 __last2, + _OutIter __result, + _Func __binary_operation, + _Proj1 __projection1 = {}, + _Proj2 __projection2 = {}) const { + return __binary(std::move(__first1), std::move(__last1), + std::move(__first2), std::move(__last2), + std::move(__result), + __binary_operation, + __projection1, + __projection2); + } + + template <input_range _Range1, + input_range _Range2, + weakly_incrementable _OutIter, + copy_constructible _Func, + class _Proj1 = identity, + class _Proj2 = identity> + requires indirectly_writable<_OutIter, indirect_result_t<_Func&, projected<iterator_t<_Range1>, _Proj1>, + projected<iterator_t<_Range2>, _Proj2>>> + _LIBCPP_HIDE_FROM_ABI constexpr + binary_transform_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter> + operator()(_Range1&& __range1, + _Range2&& __range2, + _OutIter __result, + _Func __binary_operation, + _Proj1 __projection1 = {}, + _Proj2 __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 Index: libcxx/include/CMakeLists.txt =================================================================== --- libcxx/include/CMakeLists.txt +++ libcxx/include/CMakeLists.txt @@ -75,6 +75,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 Index: libcxx/docs/Status/RangesAlgorithms.csv =================================================================== --- libcxx/docs/Status/RangesAlgorithms.csv +++ 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,`D122173 <https://llvm.org/D122173>`_,â 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 Index: clang/lib/ExtractAPI/Serialization/ranges_transform.module.verify.cpp =================================================================== --- /dev/null +++ clang/lib/ExtractAPI/Serialization/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>
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits