From 39bcfbac80fd31bdefce087c26ed383662a72cf6 Mon Sep 17 00:00:00 2001 From: binlab Date: Wed, 29 Oct 2025 15:20:06 +0100 Subject: [PATCH] =?UTF-8?q?200,=20suma=20posicional,=20redispersi=C3=B3n?= =?UTF-8?q?=20lineal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 200.tar | Bin 0 -> 20480 bytes Makefile | 13 ++++++--- cuac.hpp | 2 +- diccionariocuacs.cpp | 67 ++++++++----------------------------------- diccionariocuacs.hpp | 13 ++++----- main.cpp | 15 ++++------ tablahash.cpp | 62 +++++++++++++++++++++++++++++++++++++++ tablahash.hpp | 33 +++++++++++++++++++++ 8 files changed, 128 insertions(+), 77 deletions(-) create mode 100644 200.tar create mode 100644 tablahash.cpp create mode 100644 tablahash.hpp diff --git a/200.tar b/200.tar new file mode 100644 index 0000000000000000000000000000000000000000..1968566edbc0c6b0e66c6c42530611a4e401e24d GIT binary patch literal 20480 zcmeHOZFAek5%yQ|SDcvIp)83eDT$h)CC{{t+kB{!bnLd%aXg+Qa3s$Ga135(%Zl?~ z^06N~{R#eKeRl6~0AFOeP3Wl`csv4dSnTfY?(N$hgF*(qV6nKf!+(AJJ3BkOgUjIT zv`<(1S6&AvXZ<^a(*fQd_lIYLJN-d_bUeHx`a9&wqb{;srb66#8T+BFH>olC))$}l z;Ac7sq9RnH6Obj$7mH52$aFFjiHudYkbx3e9zJZhmqv%;_W&A?RTiXrqSNlaxNf%s zQ{>|OTohR$Q*G$BBRUV;t*Hv;vR46PsD$l&n+O3gz{V=c9?YAqmBBjz7 zF)`<;=|ntwR1))42&}tL6m-(lv&5u|A7?6$bfUT#J}gNAYzvJ=MMEdKNGz_~XwiED zEoX5d7|*jr9ErhmCNwc7a)Jv?ypX9(yCg?(FJSpScp#=S${=SgR3=gtXqDtj7@?w) zWl@wfCJ+!aDWoM#8scxHUhBZj(sw^iac#GNFi(pTpG5gC!a z`$xM*K!GbMFe@W;66!#P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rn9ksQ%iPMHVD=>5C}UYwF>egTjZu;Vu z!PwE?(5l1V#2kfcfnTMemlBF2{21^eI5-%biKDNFZP~+*|Hg21Yh461cxht^T}nP5 zh~A~I6Y&Ez@tZE_?&CtV_R-q0hfwQK>j*YUwA&8(*ipd=7I%jxM!U5}1b zBZ!nt(CJEJaUa@bb$_z8t~dJTrjbA+fkpz21R4o65@;mQNT88GBY{Q&jRYDAG!kee N&`6+>z^_CC{{}5P)gJ%= literal 0 HcmV?d00001 diff --git a/Makefile b/Makefile index c7dc4e0..170a846 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ GPP = /usr/bin/g++ -OPTS = -Wall -Wno-deprecated -std=c++17 -g -a.out: diccionariocuacs.o cuac.o fecha.o - $(GPP) $(OPTS) main.cpp diccionariocuacs.o cuac.o fecha.o +OPTS = -Wall -Wno-deprecated -std=c++17 -O2 +a.out: diccionariocuacs.o cuac.o fecha.o tablahash.o + $(GPP) $(OPTS) main.cpp tablahash.o cuac.o fecha.o diccionariocuacs.o fecha.o: fecha.cpp fecha.hpp $(GPP) $(OPTS) -c fecha.cpp @@ -9,7 +9,12 @@ fecha.o: fecha.cpp fecha.hpp cuac.o: cuac.hpp cuac.cpp fecha.hpp $(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 +clean: + rm *.o a.out diff --git a/cuac.hpp b/cuac.hpp index fbfb8e3..c39dac3 100644 --- a/cuac.hpp +++ b/cuac.hpp @@ -37,7 +37,7 @@ const string pcuac[30] = {"Afirmativo.", class Cuac { private: - friend class DiccionarioCuacs; + friend class TablaHash; string usuario; Fecha fecha; string mensaje; diff --git a/diccionariocuacs.cpp b/diccionariocuacs.cpp index 6351581..ba343aa 100644 --- a/diccionariocuacs.cpp +++ b/diccionariocuacs.cpp @@ -1,65 +1,22 @@ #include "diccionariocuacs.hpp" -DiccionarioCuacs::DiccionarioCuacs() { - contador = 0; +DiccionarioCuacs::DiccionarioCuacs(int m) { + TablaHash th = TablaHash(m); + this -> tabla = th; } - void DiccionarioCuacs::insertar(Cuac nuevo) { - list::iterator it = lista.begin(); - while (it != lista.end() && nuevo.comparar(*it)){ - it++; - } - if (it==lista.end() || !nuevo.comparar(*it)) { - lista.insert(it--, nuevo); - it++; - } - contador++; + tabla.insertar(nuevo); } -void DiccionarioCuacs::last(int n){ - cout << "last " << n << endl; - list::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){ cout << "follow " << nombre << endl; - list::iterator it; - 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::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; + tabla.consultar(nombre); } +int DiccionarioCuacs::elem() { + return tabla.elem(); +} + +DiccionarioCuacs::~DiccionarioCuacs() { + delete[] tabla.lista; +} diff --git a/diccionariocuacs.hpp b/diccionariocuacs.hpp index dd51992..e6b07a2 100644 --- a/diccionariocuacs.hpp +++ b/diccionariocuacs.hpp @@ -1,22 +1,19 @@ #pragma once #include "fecha.hpp" #include "cuac.hpp" -#include +#include "tablahash.hpp" #include #include using namespace std; class DiccionarioCuacs { private: - list lista; - int contador; - + TablaHash tabla; public: - DiccionarioCuacs(); + DiccionarioCuacs(int m); + ~DiccionarioCuacs(); void insertar(Cuac nuevo); - void last(int n); void follow(string nombre); - void date(Fecha f1, Fecha f2); - int numElem() { return contador; } + int elem(); }; diff --git a/main.cpp b/main.cpp index 8e16e7d..8c3926c 100644 --- a/main.cpp +++ b/main.cpp @@ -4,22 +4,23 @@ #include "fecha.hpp" #include "cuac.hpp" #include "diccionariocuacs.hpp" +#include "tablahash.hpp" using namespace std; -DiccionarioCuacs dic; +DiccionarioCuacs dic = DiccionarioCuacs(20000); void procesar_pcuac() { Cuac nuevo; nuevo.leer_pcuac(); dic.insertar(nuevo); - cout << dic.numElem() << " cuac" << endl;; + cout << dic.elem() << " cuac" << endl; } void procesar_mcuac() { Cuac nuevo; nuevo.leer_mcuac(); dic.insertar(nuevo); - cout << dic.numElem() << " cuac" << endl; + cout << dic.elem() << " cuac" << endl; } void procesar_follow() { @@ -31,14 +32,12 @@ void procesar_follow() { void procesar_last() { int n; cin >> n; - dic.last(n); } void procesar_date() { Fecha f1, f2; f1.leer(); f2.leer(); - dic.date(f1, f2); } int main() { @@ -51,12 +50,10 @@ int main() { } else if (comando == "mcuac") { procesar_mcuac(); cuacs++; - } else if (comando == "last") { - procesar_last(); } else if (comando == "follow") { procesar_follow(); - } else if (comando == "date") { - procesar_date(); + } else if (comando == "exit") { + break; } } diff --git a/tablahash.cpp b/tablahash.cpp new file mode 100644 index 0000000..ccf522a --- /dev/null +++ b/tablahash.cpp @@ -0,0 +1,62 @@ +#include "tablahash.hpp" +TablaHash::TablaHash(int M) { + nElem = 0; + this -> M = M; + this -> lista = new list[M]; +} + +TablaHash::TablaHash() { +} + +TablaHash::~TablaHash() { +} + +// eeeeeeeeeeeeeeeeeeeh +void TablaHash::insertar(Cuac nuevo) { + int pos = h(nuevo.usuario); + list::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::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; +} diff --git a/tablahash.hpp b/tablahash.hpp new file mode 100644 index 0000000..d1c84b1 --- /dev/null +++ b/tablahash.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include "cuac.hpp" +#include "fecha.hpp" +#include +#include +using namespace std; + +class TablaHash { + private: + friend class DiccionarioCuacs; + int nElem; + int M; + list *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; } +}; +