-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.cpp
More file actions
70 lines (64 loc) · 2.08 KB
/
output.cpp
File metadata and controls
70 lines (64 loc) · 2.08 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
#include "output.h"
#include "info.h"
Output::Output() {
videosInCaches.resize(nCaches);
capacitiesTaken.resize(nCaches);
}
int Output::getScore() {
int total = 0;
int req_cnt = 0;
for(const auto& rd : requestDescriptions) {
int latency = endpoints[rd.endpointId].latencyToBase;
for(auto cache : endpoints[rd.endpointId].cachesConnectedToThisEndpoint){
if(videosInCaches[cache.cacheId].count(rd.videoId))
latency = min(latency, cache.cacheLatency);
}
total += rd.numberOfRequests * (endpoints[rd.endpointId].latencyToBase - latency);
req_cnt += rd.numberOfRequests;
}
return 1000 * total / req_cnt;
}
bool Output::addVideoToCache(int videoId, int cacheId) {
assert (videoId <= nVideos && videoId >= 0);
assert (cacheId <= nCaches && cacheId >= 0);
if (videosInCaches[cacheId].count(videoId)) {
//cerr << "Adding videi " << videoId << " to cache " << cacheId << " again\n";
return false;
}
if (capacitiesTaken[cacheId] + videoSizes[videoId] <= nCacheCapacity) {
capacitiesTaken[cacheId] += videoSizes[videoId];
videosInCaches[cacheId].insert(videoId);
return true;
}
return false;
}
bool Output::removeVideoFromCache(int videoId, int cacheId) {
assert (videoId <= nVideos && videoId >= 0);
assert (cacheId <= nCaches && cacheId >= 0);
if (!videosInCaches[cacheId].count(videoId)) {
cerr << "Removing not present video " << videoId << " to cache " << cacheId << endl;
return false;
}
videosInCaches[cacheId].erase(videoId);
capacitiesTaken[cacheId] -= videoSizes[videoId];
return true;
}
string Output::createOutput() {
ostringstream buff;
int n = 0;
for (auto &i : videosInCaches)
if (!i.empty())
n++;
buff << n << endl;
for (int i = 0; i < videosInCaches.size(); i++) {
if (videosInCaches[i].empty()) continue;
buff << i;
for (auto j : videosInCaches[i])
buff << " " << j;
buff << endl;
}
return buff.str();
}
void Output::printOutput() {
cout << this->createOutput();
}