/
specialstack.cpp
159 lines (135 loc) · 4.83 KB
/
specialstack.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
159
#include "specialstack.h"
#include <stdlib.h>
using namespace std;
SpecialStack::SpecialStack(){
this->offsetRegistro=0;
this->sizeRegistro=0;
this->offsetAvaillist=4;
}//constructor por defecto no hace nada
//3 es donde siempre empieza el availlist por que: el num de campos es de dos dig + la , 00,
//empieza a escribir justo despues de la ,
SpecialStack::SpecialStack(int valorInicial){
availlist.push_back(valorInicial);
this->offsetRegistro=0;
this->sizeRegistro=0;
this->offsetAvaillist=4;
}//constructor con un entero de argumento
string SpecialStack::toString(){
stringstream ss;
for (int i=0; i < availlist.size(); i++)
ss <<""<< availlist.at(i) << endl;
ss << "VIPList\n" << VIPList.toString();
return ss.str();
}//fin to string de availlist que imprime todo lo que hay en availlist
int SpecialStack::pop(){
int siguienteElementoDisponible;
if (!availlist.empty()){
siguienteElementoDisponible = availlist.back();
availlist.pop_back();
if (!VIPList.isEmpty())
VIPList.eliminarValor(siguienteElementoDisponible);
else
cerr << "ERROR: ListaOrdenada VIPList esta vacia en pop de SpecialStack." << endl;
} else {
siguienteElementoDisponible = -1;
}
return siguienteElementoDisponible;
}// fin poop :p
void SpecialStack::push(int valor){
availlist.push_back(valor);
VIPList.agregar(valor);
}// fin
int SpecialStack::peek(){
if (!availlist.empty())
return availlist.back();
return -1;
}// fin
int SpecialStack::posicionArchivo(int pos){//el valor que retorna aqui es la verdadera posicion en el archivo del registro que
//escogio el usuario para borrar del vector de registros
int RBCD0 = VIPList.cantidadNumerosSeguidosDesde(0);//registros borrados continuos desde cero
if (RBCD0<0){//significa que no ha borrado el registro 0
RBCD0=0;
}
int posAntes = VIPList.posicionesAntesDe(pos + RBCD0);
int posArchivo = posAntes+pos;
cout << "EN SpecialStack::posicionArchivo -> pos : " << pos << "\tRDCD0 : " << RBCD0 << "\tposAntes : " << posAntes << "\tposArchivi : " << posArchivo << endl;
return posArchivo;
}
bool SpecialStack::isEmpty(){
if (availlist.empty())
return true;
return false;
}
void SpecialStack::inicializar(ifstream& file){
char str[20];
string linea;
if (file.is_open()){
file.clear();
file.seekg(4, ios::beg);//coloca el offset donde esta el head del availlist en el header
file.getline(str, 20, ';');
linea = str;
int posDisp = cadena2Int(linea);//aqui toma el head del availlist
while (posDisp != -1){
cout << "En SpecialStack::inicializar pos que recibe : " << posDisp << endl;
file.seekg(offsetRegistro+(sizeRegistro * posDisp ));// se va a la poscion en el archvo que indica posDisp
file.getline(str, 20, ';');
linea = str;
availlist.insert(availlist.begin()+0, posDisp);//agrega la poscion al availlist
VIPList.agregar(posDisp);//agrega la posicion al VIPList que es la lista ordenada de las posciones en el availlist
posDisp = siguientePosicionDisponible(linea);
}
availlist.insert(availlist.begin()+0, posDisp);
VIPList.agregar(posDisp);
} else {
cerr << "ERROR: no se pudo abrir el archivo para lectura de levantar header" << endl;
}
}
int SpecialStack::siguientePosicionDisponible(string linea){
linea.erase(0,1);
//linea.erase(linea.size(), 1);
cout << "En SpecialStack::siguientePosicionDisponible " << linea << endl;
return atoi(linea.c_str());
}
int SpecialStack::getOffsetRegistro(){
return offsetRegistro;
}
int SpecialStack::getSizeRegistro(){
return sizeRegistro;
}
void SpecialStack::setOffsetRegistro(int offset){
this->offsetRegistro=offset;
}
void SpecialStack::setSizeRegistro(int size){
this->sizeRegistro=size;
}
int SpecialStack::cadena2Int(string linea){
while (linea[0] == '0')
linea.erase(0,1);
return atoi(linea.c_str());
}
int SpecialStack::buscarVIP(int valor){
return VIPList.buscar(valor);
}
string SpecialStack::headAvaillistArchivo(int valor){
stringstream ss;
cout<<"valor en headAvaillistArchivo es: "<<valor<<endl;
if (valor < 0){
ss <<"0000-1";
}else if(valor < 10){
ss << "00000" << valor;
} else if (valor <100){
ss<<"0000"<<valor;
} else if (valor < 1000){
ss<<"000"<<valor;
} else if (valor < 10000){
ss<<"00"<<valor;
} else if (valor < 100000){
ss<<"0"<<valor;
} else {
ss<<valor;
}
return ss.str();
}//fin al metodo que devuelve la cadena del head del availlist para escribirla en le header
void SpecialStack::actualizarAvaillist(vector<int> availlistActualizado){
availlist=availlistActualizado;
}