63 lines
1.3 KiB
C++
63 lines
1.3 KiB
C++
#include "tablahash.hpp"
|
|
TablaHash::TablaHash(int M) {
|
|
nElem = 0;
|
|
this -> M = M;
|
|
this -> lista = new list<Cuac>[M];
|
|
}
|
|
|
|
TablaHash::TablaHash() {
|
|
}
|
|
|
|
TablaHash::~TablaHash() {
|
|
}
|
|
|
|
// eeeeeeeeeeeeeeeeeeeh
|
|
void TablaHash::insertar(Cuac nuevo) {
|
|
int pos = h(nuevo.usuario);
|
|
list<Cuac>::iterator it = lista[pos].begin();
|
|
while (it != lista[pos].end() && nuevo.comparar(*it)){
|
|
it++;
|
|
}
|
|
if (it==lista[pos].end() || !nuevo.comparar(*it)) {
|
|
lista[pos].insert(it--, nuevo);
|
|
it++;
|
|
}
|
|
nElem++;
|
|
}
|
|
|
|
// eeeeeeeeeeeeeeeeh
|
|
// hay que consultar de más reciente a más antiguo
|
|
void TablaHash::consultar(string clave) {
|
|
int pos = h(clave);
|
|
int i = 0;
|
|
for (list<Cuac>::iterator it = lista[pos].begin(); it != lista[pos].end(); it++) {
|
|
Cuac c = *it;
|
|
i++;
|
|
cout << i << ". " << c.usuario << " ";
|
|
c.fecha.escribir();
|
|
cout << '\n' << " " << c.mensaje << endl;
|
|
}
|
|
cout << "Total: " << i << " cuac" << endl;
|
|
|
|
}
|
|
|
|
// probamos suma posicional
|
|
// necesita redispersión
|
|
// probamos lineal
|
|
unsigned int TablaHash::h(string clave) {
|
|
unsigned int res = 0;
|
|
for (int i = 0; i < clave.length(); i++) {
|
|
res = 67 * res + clave[i];
|
|
}
|
|
|
|
while (!lista[res % M].empty()) {
|
|
Cuac c = lista[res % M].front();
|
|
if (clave != c.usuario) {
|
|
res = res + 1;
|
|
} else {
|
|
return res % M;
|
|
}
|
|
}
|
|
return res % M;
|
|
}
|