-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgoritmo_1.cpp
More file actions
250 lines (185 loc) · 6.48 KB
/
algoritmo_1.cpp
File metadata and controls
250 lines (185 loc) · 6.48 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
//
// Utilización:
// Primer parámetro = ruta fichero entrada
// Segundo parámetro = ruta fichero salida
//
#include <iostream>
#include <list>
#include <set>
#include <fstream>
#include <map>
using namespace std;
const int MAX_SIZE=100000;
struct conection{
int id_endpoint;
int id_cache;
};
bool operator< (const conection& lhs, const conection& rhs){
return lhs.id_endpoint*10000+lhs.id_cache < rhs.id_endpoint*10000+rhs.id_cache;
}
struct cache{
int free_size;
set<int> videos;
};
//Nombres ficheros
char *nombre_fichero_entrada;
char *nombre_fichero_salida;
//Numeros de datos
int num_videos;
int num_caches;
int num_endpoints;
//latencias ordenadas
//key->saved_time value->connections
map<int,set<conection>> latencias_ordenadas;
//videos index->num value valor
int size_of_videos[MAX_SIZE];
//endpoints
//key->endpoint value->connected caches
map<int,set<int>> endpoints_cache;
//endpoints_videos
//key->endpoint value->videos visto por el endpoint en cuestion
map<int,set<int>> endpoints_videos;
//Registro de los caches
cache cache_register[MAX_SIZE];
//Variables error
bool error_cargar=false;
bool error_ejecuacion=false;
bool error_guardar=false;
/*
* Carga los datos en las estructuras correspondientes
*/
void cargarDatos() {
ifstream f;
int num_requests;
int size_caches;
char rubish[100];
f.open(nombre_fichero_entrada);
if (f.is_open()) {
cout << "Fichero de entrada abierto" << endl;
f >> num_videos >> num_endpoints >> num_requests >> num_caches >> size_caches;
f.getline(rubish, 1);
for (int i = 0; i < num_videos; ++i) {//cargar videos
f >> size_of_videos[i];
}
f.getline(rubish, 1);
for (int i=0; i < num_caches; ++i) {// Seteo caches
cache_register[i].free_size=size_caches;
}
for (int i = 0; i < num_endpoints; ++i) {
int number_of_connected_caches;
int datacenter_latency;
f >> datacenter_latency;
f >> number_of_connected_caches;
f.getline(rubish, 1);
for (int j = 0; j < number_of_connected_caches; ++j) {
int lat;
int cach;
f >> cach;
f >> lat;
f.getline(rubish, 1);
lat=datacenter_latency-lat;
if(lat <= 0){//ese cache no tiene sentido anyadirlo
continue;
}
endpoints_cache[i].insert(cach);
lat=-lat;//Mayores latencias posiciones mas bajas
conection insertar;
insertar.id_endpoint=i;
insertar.id_cache=cach;
latencias_ordenadas[lat].insert(insertar);
}
}
for (int i = 0; i < num_requests; ++i) {
int vid;
int end;
int req;
f >> vid >> end >> req;
endpoints_videos[end].insert(vid);
f.getline(rubish, 1);
}
cout << "Datos cargados" << endl;
}else{
cout << "Error: Fichero de entrada no abierto" <<endl;
error_cargar= true;
}
f.close();
}
/*
* Guarda el contenido de cache_register en los caches correspondientes
*/
void distribuirVideos(){
cout << "Inicio distribucion videos" << endl;
map<int,set<conection>>::const_iterator iter_latencias_ordenadas = latencias_ordenadas.begin();
int num_endpoints_actual_processed=0;
int num_max_endpoints_to_process=latencias_ordenadas.size();
while (iter_latencias_ordenadas != latencias_ordenadas.end()){//recorro todas las latencias
cout << num_endpoints_actual_processed << "/" << num_max_endpoints_to_process <<endl;
++num_endpoints_actual_processed;
if(iter_latencias_ordenadas->first>=0) {
++iter_latencias_ordenadas;
continue;
}
set<conection>::const_iterator iter_latencias_ordenadas_subset = iter_latencias_ordenadas->second.begin();
cout<<iter_latencias_ordenadas->second.size()<<endl;
while (iter_latencias_ordenadas_subset != iter_latencias_ordenadas->second.end()) {//recorro todas las latencias del subgrupo
conection conexion_actual=*iter_latencias_ordenadas_subset;
set<int> &videos_conexion_actual = endpoints_videos.find(conexion_actual.id_endpoint)->second;
set<int> &caches_conexion_actual = endpoints_cache.find(conexion_actual.id_endpoint)->second;
for (int i : videos_conexion_actual) {
bool video_almacenado = false;
for (int j : caches_conexion_actual) {
if(cache_register[j].videos.find(i)!=cache_register[j].videos.end())
{
video_almacenado=true;
break;
}
}
if(!video_almacenado){
if (cache_register[conexion_actual.id_cache].free_size>=size_of_videos[i]){
cache_register[conexion_actual.id_cache].free_size=cache_register[conexion_actual.id_cache].free_size - size_of_videos[i];
cache_register[conexion_actual.id_cache].videos.insert(i);
}
}
}
++iter_latencias_ordenadas_subset;
}
++iter_latencias_ordenadas;
}
cout << "Videos distribuidos" << endl;
}
void salida() {
ofstream f;
f.open(nombre_fichero_salida);
if (f.is_open()) {
cout << "Fichero salida creado" << endl;
int num_caches_usados=0;
for (int j = 0; j < num_caches ; ++j) {
if(cache_register[j].videos.size()>0) ++num_caches_usados;
}
f << num_caches_usados << endl;
for (int j = 0; j < num_caches ; ++j) {//cargar video
if(cache_register[j].videos.size()>0){
f << j ;
for (set<int>::iterator i = cache_register[j].videos.begin(); i != cache_register[j].videos.end(); ++i) {
f << " " << *i ;
}
f << endl;
}
}
}else{
cout << "Error: Al crear el fichero de salida" << endl;
error_guardar=true;
}
f.close();
}
int main(int argc, char *argv[]) {
if(argc!=3) cout << "Numero de argumentos incorrecto"<<endl;
nombre_fichero_entrada = argv[1];
nombre_fichero_salida = argv[2];
cargarDatos();
if(error_cargar) return -1;
distribuirVideos();
if(error_ejecuacion) return -1;
salida();
return 0;
}