-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.c
243 lines (229 loc) · 8.11 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// hashmap.c -- Basic implementation of a hashmap abstract data type
// Copyright (C) 2008-2009 Markus Gutschke <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// In addition to these license terms, the author grants the following
// additional rights:
//
// If you modify this program, or any covered work, by linking or
// combining it with the OpenSSL project's OpenSSL library (or a
// modified version of that library), containing parts covered by the
// terms of the OpenSSL or SSLeay licenses, the author
// grants you additional permission to convey the resulting work.
// Corresponding Source for a non-source form of such a combination
// shall include the source code for the parts of OpenSSL used as well
// as that of the covered work.
//
// You may at your option choose to remove this additional permission from
// the work, or from any part of it.
//
// It is possible to build this program in a way that it loads OpenSSL
// libraries at run-time. If doing so, the following notices are required
// by the OpenSSL and SSLeay licenses:
//
// This product includes software developed by the OpenSSL Project
// for use in the OpenSSL Toolkit. (http://www.openssl.org/)
//
// This product includes cryptographic software written by Eric Young
// ([email protected])
//
//
// The most up-to-date version of this program is always available from
// http://shellinabox.com
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "libhttp/hashmap.h"
#include "logging/logging.h"
struct HashMap *newHashMap(void (*destructor)(void *arg, char *key,
char *value),
void *arg) {
struct HashMap *hashmap;
check(hashmap = malloc(sizeof(struct HashMap)));
initHashMap(hashmap, destructor, arg);
return hashmap;
}
void initHashMap(struct HashMap *hashmap,
void (*destructor)(void *arg, char *key, char *value),
void *arg) {
hashmap->destructor = destructor;
hashmap->arg = arg;
hashmap->entries = NULL;
hashmap->mapSize = 0;
hashmap->numEntries = 0;
}
void destroyHashMap(struct HashMap *hashmap) {
if (hashmap) {
for (int i = 0; i < hashmap->mapSize; i++) {
if (hashmap->entries[i]) {
for (int j = 0; hashmap->entries[i][j].key; j++) {
if (hashmap->destructor) {
hashmap->destructor(hashmap->arg,
(char *)hashmap->entries[i][j].key,
(char *)hashmap->entries[i][j].value);
}
}
free(hashmap->entries[i]);
}
}
free(hashmap->entries);
}
}
void deleteHashMap(struct HashMap *hashmap) {
destroyHashMap(hashmap);
free(hashmap);
}
static unsigned int stringHashFunc(const char *s) {
unsigned int h = 0;
while (*s) {
h = 31*h + *(unsigned char *)s++;
}
return h;
}
const void *addToHashMap(struct HashMap *hashmap, const char *key,
const char *value) {
if (hashmap->numEntries + 1 > (hashmap->mapSize * 8)/10) {
struct HashMap newMap;
newMap.numEntries = hashmap->numEntries;
if (hashmap->mapSize == 0) {
newMap.mapSize = 32;
} else if (hashmap->mapSize < 1024) {
newMap.mapSize = 2*hashmap->mapSize;
} else {
newMap.mapSize = hashmap->mapSize + 1024;
}
check(newMap.entries = calloc(sizeof(void *), newMap.mapSize));
for (int i = 0; i < hashmap->mapSize; i++) {
if (!hashmap->entries[i]) {
continue;
}
for (int j = 0; hashmap->entries[i][j].key; j++) {
addToHashMap(&newMap, hashmap->entries[i][j].key,
hashmap->entries[i][j].value);
}
free(hashmap->entries[i]);
}
free(hashmap->entries);
hashmap->entries = newMap.entries;
hashmap->mapSize = newMap.mapSize;
hashmap->numEntries = newMap.numEntries;
}
unsigned hash = stringHashFunc(key);
int idx = hash % hashmap->mapSize;
int i = 0;
if (hashmap->entries[idx]) {
for (i = 0; hashmap->entries[idx][i].key; i++) {
if (!strcmp(hashmap->entries[idx][i].key, key)) {
if (hashmap->destructor) {
hashmap->destructor(hashmap->arg,
(char *)hashmap->entries[idx][i].key,
(char *)hashmap->entries[idx][i].value);
}
hashmap->entries[idx][i].key = key;
hashmap->entries[idx][i].value = value;
return value;
}
}
}
check(hashmap->entries[idx] = realloc(hashmap->entries[idx],
(i+2)*sizeof(*hashmap->entries[idx])));
hashmap->entries[idx][i].key = key;
hashmap->entries[idx][i].value = value;
memset(&hashmap->entries[idx][i+1], 0, sizeof(*hashmap->entries[idx]));
hashmap->numEntries++;
return value;
}
void deleteFromHashMap(struct HashMap *hashmap, const char *key) {
if (hashmap->mapSize == 0) {
return;
}
unsigned hash = stringHashFunc(key);
int idx = hash % hashmap->mapSize;
if (!hashmap->entries[idx]) {
return;
}
for (int i = 0; hashmap->entries[idx][i].key; i++) {
if (!strcmp(hashmap->entries[idx][i].key, key)) {
int j = i + 1;
while (hashmap->entries[idx][j].key) {
j++;
}
if (hashmap->destructor) {
hashmap->destructor(hashmap->arg,
(char *)hashmap->entries[idx][i].key,
(char *)hashmap->entries[idx][i].value);
}
if (i != j-1) {
memcpy(&hashmap->entries[idx][i], &hashmap->entries[idx][j-1],
sizeof(*hashmap->entries[idx]));
}
memset(&hashmap->entries[idx][j-1], 0, sizeof(*hashmap->entries[idx]));
check(--hashmap->numEntries >= 0);
}
}
}
char **getRefFromHashMap(const struct HashMap *hashmap, const char *key) {
if (hashmap->mapSize == 0) {
return NULL;
}
unsigned hash = stringHashFunc(key);
int idx = hash % hashmap->mapSize;
if (!hashmap->entries[idx]) {
return NULL;
}
for (int i = 0; hashmap->entries[idx][i].key; i++) {
if (!strcmp(hashmap->entries[idx][i].key, key)) {
return (char **)&hashmap->entries[idx][i].value;
}
}
return NULL;
}
const char *getFromHashMap(const struct HashMap *hashmap, const char *key) {
char **ref = getRefFromHashMap(hashmap, key);
return ref ? *ref : NULL;
}
void iterateOverHashMap(struct HashMap *hashmap,
int (*fnc)(void *arg, const char *key, char **value),
void *arg) {
for (int i = 0; i < hashmap->mapSize; i++) {
if (hashmap->entries[i]) {
int count = 0;
while (hashmap->entries[i][count].key) {
count++;
}
for (int j = 0; j < count; j++) {
if (!fnc(arg, hashmap->entries[i][j].key,
(char **)&hashmap->entries[i][j].value)) {
if (hashmap->destructor) {
hashmap->destructor(hashmap->arg,
(char *)hashmap->entries[i][j].key,
(char *)hashmap->entries[i][j].value);
}
if (j != count-1) {
memcpy(&hashmap->entries[i][j], &hashmap->entries[i][count-1],
sizeof(*hashmap->entries[i]));
}
memset(&hashmap->entries[i][count-1], 0,
sizeof(*hashmap->entries[i]));
count--;
j--;
}
}
}
}
}
int getHashmapSize(const struct HashMap *hashmap) {
return hashmap->numEntries;
}