-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
158 lines (119 loc) · 4.59 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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include "include/Hash.h"
#define PATH_FILE_IN "./GoogleFiles/trending_today.in"
#define PATH_FILE_OUT "./trending_today.out"
using namespace std;
void getFirstLine(Hash &hash, string buff) {
string token;
istringstream iss(buff);
for (int i = 0; getline(iss, token, ' '); i++)
(i == 0) ? hash.nbVideos = atoi(token.c_str()) :
(i == 1) ? hash.nbEndpoints = atoi(token.c_str()) :
(i == 2) ? hash.nbRequests = atoi(token.c_str()) :
(i == 3) ? hash.nbCaches = atoi(token.c_str()) :
(i == 4) ? hash.cacheCapacity = atoi(token.c_str()) : 0;
}
void getVideos(Hash &hash, string buff) {
string token;
istringstream iss(buff);
for (int i = 0; getline(iss, token, ' '); i++) {
Video video(i, atoi(token.c_str()));
hash.videos.push_back(video);
}
}
void getCacheLatency(Endpoint &endpoint, ifstream &file) {
int lat, idCache;
string token;
string buff;
for (int k = 0; k < endpoint.nbCache; k++) {
getline(file, buff);
istringstream iss(buff);
for (int i = 0; getline(iss, token, ' '); i++)
(i == 0) ? idCache = atoi(token.c_str()) :
(i == 1) ? lat = atoi(token.c_str()) : 0;
Latency latency(idCache, lat);
endpoint.cacheLatency.push_back(latency);
}
}
void getEndpoints(Hash &hash, ifstream &file) {
int latency, nbCache;
string token;
string buff;
for (int k = 0; k < hash.nbEndpoints; k++) {
getline(file, buff);
istringstream iss(buff);
for (int i = 0; getline(iss, token, ' '); i++)
(i == 0) ? latency = atoi(token.c_str()) :
(i == 1) ? nbCache = atoi(token.c_str()) : 0;
Endpoint endpoint(k, latency, nbCache);
getCacheLatency(endpoint, file);
hash.endpoints.push_back(endpoint);
}
}
void getRequests(Hash &hash, ifstream &file) {
int idVideo, idEndpoint, nbReq;
string token;
string buff;
while (getline(file, buff)) {
istringstream iss(buff);
for (int i = 0; getline(iss, token, ' '); i++)
(i == 0) ? idVideo = atoi(token.c_str()) :
(i == 1) ? idEndpoint = atoi(token.c_str()) :
(i == 2) ? nbReq = atoi(token.c_str()) : 0;
Request request(idVideo, nbReq, hash.endpoints[idEndpoint]);
hash.videos[idVideo].req.push_back(request);
}
}
int getInfos(Hash &hash) {
ifstream file(PATH_FILE_IN, ios::in);
string buff;
if (!file) {
cout << "Error opening file" << endl;
return (-1);
}
getline(file, buff);
getFirstLine(hash, buff);
getline(file, buff);
getVideos(hash, buff);
getEndpoints(hash, file);
getRequests(hash, file);
for (int i = 0; i < hash.nbCaches; i++) {
Cache cache(i);
hash.cache.push_back(cache);
}
file.close();
return (0);
}
void myAlgo(Hash &hash) {
for (int i = 0; i < hash.videos.size(); i++) {
for (int k = 0; k < hash.videos[i].req.size(); k++) {
bool full = false;
for (int j = 0; j < hash.videos[i].req[k].endpoint.nbCache; j++) {
if (!full && hash.cache[hash.videos[i].req[k].endpoint.cacheLatency[j].idCache].size >= hash.videos[i].size) {
hash.cache[hash.videos[i].req[k].endpoint.cacheLatency[j].idCache].videos.push_back(hash.videos[i]);
full = true;
}
}
}
}
}
void display(Hash hash)
{
ofstream file(PATH_FILE_OUT, ios::out | ios::trunc);
file << hash.nbCaches << endl;
for (int i = 0; i < hash.cache.size(); i++) {
file << hash.cache[i].id;
for (int j = 0; j < hash.cache[i].videos.size(); j++)
file << " " << hash.cache[i].videos[j].id;
file << endl;
}
}
int main() {
Hash hash;
getInfos(hash);
myAlgo(hash);
display(hash);
return (0);
}