200, suma posicional, redispersión lineal

This commit is contained in:
2025-10-29 15:20:06 +01:00
parent 6bb476f53e
commit 39bcfbac80
8 changed files with 128 additions and 77 deletions

BIN
200.tar Normal file

Binary file not shown.

View File

@@ -1,7 +1,7 @@
GPP = /usr/bin/g++ GPP = /usr/bin/g++
OPTS = -Wall -Wno-deprecated -std=c++17 -g OPTS = -Wall -Wno-deprecated -std=c++17 -O2
a.out: diccionariocuacs.o cuac.o fecha.o a.out: diccionariocuacs.o cuac.o fecha.o tablahash.o
$(GPP) $(OPTS) main.cpp diccionariocuacs.o cuac.o fecha.o $(GPP) $(OPTS) main.cpp tablahash.o cuac.o fecha.o diccionariocuacs.o
fecha.o: fecha.cpp fecha.hpp fecha.o: fecha.cpp fecha.hpp
$(GPP) $(OPTS) -c fecha.cpp $(GPP) $(OPTS) -c fecha.cpp
@@ -9,7 +9,12 @@ fecha.o: fecha.cpp fecha.hpp
cuac.o: cuac.hpp cuac.cpp fecha.hpp cuac.o: cuac.hpp cuac.cpp fecha.hpp
$(GPP) $(OPTS) -c cuac.cpp $(GPP) $(OPTS) -c cuac.cpp
diccionariocuacs.o: diccionariocuacs.hpp diccionariocuacs.cpp cuac.hpp fecha.hpp tablahash.o: cuac.hpp fecha.hpp tablahash.cpp tablahash.hpp
$(GPP) $(OPTS) -c tablahash.cpp
diccionariocuacs.o: diccionariocuacs.cpp diccionariocuacs.hpp cuac.hpp fecha.hpp tablahash.hpp
$(GPP) $(OPTS) -c diccionariocuacs.cpp $(GPP) $(OPTS) -c diccionariocuacs.cpp
clean:
rm *.o a.out

View File

