-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinqCore.h
163 lines (130 loc) · 4.3 KB
/
LinqCore.h
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
//
// Created by angel on 01/02/17.
//
#ifndef NATIVELINQ_LINQCORE_H
#define NATIVELINQ_LINQCORE_H
#include "CommonHeaders.h"
// Enumerable methods
// https://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods(v=vs.110).aspx
template <class T>
class LinqCore {
public:
/*
* Filters a sequence of values based on a predicate
*/
static std::vector<T> Where(std::vector<T> collection, std::function<bool(T)> condition) {
std::vector<T> retval;
if (collection.size() > 0 && condition != nullptr) {
for (auto &p : collection) {
if (condition(p))
retval.push_back(p);
}
}
return retval;
}
/*
* Returns a specified number of contiguous elements from the start of a sequence
*/
static std::vector<T> Take(std::vector<T> collection, int count, std::function<bool(T)> condition) {
auto index = 0;
std::vector<T> retval;
count = count < 0 ? 1 : count;
if (collection.size() > 0 && condition != nullptr) {
for (auto &p : collection) {
if (condition(p)) {
retval.push_back(p);
if (++index == count)
break;
}
}
}
return retval;
}
/*
* Returns a number that represents how many elements in the specified sequence satisfy a condition
*/
static int Count(std::vector<T> collection, std::function<bool(T)> condition) {
auto retval = 0;
if (collection.size() > 0 && condition != nullptr) {
for (auto &p : collection) {
if (condition(p))
retval++;
}
return retval;
}
}
/*
* Computes the average of a sequence of Decimal or Float values
*/
static T Average(std::vector<T> collection) {
long length = 0;
T retval = 0, sum = 0;
auto type = std::string(typeid(T).name());
// we process float and double only
if (type == "f" || type == "d" ) {
if ((length = collection.size()) > 0) {
for (auto &p : collection) {
sum += p;
}
retval = sum / length;
}
}
return retval;
}
/*
* Casts the elements of an array to a std::vector
*/
static std::vector<T> Cast(T source[], int size) {
std::vector<T> retval;
try {
for (auto index = 0; index < size; index++)
retval.push_back(source[index]);
} catch(std::exception &ex) {
// just to handle any undefined behavior (e.g.: such as bad::alloc)
}
return retval;
}
/*
* Returns the maximum value in a sequence of Float, Decimal or Integer values
*/
static T Max(std::vector<T> collection) {
T retval = 0;
auto type = std::string(typeid(T).name());
auto comparison = [&](T a, T b) -> bool {return a < b; };
// we process float, double and integer only
if (type == "f" || type == "d" || type == "i") {
retval = *std::max_element(collection.begin(), collection.end(), comparison);
}
return retval;
}
/*
* Returns the minimum value in a sequence of Float, Decimal or Integer values
*/
static T Min(std::vector<T> collection) {
T retval = 0;
auto type = std::string(typeid(T).name());
auto comparison = [&](T a, T b) -> bool {return a < b; };
// we process float, double and integer only
if (type == "f" || type == "d" || type == "i") {
retval = *std::min_element(collection.begin(), collection.end(), comparison);
}
return retval;
}
/*
* Computes the sum of a sequence of Float, Decimal or Integer values.
*/
static T Sum(std::vector<T> collection) {
T retval = 0;
auto type = std::string(typeid(T).name());
// we process float, double and integer only
if (type == "f" || type == "d" || type == "i") {
if (collection.size() > 0) {
for (auto &p : collection) {
retval += p;
}
}
}
return retval;
}
};
#endif //NATIVELINQ_LINQCORE_H