-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneral_utils.h
More file actions
262 lines (215 loc) · 7 KB
/
general_utils.h
File metadata and controls
262 lines (215 loc) · 7 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
#include <iostream>
#include "Graph.h"
#include <limits.h>
#include <vector>
using namespace std;
void add_vertex_m() {
string name;
cout << "> Ingrese el nombre del vertice: ";
cin.ignore();
getline(cin, name, '\n');
if (!get_vertex_by_name(name, *G).found) {
add_node(name, *G);
save_vertex_to_db(name);
pause_f();
return;
} else {
cout << "XX - No se pudo añadir el vertice - XX" << endl;
pause_f();
return;
}
}
void add_edge_m() {
string from, to;
cout << "> Ingrese el nombre del vertice de origen: ";
cin.ignore();
getline(cin, from, '\n');
if (!get_vertex_by_name(from, *G).found) {
cout << "\n>> Vertice no existente, desea añadirlo? [yes/no]" << endl;
string answ;
getline(cin, answ, '\n');
if (answ == "yes") {
add_node(from, *G);
cout << "\n** Vertice añadido **" << endl;
save_vertex_to_db(from);
} else {
cout << "\nXX - Añadir vertice no se llevo a cabo. No se puede crear arco - XX" << endl;
pause_f();
return;
}
}
cout << "\n> Ingrese el nombre del vertice de destino: ";
getline(cin, to, '\n');
if (!get_vertex_by_name(to, *G).found) {
cout << "\n>> Vertice no existente, desea añadirlo? [yes/no]" << endl;
string answ;
getline(cin, answ, '\n');
if (answ == "yes") {
add_node(to, *G);
save_vertex_to_db(to);
cout << "\n** Vertice añadido ** " << endl;
} else {
cout << "\n XX - Añadir vertice no se llevo a cabo. No se puede crear arco - XX" << endl;
return;
}
}
Edge temp = make_pair(vertex_i[get_vertex_by_name(from, *G).vertex], vertex_i[get_vertex_by_name(to, *G).vertex]);
if (get_edge(from, to, *G).found) {
cout << "\nXX - Arista ya existente - XX" << endl;
pause_f();
return;
} else {
add_edge(temp.first, temp.second, *G);
save_edge_to_db(from, to);
pause_f();
return;
}
}
void update_vertex_m() {
string actual_name;
string new_name;
cout << "> Ingrese el nombre actual del vertice: ";
cin.ignore();
getline(cin, actual_name, '\n');
cout << "\n> Ingrese el nombre nuevo del vertice: ";
getline(cin, new_name, '\n');
vertex_struct a = get_vertex_by_name(actual_name, *G);
if (a.found) {
modify_vertex_name(a.vertex, new_name);
update_vertex_db(actual_name, new_name);
cout << "\n** Vertice actualizado **" << endl;
pause_f();
return;
} else {
cout << "\nXX - No existe el nodo - XX" << endl;
pause_f();
return;
}
}
void update_edge_m() {
int weight;
string temp_answ, from_old, to_old, from_new, to_new;
cout << "> Introduzca el arco actual: " << endl;
cout << "\n>> Vertice de origen: ";
cin.ignore();
getline(cin, from_old, '\n');
cout << "\n>> Vertice de destino: ";
getline(cin, to_old, '\n');
edge_struct a = get_edge(from_old, to_old, *G);
if (a.found) {
cout << "\n> Introduzca el arco nuevo: ";
cout << "\n>> Vertice de origen: ";
getline(cin, from_new, '\n');
cout << "\n>> Vertice de destino: ";
getline(cin, to_new, '\n');
cout << "\n>> Desea cambiar el peso[yes/no]?" << endl;
getline(cin, temp_answ, '\n');
if (temp_answ == "yes") {
cout << "\n>> Ingrese el peso: " << endl;
validateInput(weight);
}
Edge actual_edge = make_pair(vertex_i[get_vertex_by_name(from_old, *G).vertex],
vertex_i[get_vertex_by_name(to_old, *G).vertex]);
Edge new_edge = make_pair(vertex_i[get_vertex_by_name(from_new, *G).vertex],
vertex_i[get_vertex_by_name(to_new, *G).vertex]);
modify_weight(a.edge, weight);
update_edge(actual_edge, new_edge, *G);
update_edge_db(from_old, to_old, from_new, to_new, weight);
cout << "\n** Arco actualizado **" << endl;
pause_f();
} else {
cout << "\nXX - No existe el arco - XX" << endl;
pause_f();
}
}
void delete_vertex_m() {
string name;
cout << "> Ingrese el nombre del vertice: ";
cin.ignore();
getline(cin, name, '\n');
vertex_struct a = get_vertex_by_name(name, *G);
if (a.found) {
clear_vertex(a.vertex, *G);
remove_vertex(a.vertex, *G);
delete_vertex_from_db(name);
cout << "\n** Vertice eliminado **" << endl;
pause_f();
} else {
cout << "\nXX - No existe el vertice - XX" << endl;
pause_f();
}
}
void delete_edge_m() {
string from;
string to;
cout << "\n> Introduzca el vertice de origen: ";
cin.ignore();
getline(cin, from, '\n');
cout << "\n> Introduzca el vertice de destino: ";
getline(cin, to, '\n');
edge_struct a = get_edge(from, to, *G);
if (a.found) {
remove_edge(a.edge, *G);
delete_edge_from_db(from, to);
cout << "\n** Arco borrado con exito **";
pause_f();
} else {
cout << "\nXX - Arco no existe - XX" << endl;
pause_f();
}
}
void get_shortest_path() {
string from, to;
cout << "> Ingrese el nombre del vertice de origen: ";
cin.ignore();
getline(cin, from, '\n');
cout << "\n> Ingrese el nombre del vertice de destino: ";
getline(cin, to, '\n');
vertex_struct a = get_vertex_by_name(from, *G);
vertex_struct b = get_vertex_by_name(to, *G);
if (a.found && b.found) {
path_and_dist dist = shortest_path(a.vertex, b.vertex, *G);
for (std::size_t i = 0; i < dist.path.size(); i++) {
vertex_struct a = get_vertex_by_index(vertex_i(dist.path[i]), *G);
cout << name_node(a.vertex) << " --> ";
}
cout << "end \n";
pause_f();
} else {
cout << "\nXX - No existe uno de los vertices - XX" << endl;
pause_f();
}
}
void get_adjacent_vertices() {
string name;
cout << "> Ingrese el nombre del vertice: ";
cin.ignore();
getline(cin, name, '\n');
vertex_struct a = get_vertex_by_name(name, *G);
if (a.found) {
cout << "\nVertices adyacentes: " << endl;
retrieve_adjacent_vertices(a.vertex, *G);
cout << endl;
pause_f();
} else {
cout << "\nXX - No existe el vertice - XX" << endl;
pause_f();
}
}
void print_vertices() {
for (vp = vertices(*G); vp.first != vp.second; ++vp.first) {
cout << "Vertice con posicion: " << vertex_i[*vp.first] << ", Nombre: " << name_node[*vp.first] << endl;
}
pause_f();
return;
}
void print_edges() {
int i = 0;
for (tie(ei, ei_end) = edges(*G); ei != ei_end; ++ei) {
cout << "Arco #" << i << " desde: " << vertex_i[source(*ei, *G)] << " hasta: " << vertex_i[target(*ei, *G)]
<< ", peso: " <<
edge_weight_map[*ei] << endl;
i++;
}
pause_f();
}