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
|
#include "hash-table.h"
#include <string.h>
#include <stdio.h>
typedef struct entry *ENTRY;
struct entry {
ENTRY next;
char *key;
char *value;
};
struct hash_table {
size_t bucket_count;
ENTRY buckets[];
};
// NOTE(cmmm): hash * 33 ^ c might be better
static inline size_t
hash_function_djb2 (const char *str)
{
size_t hash = 5381;
unsigned char c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; // hash * 33 + c
return hash;
}
void
hash_table_print (HASH_TABLE hash_table, const char *filename)
{
FILE *stream = fopen (filename, "w");
fprintf (stream, "digraph hash_table {\n");
fprintf (stream, "\trankdir = LR\n");
fprintf (stream, "\tnode [shape = record]\n\n");
size_t entry_idx = 0;
size_t bucket_idx = 0;
for (ENTRY *bucket = hash_table->buckets;
bucket < hash_table->buckets + hash_table->bucket_count; ++bucket)
{
fprintf (stream, "\tbucket%zu [label = \"%zu\"]\n", bucket_idx, bucket_idx);
size_t saved_idx = entry_idx;
for (ENTRY entry = *bucket; entry; entry = entry->next)
fprintf (stream, "\tentry%zu [label = \"%s\"]\n", entry_idx++, entry->key);
entry_idx = saved_idx;
for (ENTRY entry = *bucket; entry; entry = entry->next) {
if (entry == *bucket)
fprintf (stream, "\tbucket%zu -> ", bucket_idx);
fprintf (stream, "entry%zu", entry_idx++);
if (entry->next)
fprintf (stream, " -> ");
} fprintf (stream, "\n\n");
bucket_idx += 1;
} fprintf (stream, "}\n");
fclose (stream);
}
HASH_TABLE
hash_table_init (size_t bucket_count)
{
HASH_TABLE hash_table = malloc (sizeof (*hash_table) +
bucket_count*sizeof (*hash_table->buckets));
hash_table->bucket_count = bucket_count;
memset (hash_table->buckets, 0,
hash_table->bucket_count*sizeof (*hash_table->buckets));
return hash_table;
}
void
hash_table_clear (HASH_TABLE hash_table)
{
if (!hash_table)
return;
for (ENTRY *bucket = hash_table->buckets;
bucket < hash_table->buckets + hash_table->bucket_count; ++bucket) {
for (ENTRY entry = *bucket, next; entry; entry = next) {
next = entry->next;
free (entry->key);
free (entry->value);
free (entry);
} *bucket = NULL;
}
}
void
hash_table_free (HASH_TABLE hash_table)
{
hash_table_clear (hash_table);
free (hash_table);
}
void
hash_table_insert (HASH_TABLE hash_table, const char *key, const char *value)
{
size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
ENTRY *entry = &hash_table->buckets[hash];
while (*entry) {
if (!strcmp (key, (*entry)->key)) { // found
free ((*entry)->value);
(*entry)->value = strdup (value);
return;
} else entry = &(*entry)->next;
}
// not found
*entry = malloc (sizeof (**entry));
(*entry)->next = NULL;
(*entry)->key = strdup (key);
(*entry)->value = strdup (value);
}
bool
hash_table_delete (HASH_TABLE hash_table, const char *key)
{
size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
ENTRY *entry = &hash_table->buckets[hash];
while (*entry) {
if (!strcmp (key, (*entry)->key)) { // found
ENTRY next = (*entry)->next;
free ((*entry)->key);
free ((*entry)->value);
free (*entry);
*entry = next;
return true;
} else entry = &(*entry)->next;
} return false;
}
const char *
hash_table_search (HASH_TABLE hash_table, const char *key)
{
size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
ENTRY entry = hash_table->buckets[hash];
while (entry) {
if (!strcmp (key, entry->key))
return entry->value;
else entry = entry->next;
} return NULL;
}
|