📋

Mini Projects

A hash table in C from scratch

a hash map implementation in C, inspired by https://github.com/jamesroutley/write-a-hash-table

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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

typedef struct {
    char* key;
    char* value;
} key_value_pair;

typedef struct {
    int size;
    int count;
    key_value_pair** pairs;
} hash_map;

static key_value_pair DELETED_PAIR = {NULL, NULL};

const int PRIME_1 = 151;
const int PRIME_2 = 163;

void set(hash_map* ht, const char* key, const char* value);
char* get(hash_map* ht, const char* key);
void delete(hash_map* ht, const char* key);

static key_value_pair* create_new_key_value_pair(const char* k, const char* v) {
    key_value_pair* pair = malloc(sizeof(pair));
    pair->key = strdup(k);
    pair->value = strdup(v);
    return pair;
}

hash_map* create_new_hash_map() {
    hash_map* hm = malloc(sizeof(hash_map));
    hm->size = 42;
    hm->count = 0;
    hm->pairs = calloc((size_t)hm->size, sizeof(key_value_pair*));
    return hm;
}


static void delete_pair(key_value_pair* p) {
    free(p->key);
    free(p->value);
    free(p);
}


static int create_hash(const char* s, const int a, const int m) {
    long hash = 0;
    const int len_s = strlen(s);
    for (int i = 0; i < len_s; i++) {
        hash += (long)pow(a, len_s - (i+1)) * s[i];
        hash = hash % m;
    }
    return (int)hash;
}


static int get_hash(
    const char* s, const int num_buckets, const int attempt
) {
    const int hash_a = create_hash(s, PRIME_1, num_buckets);
    const int hash_b = create_hash(s, PRIME_2, num_buckets);
    return (hash_a + (attempt * (hash_b + 1))) % num_buckets;
}


void set(hash_map* hm, const char* key, const char* value) {
    key_value_pair* new_pair = create_new_key_value_pair(key, value);
    int index = get_hash(new_pair->key, hm->size, 0);
    key_value_pair* cur_item = hm->pairs[index];
    int i = 1;
    while (cur_item != NULL) {
        if (cur_item != &DELETED_PAIR) {
            // update value, overwrite if key found 
            if (strcmp(cur_item->key, key) == 0) {
                delete_pair(cur_item);
                hm->pairs[index] = new_pair;
                return;
            }
        }
        index = get_hash(new_pair->key, hm->size, i);
        cur_item = hm->pairs[index];
        i++;
    } 
    hm->pairs[index] = new_pair;
    hm->count++;
}



char* get(hash_map* hm, const char* key) {
    int index = get_hash(key, hm->size, 0);
    key_value_pair* pair = hm->pairs[index];
    int i = 1;
    while (pair != NULL) {
        if (pair != &DELETED_PAIR) { 
            if (strcmp(pair->key, key) == 0) {
                return pair->value;
            }
            index = get_hash(key, hm->size, i);
            pair = hm->pairs[index];
            i++;
        } 
    }
    return NULL;
}


void delete(hash_map* hm, const char* key) {
    int index = get_hash(key, hm->size, 0);
    key_value_pair* pair  = hm->pairs[index];
    int i = 1;
    while (pair != NULL) {
        if (pair != &DELETED_PAIR) {
            if (strcmp(pair->key, key) == 0) {
                delete_pair(pair);
                hm->pairs[index] = &DELETED_PAIR;
            }
        }
        index = get_hash(key, hm->size, i);
        pair = hm->pairs[index];
        i++;
    } 
    hm->count--;
}


void delete_all(hash_map* hm) {
    for (int i = 0; i < hm->size; i++) {
        key_value_pair* p = hm->pairs[i];
        if (p != NULL) {
            delete_pair(p);
        }
    }
    free(hm->pairs);
    free(hm);
}

int main() {
    hash_map* hm = create_new_hash_map();
    set(hm, "ans","42");
    assert(get(hm, "ans") == 42);
    remove_key(hm, "ans");
    assert(get(hm, "ans") == NULL);
    delete_all(hm);
}