diff options
| author | 2026-11-07 04:42:12 +0000 | |
|---|---|---|
| committer | 2026-11-07 04:42:12 +0000 | |
| commit | f151b796a66adfb1d6b7bf151f2d85fd78cc0a64 (patch) | |
| tree | ba530b977e6e853d6e1582adbfe1e840d03ffd38 /src/hash-table.c | |
| download | lab-06-f151b796a66adfb1d6b7bf151f2d85fd78cc0a64.tar.gz | |
Diffstat (limited to 'src/hash-table.c')
| -rw-r--r-- | src/hash-table.c | 150 |
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 | |||
| 5 | typedef struct entry *ENTRY; | ||
| 6 | struct entry { | ||
| 7 | ENTRY next; | ||
| 8 | char *key; | ||
| 9 | char *value; | ||
| 10 | }; | ||
| 11 | |||
| 12 | struct hash_table { | ||
| 13 | size_t bucket_count; | ||
| 14 | ENTRY buckets[]; | ||
| 15 | }; | ||
| 16 | |||
| 17 | // NOTE(cmmm): hash * 33 ^ c might be better | ||
| 18 | static inline size_t | ||
| 19 | hash_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 | |||
| 30 | void | ||
| 31 | hash_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 | |||
| 62 | HASH_TABLE | ||
| 63 | hash_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 | |||
| 75 | void | ||
| 76 | hash_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 | |||
| 92 | void | ||
| 93 | hash_table_free (HASH_TABLE hash_table) | ||
| 94 | { | ||
| 95 | hash_table_clear (hash_table); | ||
| 96 | free (hash_table); | ||
| 97 | } | ||
| 98 | |||
| 99 | void | ||
| 100 | hash_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 | |||
| 120 | bool | ||
| 121 | hash_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 | |||
| 140 | const char * | ||
| 141 | hash_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 | } | ||
