File: conversion.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (99 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3

#include <utility> // for std::swap on C++14.
#include <map>
#include <set>
#include <list>
#include <sstream>
#include <string>
#include <vector>
#include <range/v3/core.hpp>
#include <range/v3/view/any_view.hpp>
#include <range/v3/view/concat.hpp>
#include <range/v3/view/drop.hpp>
#include <range/v3/view/take.hpp>
#include <range/v3/view/repeat.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/for_each.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/zip.hpp>
#include "../simple_test.hpp"
#include "../test_utils.hpp"

int main()
{
    using namespace ranges;

    // 1-d vector

    auto v = views::ints | views::take(10) | to<std::vector>();
    ::check_equal(v, {0,1,2,3,4,5,6,7,8,9});

    v = views::iota(10) | views::take(10) | views::reverse | to<std::vector>();
    ::check_equal(v, {19,18,17,16,15,14,13,12,11,10});

    // 1-d list

    auto l = views::ints | views::take(10) | to<std::list>();
    ::check_equal(l, {0,1,2,3,4,5,6,7,8,9});

    l = views::iota(10) | views::take(10) | views::reverse | to<std::list>();
    ::check_equal(l, {19,18,17,16,15,14,13,12,11,10});

    // 2-d vector

    auto vv = views::repeat_n(views::ints(0, 8), 10) | to<std::vector<std::vector<int>>>();
    ::check_equal(vv, std::vector<std::vector<int>>(10, {0,1,2,3,4,5,6,7}));

    // issue #556

    {
        std::string s{"abc"};
        any_view<any_view<char, category::random_access>, category::random_access> v1 =
            views::single(s | views::drop(1));
        any_view<any_view<char, category::random_access>, category::random_access> v2 =
            views::single(s | views::drop(2));
        auto v3 = views::concat(v1, v2);

        auto owner1 = v3 | to<std::vector<std::vector<char>>>();
        auto owner2 = v3 | to<std::vector<std::string>>();

        ::check_equal(owner1, std::vector<std::vector<char>>{{'b', 'c'}, {'c'}});
        ::check_equal(owner2, std::vector<std::string>{{"bc"}, {"c"}});
    }

    // map

    auto to_string = [](int i){ std::stringstream str; str << i; return str.str(); };
    auto m = views::zip(views::ints, views::ints | views::transform(to_string)) |
        views::take(5) | to<std::map<int, std::string>>();
    using P = std::pair<int const, std::string>;
    ::check_equal(m, {P{0,"0"}, P{1,"1"}, P{2,"2"}, P{3,"3"}, P{4,"4"}});

    // Another way to say the same thing, but with a range comprehension:
    m = views::for_each(views::ints(0,5), [&](int i) {
            return yield(std::make_pair(i, to_string(i)));
        }) | to<std::map<int, std::string>>();
    ::check_equal(m, {P{0,"0"}, P{1,"1"}, P{2,"2"}, P{3,"3"}, P{4,"4"}});

    // set

    CPP_assert(range<std::set<int>>);
    CPP_assert(!view_<std::set<int>>);
    auto s = views::ints | views::take(10) | to<std::set<int>>();
    ::check_equal(s, {0,1,2,3,4,5,6,7,8,9});

    static_assert(!view_<std::initializer_list<int>>, "");

    return ::test_result();
}