1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
10 | |
11 | // Some basic examples of how transform_view might be used in the wild. This is a general |
12 | // collection of sample algorithms and functions that try to mock general usage of |
13 | // this view. |
14 | |
15 | #include <ranges> |
16 | |
17 | #include <cctype> |
18 | #include <functional> |
19 | #include <list> |
20 | #include <numeric> |
21 | #include <string> |
22 | #include <vector> |
23 | |
24 | #include <cassert> |
25 | #include "MoveOnly.h" |
26 | #include "test_macros.h" |
27 | #include "test_iterators.h" |
28 | #include "types.h" |
29 | |
30 | template<class T, class F> |
31 | concept ValidTransformView = requires { typename std::ranges::transform_view<T, F>; }; |
32 | |
33 | struct BadFunction { }; |
34 | static_assert( ValidTransformView<MoveOnlyView, PlusOne>); |
35 | static_assert(!ValidTransformView<Range, PlusOne>); |
36 | static_assert(!ValidTransformView<MoveOnlyView, BadFunction>); |
37 | |
38 | template<std::ranges::range R> |
39 | auto toUpper(R range) { |
40 | return std::ranges::transform_view(range, [](char c) { return std::toupper(c); }); |
41 | } |
42 | |
43 | template<class E1, class E2, std::size_t N, class Join = std::plus<E1>> |
44 | auto joinArrays(E1 (&a)[N], E2 (&b)[N], Join join = Join()) { |
45 | return std::ranges::transform_view(a, [&a, &b, join](auto& x) { |
46 | auto idx = (&x) - a; |
47 | return join(x, b[idx]); |
48 | }); |
49 | } |
50 | |
51 | #if _LIBCPP_STD_VER >= 23 |
52 | struct MoveOnlyFunction : public MoveOnly { |
53 | template <class T> |
54 | constexpr T operator()(T x) const { |
55 | return x + 42; |
56 | } |
57 | }; |
58 | #endif |
59 | |
60 | struct NonConstView : std::ranges::view_base { |
61 | explicit NonConstView(int *b, int *e) : b_(b), e_(e) {} |
62 | const int *begin() { return b_; } // deliberately non-const |
63 | const int *end() { return e_; } // deliberately non-const |
64 | const int *b_; |
65 | const int *e_; |
66 | }; |
67 | |
68 | int main(int, char**) { |
69 | { |
70 | std::vector<int> vec = {1, 2, 3, 4}; |
71 | auto transformed = std::ranges::transform_view(vec, [](int x) { return x + 42; }); |
72 | int expected[] = {43, 44, 45, 46}; |
73 | assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4)); |
74 | const auto& ct = transformed; |
75 | assert(std::equal(ct.begin(), ct.end(), expected, expected + 4)); |
76 | } |
77 | |
78 | { |
79 | // Test a view type that is not const-iterable. |
80 | int a[] = {1, 2, 3, 4}; |
81 | auto transformed = NonConstView(a, a + 4) | std::views::transform([](int x) { return x + 42; }); |
82 | int expected[4] = {43, 44, 45, 46}; |
83 | assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4)); |
84 | } |
85 | |
86 | { |
87 | int a[4] = {1, 2, 3, 4}; |
88 | int b[4] = {4, 3, 2, 1}; |
89 | auto out = joinArrays(a, b); |
90 | int check[4] = {5, 5, 5, 5}; |
91 | assert(std::equal(out.begin(), out.end(), check, check + 4)); |
92 | } |
93 | |
94 | { |
95 | std::string_view str = "Hello, World." ; |
96 | auto upp = toUpper(str); |
97 | std::string_view check = "HELLO, WORLD." ; |
98 | assert(std::equal(upp.begin(), upp.end(), check.begin(), check.end())); |
99 | } |
100 | #if _LIBCPP_STD_VER >= 23 |
101 | // [P2494R2] Relaxing range adaptors to allow for move only types. |
102 | // Test transform_view is valid when the function object is a move only type. |
103 | { |
104 | int a[] = {1, 2, 3, 4}; |
105 | auto transformed = NonConstView(a, a + 4) | std::views::transform(MoveOnlyFunction()); |
106 | int expected[] = {43, 44, 45, 46}; |
107 | assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4)); |
108 | } |
109 | #endif |
110 | |
111 | // GH issue #70506 |
112 | // movable_box::operator= overwrites underlying view |
113 | { |
114 | auto f = [l = 0.0L, b = false](int i) { |
115 | (void)l; |
116 | (void)b; |
117 | return i; |
118 | }; |
119 | |
120 | auto v1 = std::vector{1, 2, 3, 4} | std::views::transform(f); |
121 | auto v2 = std::vector{1, 2, 3, 4} | std::views::transform(f); |
122 | |
123 | v1 = std::move(v2); |
124 | int expected[] = {1, 2, 3, 4}; |
125 | assert(std::equal(v1.begin(), v1.end(), expected, expected + 4)); |
126 | } |
127 | |
128 | return 0; |
129 | } |
130 | |