aboutsummaryrefslogtreecommitdiff
path: root/src/hash-table.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hash-table.c')
-rw-r--r--src/hash-table.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/hash-table.c b/src/hash-table.c
new file mode 100644
index 0000000..7b6c983
--- /dev/null
+++ b/src/hash-table.c
@@ -0,0 +1,150 @@
1#include "hash-table.h"
2#include <string.h>
3#include <stdio.h>
4
5typedef struct entry *ENTRY;
6struct entry {
7 ENTRY next;
8 char *key;
9 char *value;
10};
11
12struct hash_table {
13 size_t bucket_count;
14 ENTRY buckets[];
15};
16
17// NOTE(cmmm): hash * 33 ^ c might be better
18static inline size_t
19hash_function_djb2 (const char *str)
20{
21 size_t hash = 5381;
22 unsigned char c;
23
24 while ((c = *str++))
25 hash = ((hash << 5) + hash) + c; // hash * 33 + c
26
27 return hash;
28}
29
30void
31hash_table_print (HASH_TABLE hash_table, const char *filename)
32{
33 FILE *stream = fopen (filename, "w");
34 fprintf (stream, "digraph hash_table {\n");
35 fprintf (stream, "\trankdir = LR\n");
36 fprintf (stream, "\tnode [shape = record]\n\n");
37 size_t entry_idx = 0;
38 size_t bucket_idx = 0;
39 for (ENTRY *bucket = hash_table->buckets;
40 bucket < hash_table->buckets + hash_table->bucket_count; ++bucket)
41 {
42 fprintf (stream, "\tbucket%zu [label = \"%zu\"]\n", bucket_idx, bucket_idx);
43
44 size_t saved_idx = entry_idx;
45 for (ENTRY entry = *bucket; entry; entry = entry->next)
46 fprintf (stream, "\tentry%zu [label = \"%s\"]\n", entry_idx++, entry->key);
47
48 entry_idx = saved_idx;
49 for (ENTRY entry = *bucket; entry; entry = entry->next) {
50 if (entry == *bucket)
51 fprintf (stream, "\tbucket%zu -> ", bucket_idx);
52 fprintf (stream, "entry%zu", entry_idx++);
53 if (entry->next)
54 fprintf (stream, " -> ");
55 } fprintf (stream, "\n\n");
56
57 bucket_idx += 1;
58 } fprintf (stream, "}\n");
59 fclose (stream);
60}
61
62HASH_TABLE
63hash_table_init (size_t bucket_count)
64{
65 HASH_TABLE hash_table = malloc (sizeof (*hash_table) +
66 bucket_count*sizeof (*hash_table->buckets));
67
68 hash_table->bucket_count = bucket_count;
69 memset (hash_table->buckets, 0,
70 hash_table->bucket_count*sizeof (*hash_table->buckets));
71
72 return hash_table;
73}
74
75void
76hash_table_clear (HASH_TABLE hash_table)
77{
78 if (!hash_table)
79 return;
80
81 for (ENTRY *bucket = hash_table->buckets;
82 bucket < hash_table->buckets + hash_table->bucket_count; ++bucket) {
83 for (ENTRY entry = *bucket, next; entry; entry = next) {
84 next = entry->next;
85 free (entry->key);
86 free (entry->value);
87 free (entry);
88 } *bucket = NULL;
89 }
90}
91
92void
93hash_table_free (HASH_TABLE hash_table)
94{
95 hash_table_clear (hash_table);
96 free (hash_table);
97}
98
99void
100hash_table_insert (HASH_TABLE hash_table, const char *key, const char *value)
101{
102 size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
103
104 ENTRY *entry = &hash_table->buckets[hash];
105 while (*entry) {
106 if (!strcmp (key, (*entry)->key)) { // found
107 free ((*entry)->value);
108 (*entry)->value = strdup (value);
109 return;
110 } else entry = &(*entry)->next;
111 }
112
113 // not found
114 *entry = malloc (sizeof (**entry));
115 (*entry)->next = NULL;
116 (*entry)->key = strdup (key);
117 (*entry)->value = strdup (value);
118}
119
120bool
121hash_table_delete (HASH_TABLE hash_table, const char *key)
122{
123 size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
124 ENTRY *entry = &hash_table->buckets[hash];
125 while (*entry) {
126 if (!strcmp (key, (*entry)->key)) { // found
127 ENTRY next = (*entry)->next;
128
129 free ((*entry)->key);
130 free ((*entry)->value);
131 free (*entry);
132
133 *entry = next;
134
135 return true;
136 } else entry = &(*entry)->next;
137 } return false;
138}
139
140const char *
141hash_table_search (HASH_TABLE hash_table, const char *key)
142{
143 size_t hash = hash_function_djb2 (key) % hash_table->bucket_count;
144 ENTRY entry = hash_table->buckets[hash];
145 while (entry) {
146 if (!strcmp (key, entry->key))
147 return entry->value;
148 else entry = entry->next;
149 } return NULL;
150}