forked from Sol128/hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
342 lines (279 loc) · 8.6 KB
/
hash.c
File metadata and controls
342 lines (279 loc) · 8.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lista.h"
#include "hash.h"
#define CAPACIDAD_INICIAL 8
#define MAX_ESPACIO_USADO 2
#define MIN_ESPACIO_USADO 0.5
/* ******************************************************************
* DEFINICION DE LOS TIPOS DE DATOS
* *****************************************************************/
struct hash {
lista_t** tabla;
size_t cantidad;
size_t capacidad;
hash_destruir_dato_t destruir_dato;
};
typedef struct nodo {
const char* clave;
void* dato;
}nodo_t;
struct hash_iter {
size_t pos;
const hash_t* hash;
lista_iter_t* iter_actual;
};
/* ******************************************************************
* HASH FUNCTION
* *****************************************************************/
//One-at-a-time hash
unsigned long funcion_hash(const char* clave, size_t capacidad) {
unsigned h = 0;
for (int i=0; i<capacidad; i++) {
h += (unsigned)clave[i];
h += (h << 10);
h ^= (h >> 6);
}
h += (h << 3);
h ^= (h >> 11);
h += (h << 15);
return h%capacidad;
}
/* ******************************************************************
* PRIMITIVAS DEL HASH
* *****************************************************************/
hash_t *hash_crear(hash_destruir_dato_t destruir_dato) {
hash_t* tabla_hash = malloc(sizeof(hash_t));
if (!tabla_hash) {
return NULL;
}
tabla_hash->tabla = malloc(CAPACIDAD_INICIAL * sizeof(lista_t*));
if (!tabla_hash->tabla){
free(tabla_hash);
return NULL;
}
for (int i = 0; i < CAPACIDAD_INICIAL; i++) {
tabla_hash->tabla[i] = lista_crear();
}
tabla_hash->cantidad = 0;
tabla_hash->capacidad = CAPACIDAD_INICIAL;
tabla_hash->destruir_dato = destruir_dato;
return tabla_hash;
}
nodo_t* crear_nodo(const char* clave,void* dato){
nodo_t* nodo = malloc(sizeof(nodo_t));
if (!nodo){
return NULL;
}
nodo->clave = clave;
nodo->dato = dato;
return nodo;
}
//va a cada elemento y lo rehashe y lo guarda donde corresponde
bool hash_redimensionar(hash_t* hash,size_t new_tam){
nodo_t* nodo;
unsigned long clave_hash;
lista_t** new_tablas = malloc(new_tam * sizeof(lista_t*));
for (int i = 0; i < new_tam; i++) {
new_tablas[i] = lista_crear();
}
for(int i = 0;i < hash->capacidad;i++){
while (!lista_esta_vacia(hash->tabla[i])){
nodo = lista_borrar_primero(hash->tabla[i]);
clave_hash = funcion_hash(nodo->clave,new_tam);
lista_insertar_ultimo(new_tablas[clave_hash],nodo);
}
lista_destruir(hash->tabla[i], NULL);
}
hash->capacidad = new_tam;
free(hash->tabla);
hash->tabla = new_tablas;
return true;
}
/* Guarda un elemento en el hash, si la clave ya se encuentra en la
* estructura, la reemplaza. De no poder guardarlo devuelve false.
* Pre: La estructura hash fue inicializada
* Post: Se almacenó el par (clave, dato)
*/
bool hash_guardar(hash_t *hash, const char *clave, void *dato){
// Si nos pasamos del limite hay que redimensionarlo
if (hash->cantidad/hash->capacidad > MAX_ESPACIO_USADO){
if (!hash_redimensionar(hash,hash->capacidad*2)){
return false;
}
}
unsigned long clave_hash = funcion_hash(clave,hash->capacidad);
nodo_t* nodo = crear_nodo(clave,dato);
if(!lista_insertar_ultimo(hash->tabla[clave_hash],nodo)){
//si fallo al insertarlo por algun motivo
return false;
}
hash->cantidad++;
return true;
}
/* Borra un elemento del hash y devuelve el dato asociado. Devuelve
* NULL si el dato no estaba.
* Pre: La estructura hash fue inicializada
* Post: El elemento fue borrado de la estructura y se lo devolvió,
* en el caso de que estuviera guardado.
*/
void *hash_borrar(hash_t *hash, const char *clave){
// Si nos pasamos del limite hay que redimensionarlo
if (hash->cantidad/hash->capacidad < MIN_ESPACIO_USADO && hash->capacidad > CAPACIDAD_INICIAL){
if (!hash_redimensionar(hash,hash->capacidad/2)){
return NULL;
}
}
unsigned long clave_hash = funcion_hash(clave,hash->capacidad);
lista_iter_t* iter = lista_iter_crear(hash->tabla[clave_hash]);
nodo_t* nodo = lista_iter_ver_actual(iter);
while(!lista_iter_al_final(iter) && nodo->clave != clave){
//iterar hasta encontrar la clave
nodo = lista_iter_ver_actual(iter);
lista_iter_avanzar(iter);
}
if(lista_iter_al_final(iter)){
return NULL;
}
nodo = lista_iter_borrar(iter);
void* dato = nodo->dato;
free(nodo);
lista_iter_destruir(iter);
hash->cantidad--;
return dato;
}
/* Obtiene el valor de un elemento del hash, si la clave no se encuentra
* devuelve NULL.
* Pre: La estructura hash fue inicializada
*/
void *hash_obtener(const hash_t *hash, const char *clave){
unsigned long clave_hash = funcion_hash(clave,hash->capacidad);
lista_iter_t* iter = lista_iter_crear(hash->tabla[clave_hash]);
nodo_t* nodo = lista_iter_ver_actual(iter);
while(!lista_iter_al_final(iter) && nodo->clave != clave){
//iterar hasta encontrar la clave
nodo = lista_iter_ver_actual(iter);
lista_iter_avanzar(iter);
}
if(lista_iter_al_final(iter)){
return NULL;
}
lista_iter_destruir(iter);
return nodo->dato;
}
/* Determina si clave pertenece o no al hash.
* Pre: La estructura hash fue inicializada
*/
bool hash_pertenece(const hash_t *hash, const char *clave){
unsigned long clave_hash = funcion_hash(clave,hash->capacidad);
lista_iter_t* iter = lista_iter_crear(hash->tabla[clave_hash]);
nodo_t* nodo;
while(!lista_iter_al_final(iter)){
nodo = lista_iter_ver_actual(iter);
if (nodo->clave == clave){
return true;
}
//iterar hasta encontrar la clave
lista_iter_avanzar(iter);
}
return false;
}
size_t hash_cantidad(const hash_t *hash) {
return hash->cantidad;
}
void destruir_nodo(void* nodo,void destruir_dato(void*)){
nodo_t* nodo_cast = nodo;
if (destruir_dato){
destruir_dato(nodo_cast->dato);
}
free(nodo);
}
/* Destruye la estructura liberando la memoria pedida y llamando a la función
* destruir para cada par (clave, dato).
* Pre: La estructura hash fue inicializada
* Post: La estructura hash fue destruida
*/
void hash_destruir(hash_t *hash){
for (int i = 0; i < hash->capacidad;i++){
while(!lista_esta_vacia(hash->tabla[i])){
destruir_nodo(lista_borrar_primero(hash->tabla[i]),hash->destruir_dato);
}
}
}
/* ******************************************************************
* PRIMITIVAS DEL ITERADOR
* *****************************************************************/
size_t siguiente_posicion_con_elementos(const hash_t *hash, size_t pos) {
while(pos < hash->capacidad && lista_esta_vacia(hash->tabla[pos])) {
pos++;
}
return pos;
}
hash_iter_t *hash_iter_crear(const hash_t *hash) {
hash_iter_t* iterador = malloc(sizeof(hash_iter_t));
if (!iterador) {
return NULL;
}
iterador->hash = hash;
//si el hash esta vacio el iterador esta al final
if(iterador->hash->cantidad == 0){
iterador->pos = iterador->hash->capacidad - 1;
iterador->iter_actual = NULL;
} else {
//si tiene elementos busca la primera lista que tenga elementos
//le asigna la posicion de esa lista en el haintsh y crea el iterador de esa lista
size_t i= 0;
/*
while(lista_esta_vacia(hash->tabla[i]) && i < hash->capacidad) {
i++;
}
iterador->pos = i;
*/
iterador->pos = siguiente_posicion_con_elementos(hash,i);
iterador->iter_actual = lista_iter_crear(hash->tabla[iterador->pos]);
}
return iterador;
}
bool hash_iter_al_final(const hash_iter_t *iter){
size_t siguiente_lista = siguiente_posicion_con_elementos(iter->hash, iter->pos);
if((iter->hash->cantidad == 0) || (iter->pos == siguiente_lista)){
return true;
}
return false;
}
bool hash_iter_avanzar(hash_iter_t *iter) {
//si esta al final devuelve false
if (hash_iter_al_final(iter)){
return false;
}
//avanza y si el "sub-iterador" de la lista esta al final destruye el "sub-iterador"
//y va a la siguiente lista con elementos
if(!lista_iter_avanzar(iter->iter_actual)) {
lista_iter_destruir(iter->iter_actual);
//busca la siguiente pos con elementos
size_t nueva_posicion = siguiente_posicion_con_elementos(iter->hash, iter->pos);
//si es la misma posicion que la anterior, entonces no hay listas con elementos y devuelve false
if(hash_iter_al_final(iter)){
return false;
}
//si avanzó bien, se crea el iterador para la siguiente lista con elementos
iter->pos = nueva_posicion;
iter->iter_actual = lista_iter_crear(iter->hash->tabla[iter->pos]);
}
return true;
}
const char *hash_iter_ver_actual(const hash_iter_t *iter) {
if(hash_iter_al_final(iter)){
return NULL;
}
nodo_t* actual = lista_iter_ver_actual(iter->iter_actual);
return actual->clave;
}
void hash_iter_destruir(hash_iter_t* iter) {
if (iter->iter_actual) {
lista_iter_destruir(iter->iter_actual);
}
free(iter);
}