forked from devlucasgabrielt/Hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
339 lines (285 loc) · 7.14 KB
/
Copy pathhash.c
File metadata and controls
339 lines (285 loc) · 7.14 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
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include "hash.h"
#include <math.h>
typedef struct nodo nodo_t;
struct nodo {
char *llave;
void *valor;
struct nodo *siguiente;
};
struct hash {
size_t tam;
size_t cant_elementos;
nodo_t** elementos_hash;
hash_destruir_dato_t f_destruir;
};
const size_t TAMANIO_INICIAL = 1024;
#define FACTOR_UTILIZACION_MEMORIA 4
#define FACTOR_MULTIPLICADOR 2
#define TOPE_FACTOR_CARGA 0.7
struct hash_iter{
const hash_t* tabla_hash;
size_t index;
size_t iterados;
nodo_t* elemento;
};
unsigned long _funcion_hash(const char *str,size_t tamanio_hash)
{
unsigned long hash = 5381;
unsigned int c;
while ((c = (unsigned)*str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash%tamanio_hash;
}
hash_t *hash_crear(hash_destruir_dato_t destruir_dato)
{
hash_t *crear_hash;
if( !(crear_hash = malloc( sizeof(*crear_hash) )) )
{
return NULL;
}
crear_hash->tam = TAMANIO_INICIAL;
crear_hash->cant_elementos = 0;
if( !(crear_hash->elementos_hash = malloc( crear_hash->tam * sizeof(nodo_t*) ) ) )
{
free(crear_hash);
return NULL;
}
crear_hash->f_destruir = destruir_dato;
for( size_t i = 0; i < crear_hash->tam; i++ )
crear_hash->elementos_hash[i] = NULL;
return crear_hash;
}
bool _funcion_guardar(hash_t *hash, const char* clave, void *dato){
unsigned long pos = _funcion_hash(clave,hash->tam);
nodo_t* prox = hash->elementos_hash[pos];
nodo_t* ultimo = NULL;
/* El bucle aquí intenta buscar el dato aver si existe colision*/
while( prox != NULL && prox->llave != NULL && strcmp(clave, prox->llave) )
{
ultimo = prox;
prox = prox->siguiente;
}
/*Se encontro un par!!*/
if( prox != NULL && prox->llave != NULL && !strcmp(clave, prox->llave) )
{
if( hash->f_destruir )
hash->f_destruir(prox->valor);
prox->valor = dato; /*actualizamos su dato*/
}
else /* No se encontró ninguno, armemos una nuevo dato! */
{
nodo_t* nuevo_dato;
if( !(nuevo_dato = malloc(sizeof(nodo_t))) )
return false;
if( (nuevo_dato->llave = strdup(clave)) == NULL )
{
free(nuevo_dato);
return false;
}
nuevo_dato->valor = dato;
nuevo_dato->siguiente = NULL;
if( prox == hash->elementos_hash[pos] ) /* ¿Al principio? */
{
nuevo_dato->siguiente = prox;
hash->elementos_hash[pos] = nuevo_dato;
}
else if( prox == NULL )
{
ultimo->siguiente = nuevo_dato;
}
/*else
{
nuevo_dato->siguiente = prox;
ultimo->siguiente = nuevo_dato;
}*/
hash->cant_elementos++;
}
return true;
}
bool _hash_redimensionar(hash_t* hash, size_t tamanio_a_reasignar ){
nodo_t** auxiliar = malloc(sizeof(nodo_t*) * tamanio_a_reasignar);
if( !auxiliar ){
return false;
}
for( size_t i = 0; i < tamanio_a_reasignar; i++ ){
auxiliar[i] = NULL;
}
nodo_t** tabla_a_reemplazar= hash->elementos_hash;
size_t tamanio_anterior = hash->tam;
hash->elementos_hash=auxiliar;
hash->cant_elementos=0;
hash->tam=tamanio_a_reasignar;
nodo_t* actual;
for( size_t i = 0; i < tamanio_anterior; i++ ){
if(tabla_a_reemplazar[i]){
actual=tabla_a_reemplazar[i];
while(actual){
_funcion_guardar(hash,actual->llave,actual->valor);
free(actual->llave);
nodo_t* nodo_auxiliar = actual;
actual=actual->siguiente;
free(nodo_auxiliar);
}
}
}
free(tabla_a_reemplazar);
return true;
}
bool hash_guardar(hash_t *hash, const char* clave, void *dato)
{
unsigned long pos = _funcion_hash(clave,hash->tam);
float factor_de_carga = (float)hash->cant_elementos/(float)hash->tam;
if(factor_de_carga>=TOPE_FACTOR_CARGA || pos>hash->tam){
size_t nuevo_tamanio = hash->tam*FACTOR_MULTIPLICADOR;
if(!_hash_redimensionar(hash,nuevo_tamanio)){
return false;
}
}
return _funcion_guardar(hash, clave, dato);
}
void *hash_borrar(hash_t *hash, const char *clave)
{
unsigned long pos = _funcion_hash(clave,hash->tam);
nodo_t* cabeza_lista = hash->elementos_hash[pos];
nodo_t* anterior = NULL;
while( cabeza_lista != NULL ) /* debemos iterar para encontarlo*/
{
if( !strcmp(cabeza_lista->llave, clave) )
{
void* dato_eliminado = cabeza_lista->valor;
if( anterior != NULL ) /* significa que iteró al menos 1 vez*/
{
anterior->siguiente = cabeza_lista->siguiente;
}
else
{
hash->elementos_hash[pos] = cabeza_lista->siguiente;
}
free(cabeza_lista->llave);
free(cabeza_lista);
hash->cant_elementos--;
return dato_eliminado;
}
anterior = cabeza_lista;
cabeza_lista = cabeza_lista->siguiente;
}
if( (hash->tam - 1) < (hash->cant_elementos / FACTOR_UTILIZACION_MEMORIA) )
if( !_hash_redimensionar(hash, hash->tam / FACTOR_MULTIPLICADOR) )
return NULL;
/* No se encontró , devolvemos NULL */
return NULL;
}
void *hash_obtener(const hash_t *hash, const char* clave)
{
unsigned long int pos = _funcion_hash(clave,hash->tam);
nodo_t* inicio = hash->elementos_hash[pos];
while( inicio != NULL )
{
if( !strcmp(inicio->llave, clave) )
{
return inicio->valor;
}
inicio = inicio->siguiente;
}
return NULL;
}
bool hash_pertenece(const hash_t *hash, const char *clave)
{
if(!hash_cantidad(hash)){
return false;
}
unsigned long pos = _funcion_hash(clave,hash->tam);
for( nodo_t* e = hash->elementos_hash[pos]; e; e = e->siguiente )
if( !strcmp(e->llave, clave) )
return true;
return false;
}
size_t hash_cantidad(const hash_t *hash)
{
return hash->cant_elementos;
}
void hash_destruir(hash_t *hash)
{
if(hash->cant_elementos==0){
free(hash->elementos_hash);
free(hash);
return;
}
nodo_t* tmp_vagon = NULL;
for( size_t i = 0; i < hash->tam; i++ )
{
for( nodo_t* e = hash->elementos_hash[i]; e ; )
{
tmp_vagon = e;
if(hash->f_destruir)
{
hash->f_destruir(tmp_vagon->valor);
}
e = e->siguiente;
free(tmp_vagon->llave);
free(tmp_vagon);
}
}
free(hash->elementos_hash);
free(hash);
}
/*Primitivas iterador*/
hash_iter_t* hash_iter_crear(const hash_t* hash){
hash_iter_t* iter = malloc(sizeof(hash_iter_t));
if(!iter){
return NULL;
}
iter->index = 0;
iter->tabla_hash = hash;
iter->elemento = hash->elementos_hash[0];
iter->iterados = 0;
while( iter->elemento == NULL )
{
if( iter->index < iter->tabla_hash->tam-1)
{
iter->index++;
iter->elemento = iter->tabla_hash->elementos_hash[iter->index];
}
else
break;
}
return iter;
}
bool hash_iter_al_final(const hash_iter_t* iter){
return (iter->iterados == iter->tabla_hash->cant_elementos);
}
bool hash_iter_avanzar(hash_iter_t* iter){
if(hash_iter_al_final(iter)){
return false;
}
if( iter->elemento->siguiente)
{
iter->elemento = iter->elemento->siguiente;
}
else
{
do{
iter->index++;
if(iter->index>iter->tabla_hash->tam-1){
break;
}
iter->elemento=iter->tabla_hash->elementos_hash[iter->index];
}while( iter->elemento == NULL);
}
iter->iterados++;
return true;
}
const char* hash_iter_ver_actual(const hash_iter_t* iter){
if(hash_iter_al_final(iter)){
return NULL;
}
return iter->elemento->llave;
}
void hash_iter_destruir(hash_iter_t* iter){
free(iter);
}