-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLINQc.h
84 lines (71 loc) · 1.52 KB
/
LINQc.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
// Written by @sebight
// C# Linq implementation for C++
// July 2024
#pragma once
#include <vector>
#define COND(vars, cond) [](vars) { return cond; }
#define LINQ(vars, cond) COND(vars, cond)
namespace linqc {
template<typename T>
class vec : public std::vector<T> {
public:
// Internal vec
typedef std::vector<T> ivec;
using ivec::vector;
typedef T type;
T& operator[](int i) { return ivec::at(i); }
const T& operator[](int i) const { return ivec::at(i); }
template<typename LFunc>
vec<type> Where(LFunc cond) {
vec<type> passed;
for (auto it = this->cbegin(); it != this->cend(); ++it) {
if (cond(*it)) {
passed.push_back(*it);
}
}
return passed;
}
template<typename LFunc>
vec<type> Select(LFunc cond) {
vec<type> res;
for (auto it = this->cbegin(); it != this->cend(); ++it) {
res.push_back(cond(*it));
}
return res;
}
size_t Sum() {
size_t s = 0;
for (auto it = this->cbegin(); it != this->cend(); ++it) {
s += *it;
}
return s;
}
type Max() {
type max = INT64_MIN;
for (auto it = this->cbegin(); it != this->cend(); ++it) {
if (*it > max) {
max = *it;
}
}
return max;
}
size_t Count() {
return ivec::size();
}
template<typename LFunc>
vec<type> OrderBy(LFunc cond) {
// TODO: Finish impl
return vec<type>;
}
template<typename LFunc>
bool Any(LFunc cond)
{
for (auto it = this->cbegin(); it != this->cend(); ++it) {
if (cond(*it)) {
return true;
}
}
return false;
}
};
}