Repository navigation
Expand file tree
/
Copy pathMyHashTable.cpp
More file actions
89 lines (81 loc) · 2.98 KB
/
Copy pathMyHashTable.cpp
File metadata and controls
89 lines (81 loc) · 2.98 KB
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
#include "MyHashTable.h"
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <functional>
/* Empezamos con 601 para que haya muy pocos rehash y todo jale aca mas resio */
MyHashTable::MyHashTable() {
this->size = 0;
this->sizeA = 601;
this->tabla = new MyLinkedList[this->sizeA];
}
MyHashTable::~MyHashTable() {
// delete[] en lugar de delete
// para borrar memoria de un array
// robado descaradamente de "https://www.geeksforgeeks.org/cpp/delete-in-c/"
delete[] this->tabla;
}
bool MyHashTable::contains(std::string key) const {
int pos = getPos(key);
return tabla[pos].contains(key);
}
bool MyHashTable::isEmpty() {
return this->size == 0;
}
/* Calcula la posicion del arreglo para una clave
O(1) */
int MyHashTable::getPos(std::string key) const {
// devuelve un numerote
// https://www.geeksforgeeks.org/cpp/stdhash-class-in-c-stl/#syntax-of-hash-class
std::hash<std::string> hasher;
std::size_t hashCode = hasher(key);
return static_cast<int>(hashCode % this->sizeA);
}
/* Inserta o actualiza un par
Hace rehashing si el factor de carga supera al 0.75
O(1) con buena dispersion y factor de carga < 0.75
Pero sin esto, puede llegar hasta O(n), no bueno */
void MyHashTable::put(std::string key, std::string fechaHora) {
int pos = getPos(key);
// Si ya existe la IP, solo agregamos la nueva fecha al vector
if (tabla[pos].contains(key)) {
tabla[pos].get(key).push_back(fechaHora);
} else {
// Si no, generamos un nuevo
tabla[pos].insertLast(key, fechaHora);
this->size++;
if (this->size >= 0.75 * this->sizeA) {
rehashing();
}
}
}
/* Busca y devuelve el vector de fechas de una clave
Lanza excepcion si no existe :,v sad papu */
std::vector<std::string> MyHashTable::get(std::string key) const {
int pos = getPos(key);
return tabla[pos].get(key); // ya lanza excepción si no existe
}
/* Redimensiona la tabla al doble del tamaño actual + 1 para usar numeros impares, lo cual es mejor
lo ideal serían números primos, pero no quiero estar calculando el primo más cercano cada rehash
Reinserta todos los elementos con put() para evitar copias duplicadas
O(n) donde n es igual a la cantidad de elementos almacenados */
void MyHashTable::rehashing() {
int sizeAViejo = this->sizeA;
MyLinkedList* tablaVieja = this->tabla;
this->sizeA = this->sizeA * 2 + 1;
this->tabla = new MyLinkedList[this->sizeA];
this->size = 0;
for (int i = 0; i < sizeAViejo; i++) {
MyNodoLL* actual = tablaVieja[i].head;
while (actual != nullptr) {
std::string clave = actual->key;
// Recorremos TODAS las fechas de este nodo
for (const std::string& fecha : actual->data) {
put(clave, fecha); // reinsertamos una por una
}
actual = actual->next;
}
tablaVieja[i].clear(); // limpiamos la lista vieja antes de borrarla
}
delete[] tablaVieja;
}