-
Notifications
You must be signed in to change notification settings - Fork 51
/
range.example.cpp
150 lines (120 loc) · 3.68 KB
/
range.example.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// think-cell public library
//
// Copyright (C) 2016-2018 think-cell Software GmbH
//
// Distributed under 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
#include "tc/range/meta.h"
#include "tc/range/filter_adaptor.h"
#include "tc/string/format.h"
#include "tc/string/make_c_str.h"
#include <boost/range/adaptors.hpp>
#include <vector>
#include <cstdio>
namespace {
template <typename... Args>
void print(Args&&... args) noexcept {
std::printf("%s", tc::implicit_cast<char const*>(tc::make_c_str<char>(std::forward<Args>(args)...)));
}
//---- Basic ------------------------------------------------------------------------------------------------------------------
void basic () {
std::vector<int> v = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
tc::for_each(
tc::filter(v, [](const int& n){ return (n%2==0);}),
[&](auto const& n) {
print(tc::as_dec(n), ", ");
}
);
print("\n");
}
//---- Generator Range --------------------------------------------------------------------------------------------------------
namespace {
struct generator_range {
template< typename Func >
void operator()( Func func ) const& {
for(int i=0;i<50;++i) {
func(i);
}
}
};
}
void ex_generator_range () {
tc::for_each( tc::filter( generator_range(), [](int i){ return i%2==0; } ), [](int i) {
print(tc::as_dec(i), ", ");
});
print("\n");
}
//---- Generator Range (with break) -------------------------------------------------------------------------------------------
namespace {
struct generator_range_break {
template< typename Func >
tc::break_or_continue operator()( Func func ) const& {
using namespace tc;
for(int i=0;i<5000;++i) {
if (func(i)==break_) { return break_; }
}
return continue_;
}
};
}
void ex_generator_range_break () {
tc::for_each( tc::filter( generator_range_break(), [](int i){ return i%2==0; } ), [](int i) -> tc::break_or_continue {
print(tc::as_dec(i), ", ");
return (i>=50)? tc::break_ : tc::continue_;
});
print("\n");
}
//---- Stacked filters --------------------------------------------------------------------------------------------------------
void stacked_filters() {
tc::for_each( tc::filter( tc::filter( tc::filter(
generator_range_break(),
[](int i){ return i%2!=0; } ),
[](int i){ return i%3!=0; } ),
[](int i){ return i%5!=0; } )
, [](int i) -> tc::break_or_continue
{
print(tc::as_dec(i), ", ");
return (i>25)? tc::break_ : tc::continue_;
});
print("\n");
}
}
int main() {
print("-- Running Examples ----------\n");
basic();
ex_generator_range();
ex_generator_range_break();
stacked_filters();
using namespace tc;
int av[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
auto v = std::vector<int> (av, av+sizeof(av)/sizeof(int));
//---- filter example with iterators -------------------------------------------
auto r = tc::filter( tc::filter( tc::filter(
v,
[](int i){ return i%2!=0; } ),
[](int i){ return i%3!=0; } ),
[](int i){ return i%5!=0; } );
for (auto it = std::begin(r),
end = std::end(r);
it != end;
++it)
{
print(tc::as_dec(*it), ", ");
}
print("\n");
//---- boost for comparison -----------------------------------------------------
auto br = v | boost::adaptors::filtered([](int i){ return i%2!=0; })
| boost::adaptors::filtered([](int i){ return i%3!=0; })
| boost::adaptors::filtered([](int i){ return i%5!=0; });
for (auto it = std::begin(br),
end = std::end(br);
it != end;
++it)
{
print(tc::as_dec(*it), ", ");
}
print("\n");
print("-- Done ----------\n");
std::fflush(stdout);
return EXIT_SUCCESS;
}