-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_average.cpp
More file actions
327 lines (294 loc) · 9.93 KB
/
calculate_average.cpp
File metadata and controls
327 lines (294 loc) · 9.93 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <chrono>
#include <vector>
#include <algorithm>
#include <thread>
#include <array>
#include <filesystem>
// Generated with gperf using the station names inside the create_measurements.cpp
struct PerfectHash
{
std::size_t operator()(const std::string &str) const noexcept
{
static std::uint16_t asso_values[] =
{
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 110, 1269, 1269, 1269, 1269, 1269, 1269, 0,
1269, 0, 1269, 1269, 1269, 0, 0, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 145, 10, 240, 65, 175,
33, 413, 320, 280, 30, 190, 325, 0, 340, 335,
170, 1269, 385, 105, 115, 40, 456, 190, 0, 340,
0, 1269, 1269, 1269, 1269, 1269, 1269, 0, 280, 280,
310, 15, 15, 250, 225, 5, 210, 35, 25, 90,
0, 30, 325, 5, 5, 20, 105, 200, 275, 315,
5, 260, 130, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 0, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 5, 325,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 20, 1269, 1269, 1269, 10, 1269,
1269, 1269, 1269, 1269, 1269, 5, 10, 0, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269, 1269,
1269, 1269, 1269, 1269, 1269, 1269};
auto len = str.length();
auto hval = len;
switch (hval)
{
default:
hval += asso_values[static_cast<std::uint8_t>(str[4])];
/*FALLTHROUGH*/
case 4:
case 3:
hval += asso_values[static_cast<std::uint8_t>(str[2])];
/*FALLTHROUGH*/
case 2:
case 1:
hval += asso_values[static_cast<std::uint8_t>(str[0])];
break;
}
return hval + asso_values[static_cast<std::uint8_t>(str[len - 1])];
}
};
struct Measurements
{
using ValType = std::int32_t;
inline auto record(ValType measurement) noexcept
{
if (_count == 0)
{
_min = measurement;
_max = measurement;
_sum = measurement;
_count = 1;
}
else
{
_min = std::min(_min, measurement);
_max = std::max(_max, measurement);
_sum += measurement;
_count++;
}
}
inline auto roundToPositive(double n) const noexcept
{
// std::round(1) uses "round to zero" but Java's Math.round(1) uses "round to positive/round to infinity"
return std::floor(n + 0.5);
}
inline auto round(double n) const noexcept
{
// Round to nearest tenths place
return roundToPositive(n * 10.0) / 10.0;
}
inline auto mean() const noexcept
{
return round(static_cast<double>(_sum) / 10.0 / _count);
}
inline auto min() const noexcept
{
return round(static_cast<double>(_min) / 10.0);
}
inline auto max() const noexcept
{
return round(static_cast<double>(_max) / 10.0);
}
inline auto merge(const Measurements &measurements) noexcept
{
if (_count == 0)
{
*this = measurements;
}
else
{
_min = std::min(_min, measurements._min);
_max = std::max(_max, measurements._max);
_sum += measurements._sum;
_count += measurements._count;
}
}
friend auto operator<<(std::ostream &os, const Measurements &measurements) noexcept -> std::ostream &
{
return os << measurements.min() << '/'
<< measurements.mean() << '/'
<< measurements.max();
}
private:
ValType _count = 0, _min = 0, _max = 0, _sum = 0;
};
class Parser
{
enum ParserStatus
{
StationName,
Measurement,
PositiveMeasurement,
NegativeMeasurement
};
std::string _station;
Measurements::ValType _measurement = 0;
ParserStatus _status = ParserStatus::StationName;
public:
template <typename InputIterator, typename Map>
auto operator()(InputIterator begin, InputIterator end, Map &stations) noexcept
{
for (auto it = begin; it != end; it++)
{
char c = *it;
switch (_status)
{
case ParserStatus::StationName:
{
switch (c)
{
case ';':
_status = ParserStatus::Measurement;
break;
default:
_station += c;
}
}
break;
case ParserStatus::Measurement:
{
switch (c)
{
case '-':
_status = ParserStatus::NegativeMeasurement;
_measurement = 0;
break;
default:
_status = ParserStatus::PositiveMeasurement;
_measurement = static_cast<Measurements::ValType>(c - '0');
}
}
break;
case ParserStatus::PositiveMeasurement:
{
switch (c)
{
case '\n':
_status = ParserStatus::StationName;
stations[_station].record(_measurement);
_station.clear();
break;
case '.':
break;
default:
_measurement = _measurement * 10 + static_cast<Measurements::ValType>(c - '0');
}
}
break;
case ParserStatus::NegativeMeasurement:
{
switch (c)
{
case '\n':
_status = ParserStatus::StationName;
stations[_station].record(_measurement);
_station.clear();
break;
case '.':
break;
default:
_measurement = _measurement * 10 - static_cast<Measurements::ValType>(c - '0');
}
}
}
}
}
};
using MapType = std::unordered_map<std::string, Measurements, PerfectHash>;
static constexpr auto chunkSize = 1 << 12;
static constexpr auto fileName = "measurements.txt";
auto process(int index, int numberOfThreads, std::uintmax_t fileSize, MapType &stations) noexcept
{
// Compute the chunk start and size
auto partSize = (fileSize + numberOfThreads - 1) / numberOfThreads; // ceiling
auto partStart = partSize * index;
auto partEnd = std::min(partStart + partSize, fileSize);
// Adjust the part start and end pointers
auto file = std::ifstream{fileName, std::ios::binary};
if (index + 1 < numberOfThreads)
{
// Adjust partEnd to align after '\n' character
for (char c; file.seekg(partEnd - 1), file.read(&c, 1), c != '\n'; partEnd++)
{
}
}
if (0 < index)
{
// Adjust partStart to align after '\n' character
for (char c; file.seekg(partStart - 1), file.read(&c, 1), c != '\n'; partStart++)
{
}
}
// Read chunks of memory
auto parser = Parser{};
auto chunk = std::array<char, chunkSize>{};
// Move file pointer to partStart
file.seekg(partStart);
// Read each line using the format: <station>;<measurement>\n
for (auto current = partStart; current < partEnd; current += chunkSize)
{
auto size = std::min(static_cast<std::uintmax_t>(chunkSize), partEnd - current);
file.read(chunk.data(), size);
parser(chunk.cbegin(), chunk.cbegin() + size, stations);
}
}
int main()
{
auto stations = MapType{};
// Read the file using threads
auto fileSize = std::filesystem::file_size(fileName);
auto threads = std::vector<std::thread>{};
auto numberOfThreads = static_cast<int>(std::thread::hardware_concurrency());
auto stationMaps = std::vector<MapType>{static_cast<std::size_t>(numberOfThreads)};
for (auto i = 0; i < numberOfThreads; i++)
{
threads.push_back(std::thread{process, i, numberOfThreads, fileSize, std::ref(stationMaps.at(i))});
}
// Wait for the threads to finish
for (auto &thread : threads)
{
thread.join();
}
// Merge stations
for (const auto &map : stationMaps)
{
for (const auto &[key, measurements] : map)
{
stations[key].merge(measurements);
}
}
// Copy and sort the station names
auto keys = std::vector<std::string>{};
for (const auto &pair : stations)
{
keys.push_back(pair.first);
}
std::sort(keys.begin(), keys.end());
// Print each station summary using the format: <station>=<min>/<mean>/<max>
std::cout << '{' << std::fixed << std::setprecision(1);
for (auto it = keys.cbegin(); it != keys.cend(); it++)
{
const auto &key = *it;
std::cout << key << '=' << stations.at(key);
if (it + 1 != keys.cend())
{
std::cout << ", ";
}
}
std::cout << '}';
return 0;
}