-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·88 lines (78 loc) · 3.33 KB
/
main.cpp
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
#include <iostream>
#include "InputOutputHandler.h"
#include "hashgraph/api/HashGraph.h"
void displayMenu();
int main(int argc, char **argv) {
int sourceNodeKey = -1;
int targetNodeKey = -1;
std::map<int, std::vector<int>> inputFileGraphBuffer; // Buffer that contains data from the input txt file
auto inputOutputHandler = InputOutputHandler<int>();
char *relativePathToInputFile = inputOutputHandler.getPathToInputFile(argv[1]);
if (!relativePathToInputFile) {
std::cerr << "Usage: ./HashGraph inputFileName.txt hashingStrategy[OPTIONAL]" << std::endl;
return -1;
}
auto fileMetadata = inputOutputHandler.readInputGraph(relativePathToInputFile,
inputFileGraphBuffer); // Filling the buffer
if (!fileMetadata.getOperationStatus()) // File read failed
return -1;
HashGraph hashGraph = HashGraph<int>(
fileMetadata.getNumberOfNodes() + 1); // Create the hash table and inserting data into it
hashGraph.setHashingStrategy(argv[2]); // Setting the hashing strategy, by default is linear probing
hashGraph.fillTable(inputFileGraphBuffer);
int choice = 0;
do {
displayMenu();
std::cin >> choice;
switch (choice) {
case 1:
std::cout << hashGraph;
inputOutputHandler.requestInput();
break;
case 2:
inputOutputHandler.requestSourceAndTargetNode(&sourceNodeKey, &targetNodeKey);
hashGraph.addEdge(sourceNodeKey, targetNodeKey);
break;
case 3:
inputOutputHandler.requestSourceAndTargetNode(&sourceNodeKey, &targetNodeKey);
hashGraph.removeEdge(sourceNodeKey, targetNodeKey);
break;
case 4:
inputOutputHandler.requestSourceAndTargetNode(&sourceNodeKey, &targetNodeKey);
if (hashGraph.findEdge(sourceNodeKey, targetNodeKey))
std::cout << "Edge found between: " << sourceNodeKey << " --> " << targetNodeKey << std::endl;
else
std::cout <<
"There is not an edge between " << sourceNodeKey << " --> " << targetNodeKey << std::endl;
break;
case 5:
std::cout << "Insert source node key: ";
std::cin >> sourceNodeKey;
hashGraph.dfs(sourceNodeKey);
break;
case 6:
std::cout << "Insert source node key: ";
std::cin >> sourceNodeKey;
hashGraph.deleteByKey(sourceNodeKey);
break;
case 0:
std::cout << "Bye!\n";
break;
default:
std::cout << "Invalid number... retry!\n";
}
} while (choice != 0);
return 0;
}
void displayMenu() {
std::flush(std::cout);
std::cout << "\n \t \t [HashGraph menu] \n";
std::cout << "Choose an operation \n";
std::cout << "1) Display graph \n";
std::cout << "2) Add an edge between two nodes \n";
std::cout << "3) Remove an edge between two nodes \n";
std::cout << "4) Find an edge between two nodes \n";
std::cout << "5) Depth first search \n";
std::cout << "6) Delete a node \n";
std::cout << "0) Exit \n";
}