-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.h
284 lines (252 loc) · 7.63 KB
/
hashtable.h
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
274
275
276
277
278
279
280
281
282
283
/***************************************************************************
begin........: May 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 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 v3 for more details.
***************************************************************************/
/**
* \file hashtable.h
* \brief Generic hashtable.
* \author Sebastian Fedrau <[email protected]>
*/
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <stddef.h>
#include <stdbool.h>
#include "datatypes.h"
#include "pool.h"
/*! HashTable size used to enable auto-resizing. */
#define HASHTABLE_AUTO_RESIZE 0
/**
*\struct HashTable
*\brief Table containing lists of buckets to create associations between keys and values.
*/
typedef struct _HashTable
{
/*! Function to check equality of two keys. */
EqualFunc compare_keys;
/*! Function to free keys. */
FreeFunc free_key;
/*! Function to free values. */
FreeFunc free_value;
/*! Function to create a hash from a value. */
HashFunc hash;
/*! Size of the hash table. */
size_t size;
/*! Pointer to a prime number specifying the current table size. */
const size_t *sizeptr;
/*! Enable/disable auto-resize. */
bool grow;
/**
*\struct _Bucket
*\brief Singly-linked list implementation storing keys & values.
*
*\var buckets
*\brief Array containing pointers to buckets.
*/
struct _Bucket
{
/*! Key of the element. */
void *key;
/*! Value of the element. */
void *data;
/*! Pointer to next list element or NULL. */
struct _Bucket *next;
} **buckets;
/*! Number of stored elements. */
size_t count;
/*! Pool used to create/destroy list elements. */
Pool *pool;
/**
*\struct _HashTablePair
*\brief Found key-value pair.
*
*\var pair
*\brief Last found key-value pair.
*/
struct _HashTablePair
{
/*! Function to free the associated value. */
FreeFunc free_value;
/*! Bucket containing the found key-value pair. */
struct _Bucket *bucket;
} pair;
} HashTable;
/*! A found key-value pair. */
typedef struct _HashTablePair HashTablePair;
/**
*\struct HashTableIter
*\brief Structure to iterate over the elements of a HashTable.
*/
typedef struct
{
/*! Pointer to the associated HashTable. */
const HashTable *table;
/*! Index of current bucket. */
size_t offset;
/*! Pointer to current list element. */
struct _Bucket *liter;
/*! true if iteration is completed. */
bool finished;
} HashTableIter;
/**
*\enum HashTableInsertResult
*\brief result of hashtable_set() method.
*/
typedef enum
{
/*! Item has been inserted. */
HASHTABLE_INSERT_RESULT_NEW,
/*! Item has been replaced. */
HASHTABLE_INSERT_RESULT_REPLACED,
/*! Item insertion failed. */
HASHTABLE_INSERT_RESULT_FAILED
} HashTableInsertResult;
/**
*\param size size of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically
*\param hash_func function to create hash from a key
*\param compare_keys function to check equality of two keys
*\param free_key function to free keys or NULL
*\param free_value function to free values or NULL
*\return a new HashTable
*
* Creates a new HashTable.
*/
HashTable *hashtable_new(size_t size, HashFunc hash_func, EqualFunc compare_keys, FreeFunc free_key, FreeFunc free_value);
/**
*\param table a HashTable
*\param size size of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically
*\param hash_func function to create hash from a key
*\param compare_keys function to check equality of two keys
*\param free_key function to free keys or NULL
*\param free_value function to free values or NULL
*
* Initializes a HashTable.
*/
void hashtable_init(HashTable *table, size_t size, HashFunc hash_func, EqualFunc compare_keys, FreeFunc free_key, FreeFunc free_value);
/**
*\param table a HashTable
*
* Frees all keys, values and the table pointer.
*/
void hashtable_destroy(HashTable *table);
/**
*\param table a HashTable
*
* Frees all keys and values without freeing the table pointer.
*/
void hashtable_free(HashTable *table);
/**
*\param table a HashTable
*
* Removes all elements from the HashTable.
*/
void hashtable_clear(HashTable *table);
/**
*\param table a HashTable
*\param key key to insert
*\param value the value to associate with the key
*\param overwrite_key true to overwrite already existing keys
*\return type of the performed insert operation
*
* Inserts a new key and value in the HashTable. If overwrite_key is set an existing key is
* freed using the specified free_key function before it gets replaced.
*/
HashTableInsertResult hashtable_set(HashTable *table, void *key, void *value, bool overwrite_key);
/**
*\param table a HashTable
*\param key key of the element to remove
*
* Removes an element from the HashTable.
*/
void hashtable_remove(HashTable *table, const void *key);
/**
*\param table a HashTable
*\param key key to lookup
*\return the found key-value pair or NULL
*
* Looks up a key-value pair in the HashTable.
*/
HashTablePair *hashtable_lookup(HashTable *table, const void *key);
/**
*\param pair a key-value pair
*\return key of the pair
*
* Retrieves the key of a key-value pair.
*/
void *hashtable_pair_get_key(const HashTablePair *pair);
/*! Accesses the key of a key-value pair directly. */
#define hashtable_pair_key(p) p->bucket->key
/**
*\param pair a key-value pair
*\return value of the pair
*
* Retrieves the value of a key-value pair.
*/
void *hashtable_pair_get_value(const HashTablePair *pair);
/*! Accesses the value of a key-value pair directly. */
#define hashtable_pair_value(p) p->bucket->data
/**
*\param pair a HashTablePair
*\param value new value to set
*
* Overwrites the value of a key-value pair.
*/
void hashtable_pair_set_value(HashTablePair *pair, void *value);
/**
*\param table a HashTable
*\param key key to test
*\return true if given key does exist
*
* Checks if a key does exist.
*/
bool hashtable_key_exists(const HashTable *table, const void *key);
/**
*\param table a HashTable
*\return number of stored elements
*
* Gets the number of stored elements.
*/
size_t hashtable_count(const HashTable *table);
/**
*\param table a HashTable
*\param iter an uninitialized HashTableIter
*
* Initializes a key/value pair iterator and associates it with the table. Modifying the table while
* using the iterator might lead to undefined behaviour.
*/
void hashtable_iter_init(const HashTable *table, HashTableIter *iter);
/**
*\param iter a HashTableIter
*\return false if end of the HashTable has been reached
*
* Goes to next element of the HashTable.
*/
bool hashtable_iter_next(HashTableIter *iter);
/**
*\param iter a HashTableIter
*\return key of current element
*
* Retrieves the key of the current element.
*/
void *hashtable_iter_get_key(const HashTableIter *iter);
/*! Accesses the key of the current element directly. */
#define hashtable_iter_key(iter) iter.liter->key
/**
*\param iter a HashTableIter
*\return value of current element
*
* Retrieves the value of the current element.
*/
void *hashtable_iter_get_value(const HashTableIter *iter);
/*! Accesses the value of the current element directly. */
#define hashtable_iter_value(iter) iter.liter->data
#endif