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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/**
* mds — A micro-display server
* Copyright © 2014, 2015, 2016, 2017 Mattias Andrée (maandree@kth.se)
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef MDS_LIBMDSSERVER_HASH_TABLE_H
#define MDS_LIBMDSSERVER_HASH_TABLE_H
#include "table-common.h"
#define HASH_TABLE_T_VERSION 0
/**
* Hash table entry
*/
typedef struct hash_entry
{
/**
* A key
*/
size_t key;
/**
* The value associated with the key
*/
size_t value;
/**
* The truncated hash value of the key
*/
size_t hash;
/**
* The next entry in the bucket
*/
struct hash_entry *next;
} hash_entry_t;
/**
* Value lookup table based on hash value, that do not support
*/
typedef struct hash_table
{
/**
* The table's capacity, i.e. the number of buckets
*/
size_t capacity;
/**
* Entry buckets
*/
hash_entry_t **buckets;
/**
* When, in the ratio of entries comparied to the capacity, to grow the table
*/
float load_factor;
/**
* When, in the number of entries, to grow the table
*/
size_t threshold;
/**
* The number of entries stored in the table
*/
size_t size;
/**
* Check whether two values are equal
*
* If this function pointer is `NULL`, the identity is used
*
* Be aware, this variable cannot be marshalled
*/
compare_func *value_comparator;
/**
* Check whether two keys are equal
*
* If this function pointer is `NULL`, the identity is used
*
* Be aware, this variable cannot be marshalled
*/
compare_func *key_comparator;
/**
* Calculate the hash of a key
*
* If this function pointer is `NULL`, the identity hash is used
*
* Be aware, this variable cannot be marshalled
*
* @param key The key
* @return The hash of the key
*/
hash_func *hasher;
} hash_table_t;
/**
* Create a hash table
*
* @param this Memory slot in which to store the new hash table
* @param initial_capacity The initial capacity of the table
* @param load_factor The load factor of the table, i.e. when to grow the table
* @return Non-zero on error, `errno` will have been set accordingly
*/
__attribute__((nonnull))
int hash_table_create_fine_tuned(hash_table_t *restrict this, size_t initial_capacity, float load_factor);
/**
* Create a hash table
*
* @param this:hash_table_t* Memory slot in which to store the new hash table
* @param initial_capacity:size_t The initial capacity of the table
* @return :int Non-zero on error, `errno` will have been set accordingly
*/
#define hash_table_create_tuned(this, initial_capacity)\
hash_table_create_fine_tuned(this, initial_capacity, 0.75f)
/**
* Create a hash table
*
* @param this:hash_table_t* Memory slot in which to store the new hash table
* @return :int Non-zero on error, `errno` will have been set accordingly
*/
#define hash_table_create(this)\
hash_table_create_tuned(this, 16)
/**
* Release all resources in a hash table, should
* be done even if construction fails
*
* @param this The hash table
* @param keys_freer Function that frees a key, `NULL` if keys should not be freed
* @param values_freer Function that frees a value, `NULL` if value should not be freed
*/
__attribute__((nonnull(1)))
void hash_table_destroy(hash_table_t *restrict this, free_func *key_freer, free_func *value_freer);
/**
* Check whether a value is stored in the table
*
* @param this The hash table
* @param value The value
* @return Whether the value is stored in the table
*/
__attribute__((pure, nonnull))
int hash_table_contains_value(const hash_table_t *restrict this, size_t value);
/**
* Check whether a key is used in the table
*
* @param this The hash table
* @param key The key
* @return Whether the key is used
*/
__attribute__((pure, nonnull))
int hash_table_contains_key(const hash_table_t *restrict this, size_t key);
/**
* Look up a value in the table
*
* @param this The hash table
* @param key The key associated with the value
* @return The value associated with the key, 0 if the key was not used
*/
__attribute__((pure, nonnull))
size_t hash_table_get(const hash_table_t *restrict this, size_t key);
/**
* Look up an entry in the table
*
* @param this The hash table
* @param key The key associated with the value
* @return The entry associated with the key, `NULL` if the key was not used
*/
__attribute__((pure, nonnull))
hash_entry_t *hash_table_get_entry(const hash_table_t *restrict this, size_t key);
/**
* Add an entry to the table
*
* @param this The hash table
* @param key The key of the entry to add
* @param value The value of the entry to add
* @return The previous value associated with the key, 0 if the key was not used.
* 0 will also be returned on error, check the `errno` variable.
*/
__attribute__((nonnull))
size_t hash_table_put(hash_table_t *restrict this, size_t key, size_t value);
/**
* Remove an entry in the table
*
* @param this The hash table
* @param key The key of the entry to remove
* @return The previous value associated with the key, 0 if the key was not used
*/
__attribute__((nonnull))
size_t hash_table_remove(hash_table_t *restrict this, size_t key);
/**
* Remove all entries in the table
*
* @param this The hash table
*/
__attribute__((nonnull))
void hash_table_clear(hash_table_t *restrict this);
/**
* Wrapper for `for` keyword that iterates over entry element in a hash table
*
* @param this:hash_table_t The hash table
* @param i:size_t The variable to store the buckey index in at each iteration
* @param entry:hash_entry_t* The variable to store the entry in at each iteration
*/
#define foreach_hash_table_entry(this, i, entry)\
for (i = 0; i < (this).capacity; i++)\
for (entry = (this).buckets[i]; entry; entry = entry->next)
/**
* Calculate the buffer size need to marshal a hash table
*
* @param this The hash table
* @return The number of bytes to allocate to the output buffer
*/
__attribute__((pure, nonnull))
size_t hash_table_marshal_size(const hash_table_t *restrict this);
/**
* Marshals a hash table
*
* @param this The hash table
* @param data Output buffer for the marshalled data
*/
__attribute__((nonnull))
void hash_table_marshal(const hash_table_t *restrict this, char *restrict data);
/**
* Unmarshals a hash table
*
* @param this Memory slot in which to store the new hash table
* @param data In buffer with the marshalled data
* @param remapper Function that translates values, `NULL` if not translation takes place
* @return Non-zero on error, `errno` will be set accordingly.
* Destroy the table on error.
*/
__attribute__((nonnull(1, 2)))
int hash_table_unmarshal(hash_table_t *restrict this, char *restrict data, remap_func *remapper);
#endif
|