aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver/hash-table.c
blob: ef3ae1f9040230d67407f01e56c5fa70f91dd728 (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
 * mds — A micro-display server
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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/>.
 */
#include "hash-table.h"


/**
 * Test if a key matches the key in a bucket
 * 
 * @param  T  The instance of the hash table
 * @param  B  The bucket
 * @param  K  The key
 * @param  H  The hash of the key
 */
#define TEST_KEY(T, B, K, H) \
  ((B->key == K) || (T->key_comparator && (B->hash == H) && T->key_comparator(B->key, K)))


/**
 * Calculate the hash of a key
 * 
 * @param   this  The hash table
 * @param   key   The key to hash
 * @return        The hash of the key
 */
static inline size_t __attribute__((const)) hash(const hash_table_t* restrict this, size_t key)
{
  return this->hasher ? this->hasher(key) : key;
}


/**
 * Truncates the hash of a key to constrain it to the buckets
 * 
 * @param   this  The hash table
 * @param   key   The key to hash
 * @return        A non-negative value less the the table's capacity
 */
static inline size_t __attribute__((pure)) truncate_hash(const hash_table_t* restrict this, size_t hash)
{
  return hash % this->capacity;
}


/**
 * Grow the table
 * 
 * @param  this  The hash table
 */
static void rehash(hash_table_t* restrict this)
{
  hash_entry_t** old_buckets = this->buckets;
  size_t old_capacity = this->capacity;
  size_t i = old_capacity, index;
  hash_entry_t* bucket;
  hash_entry_t* destination;
  hash_entry_t* next;
  
  this->capacity = old_capacity * 2 + 1;
  this->threshold = (size_t)((float)(this->capacity) * this->load_factor);
  this->buckets = calloc(this->capacity, sizeof(hash_entry_t*));
  
  while (i)
    {
      bucket = *(this->buckets + --i);
      while (bucket)
	{
	  index = truncate_hash(this, bucket->hash);
	  if ((destination = *(this->buckets + index)))
	    {
	      next = destination->next;
	      while (next)
		{
		  destination = next;
		  next = destination->next;
		}
	      destination->next = bucket;
	    }
	  else
	    *(this->buckets + index) = bucket;
	  
	  next = bucket->next;
	  bucket->next = NULL;
	  bucket = next;
	}
    }
  
  free(old_buckets);
}


/**
 * 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
 */
int /**/__attribute__((const))/**/ hash_table_create_fine_tuned(hash_table_t* restrict this, size_t initial_capacity, float load_factor)
{
  (void) this; (void) initial_capacity; (void) load_factor; return 0; /* TODO */
}


/**
 * Clone a hash table
 * 
 * @param   this  The hash table to clone
 * @param   out   Memory slot in which to store the new hash table
 * @return        Non-zero on error, `errno` will have been set accordingly
 */
int /**/__attribute__((const))/**/ hash_table_clone(const hash_table_t* restrict this, hash_table_t* restrict out)
{
  (void) this; (void) out; return 0; /* TODO */
}



/**
 * Release all resources in a hash table, should
 * be done even if construction fails
 * 
 * @param  this        The hash table
 * @param  keys_out    Linked list to fill with all keys in the table, `NULL` if it should not be collected
 * @param  values_out  Linked list to fill with all values in the table, `NULL` if it should not be collected
 */
void hash_table_destroy(hash_table_t* restrict this, linked_list_t* keys_out, linked_list_t* values_out)
{
  size_t i = this->capacity;
  hash_entry_t* bucket;
  hash_entry_t* last;
  
  if (this->buckets != NULL)
    {
      while (i)
	{
	  bucket = *(this->buckets + --i);
	  while (bucket)
	    {
	      if (keys_out != NULL)
		linked_list_insert_end(keys_out, bucket->key); /* TODO: error support */
	      if (values_out != NULL)
		linked_list_insert_end(values_out, bucket->value); /* TODO: error support */
	      bucket = (last = bucket)->next;
	      free(last);
	    }
	}
      free(this->buckets);
    }
}


/**
 * 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
 */
int hash_table_contains_value(const hash_table_t* restrict this, size_t value)
{
  size_t i = this->capacity;
  hash_entry_t* bucket;
  
  while (i)
    {
      bucket = *(this->buckets + --i);
      while (bucket)
	{
	  if (bucket->value == value)
	    return 1;
	  if (this->value_comparator && this->value_comparator(bucket->value, value))
	    return 1;
	  bucket = bucket->next;
	}
    }
  
  return 0;
}


/**
 * Check whether a key is used in the table
 * 
 * @param   this  The hash table
 * @param   key   The key
 * @return        Whether the key is used
 */
int hash_table_contains_key(const hash_table_t* restrict this, size_t key)
{
  size_t key_hash = hash(this, key);
  size_t index = truncate_hash(this, key_hash);
  hash_entry_t* bucket = *(this->buckets + index);
  
  while (bucket)
    {
      if (TEST_KEY(this, bucket, key, key_hash))
	return 1;
      bucket = bucket->next;
    }
  
  return 0;
}


/**
 * 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
 */
size_t hash_table_get(const hash_table_t* restrict this, size_t key)
{
  size_t key_hash = hash(this, key);
  size_t index = truncate_hash(this, key_hash);
  hash_entry_t* bucket = *(this->buckets + index);
  
  while (bucket)
    {
      if (TEST_KEY(this, bucket, key, key_hash))
	return bucket->value;
      bucket = bucket->next;
    }
  
  return 0;
}


/**
 * 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
 */
size_t hash_table_put(hash_table_t* restrict this, size_t key, size_t value)
{
  size_t key_hash = hash(this, key);
  size_t index = truncate_hash(this, key_hash);
  hash_entry_t* bucket = *(this->buckets + index);
  size_t rc;
  
  while (bucket)
    if (TEST_KEY(this, bucket, key, key_hash))
      {
	rc = bucket->value;
	bucket->value = value;
	return rc;
      }
    else
      bucket = bucket->next;
  
  if (++(this->size) > this->threshold)
    {
      rehash(this);
      index = truncate_hash(this, key_hash);
    }
  
  bucket = malloc(sizeof(hash_entry_t)); /* TODO: error support */
  bucket->value = value;
  bucket->key = key;
  bucket->hash = key_hash;
  bucket->next = *(this->buckets + index);
  *(this->buckets + index) = bucket;
  
  return 0;
}


/**
 * 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
 */
size_t hash_table_remove(hash_table_t* restrict this, size_t key)
{
  size_t key_hash = hash(this, key);
  size_t index = truncate_hash(this, key_hash);
  hash_entry_t* bucket = *(this->buckets + index);
  hash_entry_t* last = NULL;
  size_t rc;
  
  while (bucket)
    {
      if (TEST_KEY(this, bucket, key, key_hash))
	{
	  if (last == NULL)
	    *(this->buckets + index) = bucket->next;
	  else
	    last->next = bucket->next;
	  this->size--;
	  rc = bucket->value;
	  free(bucket);
	  return rc;
	}
      last = bucket;
      bucket = bucket->next;
    }
  
  return 0;
}


/**
 * Remove all entries in the table
 * 
 * @param  this  The hash table
 */
void hash_table_clear(hash_table_t* restrict this)
{
  hash_entry_t** buf;
  hash_entry_t* bucket;
  size_t i, ptr;
  
  if (this->size)
    {
      buf = alloca((this->size + 1) * sizeof(hash_entry_t*));
      i = this->capacity;
      while (i)
	{
	  bucket = *(this->buckets + --i);
	  ptr = 0;
	  *(buf + ptr++) = bucket;
	  while (bucket)
	    {
	      bucket = bucket->next;
	      *(buf + ptr++) = bucket;
	    }
	  while (ptr)
	    free(*(buf + --ptr));
	  *(this->buckets + i) = NULL;
	}
      this->size = 0;
    }
}


/**
 * 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
 */
size_t /**/__attribute__((const))/**/ hash_table_marshal_size(const hash_table_t* restrict this)
{
  (void) this; return 0; /* TODO */
}


/**
 * Marshals a hash table
 * 
 * @param  this  The hash table
 * @param  data  Output buffer for the marshalled data
 */
void /**/__attribute__((const))/**/ hash_table_marshal(const hash_table_t* restrict this, char* restrict data)
{
  (void) this; (void) data; /* TODO */
}


/**
 * Unmarshals a hash table
 * 
 * @param   this  Memory slot in which to store the new hash table
 * @param   data  In buffer with the marshalled data
 * @return        Non-zero one error, errno will be set accordingly.
 *                Destroy the list on error.
 */
int /**/__attribute__((const))/**/ hash_table_unmarshal(hash_table_t* restrict this, char* restrict data)
{
  (void) this; (void) data; return 0; /* TODO */
}