-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashmap.c
138 lines (115 loc) · 3.42 KB
/
hashmap.c
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
#include <stdlib.h>
#include <string.h>
#include "hashmap.h"
typedef struct Pair {
const void *key;
void *value;
struct Pair *next;
} Pair;
struct Hashmap {
size_t num_buckets;
Pair **buckets;
HashFunction hash;
ComparisonFunction compare;
};
static Pair *pair_new(const void *key, void *value);
static Pair **get_bucket(Hashmap *hashmap, const void *key);
static Pair **get_pair_ptr(Hashmap *hashmap, const void *key);
static void init_buckets(Hashmap *hashmap);
static void free_buckets(Hashmap *hashmap);
Hashmap *hashmap_new(size_t num_buckets,
HashFunction hash, ComparisonFunction compare) {
Hashmap *hashmap = malloc(sizeof(Hashmap));
if (hashmap == NULL) {
goto error_hashmap_alloc;
}
hashmap->buckets = malloc(num_buckets * sizeof(Pair *));
if (hashmap->buckets == NULL) {
goto error_buckets_alloc;
}
hashmap->num_buckets = num_buckets;
hashmap->hash = hash;
hashmap->compare = compare;
init_buckets(hashmap);
return hashmap;
error_buckets_alloc:
free(hashmap);
error_hashmap_alloc:
return NULL;
}
void hashmap_free(Hashmap *hashmap) {
free_buckets(hashmap);
free(hashmap->buckets);
free(hashmap);
}
int hashmap_set(Hashmap *hashmap, const void *key,
void *value, void **old_value) {
Pair **pair_ptr = get_pair_ptr(hashmap, key);
if (old_value != NULL) {
*old_value = *pair_ptr == NULL ? NULL : (*pair_ptr)->value;
}
if (*pair_ptr == NULL) {
*pair_ptr = pair_new(key, value);
} else {
(*pair_ptr)->value = value;
}
return *pair_ptr != NULL;
}
void *hashmap_get(Hashmap *hashmap, const void *key) {
Pair *pair = *get_pair_ptr(hashmap, key);
return pair != NULL ? pair->value : NULL;
}
void *hashmap_delete(Hashmap *hashmap, const void *key) {
void *value = NULL;
Pair **pair_ptr = get_pair_ptr(hashmap, key);
if (*pair_ptr != NULL) {
value = (*pair_ptr)->value;
Pair *next_pair = (*pair_ptr)->next;
free(*pair_ptr);
*pair_ptr = next_pair;
}
return value;
}
static Pair *pair_new(const void *key, void *value) {
/* Since key is const, it'll have to be copied over from a struct that's
* already initialized.
*/
Pair init = {.key = key, .value = value, .next = NULL};
Pair *pair = malloc(sizeof(Pair));
if (pair != NULL) {
memcpy(pair, &init, sizeof(Pair));
}
return pair;
}
static Pair **get_bucket(Hashmap *hashmap, const void *key) {
return hashmap->buckets + hashmap->hash(key) % hashmap->num_buckets;
}
static Pair **get_pair_ptr(Hashmap *hashmap, const void *key) {
Pair **pair_ptr = get_bucket(hashmap, key);
while (*pair_ptr != NULL) {
if (hashmap->compare((*pair_ptr)->key, key)) {
break;
}
pair_ptr = &(*pair_ptr)->next;
}
return pair_ptr;
}
static void init_buckets(Hashmap *hashmap) {
/* Initializing the buckets to NULL makes it easy to check if a bucket
* is already in use.
*/
for (size_t i = 0; i < hashmap->num_buckets; i++) {
hashmap->buckets[i] = NULL;
}
}
static void free_buckets(Hashmap *hashmap) {
for (size_t i = 0; i < hashmap->num_buckets; i++) {
Pair *pair = hashmap->buckets[i];
Pair *prev_pair = NULL;
while (pair != NULL) {
prev_pair = pair;
pair = pair->next;
free(prev_pair);
}
}
}