10e09a41bSzoecarver //===----------------------------------------------------------------------===//
20e09a41bSzoecarver //
30e09a41bSzoecarver // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40e09a41bSzoecarver // See https://llvm.org/LICENSE.txt for license information.
50e09a41bSzoecarver // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60e09a41bSzoecarver //
70e09a41bSzoecarver //===----------------------------------------------------------------------===//
80e09a41bSzoecarver
90e09a41bSzoecarver // UNSUPPORTED: c++03, c++11, c++14, c++17
100e09a41bSzoecarver
110e09a41bSzoecarver // Some basic examples of how transform_view might be used in the wild. This is a general
120e09a41bSzoecarver // collection of sample algorithms and functions that try to mock general usage of
130e09a41bSzoecarver // this view.
140e09a41bSzoecarver
150e09a41bSzoecarver #include <ranges>
160e09a41bSzoecarver
170e09a41bSzoecarver #include <cctype>
180e09a41bSzoecarver #include <functional>
190e09a41bSzoecarver #include <list>
200e09a41bSzoecarver #include <numeric>
210e09a41bSzoecarver #include <string>
220e09a41bSzoecarver #include <vector>
230e09a41bSzoecarver
240e09a41bSzoecarver #include <cassert>
259f6439f1Syronglin #include "MoveOnly.h"
260e09a41bSzoecarver #include "test_macros.h"
270e09a41bSzoecarver #include "test_iterators.h"
280e09a41bSzoecarver #include "types.h"
290e09a41bSzoecarver
307b3ada71SLouis Dionne template<class T, class F>
317b3ada71SLouis Dionne concept ValidTransformView = requires { typename std::ranges::transform_view<T, F>; };
327b3ada71SLouis Dionne
337b3ada71SLouis Dionne struct BadFunction { };
34610ac8dbSArthur O'Dwyer static_assert( ValidTransformView<MoveOnlyView, PlusOne>);
359de882fdSLouis Dionne static_assert(!ValidTransformView<Range, PlusOne>);
36610ac8dbSArthur O'Dwyer static_assert(!ValidTransformView<MoveOnlyView, BadFunction>);
377b3ada71SLouis Dionne
380e09a41bSzoecarver template<std::ranges::range R>
toUpper(R range)390e09a41bSzoecarver auto toUpper(R range) {
400e09a41bSzoecarver return std::ranges::transform_view(range, [](char c) { return std::toupper(c); });
410e09a41bSzoecarver }
420e09a41bSzoecarver
43fb855eb9SMark de Wever template<class E1, class E2, std::size_t N, class Join = std::plus<E1>>
joinArrays(E1 (& a)[N],E2 (& b)[N],Join join=Join ())440e09a41bSzoecarver auto joinArrays(E1 (&a)[N], E2 (&b)[N], Join join = Join()) {
450e09a41bSzoecarver return std::ranges::transform_view(a, [&a, &b, join](auto& x) {
460e09a41bSzoecarver auto idx = (&x) - a;
470e09a41bSzoecarver return join(x, b[idx]);
480e09a41bSzoecarver });
490e09a41bSzoecarver }
500e09a41bSzoecarver
519f6439f1Syronglin #if _LIBCPP_STD_VER >= 23
529f6439f1Syronglin struct MoveOnlyFunction : public MoveOnly {
539f6439f1Syronglin template <class T>
operator ()MoveOnlyFunction549f6439f1Syronglin constexpr T operator()(T x) const {
559f6439f1Syronglin return x + 42;
569f6439f1Syronglin }
579f6439f1Syronglin };
589f6439f1Syronglin #endif
599f6439f1Syronglin
60dcdb07abSArthur O'Dwyer struct NonConstView : std::ranges::view_base {
NonConstViewNonConstView61dcdb07abSArthur O'Dwyer explicit NonConstView(int *b, int *e) : b_(b), e_(e) {}
beginNonConstView62dcdb07abSArthur O'Dwyer const int *begin() { return b_; } // deliberately non-const
endNonConstView63dcdb07abSArthur O'Dwyer const int *end() { return e_; } // deliberately non-const
64dcdb07abSArthur O'Dwyer const int *b_;
65dcdb07abSArthur O'Dwyer const int *e_;
66dcdb07abSArthur O'Dwyer };
67dcdb07abSArthur O'Dwyer
main(int,char **)680e09a41bSzoecarver int main(int, char**) {
690e09a41bSzoecarver {
70dcdb07abSArthur O'Dwyer std::vector<int> vec = {1, 2, 3, 4};
71dcdb07abSArthur O'Dwyer auto transformed = std::ranges::transform_view(vec, [](int x) { return x + 42; });
72dcdb07abSArthur O'Dwyer int expected[] = {43, 44, 45, 46};
73dcdb07abSArthur O'Dwyer assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4));
74dcdb07abSArthur O'Dwyer const auto& ct = transformed;
75dcdb07abSArthur O'Dwyer assert(std::equal(ct.begin(), ct.end(), expected, expected + 4));
76dcdb07abSArthur O'Dwyer }
77dcdb07abSArthur O'Dwyer
78dcdb07abSArthur O'Dwyer {
79dcdb07abSArthur O'Dwyer // Test a view type that is not const-iterable.
80dcdb07abSArthur O'Dwyer int a[] = {1, 2, 3, 4};
81dcdb07abSArthur O'Dwyer auto transformed = NonConstView(a, a + 4) | std::views::transform([](int x) { return x + 42; });
82dcdb07abSArthur O'Dwyer int expected[4] = {43, 44, 45, 46};
83dcdb07abSArthur O'Dwyer assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4));
840e09a41bSzoecarver }
850e09a41bSzoecarver
860e09a41bSzoecarver {
870e09a41bSzoecarver int a[4] = {1, 2, 3, 4};
880e09a41bSzoecarver int b[4] = {4, 3, 2, 1};
890e09a41bSzoecarver auto out = joinArrays(a, b);
900e09a41bSzoecarver int check[4] = {5, 5, 5, 5};
91dcdb07abSArthur O'Dwyer assert(std::equal(out.begin(), out.end(), check, check + 4));
920e09a41bSzoecarver }
930e09a41bSzoecarver
940e09a41bSzoecarver {
950e09a41bSzoecarver std::string_view str = "Hello, World.";
960e09a41bSzoecarver auto upp = toUpper(str);
970e09a41bSzoecarver std::string_view check = "HELLO, WORLD.";
980e09a41bSzoecarver assert(std::equal(upp.begin(), upp.end(), check.begin(), check.end()));
990e09a41bSzoecarver }
1009f6439f1Syronglin #if _LIBCPP_STD_VER >= 23
1019f6439f1Syronglin // [P2494R2] Relaxing range adaptors to allow for move only types.
1029f6439f1Syronglin // Test transform_view is valid when the function object is a move only type.
1039f6439f1Syronglin {
1049f6439f1Syronglin int a[] = {1, 2, 3, 4};
1059f6439f1Syronglin auto transformed = NonConstView(a, a + 4) | std::views::transform(MoveOnlyFunction());
1069f6439f1Syronglin int expected[] = {43, 44, 45, 46};
1079f6439f1Syronglin assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4));
1089f6439f1Syronglin }
1099f6439f1Syronglin #endif
1100e09a41bSzoecarver
111*120b0bfbSHui // GH issue #70506
112*120b0bfbSHui // movable_box::operator= overwrites underlying view
113*120b0bfbSHui {
114*120b0bfbSHui auto f = [l = 0.0L, b = false](int i) {
115*120b0bfbSHui (void)l;
116*120b0bfbSHui (void)b;
117*120b0bfbSHui return i;
118*120b0bfbSHui };
119*120b0bfbSHui
120*120b0bfbSHui auto v1 = std::vector{1, 2, 3, 4} | std::views::transform(f);
121*120b0bfbSHui auto v2 = std::vector{1, 2, 3, 4} | std::views::transform(f);
122*120b0bfbSHui
123*120b0bfbSHui v1 = std::move(v2);
124*120b0bfbSHui int expected[] = {1, 2, 3, 4};
125*120b0bfbSHui assert(std::equal(v1.begin(), v1.end(), expected, expected + 4));
126*120b0bfbSHui }
127*120b0bfbSHui
1280e09a41bSzoecarver return 0;
1290e09a41bSzoecarver }
130