-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppLinq.cpp
More file actions
201 lines (152 loc) · 3.96 KB
/
CppLinq.cpp
File metadata and controls
201 lines (152 loc) · 3.96 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
// CppLinq.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
template<class T>
class me_vector : public std::vector<T>
{
public:
me_vector Select()
{
return *this;
}
me_vector Select(std::function<bool(const T&)> predicate)
{
return this->Where(predicate);
}
template<class TOut>
me_vector<TOut> Select(std::function<TOut(const T&)> predicate)
{
if (this->size() == 0)
{
return me_vector<TOut>();
}
me_vector<TOut> output;
auto predicateFunc = [&output, predicate](const T& x)
{
TOut outItem = predicate(x);
output.push_back(outItem);
};
std::for_each(this->begin(), this->end(), predicateFunc);
return output;
}
T First()
{
me_vector_const_iterator it = this->begin();
if (this->size() == 0 || it == this->end())
{
throw std::string("No elements found in sequence.");
}
return *it;
}
T First(std::function<bool(const T&)> predicate)
{
return this->Where(predicate).First();
}
me_vector Skip(unsigned int count)
{
if (count > this->size() - 1)
{
return me_vector();
}
me_vector output = this->IterateOver(this->begin() + count, this->end());
return output;
}
me_vector Take(unsigned int count)
{
if (count > this->size() - 1)
{
return *this;
}
me_vector output = this->IterateOver(this->begin(), this->begin() + count);
return output;
}
me_vector Where(std::function<bool(const T&)> predicate)
{
if (this->size() == 0)
{
return *this;
}
me_vector output;
auto predicateFunc = [&output, predicate](const T& x)
{
if (predicate(x))
{
output.push_back(x);
}
};
std::for_each(this->begin(), this->end(), predicateFunc);
return output;
}
private:
typedef typename me_vector<T>::const_iterator me_vector_const_iterator;
me_vector IterateOver(me_vector_const_iterator start, me_vector_const_iterator finish)
{
me_vector output;
for (auto it = start; it != finish; ++it)
{
output.push_back(*it);
}
return output;
}
};
class ProjectionTarget
{
public:
bool isEven;
int value;
};
void PrintNumbers(std::string pre, me_vector<int> numbers)
{
std::cout << pre.c_str();
std::for_each(numbers.begin(), numbers.end(), [](int x) { std::cout << x << " "; });
}
void PrintProjectionTargets(std::string pre, me_vector<ProjectionTarget> projectionTargets)
{
std::cout << pre.c_str() << std::endl;
std::for_each(projectionTargets.begin(), projectionTargets.end(), [](ProjectionTarget projectionTarget)
{
std::cout << "{ value: " << projectionTarget.value << ", isEven: " << (projectionTarget.isEven ? "yes" : "no") << " }" << std::endl;
});
}
int _tmain(int argc, _TCHAR* argv[])
{
me_vector<int> numbers;
for (int x = 1; x <= 30; x++)
{
numbers.push_back(x);
}
/* ===== EVEN & ODD ===== */
auto evenPredicate = [](int x) { return x % 2 == 0; };
auto evenNumbers = numbers.Where(evenPredicate);
PrintNumbers("Even numbers: ", evenNumbers);
std::cout << std::endl;
auto oddPredicate = [](int x) { return x % 2 != 0; };
auto oddNumbers = numbers.Where(oddPredicate);
PrintNumbers("Odd numbers: ", oddNumbers);
std::cout << std::endl << std::endl;
/* ===== SKIP & TAKE ===== */
auto skipTake = numbers.Skip(10).Take(15);
PrintNumbers("Skip 10, Take 15: ", skipTake);
std::cout << std::endl << std::endl;
/* ===== CONDITIONAL SELECT ===== */
auto conditionalSelectPredicate = [](int x) { return x >= 20; };
auto greaterThan20 = numbers.Select(conditionalSelectPredicate);
PrintNumbers("Greater Than 20: ", greaterThan20);
std::cout << std::endl << std::endl;
/* ===== SELECT PROJECTION ===== */
auto projectionPredicate = [](int x)
{
ProjectionTarget target;
target.value = x;
target.isEven = x % 2 == 0;
return target;
};
auto projections = numbers.Select<ProjectionTarget>(projectionPredicate);
PrintProjectionTargets("Projections: ", projections);
std::cout << std::endl << std::endl << std::endl << std::endl;
return 0;
}