-
Notifications
You must be signed in to change notification settings - Fork 37
/
hashmap.c
154 lines (134 loc) · 3.09 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "hashmap.h"
#include <string.h>
hashmap_p create_hashmap(){
hashmap_p m = (hashmap_p)malloc(sizeof(struct hashmap));
int i;
m->size=0;
m->num_buckets = DEFAULT_NUM_BUCKETS;
m->buckets = (item_t**) malloc(sizeof(item_t*) * m->num_buckets);
for(i=0; i < m->num_buckets; i++)
m->buckets[i] = NULL;
m->keys = create_vector();
m->destructor = free;
return m;
}
void* hashmap_get(hashmap_p m, char * key){
if(key==NULL){
return NULL;
}
size_t h = hash_func(key) % m->num_buckets;
item_t* itm = m->buckets[h];
while(itm!=NULL){
if(strcmp(key, itm->key)==0)
return itm->val;
itm = itm->next;
}
return NULL;
}
void hashmap_put(hashmap_p m, char* key, void* val, size_t len){
int keylen = strlen(key);
size_t h = hash_func(key) % m->num_buckets;
item_t* itm = m->buckets[h];
item_t* last = NULL;
while(itm!=NULL){
if(strcmp(key, itm->key)==0){
if(itm->val!=NULL) m->destructor(itm->val);
itm->val = malloc(len);
memcpy(itm->val, val, len);
return;
}
if(itm->next==NULL) last = itm;
itm = itm->next;
}
itm = (item_t*)malloc(sizeof(item_t));
itm->key = malloc(keylen+1);
memcpy(itm->key, key, keylen+1);
itm->val = malloc(len);
memcpy(itm->val, val, len);
itm->next = NULL;
if(last==NULL) m->buckets[h] = itm;
else last->next = itm;
vector_add(m->keys, itm->key, keylen+1);
m->size++;
}
void hashmap_remove(hashmap_p m, char* key){
int n = strlen(key);
size_t h = hash_func(key) % m->num_buckets;
item_t *itm = m->buckets[h];
item_t *last = NULL;
int keyind;
while(itm!=NULL){
if(strcmp(key, itm->key)==0)
break;
last = itm;
itm = itm->next;
}
if(itm != NULL){
if(last==NULL)
m->buckets[h] = itm->next;
else last->next = itm->next;
free(itm->key);
m->destructor(itm->val);
free(itm);
keyind = vector_index(m->keys, key, n);
vector_remove(m->keys, keyind);
m->size--;
}
}
/* Uses FNV hash function, as given by Julien Walker's "The Art of Hashing" */
size_t hash_func(char * key){
size_t h = 2166136261;
int i;
for (i=0; key[i]!='\0'; ++i)
h = ( h * 16777619 ) ^ key[i];
return h;
}
void destroy_hashmap(hashmap_p m){
int x;
item_t* itm;
for(x=0;x<m->num_buckets;++x){
itm = m->buckets[x];
while(itm!=NULL){
free(itm->key);
m->destructor(itm->val);
free(itm);
itm = itm->next;
}
}
destroy_vector(m->keys);
free(m->buckets);
free(m);
}
void hashmap_resize(hashmap_p m, size_t num_buckets){
int i;
m->buckets = (item_t **)realloc(m->buckets,
sizeof(item_t *) * num_buckets);
for(i=0; i < m->size; i++){
char* key = (char*) vector_get(m->keys, i);
int h = hash_func(key) % m->num_buckets;
item_t *prev = NULL;
item_t *cur = m->buckets[h];
while(cur != NULL){
if(strcmp(cur->key, key)==0)
break;
prev = cur;
cur = cur->next;
}
if(cur != NULL){
if(prev == NULL)
m->buckets[h] = cur->next;
else prev->next = cur->next;
cur->next = NULL;
}
h = hash_func(key) % num_buckets;
prev = m->buckets[h];
if(prev == NULL)
m->buckets[h] = cur;
else {
while(prev->next != NULL)
prev = prev->next;
prev->next = cur;
}
}
m->num_buckets = num_buckets;
}