-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUFOSightings.h
186 lines (154 loc) · 4.97 KB
/
UFOSightings.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
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
//
// Created by Brianna Yanqui on 11/20/23.
//
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <utility>
#include <fstream>
#include <sstream>
using namespace std;
#ifndef P3_UFOSIGHTING_H
#define P3_UFOSIGHTING_H
//CITY-DATES = KEYS
class UFOSightings{
private:
public:
struct Sighting {
string city, date_time, desc, shape, length;
pair<long double, long double> coords;
Sighting() : city(""), date_time(""), desc(""), shape(""), length(""), coords({ 0.0, 0.0 }) {}
<<<<<<< Updated upstream
Sighting(string& _city, string& _dateTime, string& _desc, string& _length, pair<long double, long double> _coords)
: city(_city), date_time(_dateTime), desc(_desc), length(_length), coords(_coords) {}
=======
Sighting(string& _city, string& _dateTime, string& _desc, string& _length, pair<long double, long double> _coords)
: city(_city), date_time(_dateTime), desc(_desc), length(_length), coords(_coords) {}
>>>>>>> Stashed changes
};
struct HashPairString {
size_t operator()(const pair<string, string>& p) const {
size_t h1 = hash<string>{}(p.first);
size_t h2 = hash<string>{}(p.second);
return h1 ^ (h2 << 1); //xor
}
};
vector<pair<string, string>> v; //vector of keys for fast traversal
unordered_map<pair<string, string>, Sighting, HashPairString> m; //map of keys
//read CSV; construct sightings; fill map+vec
void parseFile(string filePath, int n); //does n lines
<<<<<<< Updated upstream
//Getters
vector<pair<string, string>> getCityDates();
string getData(const string field, const pair<string, string>& key);
=======
//Getters
vector<pair<string, string>> getCityDates();
string getData(const string field, const pair<string, string>& key);
pair<long double, long double> getLatLong(pair<string, string>);
>>>>>>> Stashed changes
};
string UFOSightings::getData(const string field, const pair<string, string>& key)
{
if(field == "dateTime")
return m[key].date_time;
if(field == "desc")
return m[key].desc;
if(field == "shape")
return m[key].shape;
if(field == "length")
return m[key].length;
if(field == "city")
return m[key].city;
if (field == "latitude")
return to_string(m[key].coords.first);
if (field == "longitude")
return to_string(m[key].coords.second); //Or long double getCoords()
else
return "-1";
}
<<<<<<< Updated upstream
// O(1)
pair<long double, long double> UFOSightings::getLatLong(string key)
{
return m[key].coords;
}
=======
>>>>>>> Stashed changes
/*
void UFOSighting::setLatitude(const string& key, double latitude) {
m[key].coords.first = latitude;
}
void UFOSighting::setLongitude(const string& key, double longitude) {
m[key].coords.second = longitude;
}
void UFOSighting::setCity(const string& key, const string& city) {
m[key].city = city;
}
void UFOSighting::setTime(const string& key, const string& time) {
m[key].time = time;
}
void UFOSighting::setDescription(const string& key, const string& desc) {
m[key].desc = desc;
}
void UFOSighting::setDescription(const string& key, const string& desc) {
m[key].desc = desc;
}
void UFOSighting::setLength(const string& key, const string& length) {
m[key].length = length;
}
*/
vector<pair<string, string>> UFOSightings::getCityDates()
{
return v;
}
void UFOSightings::parseFile(string filePath, int n)
{
ifstream fs;
fs.open(filePath);
if (!fs.is_open()) {
cout << "CAN'T OPEN";
return;
}
string line;
int count = -1; //
while (getline(fs, line, '\r')) { //wow \r is crazy
count++;
if (count == 0)
continue;
stringstream ss(line);
string field;
string fields[11];
for (int i = 0; i < 11; i++) {
if (i < 10)
getline(ss, field, ',');
else //Last field in line
getline(ss, field, ' ');
fields[i] = field;
//cout << field << "|"; //
}
//Date_time,city,state/proxvince,country,UFO_shape,length_of_encounter_seconds,described_duration_of_encounter,description,date_documented,latitude,longitude
string date_time = fields[0];
string city = fields[1];
string state = fields[2];
string desc = fields[7];
string length = fields[5];
long double lati = stold(fields[9]);
long double longi = stold(fields[10]);
pair<long double, long double> coords = { lati, longi };
pair<string, string> key = { city, date_time };
v.push_back(key);
m[key] = Sighting(city, date_time, desc, length, coords);
if (count == n)
break;
}
}
<<<<<<< Updated upstream
=======
pair<long double, long double> UFOSightings::getLatLong(pair<string, string> key)
{
return m[key].coords;
}
>>>>>>> Stashed changes
#endif //P3_UFOSIGHTING_H