-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_Ext.h
More file actions
286 lines (228 loc) · 9.7 KB
/
Vector_Ext.h
File metadata and controls
286 lines (228 loc) · 9.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef JUL_VECTOR_EXT_H
#define JUL_VECTOR_EXT_H
/*
MIT License
Copyright(c) 2019 Julian Steigerwald
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright noticeand this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <vector>
#include <algorithm>
#include <cassert>
#include <numeric>
namespace jul
{
// ---------------------------------------------------------------------------------
// Flatten a vector of vectors.
// Example:
// std::vector<int> a{ 1,2,3 }, b{ 4,5,6 };
// std::vector<std::vector<int>> vec_vec { a, b };
//
// auto flattened = jul::flatten(vec_vec);
// => flattened = { 1, 2, 3, 4, 5, 6 }
// ---------------------------------------------------------------------------------
template <typename T>
std::vector<T> flatten(const std::vector<std::vector<T>>& v)
{
auto total_size = 0U;
for (const auto& sub : v) {
total_size += sub.size();
}
std::vector<T> result;
result.reserve(total_size);
for (const auto& sub : v) {
result.insert(std::end(result), std::begin(sub), std::end(sub));
}
return result;
}
// ---------------------------------------------------------------------------------
// Does a vector contain a certain value?
// example:
// std::vector<int> ints = { 0,1,2,3 };
// bool has_two = contains(ints, 2);
//
// => has_two = true
// ---------------------------------------------------------------------------------
template <class T>
bool contains(const std::vector<T>& v, const T& value)
{
auto first = std::begin(v);
auto last = std::end(v);
return std::find(first, last, value) != last;
}
// ---------------------------------------------------------------------------------
// Removes all values from a vector.
// example:
// std::vector<int> ints = { 0,1,0,3 };
// remove_all(ints, 0);
//
// => ints = {1,3}
// ---------------------------------------------------------------------------------
template <class T>
void remove_all(std::vector<T>& v, T value)
{
v.erase(std::remove(std::begin(v), std::end(v), value), std::end(v));
}
// ---------------------------------------------------------------------------------
// Removes all values from a vector and returns a copy.
// example:
// std::vector<int> ints = { 0,1,0,3 };
// auto without_zero = removed_all(ints, 0);
//
// => without_zero = {1,3}
// ---------------------------------------------------------------------------------
template <class T>
std::vector<T> removed_all(std::vector<T> v, T value)
{
v.erase(std::remove(std::begin(v), std::end(v), value), std::end(v));
return v;
}
// ---------------------------------------------------------------------------------
// Wrapper for std::transform for a complete vector.
// ---------------------------------------------------------------------------------
template <class T, class Function>
void apply_each(std::vector<T>& v, Function&& fn)
{
std::transform(std::begin(v), std::end(v), std::begin(v), fn);
}
// ---------------------------------------------------------------------------------
// Returns the max value of the vector.
// Precondition: vector can't be empty!
// ---------------------------------------------------------------------------------
template <class T>
T max_value(const std::vector<T>& v)
{
assert(std::size(v) != 0);
return *std::max_element(std::begin(v), std::end(v));
}
// ---------------------------------------------------------------------------------
// Returns the max value of the vector with a custom compare function.
// Precondition: vector can't be empty!
// ---------------------------------------------------------------------------------
template <class T, class Compare>
T max_value(const std::vector<T>& v, Compare&& comp)
{
assert(std::size(v) != 0);
return *std::max_element(std::begin(v), std::end(v), comp);
}
// ---------------------------------------------------------------------------------
// Returns the min value of the vector.
// Precondition: vector can't be empty!
// ---------------------------------------------------------------------------------
template <class T>
T min_value(const std::vector<T>& v)
{
assert(std::size(v) != 0);
return *std::min_element(std::begin(v), std::end(v));
}
// ---------------------------------------------------------------------------------
// Returns the int value of the vector with a custom compare function.
// Precondition: vector can't be empty!
// ---------------------------------------------------------------------------------
template <class T, class Compare>
T min_value(const std::vector<T>& v, Compare&& comp)
{
assert(std::size(v) != 0);
return *std::min_element(std::begin(v), std::end(v), comp);
}
// ---------------------------------------------------------------------------------
// Get a copy of all elements within limits.
// Example:
// std::vector<int> ints = { 0,1,0,3 };
// auto limited = within_limits(ints, 0, 1);
//
// => limited = {0,1,0}
// ---------------------------------------------------------------------------------
template <class T>
std::vector<T> within_limits(std::vector<T> values, T min, T max)
{
auto filter = [=](T value) {return value < min || value > max;};
values.erase(std::remove_if(std::begin(values), std::end(values), filter), std::end(values));
return values;
}
// ---------------------------------------------------------------------------------
// Get a copy of all elements outside limits.
// Example:
// std::vector<int> ints = { 0,1,0,3 };
// auto outside = out_of_limits(ints, 0, 1);
//
// => outside = {3}
// ---------------------------------------------------------------------------------
template <class T>
std::vector<T> out_of_limits(std::vector<T> values, T min, T max)
{
auto filter = [=](T value) {return value >= min && value <= max;};
values.erase(std::remove_if(std::begin(values), std::end(values), filter), std::end(values));
return values;
}
// ---------------------------------------------------------------------------------
// Creates a std::vector<T> filled with (ascending) values.
// Example:
// auto vec = jul::make_vector(-3, 3);
// vec == { -3, -2, -1, 0, 1, 2, 3 }
// ---------------------------------------------------------------------------------
template <class T>
constexpr std::vector<T> make_vector(const T& start_value, const T& end_value)
{
auto size = (end_value - start_value) + 1; // including the right border!
assert(size >= 0 && "Invalid range!");
std::vector<T> range(size);
std::iota(std::begin(range), std::end(range), start_value);
return range;
}
// ---------------------------------------------------------------------------------
// Creates a std::vector<T> filled with (descending) values.
// Example:
// auto vec = jul::make_rvector(0, 4);
// vec == { 4, 3, 2, 1, 0 }
// ---------------------------------------------------------------------------------
template <class T>
constexpr std::vector<T> make_rvector(const T& end_value, const T& start_value)
{
auto size = (end_value - start_value) + 1; // including the right border!
assert(size >= 0 && "Invalid range!");
std::vector<T> range(size);
std::iota(std::rbegin(range), std::rend(range), start_value);
return range;
}
// ---------------------------------------------------------------------------------
// Calculate the mean value of some values.
// ---------------------------------------------------------------------------------
template <class T>
double mean(const std::vector<T>& values)
{
auto beg = std::begin(values);
auto end = std::end(values);
assert(beg != end && "Calculation of mean is not possible on empty range!");
const double sum = static_cast<double>(std::accumulate(beg, end, T {0}));
const double count = std::size(values);
return sum / count;
}
// ---------------------------------------------------------------------------------
// Calculate the standard_deviation of some values.
// ---------------------------------------------------------------------------------
template <class T>
double standard_deviation(const std::vector<T>& values)
{
const double m = mean(values);
double sum = 0.0;
std::for_each(std::begin(values), std::end(values), [&](const double d) {
sum += (d - m) * (d - m);
});
return static_cast<double>(sqrt(sum / (values.size() - 1)));
}
}
#endif // JUL_VECTOR_EXT_H