-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quakinator.h
87 lines (74 loc) · 2.5 KB
/
Quakinator.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
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <ctime>
#include <iomanip>
#include <chrono>
using namespace std;
struct Quakinator {
// 0: mag, 1: lon, 2: lat
static unordered_map<string, vector<double>> data; // Stores attributes of each earthquake, timestamp string is the key
static vector<string> keys; // Ordered list of the keys. Sorting will be done on this vector
static int criteriaIndex;
// Compares two earthquake keys based on the criteria to sort by
static bool compare(const string& a, const string& b) {
if (criteriaIndex >= 0)
return data[a][criteriaIndex] >= data[b][criteriaIndex];
return (a.compare(b) >= 0);
}
static int partition(int low, int high) {
string pivotKey = keys[low];
int up = low+1;
int down = high;
while (up <= down) {
while (compare(keys[up], pivotKey))
up++;
while (up <= down && (!compare(keys[down], pivotKey)))
down--;
if (up < down)
std::swap(keys[up], keys[down]);
}
std::swap(keys[low], keys[down]);
return down;
}
static void quickSort(int low, int high) {
if (low < high) {
int pivot = partition(low, high);
quickSort(low, pivot - 1);
quickSort(pivot + 1, high);
}
}
// Loads data into the hashtable
static void init() {
int count = 0;
cout << "\n";
for (int i = 1; i <= 7; i++) {
ifstream file;
file.open("data\\query_" + to_string(i) + ".csv");
cout << "Loading Earthquake(" << to_string(i) << ")...\n";
string line = "";
while(getline(file, line)) {
string time = "", temp = "";
double lat = -1, lon = -1, mag = -1;
stringstream tokens(line);
getline(tokens, time, ',');
getline(tokens, temp, ',');
lat = stod(temp);
getline(tokens, temp, ',');
lon = stod(temp);
getline(tokens, temp, ',');
mag = stod(temp);
count++;
data[time] = {mag,lon,lat};
keys.push_back(time);
line = "";
}
}
cout << count << " EQs loaded.\n\n";
}
};