-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
626 lines (519 loc) · 18.4 KB
/
main.cpp
File metadata and controls
626 lines (519 loc) · 18.4 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// ============================================================================
// Authors: Ali Peera, Michael Milligan, Taylor Tillander
//
// Description: Takes in CSV file containing users' Spotify data using tabs as
// delimiters and stores the data into either an unordered_map or ordered map
// with key: song/artist name and value: number of total streams.
//
// Primary Functions:
// Display all songs or artists in the maps with their corresponding quantity
// of streams.
// Display top 'n' songs or artists, ranked by quantity of streams
// Search for a song or artist and output corresponding quantity of streams.
// ============================================================================
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using namespace std::chrono;
//Convert map to vector for sorting by top artists and songs
vector<pair<string, int>>
unordered_mapToVector(unordered_map<string, int> inputMap) {
vector<pair<string, int>> sortedMap(inputMap.begin(), inputMap.end());
// Sort the vector based on the second element of the pair
sort(sortedMap.begin(), sortedMap.end(), // nlogn
[](const pair<string, int> &a, const pair<string, int> &b) {
return a.second > b.second;
}); // (comp) function in sort() compares the # of streams from the pair
return sortedMap;
}
vector<pair<string, int>> ordered_mapToVector(map<string, int> inputMap) {
vector<pair<string, int>> sortedMap(inputMap.begin(), inputMap.end());
// Sort the vector based on the second element of the pair
sort(sortedMap.begin(), sortedMap.end(),
[](const pair<string, int> &a, const pair<string, int> &b) {
return a.second > b.second;
});
// (comp) function in sort() compares the # of streams from the pair
return sortedMap;
}
//Print out top n elements
void printVector(vector<pair<string, int>> inputVector, int counter) {
int count = 0;
// Iterate through the vector of pairs
for (const auto &pair : inputVector) {
if (count < counter) {
cout << pair.first << ": " << pair.second << std::endl;
count++;
} else {
break; // Stop after printing the first "counter" elements
}
}
}
//Print all elements of unordered_map
void printUnorderedMap(unordered_map<string, int> inputMap) {
auto start = high_resolution_clock::now(); // Timing
for (const auto &pair : inputMap) {
cout << pair.first << ": " << pair.second << endl;
}
auto stop = high_resolution_clock::now(); // Timing
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time to print Unordered Map: "
<< duration.count() << "ms" << endl;
}
//Print all elements of ordered map
void printOrderedMap(map<string, int> inputMap) {
auto start = high_resolution_clock::now(); // Timing
for (const auto &pair : inputMap) {
cout << pair.first << ": " << pair.second << endl;
}
auto stop = high_resolution_clock::now(); // Timing
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time to print Ordered Map "
<< duration.count() << "ms" << endl;
}
//Create ordered map
map<string, int> createOrderedMap(string fileName, string keySignifier) {
ifstream file(fileName);
// If the file name could not open
if (!file.is_open()) {
cerr << "Error: Could not open file " << fileName << endl;
return {};
}
map<string, int> countMap;
// "line" is a row in the csv, separating each stream
string line;
// Read the header line to find the index of the "title" column
getline(file, line);
istringstream headerStream(line);
vector<string> headers;
string header;
while (getline(headerStream, header, '\t')) {
headers.push_back(header);
}
// keyIndex is the index of column in the csv that has the desired key
int keyIndex =
find(headers.begin(), headers.end(), keySignifier) - headers.begin();
auto start = high_resolution_clock::now();
// Read the rest of the lines
while (getline(file, line)) {
istringstream sstream(line);
string key;
// Iterate through until titleIndex is reached, get the title
for (int i = 0; i <= keyIndex; ++i) {
getline(sstream, key, '\t');
}
// Increment the count in the map
countMap[key]++;
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time to create Ordered Map: " << duration.count()
<< "ms" << endl;
return countMap;
}
//Create unordered map
unordered_map<string, int> createUnorderedMap(string fileName,
string keySignifier) {
ifstream file(fileName);
if (!file.is_open()) { // incase file does not open
cerr << "Error: Could not open file " << fileName << endl;
return {};
}
unordered_map<string, int> countMap;
// Row in csv, separating each stream
string line;
// Read the header line to find the index of the "title" column
getline(file, line);
istringstream headerStream(line);
vector<string> headers;
string header;
while (getline(headerStream, header, '\t')) {
headers.push_back(header);
}
// keyIndex is the index of column in the csv that has says the desired key
int keyIndex =
find(headers.begin(), headers.end(), keySignifier) - headers.begin();
auto start = high_resolution_clock::now();
// Read the rest of the lines
while (getline(file, line)) {
istringstream sstream(line);
string key;
// Iterate through until titleIndex is reached, get the title
for (int i = 0; i <= keyIndex; ++i) {
getline(sstream, key, '\t');
}
// Increment the count in the map
countMap[key]++;
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time to create Unordered Map: " << duration.count()
<< "ms" << endl;
return countMap;
}
//Create menu selection
void displayMenu() {
cout << endl
<< "What would you like to do?" << endl
<< "1. Create Unordered Map" << endl
<< "2. Create Ordered Map" << endl
<< "3. Output Unordered Map" << endl
<< "4. Output Ordered Map" << endl
<< "5. Search Unordered Map" << endl
<< "6. Search Ordered Map" << endl
<< "7. End Program" << endl;
}
int main() {
// Initializing maps to be used later
unordered_map<string, int> songUnorderedMap;
unordered_map<string, int> artistUnorderedMap;
map<string, int> songMap;
map<string, int> artistMap;
// First: convert desired file to istringstream
cout << "Input File Name: "; // SpotifyData/CombinedSpotifyData.csv
string fileName;
getline(cin, fileName);
string directory;
directory = "SpotifyData/" + fileName;
ifstream file(directory);
while (!file.is_open()) {
if (!file.is_open()) { // incase file does not open
cout << "Error: Could not open file " << fileName << endl << endl;
}
cout << "Input File Name: ";
getline(cin, fileName);
directory = "SpotifyData/" + fileName;
file.open(directory);
}
displayMenu();
string operation;
cin >> operation;
int resultNum;
int searchNum;
while (stoi(operation) != 7) {
// Interpret User Input
switch (stoi(operation)) {
// Create Unordered Map
case 1:
cout << endl
<< "1. Sort Unordered Map by Song Title" << endl
<< "2. Sort Unordered Map by Artist Name" << endl;
cin >> operation;
switch (stoi(operation)) {
// Sort by Song Title
case 1:
// Create unordered map with csv input
if (songUnorderedMap.empty()) {
songUnorderedMap =
createUnorderedMap(directory, "master_metadata_track_name");
} else {
cout << "This map has already been created!" << endl << endl;
}
break;
// Sort by Artist Name
case 2:
if (artistUnorderedMap.empty()) {
artistUnorderedMap =
createUnorderedMap(directory, "master_metadata_album_artist_name");
} else {
cout << "This map has already been created!" << endl << endl;
}
break;
default:
cout << "Invalid Input!" << endl;
}
break;
// Create Ordered Map
case 2:
cout << endl
<< "1. Sort Ordered Map by Song Title" << endl
<< "2. Sort Ordered Map by Artist Name" << endl;
cin >> operation;
switch (stoi(operation)) {
// Sort by Song Title
case 1:
// Create Ordered map with csv input
if (songMap.empty()) {
songMap = createOrderedMap(directory, "master_metadata_track_name");
} else {
cout << "This map has already been created!" << endl << endl;
}
break;
// Sort by Artist Name
case 2:
if (artistMap.empty()) {
artistMap =
createOrderedMap(directory, "master_metadata_album_artist_name");
} else {
cout << "This map has already been created!" << endl << endl;
}
break;
default:
cout << "Invalid Input!" << endl;
}
break;
// Output Unordered Map
case 3:
cout << endl
<< "1. Display Top Song Titles" << endl
<< "2. Display Top Artist Names" << endl
<< "3. Display All Song Titles" << endl
<< "4. Display All Artist Names" << endl;
cin >> operation;
//Ask user how many entries they want outputted
if (stoi(operation) == 1 || stoi(operation) == 2) {
cout << endl;
cout << "Enter the number of results to display: ";
cin >> resultNum;
cout << endl;
}
switch (stoi(operation)) {
// Display Song Titles based on top n results
case 1:
if (!songUnorderedMap.empty()) {
auto start = chrono::high_resolution_clock::now();
printVector(unordered_mapToVector(songUnorderedMap), resultNum);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time to Sort Unordered Map and print " << resultNum
<< " songs: " << duration.count() << " microseconds" << endl;
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
// Display Artist Names based on top n artists
case 2:
if (!artistUnorderedMap.empty()) {
auto start = chrono::high_resolution_clock::now();
printVector(unordered_mapToVector(artistUnorderedMap), resultNum);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << endl << "Time to Sort Unordered Map and print " << resultNum
<< " artists: " << duration.count() << " microseconds" << endl;
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
//Display all songs
case 3:
if (!songUnorderedMap.empty()) {
printUnorderedMap(songUnorderedMap);
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
//Display all artists
case 4:
if (!artistUnorderedMap.empty()) {
printUnorderedMap(artistUnorderedMap);
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
default:
cout << "Invalid Input!" << endl;
}
break;
// Output Ordered Map
case 4:
cout << endl
<< "1. Display Top Song Titles" << endl
<< "2. Display Top Artist Names" << endl
<< "3. Display All Song Titles" << endl
<< "4. Display All Artist Names" << endl;
cin >> operation;
//Ask user how many entries they want outputted
if (stoi(operation) == 1 || stoi(operation) == 2) {
cout << endl;
cout << "Enter the number of results to display: ";
cin >> resultNum;
cout << endl;
}
switch (stoi(operation)) {
// Display Song Titles based on top n results
case 1:
if (!songMap.empty()) {
auto start = high_resolution_clock::now();
printVector(ordered_mapToVector(songMap), resultNum);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time to Sort Ordered Map and print " << resultNum
<< " songs: " << duration.count() << " microseconds" << endl;
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
// Display Artist Names based on top n results
case 2:
if (!artistMap.empty()) {
auto start = high_resolution_clock::now();
printVector(ordered_mapToVector(artistMap), resultNum);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time to Sort Ordered Map and print " << resultNum
<< " artists: " << duration.count() << " microseconds" << endl;
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
//print all songs
case 3:
if (!songMap.empty()) {
printOrderedMap(songMap);
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
//print all artists
case 4:
if (!artistMap.empty()) {
printOrderedMap(artistMap);
} else {
cout << "This map has not been created yet!" << endl << endl;
}
break;
default:
cout << "Invalid Input!" << endl;
}
break;
// search unordered
case 5: {
cout << endl;
cout << "1. Search by Song" << endl << "2. Search by Artist" << endl;
cin >> searchNum;
cout << endl;
string searchInput;
int streamCount;
switch (searchNum) {
// Search by Song
case 1: {
if (songUnorderedMap.empty()) {
cout << "Song-sorted map has not yet been created!" << endl;
break;
}
cout << "Song Title: ";
cin.ignore();
getline(cin, searchInput);
//Create timer to track how long it takes to search
auto start = high_resolution_clock::now();
// Search; determines time complexity
streamCount = songUnorderedMap[searchInput];
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << endl;
if (streamCount > 0) {
cout << "Number of streams: " << streamCount << endl;
} else {
cout << "Song Title Not Found!" << endl;
}
cout << "Time to Search Unordered Map: "
<< duration.count() << "ns" << endl;
break;
}
// Search by Artist
case 2: {
if (artistUnorderedMap.empty()) {
cout << "Artist-sorted map has not yet been created!" << endl;
break;
}
cout << "Artist Name: ";
cin.ignore();
getline(cin, searchInput);
//Create timer to track how long it takes to search
auto start = high_resolution_clock::now();
streamCount =
artistUnorderedMap[searchInput]; // Determines this time complexity
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << endl;
if (streamCount > 0) {
cout << "Number of streams: " << streamCount << endl;
} else {
cout << "Artist Name Not Found!" << endl;
}
cout << "Time to Search Unordered Map: "
<< duration.count() << "ns" << endl;
break;
}
}
break;
}
// search ordered map
case 6: {
cout << endl;
cout << "1. Search by Song" << endl << "2. Search by Artist" << endl;
cin >> searchNum;
cout << endl;
string searchInput;
int streamCount;
switch (searchNum) {
// Search by Song
case 1: {
if (songMap.empty()) {
cout << "Song-sorted map has not yet been created!" << endl;
break;
}
cout << "Song Title: ";
cin.ignore();
getline(cin, searchInput);
//Create timer to track how long it takes to search
auto start = high_resolution_clock::now();
streamCount = songMap[searchInput]; // Determines this time complexity
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
if (streamCount > 0) {
cout << "Number of streams: " << streamCount << endl;
} else {
cout << "Song Title Not Found!" << endl;
}
cout << "Time to Search Ordered Map: " << duration.count() << "ns"
<< endl;
cout << endl;
break;
}
// Search by Artist
case 2: {
if (artistMap.empty()) {
cout << "Artist-sorted map has not yet been created!" << endl;
break;
}
cout << "Artist Name: ";
cin.ignore();
getline(cin, searchInput);
//Create timer to track how long it takes to search
auto start = high_resolution_clock::now();
streamCount = artistMap[searchInput]; // Determines this time complexity
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
if (streamCount > 0) {
cout << "Number of streams: " << streamCount << endl;
} else {
cout << "Artist Name Not Found!" << endl;
}
cout << "Time to Search Ordered Map: " << duration.count() << "ns"
<< endl;
cout << endl;
break;
}
}
break;
}
// End Program
case 7:
cout << "Thank you for using MusicStats!" << endl;
break;
default:
cout << "Invalid Input!" << endl;
}
displayMenu();
cin >> operation;
}
return 0;
}