-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_function.cpp
36 lines (31 loc) · 1.28 KB
/
aggregate_function.cpp
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
#include "aggregate_functions.h"
#include <algorithm>
#include <numeric>
double AggregateFunctions::avg(const std::vector<std::unordered_map<std::string, std::string>>& data, const std::string& column) {
double total = sum(data, column);
return total / data.size();
}
int AggregateFunctions::count(const std::vector<std::unordered_map<std::string, std::string>>& data) {
return data.size();
}
int AggregateFunctions::max(const std::vector<std::unordered_map<std::string, std::string>>& data, const std::string& column) {
int max_value = std::numeric_limits<int>::min();
for (const auto& record : data) {
max_value = std::max(max_value, std::stoi(record.at(column)));
}
return max_value;
}
int AggregateFunctions::min(const std::vector<std::unordered_map<std::string, std::string>>& data, const std::string& column) {
int min_value = std::numeric_limits<int>::max();
for (const auto& record : data) {
min_value = std::min(min_value, std::stoi(record.at(column)));
}
return min_value;
}
int AggregateFunctions::sum(const std::vector<std::unordered_map<std::string, std::string>>& data, const std::string& column) {
int total = 0;
for (const auto& record : data) {
total += std::stoi(record.at(column));
}
return total;
}