-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (112 loc) · 3.69 KB
/
main.cpp
File metadata and controls
132 lines (112 loc) · 3.69 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
// main.cpp
#include "calcs/calc.hpp"
#include <queue>
#include <memory>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
#include <emscripten/bind.h>
using namespace emscripten;
class chart {
public:
double sa_points;
std::string song_name;
int level;
int score;
chart(std::string n, int s, int l) { //constructor
song_name = n;
level = l;
score = s;
sa_points = calculate_sa(level, score);
}
std::string formatPrint() const {
return song_name + "\t" + std::to_string(level) + "\t" + std::to_string(score) + "\t" + std::to_string(sa_points) + "\n";
}
};
std::vector<std::string> splitLines(const std::string& text) {
std::vector<std::string> lines;
std::stringstream ss(text);
std::string line;
while(std::getline(ss, line)) {
lines.push_back(line);
}
return lines;
}
std::vector<std::string> parseCSVLine(const std::string& line) {
std::vector<std::string> fields;
fields.push_back(line);
return fields;
}
struct CompareCharts {
bool operator()(const std::shared_ptr<chart>& a,const std::shared_ptr<chart>& b) const {
return a->sa_points > b->sa_points; // min-heap (largest sa stays)
}
};
// This function takes the entire CSV contents as one string and returns the output
std::string process_csv_string(const std::string& csv_contents) {
auto lines = splitLines(csv_contents);
std::priority_queue<std::shared_ptr<chart>, std::vector<std::shared_ptr<chart>>, CompareCharts> min_heap;
std::stack<std::shared_ptr<chart>> top30;
bool firstline = true;
for (const auto& line : lines) {
std::vector<std::string> fields = parseCSVLine(line);
for (const std::string& field : fields) {
if (!firstline) {
std::stringstream ss(field);
std::string segment;
std::vector<std::string> segments;
while (std::getline(ss, segment, '\t')) { // split by tab as in original
segments.push_back(segment);
}
// Defensive checks (in case CSV formatting is slightly different)
if (segments.size() < 5) {
continue;
}
std::string name = segments[1];
int level = 0;
int score = 0;
try {
level = std::stoi(segments[3]);
score = std::stoi(segments[4]);
} catch (...) {
// skip bad lines
continue;
}
auto c = std::make_shared<chart>(name, score, level);
if (min_heap.size() == 30) {
if (c->sa_points > min_heap.top()->sa_points) {
min_heap.pop();
min_heap.push(c);
}
} else {
min_heap.push(c);
}
} else {
firstline = false;
}
}
}
while(!min_heap.empty()) {
top30.push(min_heap.top());
min_heap.pop();
}
double sum = 0.0;
std::ostringstream out;
out << "Here are your top 30 songs used for SA calc: \n";
while(!top30.empty()) {
out << top30.top()->formatPrint();
sum += top30.top()->sa_points;
top30.pop();
}
double points = (sum / 30.0);
out << "\n \n";
out << "your total SA is: " << sum << ". And your avg SA in top 30 is: " << points << "\n";
return out.str();
}
// Bindings for embind
EMSCRIPTEN_BINDINGS(sa_module) {
emscripten::function("process_csv_string", &process_csv_string);
}