forked from MohanadKh03/Mini-Wasalni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.cpp
170 lines (146 loc) · 4.92 KB
/
Map.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
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
#include "Map.h"
Map::Map() {
ifstream citiesFile("cities.txt");
ifstream edgesFile("edges.txt");
string name, neighborName;
int x, y, size, weight;
while (citiesFile >> name >> x >> y) {
Node *newCity = new Node(name, x, y);
graph[name] = newCity;
int id = NodeConverter::axisToId(x, y, limitY);
newCity->id = id;
convertedGraph[id];
IdToName[id] = name;
}
while (edgesFile >> name >> size) {
int id = NodeConverter::axisToId(graph[name]->point.x, graph[name]->point.y, limitY);
for (int i = 0; i < size; ++i) {
edgesFile >> neighborName >> weight;
int neighourId = NodeConverter::axisToId(graph[neighborName]->point.x, graph[neighborName]->point.y, limitY);
graph[name]->edges[neighborName] = weight;
convertedGraph[id].insert(neighourId);
}
}
}
void Map::addCity() {
string city;
int x, y;
cout << "Enter city name , position(x,y)\n";
cin >> city >> x >> y;
for(auto& node : graph){
if(node.second->point.x == x && node.second->point.y == y){
cout << "City coordinates already exist \n";
return;
}
}
if (graph[city]) {
cout << "City already exists \n";
return;
}
Node *newNode = new Node(city, x, y);
graph[city] = newNode;
int id = NodeConverter::axisToId(graph[city]->point.x, graph[city]->point.y, limitY);
newNode->id = id;
convertedGraph[id];
IdToName[id] = city;
}
void Map::deleteCity() {
displayMap();
string city;
cout << "Enter city \t";
cin >> city;
if (!graph[city]) {
cout << "City doesn't exist \n";
return;
}
//deleting all edges that go to our city
// for (auto &edge: graph[city]->edges) {
// graph[edge.first]->edges.erase(city);
// }
for(auto &cityNode : graph){
cityNode.second->edges.erase(city);
}
int id = NodeConverter::axisToId(graph[city]->point.x, graph[city]->point.y, limitY);
graph.erase(city);
for (auto it: convertedGraph) {
convertedGraph.erase(id);
}
convertedGraph[id].clear();
convertedGraph.erase(id);
IdToName.erase(id);
}
void Map::addEdge() {
displayMap();
string city1, city2;
bool isDirected;
cout << "Enter city1(from),city2(to) names\n";
cin >> city1 >> city2;
if (!graph[city1] || !graph[city2]) {
cout << "City does not exist\n";
return;
}
do{
cout << "Directed or Undirected ?(1,0): ";
cin >> isDirected;
}while(isDirected != 1 && isDirected != 0);
int distance = (int) sqrt(pow((graph[city1]->point.x - graph[city2]->point.x), 2) +
pow((graph[city1]->point.y - graph[city2]->point.y), 2));
graph[city1]->edges[city2] = distance;
if(!isDirected)
graph[city2]->edges[city1] = distance;
//Converter
int id1 = NodeConverter::axisToId(graph[city1]->point.x, graph[city1]->point.y, limitY);
int id2 = NodeConverter::axisToId(graph[city2]->point.x, graph[city2]->point.y, limitY);
//
convertedGraph[id1].insert(id2); //
///./////
if(!isDirected)
convertedGraph[id2].insert(id1); //
}
//Deleting edge FROM and TO .. cant delete both ways at once .. if undirected you must call it two times
void Map::deleteEdge() {
displayMap();
cout << "Enter cities name(from,to) \n";
string city1, city2;
cin >> city1 >> city2;
if (!graph[city1] || !graph[city2]) {
cout << "one of cities doesn't exist\n";
return;
}
int id1 = NodeConverter::axisToId(graph[city1]->point.x, graph[city1]->point.y, limitY);
int id2 = NodeConverter::axisToId(graph[city2]->point.x, graph[city2]->point.y, limitY);
if(graph[city1]->edges.find(city2) != graph[city1]->edges.end()){
graph[city1]->edges.erase(city2);
convertedGraph[id1].erase(id2);
}
else
cout << "No edge from " << city1 << " to " << city2 << endl;
}
void Map::displayMap() {
for (auto &node: graph) {
cout << node.first << endl;
for (auto &edge: graph[node.first]->edges)
cout << edge.first << " " << edge.second << endl;
cout << "------------------------------------------------------------------------------\n";
}
}
void Map::deleteMap() {
cout << "Map Deleted\n";
for (auto &node: graph) {
delete node.second;
}
//have to delete the pointers of each node first then clear
graph.clear();
}
Map::~Map() {
ofstream citiesFile("cities.txt");
ofstream edgesFile("edges.txt");
for (auto &city: graph) {
citiesFile << city.second->name << ' ' << city.second->point.x << ' ' << city.second->point.y << endl;
edgesFile << city.first << ' ' << city.second->edges.size() << endl;
for (auto &edge: city.second->edges) {
edgesFile << edge.first << ' ' << edge.second << endl;
}
}
deleteMap();
}