-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional_examples.cpp
More file actions
225 lines (189 loc) · 4.79 KB
/
Copy pathfunctional_examples.cpp
File metadata and controls
225 lines (189 loc) · 4.79 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "pch.h"
#include <functional>
#include <algorithm>
#include <array>
#include <map>
#include <iostream>
#include <iomanip>
//standard
template<typename NumberType>
void output(NumberType value)
{
std::cout << std::setw(3) << value << ",";
}
//standard
bool lesser_15(int element)
{
return element < 15;
}
//operator()
class TLesser {
private:
int iMax;
public:
TLesser() = delete;
TLesser(const TLesser&) = default;
TLesser(int v):iMax(v){}
bool operator()(int const& element) const {
return element < iMax;
}
};
class TOut {
int iCols;
int iWidth;
mutable int iCount;
public:
TOut(int cols, int width)
:iCols(cols), iWidth(width), iCount(0)
{}
void operator()(int const& element) const
{
if (iCount == 0) { std::cout << "\n"; }
std::cout << std::setw(iWidth) << element;
++iCount;
if (iCount%iCols == 0 ) { std::cout << "\n"; }
else{ std::cout << ","; }
}
};
template<int maxval>
constexpr bool t_lesser(int val) {
return val < maxval;
}
void test(
std::function<bool(int, int)> sortFct
)
{
auto tstBind = std::bind([](const int maxval, const int x)->bool {return x < maxval; }, 15, std::placeholders::_1);
std::vector<int> values = { 12,3,54,13,63,14,7,23,5,35,1,65,15,9,6,11,41 };
auto it = std::partition(values.begin(), values.end(), TLesser(15) );
auto it2 = std::partition(values.begin(), values.end(), tstBind );
auto it3 = std::partition(values.begin(), values.end(), t_lesser<15> );
std::for_each(values.begin(), values.end(), TOut(10,2));
std::cout << "\n";
std::sort(values.begin(), it, sortFct);
std::for_each(values.begin(), it, TOut(5,4) );
std::cout << "\n";
std::sort(it, values.end(), sortFct);
std::for_each(it, values.end(), TOut(5,4) );
std::cout << "\n";
}
static bool is_lesser(int l, int r) { return l < r; }
struct my_iterator {
using iterator_category = std::input_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using reference_type = value_type & ;
using pointer_type = value_type * ;
my_iterator()
{}
my_iterator(value_type p[]) : data(p)
{
//++*this;
}
//cpy
//assign
//++
//++(int)
//friend ==
//friend !=
private:
value_type* data=nullptr;
int start_pos = -1;
int end_pos = -1;
};
//ideen: fileiterator foreach(char in stream)
//ideen: reflection iterator, der über felder zählt
//ideen: sql-select mit ++
class MyArray {
private:
int data[5];
public:
my_iterator begin() { return my_iterator{data}; }
my_iterator end() { return my_iterator{}; };
};
TEST(functional_ex, main)
{
test(is_lesser);
}
template <typename iterator, typename Pred, typename Operation>
void for_each_if(iterator begin, iterator end, Pred pred, Operation op)
{
if (begin < end)
{
for (; begin != end; ++begin)
{
if (pred(*begin))
{
op(*begin);
}
}
}
else throw std::range_error("iterator begin isn't before iterator end");
return;
}
using NumberType = __int64;
bool is_odd(NumberType v) { return (v % 2) != 0; }
bool always(NumberType) { return true; }
void mult_2(NumberType& v) { v *= 2; }
TEST(functional_ex, combinations)
{
NumberType values[] = { 12,3,54,13,63,14,7,23,5,35,1,65,15,9,6,255,255,255,255 };
//constexpr size_t maxindex = (sizeof(values) / sizeof(int))-1LL;
auto* begin = std::begin(values); // &values[0];
auto* end = std::end(values);//!! gefährlich? std::end zeigt eins hinter array, &values[maxindex];
void* addressof_begin = &begin;
for_each_if(begin, end, always, output<NumberType>); std::cout << "\n";
for_each_if(begin, end, is_odd, mult_2);
for_each_if(begin, end, always, output<NumberType>); std::cout << "\n";
}
struct my_map_not_found{};
template<typename ty>
typename ty::mapped_type& my_map_find(
ty& container,
typename ty::key_type const& key)
{
auto it = container.find(key);
if (it != container.end())
{
return it->second;
}
else
{
throw my_map_not_found();
}
}
TEST(functional_ex, maps)
{
std::map<int, int> test = {
{1,20},{12,12}
};
try
{
auto& found = my_map_find(test, 12);
std::cout << found << " found\n";
}
catch (my_map_not_found& )
{
std::cout << "key not found\n";
}
}
#include <thread>
#include <future>
#include <mutex>
#include <atomic>
TEST(functional_ex, threading)
{
std::vector<int> values = { 12,3,54,13,63,14,7,23,5,35,1,65,15,9,6,11,41 };
auto it = std::partition(values.begin(), values.end(), is_odd);
for_each_if(values.begin(), values.end(), always, output<int>);
std::cout << "\n";
auto thread_func = [](auto begin, auto end, auto op) {
std::for_each(begin, end, op);
};
std::thread t1( thread_func,values.begin(), it, [](auto& val) mutable {val *= 2; } );
std::thread t2( thread_func, it, values.end(), [](auto& val) mutable {val *= 3; } );
t1.join(); // t1.detach(); //"laufen lassen", bis zum ProgrammEnde
t2.join();
for_each_if(values.begin(), values.end(), always, output<int>);
std::cout << "\n";
}