aboutsummaryrefslogtreecommitdiff
path: root/src/hashmap.c
blob: 16a950f31ee6fa07280825f385e83614c98c1fdc (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
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
149
150
151
152
153
154
155
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include "hashmap.h"

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

unsigned long
hash(char *str)
{
    unsigned long hash = 5381;
    int c;

    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }

    return hash;
}

/* allocate a new hashmap */
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;
}

/* Inserts a key-value pair into the map. Returns NULL if map did not have key, old value if it did. */
void *hashmap_insert(struct hashmap *hm, char *key, void *value) {
    int pos = hash(key) % HASHMAP_CAP;
    struct node *head = hm->buckets[pos];
    struct node *node = head;
    void *old_value;

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

/* Returns a pointer to the value corresponding to the key. */
void *hashmap_get(struct hashmap *hm, char *key) {
    unsigned int pos = hash(key) % HASHMAP_CAP;
    struct node *node = hm->buckets[pos];
    while (node != NULL) {
        if (strcmp(node->key, key) == 0) {
            return node->value;
        }

        node = node->next;
    }

    return NULL;
}

/* Retrieve pointer to value by key, handles dot notation for nested hashmaps */
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;
}

/* Removes a key from the map, returning the value at the key if the key was previously in the map. */
void *hashmap_remove(struct hashmap *hm, char *key) {
    int pos = hash(key) % HASHMAP_CAP;
    struct node *node = hm->buckets[pos];
    struct node *prev = NULL;
    void *old_value;

    while (node) {
        if (strcmp(node->key, key) == 0) {
            if (prev) {
                prev->next = node->next;
            } else {
                hm->buckets[pos] = node->next;
            }
            old_value = node->value;
            free(node);            
            return old_value;
        }

        node = node->next;
        prev = node;
    }

    return NULL;
}

void hashmap_walk(struct hashmap *hm, void (*fn)(void *value)) {
    struct node *node;
    struct node *next;

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

/* free hashmap related memory */
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);
}