NetSurf
hashmap.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Daniel Silverstone <dsilvers@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef NETSURF_HASHMAP_H
20#define NETSURF_HASHMAP_H
21
22#include <stdint.h>
23#include <stdbool.h>
24
25/**
26 * Generic hashmap.
27 *
28 * Hashmaps take ownership of the keys inserted into them by means of a
29 * clone function in their parameters. They also manage the value memory
30 * directly.
31 */
32typedef struct hashmap_s hashmap_t;
33
34/**
35 * Key cloning function type
36 */
37typedef void* (*hashmap_key_clone_t)(void *);
38
39/**
40 * Key destructor function type
41 */
42typedef void (*hashmap_key_destroy_t)(void *);
43
44/**
45 * Key hashing function type
46 */
47typedef uint32_t (*hashmap_key_hash_t)(void *);
48
49/**
50 * Key comparison function type
51 */
52typedef bool (*hashmap_key_eq_t)(void *, void*);
53
54/**
55 * Value allocation function type
56 */
57typedef void* (*hashmap_value_alloc_t)(void *);
58
59/**
60 * Value destructor function type
61 */
62typedef void (*hashmap_value_destroy_t)(void *);
63
64/**
65 * Hashmap iteration callback function type.
66 *
67 * First parameter is the key, second is the value.
68 * The final parameter is the context pointer for the iteration.
69 *
70 * Return true to stop iterating early
71 */
72typedef bool (*hashmap_iteration_cb_t)(void *, void *, void *);
73
74/**
75 * Parameters for hashmaps
76 */
77typedef struct {
78 /**
79 * A function which when called will clone a key and give
80 * ownership of the returned object to the hashmap
81 */
83
84 /**
85 * A function which when given a key will return its hash.
86 */
88
89 /**
90 * A function to compare two keys and return if they are equal.
91 * Note: identity is not necessary, nor strict equality, so long
92 * as the function is a full equality model.
93 * (i.e. key1 == key2 => key2 == key1)
94 */
96
97 /**
98 * A function which when called will destroy a key object
99 */
101
102 /**
103 * A function which when called will allocate a value object
104 */
106
107 /**
108 * A function which when called will destroy a value object
109 */
112
113
114/**
115 * Create a hashmap
116 *
117 * The provided hashmap parameter table will be used for map operations
118 * which need to allocate/free etc.
119 *
120 * \param params The hashmap parameters for this map
121 */
123
124/**
125 * Destroy a hashmap
126 *
127 * After this, all keys and values will have been destroyed and all memory
128 * associated with this hashmap will be invalidated.
129 *
130 * \param hashmap The hashmap to destroy
131 */
132void hashmap_destroy(hashmap_t *hashmap);
133
134/**
135 * Look up a key in a hashmap
136 *
137 * If the key has an associated value in the hashmap then the pointer to it
138 * is returned, otherwise NULL.
139 *
140 * \param hashmap The hashmap to look up the key inside
141 * \param key The key to look up in the hashmap
142 * \return A pointer to the value if found, NULL otherwise
143 */
144void* hashmap_lookup(hashmap_t *hashmap, void *key);
145
146/**
147 * Create an entry in a hashmap
148 *
149 * This creates a blank value using the parameters and then associates it with
150 * a clone of the given key, inserting it into the hashmap. If a value was
151 * present for the given key already, then it is destroyed first.
152 *
153 * NOTE: If allocation of the new value object fails, then any existing entry
154 * will be left alone, but NULL will be returned.
155 *
156 * \param hashmap The hashmap to insert into
157 * \param key The key to insert an entry for
158 * \return The value pointer for that key, or NULL if allocation failed.
159 */
160void *hashmap_insert(hashmap_t *hashmap, void *key);
161
162/**
163 * Remove an entry from the hashmap
164 *
165 * This will remove the entry for the given key from the hashmap
166 * If there is no such entry, this will safely do nothing.
167 * The value associated with the entry will be destroyed and so should not
168 * be used beyond calling this function.
169 *
170 * \param hashmap The hashmap to remove the entry from
171 * \param key The key to remove the entry for
172 * \return true if an entry was removed, false otherwise
173 */
174bool hashmap_remove(hashmap_t *hashmap, void *key);
175
176/**
177 * Iterate the hashmap
178 *
179 * For each key/value pair in the hashmap, call the callback passing in
180 * the key and value. During iteration you MUST NOT mutate the hashmap.
181 *
182 * \param hashmap The hashmap to iterate
183 * \param cb The callback for each key,value pair
184 * \param ctx The callback context
185 * \return Whether or not we stopped iteration early
186 */
187bool hashmap_iterate(hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx);
188
189/**
190 * Get the number of entries in this map
191 *
192 * \param hashmap The hashmap to retrieve the entry count from
193 * \return The number of entries in the hashmap
194 */
195size_t hashmap_count(hashmap_t *hashmap);
196
197#endif
void(* hashmap_value_destroy_t)(void *)
Value destructor function type.
Definition: hashmap.h:62
bool hashmap_remove(hashmap_t *hashmap, void *key)
Remove an entry from the hashmap.
Definition: hashmap.c:206
hashmap_t * hashmap_create(hashmap_parameters_t *params)
Create a hashmap.
Definition: hashmap.c:67
void(* hashmap_key_destroy_t)(void *)
Key destructor function type.
Definition: hashmap.h:42
void *(* hashmap_key_clone_t)(void *)
Key cloning function type.
Definition: hashmap.h:37
size_t hashmap_count(hashmap_t *hashmap)
Get the number of entries in this map.
Definition: hashmap.c:252
uint32_t(* hashmap_key_hash_t)(void *)
Key hashing function type.
Definition: hashmap.h:47
bool(* hashmap_key_eq_t)(void *, void *)
Key comparison function type.
Definition: hashmap.h:52
void * hashmap_lookup(hashmap_t *hashmap, void *key)
Look up a key in a hashmap.
Definition: hashmap.c:113
void * hashmap_insert(hashmap_t *hashmap, void *key)
Create an entry in a hashmap.
Definition: hashmap.c:131
bool hashmap_iterate(hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx)
Iterate the hashmap.
Definition: hashmap.c:233
void *(* hashmap_value_alloc_t)(void *)
Value allocation function type.
Definition: hashmap.h:57
bool(* hashmap_iteration_cb_t)(void *, void *, void *)
Hashmap iteration callback function type.
Definition: hashmap.h:72
void hashmap_destroy(hashmap_t *hashmap)
Destroy a hashmap.
Definition: hashmap.c:91
Parameters for hashmaps.
Definition: hashmap.h:77
hashmap_value_destroy_t value_destroy
A function which when called will destroy a value object.
Definition: hashmap.h:110
hashmap_value_alloc_t value_alloc
A function which when called will allocate a value object.
Definition: hashmap.h:105
hashmap_key_hash_t key_hash
A function which when given a key will return its hash.
Definition: hashmap.h:87
hashmap_key_clone_t key_clone
A function which when called will clone a key and give ownership of the returned object to the hashma...
Definition: hashmap.h:82
hashmap_key_destroy_t key_destroy
A function which when called will destroy a key object.
Definition: hashmap.h:100
hashmap_key_eq_t key_eq
A function to compare two keys and return if they are equal.
Definition: hashmap.h:95
The content of a hashmap.
Definition: hashmap.c:43