43#ifndef CWARE_LIBCHASH_H
44#define CWARE_LIBCHASH_H
46#define CWARE_LIBCHASH_VERSION "x.0.0"
49#ifndef CHASH_INITIAL_SIZE
50#define CHASH_INITIAL_SIZE 10
51#elif CHASH_INITIAL_SIZE <= 0
52 "chash_init: default length must be greater than 0"
57#define CHASH_RESIZE(size) \
62#ifndef CHASH_LOAD_THRESHOLD
63#define CHASH_LOAD_THRESHOLD 0.8
68#ifndef CHASH_COUNTER_TYPE
69#define CHASH_COUNTER_TYPE int
73#ifndef CHASH_KEY_FIELD
74#define CHASH_KEY_FIELD key
78#ifndef CHASH_VALUE_FIELD
79#define CHASH_VALUE_FIELD value
83#ifndef CHASH_STATE_FIELD
84#define CHASH_STATE_FIELD state
88#ifndef CHASH_BUCKETS_FIELD
89#define CHASH_BUCKETS_FIELD buckets
93#ifndef CHASH_LENGTH_FIELD
94#define CHASH_LENGTH_FIELD length
98#ifndef CHASH_CAPACITY_FIELD
99#define CHASH_CAPACITY_FIELD capacity
103#define CHASH_UNFILLED 0
104#define CHASH_FILLED 1
105#define CHASH_TOMBSTONE 2
109#define chash_string_hash(key, hash) \
112 int __CHASH_HINDEX = 0; \
114 for(__CHASH_HINDEX = 0; (key)[__CHASH_HINDEX] != '\0'; \
115 __CHASH_HINDEX++) { \
116 (hash) = (((hash) << 1) + (hash)) + (key)[__CHASH_HINDEX]; \
120#define chash_string_compare(cmp_a, cmp_b) \
121 (strcmp((cmp_a), (cmp_b)) == 0)
123#define chash_default_init(bucket, _key, _value) \
124 (bucket).CHASH_KEY_FIELD = (_key); \
125 (bucket).CHASH_VALUE_FIELD = _value
148#define __chash_abs(x) \
149 ((x) < 0 ? (x) * - 1 : (x))
151#define __chash_hash(mod, _key, namespace) \
152 __CHASH_HASH = namespace ## _HASH((_key), __CHASH_HASH); \
153 __CHASH_HASH = __CHASH_HASH % (mod); \
154 __CHASH_HASH = __chash_abs(__CHASH_HASH);
156#define __chash_probe(hashtable, _key, namespace) \
157 while(__CHASH_INDEX < (hashtable)->CHASH_CAPACITY_FIELD) { \
158 if((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD == \
162 if((namespace ## _COMPARE((_key), \
163 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_KEY_FIELD)) == 1) { \
165 __CHASH_INDEX = -1; \
169 __CHASH_HASH = (__CHASH_HASH + 1) % (hashtable)->CHASH_CAPACITY_FIELD; \
173#define __chash_probe_to_unfilled(mod, _key, buffer, namespace) \
175 if(buffer[__CHASH_HASH].CHASH_STATE_FIELD != CHASH_FILLED) \
178 if((namespace ## _COMPARE((_key), buffer[__CHASH_HASH].CHASH_KEY_FIELD)) \
182 __CHASH_HASH = (__CHASH_HASH + 1) % mod; \
185#define __chash_resize(hashtable, namespace) \
187 CHASH_COUNTER_TYPE __CHASH_INDEX = 0; \
188 namespace ## _BUCKET *__CHASH_BUCKETS = NULL; \
189 CHASH_COUNTER_TYPE __CHASH_NEXT_SIZE = (CHASH_COUNTER_TYPE) \
190 CHASH_RESIZE((hashtable)->CHASH_CAPACITY_FIELD); \
192 if((namespace ## _HEAP) == 0) { \
193 if((hashtable)->CHASH_LENGTH_FIELD != \
194 (hashtable)->CHASH_CAPACITY_FIELD) { \
198 fprintf(stderr, "__chash_resize: hashtable is full. could not resize" \
199 " (%s:%i)\n", __FILE__, __LINE__); \
203 if((double) (hashtable)->CHASH_LENGTH_FIELD / \
204 (double) (hashtable)->CHASH_CAPACITY_FIELD < CHASH_LOAD_THRESHOLD) \
207 __CHASH_BUCKETS = malloc((size_t) (__CHASH_NEXT_SIZE \
208 * ((CHASH_COUNTER_TYPE) \
209 sizeof(namespace ## _BUCKET)))); \
210 memset(__CHASH_BUCKETS, 0, ((size_t) (__CHASH_NEXT_SIZE \
211 * ((CHASH_COUNTER_TYPE) \
212 sizeof(namespace ## _BUCKET))))); \
214 for(__CHASH_INDEX = 0; __CHASH_INDEX < (hashtable)->CHASH_CAPACITY_FIELD; \
216 namespace ## _BUCKET __CHASH_NEW_KEY_BUCKET; \
217 memset(&__CHASH_NEW_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
218 namespace ## _INIT(__CHASH_NEW_KEY_BUCKET, \
219 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_INDEX].CHASH_KEY_FIELD, \
220 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_INDEX].CHASH_VALUE_FIELD); \
222 if((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_INDEX].CHASH_STATE_FIELD \
226 __chash_hash(__CHASH_NEXT_SIZE, __CHASH_NEW_KEY_BUCKET.CHASH_KEY_FIELD, \
228 __chash_probe_to_unfilled(__CHASH_NEXT_SIZE, \
229 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_INDEX].CHASH_KEY_FIELD, \
230 __CHASH_BUCKETS, namespace) \
232 __CHASH_BUCKETS[__CHASH_HASH] = __CHASH_NEW_KEY_BUCKET; \
233 __CHASH_BUCKETS[__CHASH_HASH].CHASH_STATE_FIELD = CHASH_FILLED; \
237 free((hashtable)->CHASH_BUCKETS_FIELD); \
238 (hashtable)->CHASH_BUCKETS_FIELD = __CHASH_BUCKETS; \
239 (hashtable)->CHASH_CAPACITY_FIELD = __CHASH_NEXT_SIZE; \
243#define __chash_assert_nonnull(func, ptr) \
245 if((ptr) == NULL) { \
246 fprintf(stderr, #func ": " #ptr " cannot be null (%s:%i)\n", \
247 __FILE__, __LINE__); \
268#define __chash_init(hashtable, namespace) \
269 (hashtable)->CHASH_LENGTH_FIELD = 0; \
270 (hashtable)->CHASH_CAPACITY_FIELD = CHASH_INITIAL_SIZE; \
271 (hashtable)->CHASH_BUCKETS_FIELD = malloc(CHASH_INITIAL_SIZE \
272 * sizeof(*((hashtable)->CHASH_BUCKETS_FIELD))); \
273 memset((hashtable)->CHASH_BUCKETS_FIELD, 0, \
274 sizeof(*((hashtable)->CHASH_BUCKETS_FIELD)) * CHASH_INITIAL_SIZE)
276#define chash_init(hashtable, namespace) \
279 (hashtable) = malloc(sizeof((*(hashtable)))); \
280 __chash_init(hashtable, namespace)
282#define chash_init_stack(hashtable, buffer, _length, namespace) \
285 if((_length) <= 0) { \
286 fprintf(stderr, "chash_init_stack: hashtable cannot have a maximum " \
287 "length of 0 or less (%s:%i)\n", __FILE__, __LINE__); \
291 __chash_assert_nonnull(chash_init_stack, buffer); \
293 (hashtable)->CHASH_LENGTH_FIELD = 0; \
294 (hashtable)->CHASH_CAPACITY_FIELD = _length; \
295 (hashtable)->CHASH_BUCKETS_FIELD = buffer
297#define chash_assign(hashtable, _key, _value, namespace) \
299 long __CHASH_HASH = 0; \
300 namespace ## _BUCKET __CHASH_KEY_BUCKET; \
301 memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
302 namespace ## _INIT(__CHASH_KEY_BUCKET, _key, _value); \
304 __chash_assert_nonnull(chash_assign, hashtable); \
305 __chash_assert_nonnull(chash_assign, (hashtable)->CHASH_BUCKETS_FIELD); \
306 __chash_resize(hashtable, namespace); \
307 __chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
308 __chash_probe_to_unfilled((hashtable)->CHASH_CAPACITY_FIELD, \
309 (_key), (hashtable)->CHASH_BUCKETS_FIELD, namespace) \
311 if((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD == \
313 namespace ## _FREE_VALUE( \
314 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD); \
316 (hashtable)->CHASH_LENGTH_FIELD++; \
319 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH] = __CHASH_KEY_BUCKET; \
320 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD = \
324#define chash_lookup(hashtable, _key, storage, namespace) \
328 int __CHASH_INDEX = 0; \
329 long __CHASH_HASH = 0; \
330 namespace ## _BUCKET __CHASH_KEY_BUCKET; \
331 memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
332 namespace ## _INIT(__CHASH_KEY_BUCKET, _key, \
333 __CHASH_KEY_BUCKET.CHASH_VALUE_FIELD); \
335 (void) __CHASH_KEY_BUCKET; \
337 __chash_assert_nonnull(chash_lookup, hashtable); \
338 __chash_assert_nonnull(chash_lookup, (hashtable)->CHASH_BUCKETS_FIELD); \
339 __chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
340 __chash_probe(hashtable, _key, namespace) \
342 if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
343 CHASH_FILLED) || __CHASH_INDEX != -1) { \
344 fprintf(stderr, "chash_lookup: failed to find key in hashtable (%s:%i)" \
345 "\n", __FILE__, __LINE__); \
349 storage = (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD; \
352#define chash_delete(hashtable, _key, namespace) \
354 int __CHASH_INDEX = 0; \
355 long __CHASH_HASH = 0; \
357 __chash_assert_nonnull(chash_delete, hashtable); \
358 __chash_assert_nonnull(chash_delete, (hashtable)->CHASH_BUCKETS_FIELD); \
359 __chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
360 __chash_probe(hashtable, _key, namespace) \
362 if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
363 CHASH_FILLED) || __CHASH_INDEX != -1) { \
364 fprintf(stderr, "chash_delete: failed to find key in hashtable (%s:%i)" \
365 "\n", __FILE__, __LINE__); \
369 namespace ## _FREE_KEY((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH] \
371 namespace ## _FREE_VALUE( \
372 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_VALUE_FIELD); \
373 (hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD = \
375 (hashtable)->CHASH_LENGTH_FIELD--; \
378#define chash_contains(hashtable, _key, storage, namespace) \
382 int __CHASH_INDEX = 0; \
383 long __CHASH_HASH = 0; \
385 __chash_assert_nonnull(chash_contents, hashtable); \
386 __chash_assert_nonnull(chash_contents, (hashtable)->CHASH_BUCKETS_FIELD); \
387 __chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
388 __chash_probe(hashtable, _key, namespace) \
390 if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
391 CHASH_FILLED) || __CHASH_INDEX != -1) { \
396#define chash_lookup_bucket(hashtable, _key, storage, namespace) \
400 CHASH_COUNTER_TYPE __CHASH_INDEX = 0; \
401 long __CHASH_HASH = 0; \
402 namespace ## _BUCKET __CHASH_KEY_BUCKET; \
403 memset(&__CHASH_KEY_BUCKET, 0, sizeof(namespace ## _BUCKET)); \
404 namespace ## _INIT(__CHASH_KEY_BUCKET, _key, \
405 __CHASH_KEY_BUCKET.CHASH_VALUE_FIELD); \
407 (void) __CHASH_KEY_BUCKET; \
409 __chash_assert_nonnull(chash_lookup_bucket, hashtable); \
410 __chash_assert_nonnull(chash_lookup_bucket, \
411 (hashtable)->CHASH_BUCKETS_FIELD); \
412 __chash_hash((hashtable)->CHASH_CAPACITY_FIELD, _key, namespace); \
413 __chash_probe(hashtable, _key, namespace) \
415 if(((hashtable)->CHASH_BUCKETS_FIELD[__CHASH_HASH].CHASH_STATE_FIELD != \
416 CHASH_FILLED) || __CHASH_INDEX != -1) { \
417 fprintf(stderr, "chash_lookup_bucket: failed to find key in hashtable" \
418 "(%s:%i) \n", __FILE__, __LINE__); \
422 storage = ((hashtable)->CHASH_BUCKETS_FIELD + __CHASH_HASH); \
425#define __chash_free(hashtable, namespace) \
427 __chash_assert_nonnull(__chash_free, hashtable); \
428 __chash_assert_nonnull(__chash_free, (hashtable)->CHASH_BUCKETS_FIELD); \
429 (hashtable)->CHASH_CAPACITY_FIELD--; \
431 while((hashtable)->CHASH_CAPACITY_FIELD != -1) { \
432 if((hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
433 .CHASH_STATE_FIELD != CHASH_FILLED) { \
434 (hashtable)->CHASH_CAPACITY_FIELD--; \
438 namespace ##_FREE_KEY( \
439 (hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
441 namespace ##_FREE_VALUE( \
442 (hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
443 .CHASH_VALUE_FIELD); \
444 (hashtable)->CHASH_CAPACITY_FIELD--; \
445 (hashtable)->CHASH_LENGTH_FIELD--; \
448 if((namespace ## _HEAP) == 1) { \
449 free((hashtable)->CHASH_BUCKETS_FIELD); \
453#define chash_free(hashtable, namespace) \
455 __chash_assert_nonnull(chash_free, hashtable); \
456 __chash_assert_nonnull(chash_free, (hashtable)->CHASH_BUCKETS_FIELD); \
457 (hashtable)->CHASH_CAPACITY_FIELD--; \
459 while((hashtable)->CHASH_CAPACITY_FIELD != -1) { \
460 if((hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
461 .CHASH_STATE_FIELD != CHASH_FILLED) { \
462 (hashtable)->CHASH_CAPACITY_FIELD--; \
466 namespace ##_FREE_KEY( \
467 (hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
469 namespace ##_FREE_VALUE( \
470 (hashtable)->CHASH_BUCKETS_FIELD[(hashtable)->CHASH_CAPACITY_FIELD] \
471 .CHASH_VALUE_FIELD); \
472 (hashtable)->CHASH_CAPACITY_FIELD--; \
473 (hashtable)->CHASH_LENGTH_FIELD--; \
476 if((namespace ## _HEAP) == 1) { \
477 free((hashtable)->CHASH_BUCKETS_FIELD); \
482#define chash_is_full(hashtable, namespace) \
483 (((hashtable)->CHASH_LENGTH_FIELD) == ((hashtable)->CHASH_CAPACITY_FIELD))
494#define chash_iter(hashtable, index, _key, _value) \
495 for((index) = 0, (_key) = (hashtable)->CHASH_BUCKETS_FIELD[index]. \
497 (_value) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_VALUE_FIELD; \
498 (index) < (hashtable)->CHASH_CAPACITY_FIELD; \
499 (index) = ((index) < (hashtable)->CHASH_CAPACITY_FIELD) \
500 ? ((index) + 1) : index, \
501 (_key) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_KEY_FIELD, \
502 (_value) = (hashtable)->CHASH_BUCKETS_FIELD[index].CHASH_VALUE_FIELD, \
503 (index) = (hashtable)->CHASH_CAPACITY_FIELD)
505#define chash_skip(hashtable, index) \
506 if((hashtable)->CHASH_BUCKETS_FIELD[index]. \
507 CHASH_STATE_FIELD != CHASH_FILLED) \