-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSTF.cpp
68 lines (62 loc) · 1.4 KB
/
SSTF.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
#include "SSTF.h"
SSTF::SSTF(HardDrive drive, std::vector<AccessTask> tab) : hardDrive(drive)
{
for (AccessTask a : tab) {
if (a.getDelay() == 0) {
map[a.getBlock()] = a;
}
else
{
waitList.push_back(a);
}
}
std::sort(waitList.begin(), waitList.end(), [](AccessTask &a, AccessTask &b) {
return a.getDelay() < b.getDelay();
});
}
SSTF::~SSTF()
{
}
void SSTF::run()
{
int time = 0;
while (!map.empty() || !waitList.empty()) {
if(map.size() > 0) {
std::map<int, AccessTask>::iterator it, it2;
it = map.upper_bound(hardDrive.getPosition());
it2 = it--;
if (it2 == map.end()) {
it2 = it;
}
else if (it == map.end()) {
it = it2;
}
int odl1 = abs(it->second.getBlock() - hardDrive.getPosition());
int odl2 = abs(it2->second.getBlock() - hardDrive.getPosition());
if (odl1 <= odl2) {
int move = hardDrive.moveTo(it->second.getBlock());
distance += move;
time += move;
map.erase(it);
}
else if (odl2 < odl1) {
int move = hardDrive.moveTo(it2->second.getBlock());
distance += move;
time += move;
map.erase(it2);
}
}
else {
time++;
}
for (; !waitList.empty() && time >= waitList[0].getDelay(); )
{
map[waitList[0].getBlock()] = waitList[0];
waitList.erase(waitList.begin());
}
}
}
void SSTF::statistic()
{
std::cout << "Odleglosc przebyta przez glowice w przypadku SSTF wyniosla: " << distance << std::endl;
}