aboutsummaryrefslogtreecommitdiff
path: root/src/hashmap.c
blob: 9f36a58e59b43f1d8bf6760d1ac54e880dd26449 (plain)
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
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include "hashmap.h"

#define HASH(v) (v[0] - 'a') % HASHMAP_CAP

struct node {
    char *key;
    void *value;
    struct node *next;
};

struct hashmap *hashmap_new() {
    struct hashmap *hm = malloc(sizeof *hm);
    if (!hm) err(EXIT_FAILURE, "out of memory");
    for (int i=0; i < HASHMAP_CAP; i++) {
        hm->buckets[i] = NULL;
    }

    return hm;
}

void hashmap_insert(struct hashmap *hm, char *key, void *value) {
    int pos = HASH(key);
    struct node *head = hm->buckets[pos];
    struct node *node = head;

    while (node) {
        if (strcmp(node->key, key) == 0) {
            node->value = value;
            return;
        }
        node = node->next;
    }
    
    node = malloc(sizeof *node);
    node->key = key;
    node->value = value;
    node->next = head;
    hm->buckets[pos] = node;
}

void *hashmap_get(struct hashmap *hm, char *key) {
    int pos = HASH(key);
    struct node *node = hm->buckets[pos];
    while (node) {
        if (strcmp(node->key, key) == 0) {
            return node->value;
        }

        node = node->next;
    }

    return NULL;
}

void *hashmap_resolve(struct hashmap *hm, char *key) {
    char tmp_key[64];
    int i = 0;
    int j = 0;

    while (1) {
        for (j=0; key[i] != '.' && key[i] != '\0'; i++, j++) {
            tmp_key[j] = key[i];
        }
        tmp_key[j] = '\0';
        hm = hashmap_get(hm, tmp_key);
        
        // stop if we read key to end of string
        if (key[i] == '\0') {
            break;
        }

        // otherwise, continue reading keys
        i++;
    }

    return hm;
}

void hashmap_remove(char *key) {
    // TODO: Implement this
}

void hashmap_free(struct hashmap *hm) {
    struct node *node;
    struct node *next;

    for (int i=0; i < HASHMAP_CAP; i++) {
        node = hm->buckets[i];
        while (node) {
            next = node->next;
            free(node);            
            node = next;
        }
    }

    free(hm);
}