-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminal.cc
215 lines (193 loc) · 5.98 KB
/
Terminal.cc
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
#include "Terminal.hh"
#include <iostream>
using namespace std;
Terminal::Terminal() {
n = 0;
m = 0;
h = 0;
}
Terminal::Terminal(const Terminal& t) {
n = t.n;
m = t.m;
h = t.h;
terminal = t.terminal;
}
Terminal::~Terminal() {}
void Terminal::dimensiona(int fil, int pla, int pis) {
n = fil;
m = pla;
h = pis;
terminal = vector<vector<vector<string>>> (n,vector<vector<string>>(m,vector<string>(h)));
}
int Terminal::nombre_fileres() const {
return n;
}
int Terminal::nombre_places() const {
return m;
}
int Terminal::nombre_pisos() const {
return h;
}
bool Terminal::esta_ocupada(const Ubicacion& u) const {
return (terminal[u.hilera()][u.plaza()][u.piso()] != "");
}
bool Terminal::existeix_ubi(int i, int j, int k) const {
if (i < 0 or i >= n) return false;
else if (j < 0 or j >= m) return false;
else if (k < 0 or k >= h) return false;
else return true;
}
void Terminal::mostra_cont(int ubi_fileres, int ubi_places, int ubi_pisos) const {
if (terminal[ubi_fileres][ubi_places][ubi_pisos] != "") {
cout << terminal[ubi_fileres][ubi_places][ubi_pisos] << endl;
} else {
cout << endl;
}
}
void Terminal::imprimeix_magatzem() {
for (int i=0; i<n; ++i) {
cout << "hilera " << i << endl;
for (int k=h-1; k>=0; --k) {
cout << k << ' ';
for (int j=0; j<m; ++j) {
if (terminal[i][j][k] != "") {
string s = terminal[i][j][k];
cout << s.at(0);
}
else cout << ' ';
}
cout << endl;
}
cout << " ";
for (int npla=0; npla<m; ++npla) cout << npla%10;
cout << endl << endl;
}
}
void Terminal::funcio_recursiva(const Ubicacion& u, int lon, Conj_huecos& ch, Area_espera& aesp, Conj_contenedor& cc) {
int i=u.hilera(), j=u.plaza(), k=u.piso();
for (int it=0; it<lon and k+1<h; ++it) {
string mat = terminal[i][j+it][k+1];
if (mat != "") {
Ubicacion pos = cc.ubi(mat);
int length = cc.longitud(mat);
funcio_recursiva(pos,length,ch,aesp,cc);
}
}
string matricula = terminal[i][j][k];
Contenedor cont(matricula, lon);
retira_contenidor(cont,u); //Desapareix del p.i. terminal
cc.elimina_contenidor(matricula); //Desapareix del cc
Ubicacion ubic(-1,0,0);
cc.afegeix_contenidor(cont, ubic); //S'afegeix al cc com a element àrea espera
regenera_retira(u,ch,lon); // posar en private si no surt a program.cc
aesp.afegeix_contenidor(cont);
}
void Terminal::inserta_area_espera(Conj_huecos& ch, Area_espera& aesp, Conj_contenedor& cc) {
bool es_pot_colocar = true;
list<Contenedor> area_espera = aesp.llista();
while (es_pot_colocar) {
list<Contenedor>::iterator it = area_espera.begin();
bool trobat = false;
while (it != area_espera.end() and !trobat) {
Ubicacion u(ch.espai_valid((*it).longitud())); //troba l'espai vàlid per al cont
Ubicacion u_no_existeix(-1,0,0);
if (u == u_no_existeix) ++it;
else {
Contenedor c((*it).matricula(),(*it).longitud());
afegeix_contenidor(c,u);
regenera_inserta(u,ch,(*it).longitud());
cc.elimina_contenidor((*it).matricula());
cc.afegeix_contenidor(c,u);
aesp.retira_contenidor((*it).matricula());
it = area_espera.erase(it);
trobat = true;
}
}
if (it == area_espera.end() and !trobat) es_pot_colocar = false;
}
}
void Terminal::afegeix_contenidor(const Contenedor& cont, Ubicacion& u/*, Area_espera& aesp*/) {
for (int i=0; i<cont.longitud(); ++i) {
terminal[u.hilera()][u.plaza()+i][u.piso()] = cont.matricula();
}
}
void Terminal::retira_contenidor(const Contenedor& cont, const Ubicacion& u) {
for (int i=0; i<cont.longitud(); ++i) {
terminal[u.hilera()][u.plaza()+i][u.piso()] = "";
}
}
void Terminal::regenera_inserta(const Ubicacion& u, Conj_huecos& ch, int lon) {
int i = u.hilera(), j = u.plaza(), k = u.piso();
int dist_r = dist_dreta(i,j+lon,k);
if (dist_r != 0) {
Ubicacion ubic(i,j+lon,k);
ch.afegeix_espai(ubic,dist_r);
}
if (k+1 < h) {
int dist_l_sup = dist_esquerra(i,j-1,k+1);
int dist_r_sup = dist_dreta(i,j+lon,k+1);
if (dist_l_sup != 0) {
Ubicacion ubic_sup(i,j-dist_l_sup,k+1);
ch.elimina_espai(ubic_sup,dist_l_sup);
ch.afegeix_espai(ubic_sup,dist_l_sup+lon+dist_r_sup);
} else {
Ubicacion ubic_sup(i,j,k+1);
ch.afegeix_espai(ubic_sup,lon+dist_r_sup);
}
if (dist_r_sup != 0) {
Ubicacion ubic_sup(i,j+lon,k+1);
ch.elimina_espai(ubic_sup,dist_r_sup);
}
}
}
void Terminal::regenera_retira(const Ubicacion& u, Conj_huecos& ch, int lon) {
int i = u.hilera(), j = u.plaza(), k = u.piso();
int dist_r = dist_dreta(i,j+lon,k);
int dist_l = dist_esquerra(i,j-1,k);
if (dist_l != 0) {
Ubicacion ubic(i,j-dist_l,k);
ch.elimina_espai(ubic,dist_l);
ch.afegeix_espai(ubic,dist_l+lon+dist_r);
} else {
Ubicacion ubic(i,j,k);
ch.afegeix_espai(ubic,lon+dist_r);
}
if (dist_r != 0) {
Ubicacion ubic(i,j+lon,k);
ch.elimina_espai(ubic,dist_r);
}
if (k+1 < h) {
int dist_l_sup = dist_esquerra(i,j-1,k+1);
int dist_r_sup = dist_dreta(i,j+lon,k+1);
if (dist_l_sup != 0) {
Ubicacion ubic_sup(i,j-dist_l_sup,k+1);
ch.elimina_espai(ubic_sup,dist_l_sup+lon+dist_r_sup);
ch.afegeix_espai(ubic_sup,dist_l_sup);
} else {
Ubicacion ubic_sup(i,j,k+1);
ch.elimina_espai(ubic_sup,lon+dist_r_sup);
}
if (dist_r_sup != 0) {
Ubicacion ubic_sup(i,j+lon,k+1);
ch.afegeix_espai(ubic_sup,dist_r_sup);
}
}
}
int Terminal::dist_esquerra(int i, int j, int k) {
int dist_esq = 0;
bool final = false;
while (!final and j-dist_esq >= 0) {
if (terminal[i][j-dist_esq][k]=="" and (k==0 or terminal[i][j-dist_esq][k-1]!="")) ++dist_esq;
else final = true;
}
return dist_esq;
}
int Terminal::dist_dreta(int i, int j, int k) {
int dist_dre = 0;
bool final = false;
while (!final and j+dist_dre < m) {
if (terminal[i][j+dist_dre][k]=="" and (k==0 or terminal[i][j+dist_dre][k-1]!="")) ++dist_dre;
else final = true;
}
return dist_dre;
}