-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.h
222 lines (189 loc) · 6.12 KB
/
stream.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
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
#pragma once
#undef min
#undef max
#include <algorithm>
#include <numeric>
#include <ranges>
template<typename T>
struct StreamImpl
{
T View{};
/// Transforms each element in the view using the provided function.
constexpr auto Map(auto Func)
{
auto NewView = View | std::views::transform(Func);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Applies a function to each element of the view but returns the original value.
constexpr auto Each(auto Func)
{
return Map([Func](auto Value)
{
Func(Value);
return Value;
});
}
/// Reduces the view to a single value using the provided function.
constexpr auto Reduce(auto InitialValue, auto Func)
{
auto Result = std::accumulate(View.begin(), View.end(), InitialValue, Func);
auto NewView = std::views::single(Result);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Filters elements based on the provided predicate function.
constexpr auto Filter(auto Func)
{
auto NewView = View | std::views::filter(Func);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Filters out elements that do not satisfy the provided predicate function.
constexpr auto Reject(auto Func)
{
auto NewFunc = [Func](auto Value){ return !Func(Value); };
auto NewView = View | std::views::filter(NewFunc);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Takes the first 'Count' elements from the view.
constexpr auto Take(auto Count)
{
auto NewView = View | std::views::take(Count);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Splits the view by the given token.
constexpr auto SplitBy(auto Token)
{
auto NewView = View | std::views::split(Token);
return StreamImpl<decltype(NewView)>{NewView};
}
/// Joins nested ranges into a single range.
constexpr auto Join()
{
auto NewView = View | std::views::join;
return StreamImpl<decltype(NewView)>{NewView};
}
/// Returns the keys of the view.
constexpr auto Keys()
{
auto NewView = View | std::views::keys;
return StreamImpl<decltype(NewView)>{NewView};
}
/// Returns the values of the view.
constexpr auto Values()
{
auto NewView = View | std::views::values;
return StreamImpl<decltype(NewView)>{NewView};
}
/// Checks if all elements satisfy the provided predicate function.
constexpr auto All(auto Func)
{
auto FilteredData = Filter(Func);
return Count() == FilteredData.Count();
}
/// Checks if any element satisfies the provided predicate function.
constexpr auto Any(auto Func)
{
auto FilteredData = Filter(Func);
return FilteredData.Count() > 0;
}
/// Returns unique elements by counting occurrences and filtering out duplicates.
constexpr auto Uniq()
{
return WithIndex()
.Map([this](auto ValuePair)
{
auto [Index, Value] = ValuePair;
return std::pair{Take(Index).Count(Value), Value};
})
.Reject([](auto ValuePair)
{
auto [Count, Value] = ValuePair;
return Count > 0;
})
.Map([](auto ValuePair)
{
auto [Count, Value] = ValuePair;
return Value;
});
}
/// Pairs each element with its index.
constexpr auto WithIndex()
{
auto Iota = std::views::iota(0, Count());
auto NewView = Iota | std::views::transform([this](auto Index)
{
return std::pair(Index, *std::next(View.begin(), Index));
});
return StreamImpl<decltype(NewView)>{NewView};
}
/// Returns the minimum element in the view.
constexpr auto Min()
{
return *std::ranges::min_element(View.begin(), View.end());
}
/// Returns the maximum element in the view.
constexpr auto Max()
{
return *std::ranges::max_element(View.begin(), View.end());
}
/// Returns the sum of all elements in the view.
constexpr auto Sum()
{
return std::accumulate(View.begin(), View.end(), 0);
}
/// Checks if the view contains a specific value.
constexpr auto Contains(auto Value)
{
return Count(Value) > 0;
}
/// Returns the number of elements in the view.
constexpr auto Count()
{
return std::ranges::distance(View);
}
/// Counts the occurrences of a specific value in the view.
constexpr auto Count(auto Value)
{
return std::ranges::count(View, Value);
}
/// Chunks the view into sub ranges of a given size.
constexpr auto ChunkEvery(auto Count)
{
// TODO: Replace with std::views::chunk when used with C++23
auto Iota = std::views::iota(0, (std::ranges::distance(View) + Count - 1) / Count);
auto NewView = Iota | std::views::transform([this, Count](auto Index)
{
auto Begin = std::next(View.begin(), Index * Count);
auto End = std::next(Begin, std::min(static_cast<size_t>(Count), static_cast<size_t>(std::ranges::distance(Begin, View.end()))));
return std::ranges::subrange(Begin, End);
});
return StreamImpl<decltype(NewView)>{NewView};
}
/// Collects the view elements into a std::vector.
auto Collect()
{
std::vector<std::ranges::range_value_t<T>> Result;
// Reserve space if the view size is known (e.g., random access or sized ranges)
if constexpr (std::ranges::sized_range<T>)
{
Result.reserve(std::ranges::distance(View));
}
std::ranges::copy(View, std::back_inserter(Result));
return Result;
}
/// Runs the stream operations.
auto Run()
{
Collect();
}
};
namespace Stream
{
static constexpr auto of(auto Value)
{
return StreamImpl{Value};
}
static constexpr auto range(auto Begin, auto End)
{
return StreamImpl{std::views::iota(Begin, End + 1)};
}
}