-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash-table.h
More file actions
163 lines (133 loc) · 5.51 KB
/
hash-table.h
File metadata and controls
163 lines (133 loc) · 5.51 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
/*
* Licencia Siumba v1.0 basada en Apache 2.0
*
* Copyright 2023 Desmon (David)
*
* Se concede permiso, de forma gratuita, a cualquier persona que obtenga una copia de
* este software y archivos de documentación asociados (el "Software"), para tratar el
* Software sin restricciones, incluidos, entre otros, los derechos de uso, copia,
* modificación, fusión, publicación, distribución, sublicencia y/o venta de copias del
* Software, y para permitir a las personas a quienes se les proporcione el Software
* hacer lo mismo, sujeto a las siguientes condiciones:
*
* El anterior aviso de copyright y este aviso de permiso se incluirán en todas las
* copias o partes sustanciales del Software.
*
* EL SOFTWARE SE PROPORCIONA "TAL CUAL", SIN GARANTÍA DE NINGÚN TIPO, EXPRESA O
* IMPLÍCITA, INCLUYENDO PERO NO LIMITADO A LAS GARANTÍAS DE COMERCIABILIDAD, IDONEIDAD
* PARA UN PROPÓSITO PARTICULAR Y NO INFRACCIÓN. EN NINGÚN CASO LOS TITULARES DEL
* COPYRIGHT O LOS TITULARES DE LOS DERECHOS DE AUTOR SERÁN RESPONSABLES DE NINGÚN
* RECLAMO, DAÑO U OTRA RESPONSABILIDAD, YA SEA EN UNA ACCIÓN DE CONTRATO, AGRAVIO O DE
* OTRA MANERA, QUE SURJA DE, FUERA DE O EN CONEXIÓN CON EL SOFTWARE O EL USO U OTRO TIPO
* DE ACCIONES EN EL SOFTWARE.
*
* Además, cualquier modificación realizada por terceros se considerará propiedad del
* titular original de los derechos de autor. Los titulares de derechos de autor
* originales no se responsabilizan de las modificaciones realizadas por terceros.
*
* Queda explícitamente establecido que no es obligatorio especificar ni notificar los
* cambios realizados entre versiones, ni revelar porciones específicas de código
* modificado.
*/
#ifndef __HASH_TABLE_H__
#define __HASH_TABLE_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "debug_c.h"
typedef struct Entry {
char* key;
void* value;
struct Entry* next;
} Entry;
typedef struct HashTable {
size_t size;
size_t capacity;
Entry** table;
} HashTable;
HashTable* createHashTable(size_t size);
static inline unsigned long hash_djb2(register const char* str, register size_t size) {
DEBUG_PRINT(DEBUG_LEVEL_INFO,
INIT_TYPE_FUNC_DBG(unsigned long, hash)
TYPE_DATA_DBG(const char*, "str = %s")
TYPE_DATA_DBG(size_t, "size = %zu")
END_TYPE_FUNC_DBG,
str, size);
register size_t hash = 0x1505;
register int c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; /* hash * 33 + c djb2 hash algorithm */
}
return hash % size;
}
void *get_static(HashTable *hashTable, const char *key);
#ifndef get
#define get(hash_table, ...) _Generic((hash_table), \
HashTable*: get_static, \
default: get_static)(hash_table, __VA_ARGS__)
#endif
void* get_static_struct(HashTable* hashTable, void *key, size_t lenght_key);
void put_static_struct(
HashTable* hashTable, void* key, size_t lenght_key, void* value);
void put_static(HashTable *hashTable, const char *key, void *value);
#ifndef put
#define put(hash_table, ...) _Generic((hash_table), \
HashTable*: put_static, \
default: put_static)(hash_table, __VA_ARGS__);
#endif
void printHashTable(HashTable *hashTable);
void freeHashTable_static_struct(HashTable *hashTable);
#ifndef freeHashTable_struct
#define freeHashTable_struct(hash_table) _Generic((hash_table), \
HashTable*: freeHashTable_static_struct, \
default: freeHashTable_static_struct)(hash_table);
#endif
void freeHashTable_static_all(HashTable* hashTable, void (*freeValue)(void*));
#ifndef freeHashTable_all
#define freeHashTable_all(hash_table, ...) _Generic((hash_table), \
HashTable*: freeHashTable_static_all, \
default: freeHashTable_static_all)(hash_table,__VA_ARGS__);
#endif
void updateValue_static(HashTable* hashTable, const char* key, void* newValue);
#ifndef updateValue
#define updateValue(hash_table, ...) _Generic((hash_table), \
HashTable*: updateValue_static, \
default: updateValue_static)(hash_table, __VA_ARGS__);
#endif
/**
* Permite obtener el tamaño de una tabla de hash estatica en especifico
* @param hashTable tabla de hash estatica de la que averiguar cuantas entradas
* fueron ocupadas
* @return cantidad de entradas ocupadas
*/
static inline size_t get_size_HashTable(HashTable *hashTable) {
return hashTable->size;
}
/**
* Permite obtener la capacidad maxima de la tabla de hash estatica
* @param hashTable tabla de hash de la que averiguar su capacidad
* @return capacidad de la tabla de hash.
*/
static inline size_t get_capacity_HashTable(HashTable *hashTable) {
return hashTable->capacity;
}
bool resizeHashTable(HashTable* hashTable, size_t newCapacity);
bool remove_static(HashTable* hashTable, const char* key);
bool remove_static_struct(HashTable* hashTable, void* key, size_t length_key);
char* base64_encode(const unsigned char* data, size_t input_length, size_t* output_length);
unsigned char* base64_decode(const char* data, size_t input_length, size_t* output_length);
// Macro para detectar número de argumentos
#define GET_MACRO(_1, _2, NAME, ...) NAME
#ifndef freeHashTable_static
#define freeHashTable_static(...) GET_MACRO(__VA_ARGS__, freeHashTable_static_all, freeHashTable_static_struct)(__VA_ARGS__)
#endif
#ifndef freeHashTable
#define freeHashTable freeHashTable_struct
#endif
#ifdef INCLUDE_COLORS_C
#include "hash-table.c"
#endif
#endif