diff --git a/200.tar b/200.tar new file mode 100644 index 0000000..1968566 Binary files /dev/null and b/200.tar differ 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; } +}; +