-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineManager.cpp
106 lines (92 loc) · 2.84 KB
/
LineManager.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
#include <iostream>
#include <fstream>
#include <algorithm>
#include "LineManager.h"
#include "Utilities.h"
namespace sdds {
LineManager::LineManager(const std::string& file, const std::vector<Workstation*>& stations) {
std::ifstream infile(file);
if (!infile) {
throw "ERROR: Unable to open file";
}
Utilities utils{};
std::string str{};
size_t next_pos{ 0 };
size_t next_copy = next_pos;
bool more{ true };
std::vector<std::string> leftStations;
std::vector<std::string> rightStations;
while (std::getline(infile, str)) {
next_pos = next_copy;
std::string tempCurrStation{}, tempNextStation{};
tempCurrStation = utils.extractToken(str, next_pos, more);
if (!(next_pos == next_copy)) {
tempNextStation = utils.extractToken(str, next_pos, more);
leftStations.push_back(tempCurrStation);
rightStations.push_back(tempNextStation);
}
auto currStation = std::find_if(stations.begin(), stations.end(), [&](Workstation* station) {
return station->getItemName() == tempCurrStation;
});
m_activeLine.push_back(*currStation);
if (tempNextStation.length() > 0) {
auto nextStation = std::find_if(stations.begin(), stations.end(), [&](Workstation* station) {
return station->getItemName() == tempNextStation;
});
(*currStation)->setNextStation(*nextStation);
}
}
size_t count{};
std::string fStn{};
for (const auto& leftStation : leftStations) {
count = 0;
for (const auto& rightStation : rightStations) {
if (leftStation == rightStation) {
count++;
}
}
if (count == 0) {
fStn = leftStation;
break;
}
}
auto firstStation = std::find_if(stations.begin(), stations.end(), [&](Workstation* station) {
return station->getItemName() == fStn;
});
m_firstStation = *firstStation;
m_cntCustomer = g_pending.size();
}
void LineManager::reorderStations() {
std::vector<Workstation*> reorderedStations;
Workstation* currentStation = m_firstStation;
while (currentStation != nullptr) {
reorderedStations.push_back(currentStation);
currentStation = currentStation->getNextStation();
}
m_activeLine = reorderedStations;
}
bool LineManager::run(std::ostream& os) {
static size_t iterationCount = 1;
os << "Line Manager Iteration: " << iterationCount << std::endl;
++iterationCount;
if (!g_pending.empty()) {
*m_firstStation += std::move(g_pending.front());
g_pending.pop_front();
}
std::for_each(m_activeLine.begin(), m_activeLine.end(),
[&](auto& i) {
i->fill(os);
});
std::for_each(m_activeLine.begin(), m_activeLine.end(),
[&](auto& i) {
i->attemptToMoveOrder();
});
return g_completed.
size() + g_incomplete.size() == m_cntCustomer;
}
void LineManager::display(std::ostream& os) const {
std::for_each(m_activeLine.begin(), m_activeLine.end(), [&](auto station) {
station->display(os);
});
}
}