This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_table.c
172 lines (147 loc) · 3.93 KB
/
hash_table.c
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
#include "hash_table.h"
#include "prime.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
static Item DELETED_ITEM = {NULL, NULL};
static Item*
hash_table_item_new(const char *key, const char *value)
{
Item *item = malloc(sizeof(Item));
item->key = strdup(key);
item->value = strdup(value);
return item;
}
static HashTable*
hash_table_new_sized(int size)
{
HashTable *ht = malloc(sizeof(HashTable));
ht->size = next_prime(size);
ht->count = 0;
ht->items = calloc((size_t)ht->size, sizeof(Item*));
return ht;
}
HashTable*
hash_table_new()
{
return hash_table_new_sized(HASH_TABLE_INITIAL_BASE_SIZE);
}
static void
hash_table_resize(HashTable *ht, int size)
{
HashTable *new_ht = hash_table_new_sized(size);
for (int i = 0; i < ht->size; ++i) {
Item *item = ht->items[i];
if (item != NULL && item != &DELETED_ITEM)
hash_table_insert(new_ht, item->key, item->value);
}
ht->count = new_ht->count;
const int tmp_size = new_ht->size;
new_ht->size = ht->size;
ht->size = tmp_size;
Item **items = new_ht->items;
new_ht->items = ht->items;
ht->items = items;
hash_table_free(new_ht);
}
static void
hash_table_item_free(Item *item)
{
free(item->key);
free(item->value);
free(item);
}
void
hash_table_free(HashTable *ht)
{
for (int i = 0; i < ht->size; ++i) {
Item *item = ht->items[i];
if (item != NULL && item != &DELETED_ITEM)
hash_table_item_free(item);
}
free(ht->items);
free(ht);
}
static inline int
hash_table_hash(const char *str, const int prime, const int mod)
{
long hash = 0;
const int len = strlen(str);
for (int i = 0; i < len ; ++i) {
hash += (long)pow(prime, len - (i + 1)) * str[i];
hash %= mod;
}
return (int)hash;
}
static inline int
hash_table_get_hash(const char *str, const int buckets, const int attempt)
{
const int hash_a = hash_table_hash(str, HASH_TABLE_PRIME_A, buckets);
const int hash_b = hash_table_hash(str, HASH_TABLE_PRIME_B, buckets);
return (hash_a + (attempt * (hash_b + 1))) % buckets;
}
void
hash_table_insert(HashTable *ht, const char *key, const char *value)
{
if (key == NULL)
return;
const int load = (ht->count * 100) / ht->size;
if (load > 70)
hash_table_resize(ht, ht->size * 2);
Item *item = hash_table_item_new(key, value);
int index = hash_table_get_hash(key, ht->size, 0);
Item *current_item = ht->items[index];
int i = 1;
while (current_item != NULL && current_item != &DELETED_ITEM) {
if (strcmp(current_item->key, key) == 0) {
hash_table_erase(ht, key);
ht->items[index] = item;
++(ht->count);
return;
}
index = hash_table_get_hash(key, ht->size, i);
current_item = ht->items[index];
++i;
}
ht->items[index] = item;
++(ht->count);
}
char*
hash_table_find(HashTable *ht, const char *key)
{
if (key == NULL)
return NULL;
int index = hash_table_get_hash(key, ht->size, 0);
Item *item = ht->items[index];
int i = 1;
while (item != NULL) {
if (item != &DELETED_ITEM && strcmp(item->key, key) == 0)
return item->value;
index = hash_table_get_hash(key, ht->size, i);
item = ht->items[index];
++i;
}
return NULL;
}
void
hash_table_erase(HashTable *ht, const char *key)
{
if (key == NULL)
return;
int index = hash_table_get_hash(key, ht->size, 0);
Item *item = ht->items[index];
int i = 1;
while (item != NULL) {
if (item != &DELETED_ITEM) {
if (strcmp(item->key, key) == 0) {
hash_table_item_free(item);
ht->items[index] = &DELETED_ITEM;
--(ht->count);
return;
}
}
index = hash_table_get_hash(key, ht->size, i);
item = ht->items[index];
++i;
}
}