File: adjacent_remove_if.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 (117 lines) | stat: -rw-r--r-- 4,386 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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 <vector>
#include <iterator>
#include <functional>
#include <range/v3/core.hpp>
#include <range/v3/view/adjacent_remove_if.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/counted.hpp>
#include <range/v3/utility/copy.hpp>
#include <range/v3/algorithm/copy.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/iterator/insert_iterators.hpp>
#include "../simple_test.hpp"
#include "../test_utils.hpp"
#include "../test_iterators.hpp"

int main()
{
    using namespace ranges;

    int const rgi[] = {1, 1, 1, 2, 3, 4, 4};
    std::vector<int> out;

    {
        auto rng = rgi | views::adjacent_remove_if(std::equal_to<int>{});
        has_type<int const &>(*begin(rng));
        CPP_assert(view_<decltype(rng)>);
        CPP_assert(common_range<decltype(rng)>);
        CPP_assert(!sized_range<decltype(rng)>);
        CPP_assert(bidirectional_iterator<decltype(begin(rng))>);
        CPP_assert(!random_access_iterator<decltype(begin(rng))>);
        CPP_assert(output_iterator<decltype(ranges::back_inserter(out)), int>);
        CPP_assert(!equality_comparable<decltype(ranges::back_inserter(out))>);
        copy(rng, ranges::back_inserter(out));
        ::check_equal(out, {1, 2, 3, 4});
    }

    {
        auto rng2 = views::counted(rgi, 7)
          | views::adjacent_remove_if([&](int i, int j) { return i == j; });
        has_type<int const &>(*begin(rng2));
        CPP_assert(view_<decltype(rng2)>);
        CPP_assert(forward_range<decltype(rng2)>);
        CPP_assert(common_range<decltype(rng2)>);
        CPP_assert(!sized_range<decltype(rng2)>);
        CPP_assert(bidirectional_iterator<decltype(begin(rng2))>);
        CPP_assert(!random_access_iterator<decltype(begin(rng2))>);
        ::check_equal(rng2, {1, 2, 3, 4});
    }

    {
        auto rng3 = views::counted(ForwardIterator<int const*>(rgi), 7)
            | views::adjacent_remove_if(std::equal_to<int>{});
        has_type<int const &>(*begin(rng3));
        CPP_assert(view_<decltype(rng3)>);
        CPP_assert(forward_range<decltype(rng3)>);
        CPP_assert(!common_range<decltype(rng3)>);
        CPP_assert(!sized_range<decltype(rng3)>);
        CPP_assert(forward_iterator<decltype(begin(rng3))>);
        CPP_assert(!bidirectional_iterator<decltype(begin(rng3))>);
        ::check_equal(rng3, {1, 2, 3, 4});
    }

    {
        auto rng4 = views::counted(ForwardIterator<int const*>(rgi), 7)
          | views::adjacent_remove_if([](int,int){return true;});
        has_type<int const &>(*begin(rng4));
        CHECK(*begin(rng4) == 4);
        CPP_assert(view_<decltype(rng4)>);
        CPP_assert(forward_range<decltype(rng4)>);
        CPP_assert(!common_range<decltype(rng4)>);
        CPP_assert(!sized_range<decltype(rng4)>);
        CPP_assert(forward_iterator<decltype(begin(rng4))>);
        CPP_assert(!bidirectional_iterator<decltype(begin(rng4))>);
        ::check_equal(rng4, {4});
    }

    {
        auto is_odd_then_even = [](int i, int j){return 1==i%2 && 0 == j%2;};
        auto rng5 = views::iota(0, 11) | views::adjacent_remove_if(is_odd_then_even);
        has_type<int>(*begin(rng5));
        CPP_assert(view_<decltype(rng5)>);
        CPP_assert(forward_range<decltype(rng5)>);
        CPP_assert(common_range<decltype(rng5)>);
        CPP_assert(!sized_range<decltype(rng5)>);
        CPP_assert(bidirectional_iterator<decltype(begin(rng5))>);
        CPP_assert(!random_access_iterator<decltype(begin(rng5))>);
        ::check_equal(rng5, {0,2,4,6,8,10});
    }

    {
        // Verify that forward and backward traversal both select the same elements.
        auto rng = views::adjacent_remove_if(rgi, std::equal_to<int>{});
        std::vector<int const*> pointers;
        for(auto& i : rng)
            pointers.push_back(&i);
        auto pos = ranges::end(rng);
        for(auto i = pointers.size(); i != 0;)
        {
            CHECK(pos != ranges::begin(rng));
            CHECK(&*--pos == pointers[--i]);
        }
        CHECK(pos == ranges::begin(rng));
    }

    return test_result();
}