@@ -37,7 +37,7 @@ const string pcuac[30] = {"Afirmativo.",
class Cuac { class Cuac {
private: private:
friend class DiccionarioCuacs; friend class TablaHash;
string usuario; string usuario;
Fecha fecha; Fecha fecha;
string mensaje; string mensaje;

View File

@@ -1,65 +1,22 @@
#include "diccionariocuacs.hpp" #include "diccionariocuacs.hpp"
DiccionarioCuacs::DiccionarioCuacs() { DiccionarioCuacs::DiccionarioCuacs(int m) {
contador = 0; TablaHash th = TablaHash(m);
this -> tabla = th;
} }
void DiccionarioCuacs::insertar(Cuac nuevo) { void DiccionarioCuacs::insertar(Cuac nuevo) {
list<Cuac>::iterator it = lista.begin(); tabla.insertar(nuevo);
while (it != lista.end() && nuevo.comparar(*it)){
it++;
}
if (it==lista.end() || !nuevo.comparar(*it)) {
lista.insert(it--, nuevo);
it++;
}
contador++;
} }
void DiccionarioCuacs::last(int n){
cout << "last " << n << endl;
list<Cuac>::iterator it = lista.begin();
int i = 0;
while (it != lista.end() && n--) {
Cuac c = *it;
i++;
cout << i << ". ";
c.escribir();
it++;
}
cout << "Total: " << i << " cuac" << endl;
}
void DiccionarioCuacs::follow(string nombre){ void DiccionarioCuacs::follow(string nombre){
cout << "follow " << nombre << endl; cout << "follow " << nombre << endl;
list<Cuac>::iterator it; tabla.consultar(nombre);
int i = 0;
for (it = lista.begin(); it != lista.end(); it++) {
Cuac c = *it;
if (c.usuario == nombre) {
i++;
cout << i << ". ";
c.escribir();
}
}
cout << "Total: " << i << " cuac" << endl;
}
void DiccionarioCuacs::date(Fecha f1, Fecha f2){
cout << "date ";
f1.escribir();
cout << ' ';
f2.escribir();
cout << '\n';
int i = 0;
list<Cuac>::iterator it;
for (it = lista.begin(); it != lista.end(); it++) {
Cuac c = *it;
if ((f1.es_menor(c.fecha) && !f2.es_menor(c.fecha)) || f1.es_igual(c.fecha) || f2.es_igual(c.fecha)) {
i++;
cout << i << ". ";
c.escribir();
}
}
cout << "Total: " << i << " cuac" << endl;
} }
int DiccionarioCuacs::elem() {
return tabla.elem();
}
DiccionarioCuacs::~DiccionarioCuacs() {
delete[] tabla.lista;
}

View File

@@ -1,22 +1,19 @@
#pragma once #pragma once
#include "fecha.hpp" #include "fecha.hpp"
#include "cuac.hpp" #include "cuac.hpp"
#include <list> #include "tablahash.hpp"
#include <string> #include <string>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class DiccionarioCuacs { class DiccionarioCuacs {
private: private:
list<Cuac> lista; TablaHash tabla;
int contador;
public: public:
DiccionarioCuacs(); DiccionarioCuacs(int m);
~DiccionarioCuacs();
void insertar(Cuac nuevo); void insertar(Cuac nuevo);
void last(int n);
void follow(string nombre); void follow(string nombre);
void date(Fecha f1, Fecha f2); int elem();
int numElem() { return contador; }
}; };

View File

@@ -4,22 +4,23 @@
#include "fecha.hpp" #include "fecha.hpp"
#include "cuac.hpp" #include "cuac.hpp"
#include "diccionariocuacs.hpp" #include "diccionariocuacs.hpp"
#include "tablahash.hpp"
using namespace std; using namespace std;
DiccionarioCuacs dic; DiccionarioCuacs dic = DiccionarioCuacs(20000);
void procesar_pcuac() { void procesar_pcuac() {
Cuac nuevo; Cuac nuevo;
nuevo.leer_pcuac(); nuevo.leer_pcuac();
dic.insertar(nuevo); dic.insertar(nuevo);
cout << dic.numElem() << " cuac" << endl;; cout << dic.elem() << " cuac" << endl;
} }
void procesar_mcuac() { void procesar_mcuac() {
Cuac nuevo; Cuac nuevo;
nuevo.leer_mcuac(); nuevo.leer_mcuac();
dic.insertar(nuevo); dic.insertar(nuevo);
cout << dic.numElem() << " cuac" << endl; cout << dic.elem() << " cuac" << endl;
} }
void procesar_follow() { void procesar_follow() {
@@ -31,14 +32,12 @@ void procesar_follow() {
void procesar_last() { void procesar_last() {
int n; int n;
cin >> n; cin >> n;
dic.last(n);
} }
void procesar_date() { void procesar_date() {
Fecha f1, f2; Fecha f1, f2;
f1.leer(); f1.leer();
f2.leer(); f2.leer();
dic.date(f1, f2);
} }
int main() { int main() {
@@ -51,12 +50,10 @@ int main() {
} else if (comando == "mcuac") { } else if (comando == "mcuac") {
procesar_mcuac(); procesar_mcuac();
cuacs++; cuacs++;
} else if (comando == "last") {
procesar_last();
} else if (comando == "follow") { } else if (comando == "follow") {
procesar_follow(); procesar_follow();
} else if (comando == "date") { } else if (comando == "exit") {
procesar_date(); break;
} }
} }

62
tablahash.cpp Normal file
View File

@@ -0,0 +1,62 @@
#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;
}

33
tablahash.hpp Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <list>
#include "cuac.hpp"
#include "fecha.hpp"
#include <iostream>
#include <string>
using namespace std;
class TablaHash {
private:
friend class DiccionarioCuacs;
int nElem;
int M;
list<Cuac> *lista;
public:
// implementar dispersión abierta
// tamaño variable (memoria dinámica)
// probar funciones de dispersión
// suma posicional
// posicional por trozos
// extracción
// etc
TablaHash();
TablaHash(int M);
~TablaHash();
void insertar(Cuac nuevo);
void consultar(string nombre);
unsigned int h(string clave);
// unsigned int h_spt(string clave);
int elem() { return nElem; }
};