summaryrefslogtreecommitdiff
path: root/utils/hashmap.c
blob: 814dc55b5eb9a17743258538c6b97927c0ec02be (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
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
244
245
246
247
248
249
250
251
252
/*
 * Copyright 2020 Daniel Silverstone <dsilvers@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>

#include "utils/hashmap.h"

/**
 * The default number of buckets in the hashmaps we create.
 */
#define DEFAULT_HASHMAP_BUCKETS (4091)

/**
 * Hashmaps have chains of entries in buckets.
 */
typedef struct hashmap_entry_s {
	struct hashmap_entry_s **prevptr;
	struct hashmap_entry_s *next;
	void *key;
	void *value;
	uint32_t key_hash;
} hashmap_entry_t;

/**
 * The content of a hashmap
 */
struct hashmap_s {
	/**
	 * The parameters to be used for this hashmap
	 */
	hashmap_parameters_t *params;
	
	/**
	 * The buckets for the hash chains
	 */
	hashmap_entry_t **buckets;
	
	/**
	 * The number of buckets in this map
	 */
	uint32_t bucket_count;
	
	/**
	 * The number of entries in this map
	 */
	size_t entry_count;
};

/* Exported function, documented in hashmap.h */
hashmap_t *
hashmap_create(hashmap_parameters_t *params)
{
	hashmap_t *ret = malloc(sizeof(hashmap_t));

	ret->params = params;
	ret->bucket_count = DEFAULT_HASHMAP_BUCKETS;
	ret->entry_count = 0;
	ret->buckets = malloc(ret->bucket_count * sizeof(hashmap_entry_t *));

	if (ret->buckets == NULL) {
		free(ret);
		return NULL;
	}

	memset(ret->buckets, 0, ret->bucket_count * sizeof(hashmap_entry_t *));

	return ret;
}

/* Exported function, documented in hashmap.h */
void
hashmap_destroy(hashmap_t *hashmap)
{
	uint32_t bucket;
	hashmap_entry_t *entry;

	for (bucket = 0; bucket < hashmap->bucket_count; bucket++) {
		for (entry = hashmap->buckets[bucket];
		     entry != NULL;) {
			hashmap_entry_t *next = entry->next;
			hashmap->params->value_destroy(entry->value);
			hashmap->params->key_destroy(entry->key);
			free(entry);
			entry = next;
		}
	}

	free(hashmap->buckets);
	free(hashmap);
}

/* Exported function, documented in hashmap.h */
void *
hashmap_lookup(hashmap_t *hashmap, void *key)
{
	uint32_t hash = hashmap->params->key_hash(key);
	hashmap_entry_t *entry = hashmap->buckets[hash % hashmap->bucket_count];

	for(;entry != NULL; entry = entry->next) {
		if (entry->key_hash == hash) {
			if (hashmap->params->key_eq(key, entry->key)) {
				return entry->value;
			}
		}
	}

	return NULL;
}

/* Exported function, documented in hashmap.h */
void *
hashmap_insert(hashmap_t *hashmap, void *key)
{
	uint32_t hash = hashmap->params->key_hash(key);
	uint32_t bucket = hash % hashmap->bucket_count;
	hashmap_entry_t *entry = hashmap->buckets[bucket];
	void *new_key, *new_value;

	for(;entry != NULL; entry = entry->next) {
		if (entry->key_hash == hash) {
			if (hashmap->params->key_eq(key, entry->key)) {
				/* This key is already here */
				new_key = hashmap->params->key_clone(key);
				if (new_key == NULL) {
					/* Allocation failed */
					return NULL;
				}
				new_value = hashmap->params->value_alloc(entry->key);
				if (new_value == NULL) {
					/* Allocation failed */
					hashmap->params->key_destroy(new_key);
					return NULL;
				}
				hashmap->params->value_destroy(entry->value);
				hashmap->params->key_destroy(entry->key);
				entry->value = new_value;
				entry->key = new_key;
				return entry->value;
			}
		}
	}

	/* The key was not found in the map, so allocate a new entry */
	entry = malloc(sizeof(*entry));

	if (entry == NULL) {
		return NULL;
	}
	
	memset(entry, 0, sizeof(*entry));

	entry->key = hashmap->params->key_clone(key);
	if (entry->key == NULL) {
		goto err;
	}
	entry->key_hash = hash;

	entry->value = hashmap->params->value_alloc(entry->key);
	if (entry->value == NULL) {
		goto err;
	}

	entry->prevptr = &(hashmap->buckets[bucket]);
	entry->next = hashmap->buckets[bucket];
	if (entry->next != NULL) {
		entry->next->prevptr = &entry->next;
	}

	hashmap->buckets[bucket] = entry;

	hashmap->entry_count++;

	return entry->value;

err:
	if (entry->value != NULL)
		hashmap->params->value_destroy(entry->value);
	if (entry->key != NULL)
		hashmap->params->key_destroy(entry->key);
	free(entry);

	return NULL;
}

/* Exported function, documented in hashmap.h */
bool
hashmap_remove(hashmap_t *hashmap, void *key)
{
	uint32_t hash = hashmap->params->key_hash(key);
	
	hashmap_entry_t *entry = hashmap->buckets[hash % hashmap->bucket_count];

	for(;entry != NULL; entry = entry->next) {
		if (entry->key_hash == hash) {
			if (hashmap->params->key_eq(key, entry->key)) {
				hashmap->params->value_destroy(entry->value);
				hashmap->params->key_destroy(entry->key);
				if (entry->next != NULL) {
					entry->next->prevptr = entry->prevptr;
				}
				*entry->prevptr = entry->next;
				free(entry);
				hashmap->entry_count--;
				return true;
			}
		}
	}

	return false;
}

/* Exported function, documented in hashmap.h */
bool
hashmap_iterate(hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx)
{
	for (uint32_t bucket = 0;
	     bucket < hashmap->bucket_count;
	     bucket++) {
		for (hashmap_entry_t *entry = hashmap->buckets[bucket];
		     entry != NULL;
		     entry = entry->next) {
			/* If the callback returns true, we early-exit */
			if (cb(entry->key, entry->value, ctx))
				return true;
		}
	}

	return false;
}

/* Exported function, documented in hashmap.h */
size_t
hashmap_count(hashmap_t *hashmap)
{
	return hashmap->entry_count;
}