#include "hash-table.h" #include #include 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; }