10
10
* @param {std::string} id : location id
11
11
* @return {double} : latitude
12
12
*/
13
- double TrojanMap::GetLat (const std::string &id) {
13
+ double TrojanMap::GetLat (const std::string &id)
14
+ {
14
15
return 0 ;
15
16
}
16
17
@@ -21,7 +22,8 @@ double TrojanMap::GetLat(const std::string &id) {
21
22
* @param {std::string} id : location id
22
23
* @return {double} : longitude
23
24
*/
24
- double TrojanMap::GetLon (const std::string &id) {
25
+ double TrojanMap::GetLon (const std::string &id)
26
+ {
25
27
return 0 ;
26
28
}
27
29
@@ -32,7 +34,8 @@ double TrojanMap::GetLon(const std::string &id) {
32
34
* @param {std::string} id : location id
33
35
* @return {std::string} : name
34
36
*/
35
- std::string TrojanMap::GetName (const std::string &id) {
37
+ std::string TrojanMap::GetName (const std::string &id)
38
+ {
36
39
return " " ;
37
40
}
38
41
@@ -43,7 +46,8 @@ std::string TrojanMap::GetName(const std::string &id) {
43
46
* @param {std::string} id : location id
44
47
* @return {std::vector<std::string>} : neighbor ids
45
48
*/
46
- std::vector<std::string> TrojanMap::GetNeighborIDs (const std::string &id) {
49
+ std::vector<std::string> TrojanMap::GetNeighborIDs (const std::string &id)
50
+ {
47
51
return {};
48
52
}
49
53
@@ -55,7 +59,8 @@ std::vector<std::string> TrojanMap::GetNeighborIDs(const std::string &id) {
55
59
* @param {std::string} name : location name
56
60
* @return {std::string} : id
57
61
*/
58
- std::string TrojanMap::GetID (const std::string &name) {
62
+ std::string TrojanMap::GetID (const std::string &name)
63
+ {
59
64
std::string res = " " ;
60
65
return res;
61
66
}
@@ -67,8 +72,21 @@ std::string TrojanMap::GetID(const std::string &name) {
67
72
* @param {std::string} name : location name
68
73
* @return {std::pair<double,double>} : (lat, lon)
69
74
*/
70
- std::pair<double , double > TrojanMap::GetPosition (std::string name) {
75
+ std::pair<double , double > TrojanMap::GetPosition (std::string name)
76
+ {
71
77
std::pair<double , double > results (-1 , -1 );
78
+
79
+ for (const auto &entry : data)
80
+ {
81
+ const Node &node = entry.second ; // retrieve current node
82
+
83
+ // if node matches name
84
+ if (node.name == name)
85
+ {
86
+ results.first = node.lat ; // latitude
87
+ results.second = node.lon ; // longitude
88
+ }
89
+ }
72
90
return results;
73
91
}
74
92
@@ -78,7 +96,8 @@ std::pair<double, double> TrojanMap::GetPosition(std::string name) {
78
96
* @param {std::string} b : second string
79
97
* @return {int} : edit distance between two strings
80
98
*/
81
- int TrojanMap::CalculateEditDistance (std::string a, std::string b) {
99
+ int TrojanMap::CalculateEditDistance (std::string a, std::string b)
100
+ {
82
101
return 0 ;
83
102
}
84
103
@@ -89,7 +108,8 @@ int TrojanMap::CalculateEditDistance(std::string a, std::string b) {
89
108
* @param {std::string} name : location name
90
109
* @return {std::string} tmp : the closest name
91
110
*/
92
- std::string TrojanMap::FindClosestName (std::string name) {
111
+ std::string TrojanMap::FindClosestName (std::string name)
112
+ {
93
113
std::string tmp = " " ; // Start with a dummy word
94
114
return tmp;
95
115
}
@@ -101,8 +121,49 @@ std::string TrojanMap::FindClosestName(std::string name) {
101
121
* @param {std::string} name : partial name
102
122
* @return {std::vector<std::string>} : a vector of full names
103
123
*/
104
- std::vector<std::string> TrojanMap::Autocomplete (std::string name) {
124
+ std::vector<std::string> TrojanMap::Autocomplete (std::string name)
125
+ {
105
126
std::vector<std::string> results;
127
+
128
+ // test case: input empty --> return empty vector
129
+ if (name.empty ())
130
+ {
131
+ return results;
132
+ }
133
+
134
+ // convert input string to lowercase
135
+ for (size_t i = 0 ; i < name.length (); i++)
136
+ {
137
+ name[i] = tolower (name[i]); // found tolower() funciton on geeksforgeeks.org
138
+ }
139
+
140
+ // iterate through all nodes in data map
141
+ for (const auto &entry : data)
142
+ {
143
+ const Node &node = entry.second ; // retrieve current node
144
+ std::string node_name = node.name ; // retreieve name of current node
145
+
146
+ // convert node name --> lowercase
147
+ std::transform (node_name.begin (), node_name.end (), node_name.begin (), ::tolower);
148
+
149
+ // check if node name starts with string
150
+ bool match = true ; // assume initially matches
151
+ for (size_t i = 0 ; i < name.length (); i++)
152
+ {
153
+ if (node_name[i] != name[i]) // if does not match, switch to false
154
+ {
155
+ match = false ;
156
+ break ; // exit loop since mismatch found
157
+ }
158
+ }
159
+
160
+ // if match found, add to vector
161
+ if (match)
162
+ {
163
+ results.push_back (node.name );
164
+ }
165
+ }
166
+ // return vector list
106
167
return results;
107
168
}
108
169
@@ -112,7 +173,8 @@ std::vector<std::string> TrojanMap::Autocomplete(std::string name) {
112
173
*
113
174
* @return {std::vector<std::string>} : all unique location categories
114
175
*/
115
- std::vector<std::string> TrojanMap::GetAllCategories () {
176
+ std::vector<std::string> TrojanMap::GetAllCategories ()
177
+ {
116
178
return {};
117
179
}
118
180
@@ -125,7 +187,8 @@ std::vector<std::string> TrojanMap::GetAllCategories() {
125
187
* @return {std::vector<std::string>} : ids
126
188
*/
127
189
std::vector<std::string> TrojanMap::GetAllLocationsFromCategory (
128
- std::string category) {
190
+ std::string category)
191
+ {
129
192
std::vector<std::string> res;
130
193
return res;
131
194
}
@@ -139,7 +202,8 @@ std::vector<std::string> TrojanMap::GetAllLocationsFromCategory(
139
202
* names
140
203
* @return {std::vector<std::string>} : ids
141
204
*/
142
- std::vector<std::string> TrojanMap::GetLocationRegex (std::regex location) {
205
+ std::vector<std::string> TrojanMap::GetLocationRegex (std::regex location)
206
+ {
143
207
return {};
144
208
}
145
209
@@ -150,13 +214,14 @@ std::vector<std::string> TrojanMap::GetLocationRegex(std::regex location) {
150
214
* The distance is in mile.
151
215
* The distance is calculated using the Haversine formula.
152
216
* https://en.wikipedia.org/wiki/Haversine_formula
153
- *
217
+ *
154
218
* @param {std::string} a : a_id
155
219
* @param {std::string} b : b_id
156
220
* @return {double} : distance in mile
157
221
*/
158
222
double TrojanMap::CalculateDistance (const std::string &a_id,
159
- const std::string &b_id) {
223
+ const std::string &b_id)
224
+ {
160
225
// Do not change this function
161
226
Node a = data[a_id];
162
227
Node b = data[b_id];
@@ -173,14 +238,16 @@ double TrojanMap::CalculateDistance(const std::string &a_id,
173
238
* CalculatePathLength: Calculates the total path length for the locations
174
239
* inside the vector.
175
240
* We have provided the code for you. Please do not need to change this function.
176
- *
241
+ *
177
242
* @param {std::vector<std::string>} path : path
178
243
* @return {double} : path length
179
244
*/
180
- double TrojanMap::CalculatePathLength (const std::vector<std::string> &path) {
245
+ double TrojanMap::CalculatePathLength (const std::vector<std::string> &path)
246
+ {
181
247
// Do not change this function
182
248
double sum = 0 ;
183
- for (int i = 0 ; i < int (path.size ()) - 1 ; i++) {
249
+ for (int i = 0 ; i < int (path.size ()) - 1 ; i++)
250
+ {
184
251
sum += CalculateDistance (path[i], path[i + 1 ]);
185
252
}
186
253
return sum;
@@ -195,7 +262,8 @@ double TrojanMap::CalculatePathLength(const std::vector<std::string> &path) {
195
262
* @return {std::vector<std::string>} : path
196
263
*/
197
264
std::vector<std::string> TrojanMap::CalculateShortestPath_Dijkstra (
198
- std::string location1_name, std::string location2_name) {
265
+ std::string location1_name, std::string location2_name)
266
+ {
199
267
std::vector<std::string> path;
200
268
return path;
201
269
}
@@ -210,7 +278,8 @@ std::vector<std::string> TrojanMap::CalculateShortestPath_Dijkstra(
210
278
* @return {std::vector<std::string>} : path
211
279
*/
212
280
std::vector<std::string> TrojanMap::CalculateShortestPath_Bellman_Ford (
213
- std::string location1_name, std::string location2_name) {
281
+ std::string location1_name, std::string location2_name)
282
+ {
214
283
std::vector<std::string> path;
215
284
return path;
216
285
}
@@ -220,37 +289,41 @@ std::vector<std::string> TrojanMap::CalculateShortestPath_Bellman_Ford(
220
289
* path which visit all the places and back to the start point.
221
290
*
222
291
* @param {std::vector<std::string>} input : a list of locations needs to visit
223
- * @return {std::pair<double, std::vector<std::vector<std::string>>} : a pair of total distance and the all the progress to get final path,
292
+ * @return {std::pair<double, std::vector<std::vector<std::string>>} : a pair of total distance and the all the progress to get final path,
224
293
* for example: {10.3, {{0, 1, 2, 3, 4, 0}, {0, 1, 2, 3, 4, 0}, {0, 4, 3, 2, 1, 0}}},
225
- * where 10.3 is the total distance,
294
+ * where 10.3 is the total distance,
226
295
* and the first vector is the path from 0 and travse all the nodes and back to 0,
227
296
* and the second vector is the path shorter than the first one,
228
297
* and the last vector is the shortest path.
229
298
*/
230
299
// Please use brute force to implement this function, ie. find all the permutations.
231
300
std::pair<double , std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_Brute_force (
232
- std::vector<std::string> location_ids) {
301
+ std::vector<std::string> location_ids)
302
+ {
233
303
std::pair<double , std::vector<std::vector<std::string>>> records;
234
304
return records;
235
305
}
236
306
237
307
// Please use backtracking to implement this function
238
308
std::pair<double , std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_Backtracking (
239
- std::vector<std::string> location_ids) {
309
+ std::vector<std::string> location_ids)
310
+ {
240
311
std::pair<double , std::vector<std::vector<std::string>>> records;
241
312
return records;
242
313
}
243
314
244
315
// Hint: https://en.wikipedia.org/wiki/2-opt
245
316
std::pair<double , std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_2opt (
246
- std::vector<std::string> location_ids){
317
+ std::vector<std::string> location_ids)
318
+ {
247
319
std::pair<double , std::vector<std::vector<std::string>>> records;
248
320
return records;
249
321
}
250
322
251
323
// This is optional
252
324
std::pair<double , std::vector<std::vector<std::string>>> TrojanMap::TravelingTrojan_3opt (
253
- std::vector<std::string> location_ids){
325
+ std::vector<std::string> location_ids)
326
+ {
254
327
std::pair<double , std::vector<std::vector<std::string>>> records;
255
328
return records;
256
329
}
@@ -259,7 +332,7 @@ std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTro
259
332
* Given CSV filename, it read and parse locations data from CSV file,
260
333
* and return locations vector for topological sort problem.
261
334
* We have provided the code for you. Please do not need to change this function.
262
- * Example:
335
+ * Example:
263
336
* Input: "topologicalsort_locations.csv"
264
337
* File content:
265
338
* Name
@@ -271,13 +344,15 @@ std::pair<double, std::vector<std::vector<std::string>>> TrojanMap::TravelingTro
271
344
* @return {std::vector<std::string>} : locations
272
345
*/
273
346
std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile (
274
- std::string locations_filename) {
347
+ std::string locations_filename)
348
+ {
275
349
std::vector<std::string> location_names_from_csv;
276
350
std::fstream fin;
277
351
fin.open (locations_filename, std::ios::in);
278
352
std::string line, word;
279
353
getline (fin, line);
280
- while (getline (fin, word)) {
354
+ while (getline (fin, word))
355
+ {
281
356
location_names_from_csv.push_back (word);
282
357
}
283
358
fin.close ();
@@ -288,7 +363,7 @@ std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile(
288
363
* Given CSV filenames, it read and parse dependencise data from CSV file,
289
364
* and return dependencies vector for topological sort problem.
290
365
* We have provided the code for you. Please do not need to change this function.
291
- * Example:
366
+ * Example:
292
367
* Input: "topologicalsort_dependencies.csv"
293
368
* File content:
294
369
* Source,Destination
@@ -300,16 +375,19 @@ std::vector<std::string> TrojanMap::ReadLocationsFromCSVFile(
300
375
* @return {std::vector<std::vector<std::string>>} : dependencies
301
376
*/
302
377
std::vector<std::vector<std::string>> TrojanMap::ReadDependenciesFromCSVFile (
303
- std::string dependencies_filename) {
378
+ std::string dependencies_filename)
379
+ {
304
380
std::vector<std::vector<std::string>> dependencies_from_csv;
305
381
std::fstream fin;
306
382
fin.open (dependencies_filename, std::ios::in);
307
383
std::string line, word;
308
384
getline (fin, line);
309
- while (getline (fin, line)) {
385
+ while (getline (fin, line))
386
+ {
310
387
std::stringstream s (line);
311
388
std::vector<std::string> dependency;
312
- while (getline (s, word, ' ,' )) {
389
+ while (getline (s, word, ' ,' ))
390
+ {
313
391
dependency.push_back (word);
314
392
}
315
393
dependencies_from_csv.push_back (dependency);
@@ -329,9 +407,10 @@ std::vector<std::vector<std::string>> TrojanMap::ReadDependenciesFromCSVFile(
329
407
*/
330
408
std::vector<std::string> TrojanMap::DeliveringTrojan (
331
409
std::vector<std::string> &locations,
332
- std::vector<std::vector<std::string>> &dependencies) {
410
+ std::vector<std::vector<std::string>> &dependencies)
411
+ {
333
412
std::vector<std::string> result;
334
- return result;
413
+ return result;
335
414
}
336
415
337
416
/* *
@@ -341,11 +420,11 @@ std::vector<std::string> TrojanMap::DeliveringTrojan(
341
420
* @param {std::vector<double>} square: four vertexes of the square area
342
421
* @return {bool} : in square or not
343
422
*/
344
- bool TrojanMap::inSquare (std::string id, std::vector<double > &square) {
423
+ bool TrojanMap::inSquare (std::string id, std::vector<double > &square)
424
+ {
345
425
return true ;
346
426
}
347
427
348
-
349
428
/* *
350
429
* GetSubgraph: Give four vertexes of the square area, return a list of location
351
430
* ids in the squares
@@ -355,7 +434,8 @@ bool TrojanMap::inSquare(std::string id, std::vector<double> &square) {
355
434
* @return {std::vector<std::string>} subgraph : list of location ids in the
356
435
* square
357
436
*/
358
- std::vector<std::string> TrojanMap::GetSubgraph (std::vector<double > &square) {
437
+ std::vector<std::string> TrojanMap::GetSubgraph (std::vector<double > &square)
438
+ {
359
439
// include all the nodes in subgraph
360
440
std::vector<std::string> subgraph;
361
441
return subgraph;
@@ -370,7 +450,8 @@ std::vector<std::string> TrojanMap::GetSubgraph(std::vector<double> &square) {
370
450
* @param {std::vector<double>} square: four vertexes of the square area
371
451
* @return {bool}: whether there is a cycle or not
372
452
*/
373
- bool TrojanMap::CycleDetection (std::vector<std::string> &subgraph, std::vector<double > &square) {
453
+ bool TrojanMap::CycleDetection (std::vector<std::string> &subgraph, std::vector<double > &square)
454
+ {
374
455
return false ;
375
456
}
376
457
@@ -385,7 +466,8 @@ bool TrojanMap::CycleDetection(std::vector<std::string> &subgraph, std::vector<d
385
466
* @param {int} k: search numbers
386
467
* @return {std::vector<std::string>}: location name that meets the requirements
387
468
*/
388
- std::vector<std::string> TrojanMap::FindNearby (std::string attributesName, std::string name, double r, int k) {
469
+ std::vector<std::string> TrojanMap::FindNearby (std::string attributesName, std::string name, double r, int k)
470
+ {
389
471
std::vector<std::string> res;
390
472
return res;
391
473
}
@@ -398,9 +480,10 @@ std::vector<std::string> TrojanMap::FindNearby(std::string attributesName, std::
398
480
* @return {std::vector<std::string> } : the shortest path
399
481
*/
400
482
std::vector<std::string> TrojanMap::TrojanPath (
401
- std::vector<std::string> &location_names) {
402
- std::vector<std::string> res;
403
- return res;
483
+ std::vector<std::string> &location_names)
484
+ {
485
+ std::vector<std::string> res;
486
+ return res;
404
487
}
405
488
406
489
/* *
@@ -409,28 +492,32 @@ std::vector<std::string> TrojanMap::TrojanPath(
409
492
* @param {std::vector<std::pair<double, std::vector<std::string>>>} Q : a list of queries
410
493
* @return {std::vector<bool> } : existence of the path
411
494
*/
412
- std::vector<bool > TrojanMap::Queries (const std::vector<std::pair<double , std::vector<std::string>>>& q) {
413
- std::vector<bool > ans (q.size ());
414
- return ans;
495
+ std::vector<bool > TrojanMap::Queries (const std::vector<std::pair<double , std::vector<std::string>>> &q)
496
+ {
497
+ std::vector<bool > ans (q.size ());
498
+ return ans;
415
499
}
416
500
417
501
/* *
418
502
* CreateGraphFromCSVFile: Read the map data from the csv file
419
503
* We have provided the code for you. Please do not need to change this function.
420
504
*/
421
- void TrojanMap::CreateGraphFromCSVFile () {
505
+ void TrojanMap::CreateGraphFromCSVFile ()
506
+ {
422
507
// Do not change this function
423
508
std::fstream fin;
424
509
fin.open (" src/lib/data.csv" , std::ios::in);
425
510
std::string line, word;
426
511
427
512
getline (fin, line);
428
- while (getline (fin, line)) {
513
+ while (getline (fin, line))
514
+ {
429
515
std::stringstream s (line);
430
516
431
517
Node n;
432
518
int count = 0 ;
433
- while (getline (s, word, ' ,' )) {
519
+ while (getline (s, word, ' ,' ))
520
+ {
434
521
word.erase (std::remove (word.begin (), word.end (), ' \' ' ), word.end ());
435
522
word.erase (std::remove (word.begin (), word.end (), ' "' ), word.end ());
436
523
word.erase (std::remove (word.begin (), word.end (), ' {' ), word.end ());
@@ -443,10 +530,13 @@ void TrojanMap::CreateGraphFromCSVFile() {
443
530
n.lon = stod (word);
444
531
else if (count == 3 )
445
532
n.name = word;
446
- else {
533
+ else
534
+ {
447
535
word.erase (std::remove (word.begin (), word.end (), ' ' ), word.end ());
448
- if (isalpha (word[0 ])) n.attributes .insert (word);
449
- if (isdigit (word[0 ])) n.neighbors .push_back (word);
536
+ if (isalpha (word[0 ]))
537
+ n.attributes .insert (word);
538
+ if (isdigit (word[0 ]))
539
+ n.neighbors .push_back (word);
450
540
}
451
541
count++;
452
542
}
0 commit comments