Debian Codegraph @

dhash/dhash.c

raw symbols SCIP
1 /*
2 Authors:
3 John Dennis <jdennis.redhat.com>
4 Esmond Pitt <[email protected]>
5
6 This implementation was based on a 3/7/1989 public domain submission
7 to comp.sources.misc by Esmond Pitt <[email protected]>.
8
9 Copyright (C) 2009 Red Hat
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*****************************************************************************/
26 /******************************** Documentation ******************************/
27 /*****************************************************************************/
28
29 /*
30 * See documentation in corresponding header file dhash.h.
31 *
32 * Compilation controls:
33 * DEBUG controls some informative traces, mainly for debugging.
34 * HASH_STATISTICS causes hash_accesses and hash_collisions to be maintained;
35 * when combined with DEBUG, these are displayed by hash_destroy().
36 *
37 */
38
39 /*****************************************************************************/
40 /******************************* Include Files *******************************/
41 /*****************************************************************************/
42
43 #include "config.h"
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include "dhash.h"
49
50 /*****************************************************************************/
51 /****************************** Internal Defines *****************************/
52 /*****************************************************************************/
53
54 #define PRIME_1 37
55 #define PRIME_2 1048583
56
57 #ifndef MIN
58 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
59 #endif
60
61 #ifndef MAX
62 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
63 #endif
64
65 #define halloc(table, size) table->halloc(size, table->halloc_pvt)
66 #define hfree(table, ptr) table->hfree(ptr, table->halloc_pvt)
67 #define hdelete_callback(table, type, entry) do { \
68 if (table->delete_callback) { \
69 table->delete_callback(entry, type, table->delete_pvt); \
70 } \
71 } while(0)
72
73 /*****************************************************************************/
74 /************************** Internal Type Definitions ************************/
75 /*****************************************************************************/
76
77 typedef struct element_t {
78 hash_entry_t entry;
79 struct element_t *next;
80 } element_t, *segment_t;
81
82
83 struct hash_table_str {
84 unsigned long p; /* Next bucket to be split */
85 unsigned long maxp; /* upper bound on p during expansion */
86 unsigned long entry_count; /* current # entries */
87 unsigned long bucket_count; /* current # buckets */
88 unsigned long segment_count; /* current # segments */
89 unsigned long min_load_factor;
90 unsigned long max_load_factor;
91 unsigned long directory_size;
92 unsigned int directory_size_shift;
93 unsigned long segment_size;
94 unsigned int segment_size_shift;
95 hash_delete_callback *delete_callback;
96 void *delete_pvt;
97 hash_alloc_func *halloc;
98 hash_free_func *hfree;
99 void *halloc_pvt;
100 segment_t **directory;
101 #ifdef HASH_STATISTICS
102 hash_statistics_t statistics;
103 #endif
104
105 };
106
107 typedef unsigned long address_t;
108
109 typedef struct hash_keys_callback_data_t {
110 unsigned long index;
111 hash_key_t *keys;
112 } hash_keys_callback_data_t;
113
114 typedef struct hash_values_callback_data_t {
115 unsigned long index;
116 hash_value_t *values;
117 } hash_values_callback_data_t;
118
119 struct _hash_iter_context_t {
120 struct hash_iter_context_t iter;
121 hash_table_t *table;
122 unsigned long i, j;
123 segment_t *s;
124 element_t *p;
125 };
126
127 enum hash_iter_state {
128 HI_STATE_0,
129 HI_STATE_1,
130 HI_STATE_2,
131 HI_STATE_3A,
132 HI_STATE_3B
133 };
134
135 /*****************************************************************************/
136 /********************** External Function Declarations *********************/
137 /*****************************************************************************/
138
139 /*****************************************************************************/
140 /********************** Internal Function Declarations *********************/
141 /*****************************************************************************/
142
143 static address_t convert_key(hash_key_t *key);
144 static address_t hash(hash_table_t *table, hash_key_t *key);
145 static bool key_equal(hash_key_t *a, hash_key_t *b);
146 static int contract_table(hash_table_t *table);
147 static int expand_table(hash_table_t *table);
148 static hash_entry_t *hash_iter_next(struct hash_iter_context_t *iter);
149
150 /*****************************************************************************/
151 /************************* External Global Variables ***********************/
152 /*****************************************************************************/
153
154 /*****************************************************************************/
155 /************************* Internal Global Variables ***********************/
156 /*****************************************************************************/
157
158 #ifdef DEBUG
159 int debug_level = 1;
160 #endif
161
162 /*****************************************************************************/
163 /*************************** Internal Functions ****************************/
164 /*****************************************************************************/
165
166 static void *sys_malloc_wrapper(size_t size, void *pvt)
167 {
168 return malloc(size);
169 }
170
171 static void sys_free_wrapper(void *ptr, void *pvt)
172 {
173 return free(ptr);
174 }
175
176 static address_t convert_key(hash_key_t *key)
177 {
178 address_t h;
179 const unsigned char *k;
180
181 switch(key->type) {
182 case HASH_KEY_ULONG:
183 h = key->ul;
184 break;
185 case HASH_KEY_STRING:
186 /* Convert string to integer */
187 for (h = 0, k = (const unsigned char *) key->str; *k; k++)
188 h = h * PRIME_1 ^ (*k - ' ');
189 break;
190 case HASH_KEY_CONST_STRING:
191 /* Convert string to integer */
192 for (h = 0, k = (const unsigned char *) key->c_str; *k; k++)
193 h = h * PRIME_1 ^ (*k - ' ');
194 break;
195 default:
196 h = key->ul;
197 break;
198 }
199 return h;
200 }
201
202 static address_t hash(hash_table_t *table, hash_key_t *key)
203 {
204 address_t h, address;
205
206 h = convert_key(key);
207 h %= PRIME_2;
208 address = h & (table->maxp-1); /* h % maxp */
209 if (address < table->p)
210 address = h & ((table->maxp << 1)-1); /* h % (2*table->maxp) */
211
212 return address;
213 }
214
215 static bool is_valid_key_type(hash_key_enum key_type)
216 {
217 switch(key_type) {
218 case HASH_KEY_ULONG:
219 case HASH_KEY_STRING:
220 case HASH_KEY_CONST_STRING:
221 return true;
222 default:
223 return false;
224 }
225 }
226
227 static bool is_valid_value_type(hash_value_enum value_type)
228 {
229 switch(value_type) {
230 case HASH_VALUE_UNDEF:
231 case HASH_VALUE_PTR:
232 case HASH_VALUE_INT:
233 case HASH_VALUE_UINT:
234 case HASH_VALUE_LONG:
235 case HASH_VALUE_ULONG:
236 case HASH_VALUE_FLOAT:
237 case HASH_VALUE_DOUBLE:
238 return true;
239 default:
240 return false;
241 }
242 }
243
244 static bool key_equal(hash_key_t *a, hash_key_t *b)
245 {
246 if (a->type != b->type) return false;
247
248 switch(a->type) {
249 case HASH_KEY_ULONG:
250 return (a->ul == b->ul);
251 case HASH_KEY_STRING:
252 return (strcmp(a->str, b->str) == 0);
253 case HASH_KEY_CONST_STRING:
254 return (strcmp(a->c_str, b->c_str) == 0);
255 }
256 return false;
257 }
258
259
260 static int expand_table(hash_table_t *table)
261 {
262 address_t new_address;
263 unsigned long old_segment_index, new_segment_index;
264 unsigned long old_segment_dir, new_segment_dir;
265 segment_t *old_segment, *new_segment;
266 element_t *current, **previous, **last_of_new;
267
268 if (table->bucket_count < (table->directory_size << table->segment_size_shift)) {
269 #ifdef DEBUG
270 if (debug_level >= 2)
271 fprintf(stderr, "expand_table on entry: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n",
272 table->bucket_count, table->segment_count, table->p, table->maxp);
273 #endif
274 #ifdef HASH_STATISTICS
275 table->statistics.table_expansions++;
276 #endif
277
278 /*
279 * Locate the bucket to be split
280 */
281 old_segment_dir = table->p >> table->segment_size_shift;
282 old_segment = table->directory[old_segment_dir];
283 old_segment_index = table->p & (table->segment_size-1); /* p % segment_size */
284 /*
285 * Expand address space; if necessary create a new segment
286 */
287 new_address = table->maxp + table->p;
288 new_segment_dir = new_address >> table->segment_size_shift;
289 new_segment_index = new_address & (table->segment_size-1); /* new_address % segment_size */
290 if (new_segment_index == 0) {
291 table->directory[new_segment_dir] = (segment_t *)halloc(table, table->segment_size * sizeof(segment_t));
292 if (table->directory[new_segment_dir] == NULL) {
293 return HASH_ERROR_NO_MEMORY;
294 }
295 memset(table->directory[new_segment_dir], 0, table->segment_size * sizeof(segment_t));
296 table->segment_count++;
297 }
298 new_segment = table->directory[new_segment_dir];
299 /*
300 * Adjust state variables
301 */
302 table->p++;
303 if (table->p == table->maxp) {
304 table->maxp <<= 1; /* table->maxp *= 2 */
305 table->p = 0;
306 }
307 table->bucket_count++;
308 /*
309 * Relocate records to the new bucket
310 */
311 previous = &old_segment[old_segment_index];
312 current = *previous;
313 last_of_new = &new_segment[new_segment_index];
314 *last_of_new = NULL;
315 while (current != NULL) {
316 if (hash(table, &current->entry.key) == new_address) {
317 /*
318 * Attach it to the end of the new chain
319 */
320 *last_of_new = current;
321 /*
322 * Remove it from old chain
323 */
324 *previous = current->next;
325 last_of_new = &current->next;
326 current = current->next;
327 *last_of_new = NULL;
328 } else {
329 /*
330 * leave it on the old chain
331 */
332 previous = &current->next;
333 current = current->next;
334 }
335 }
336 #ifdef DEBUG
337 if (debug_level >= 2)
338 fprintf(stderr, "expand_table on exit: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n",
339 table->bucket_count, table->segment_count, table->p, table->maxp);
340 #endif
341 }
342 return HASH_SUCCESS;
343 }
344
345 static int contract_table(hash_table_t *table)
346 {
347 address_t new_address, old_address;
348 unsigned long old_segment_index, new_segment_index;
349 unsigned long old_segment_dir, new_segment_dir;
350 segment_t *old_segment, *new_segment;
351 element_t *current;
352
353 if ((table->bucket_count > table->segment_size) && (table->segment_count > 1)) {
354 #ifdef DEBUG
355 if (debug_level >= 2)
356 fprintf(stderr, "contract_table on entry: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n",
357 table->bucket_count, table->segment_count, table->p, table->maxp);
358 #endif
359
360 #ifdef HASH_STATISTICS
361 table->statistics.table_contractions++;
362 #endif
363 /*
364 * Locate the bucket to be merged with the last bucket
365 */
366 old_address = table->bucket_count - 1;
367 old_segment_dir = old_address >> table->segment_size_shift;
368 old_segment = table->directory[old_segment_dir];
369 old_segment_index = old_address & (table->segment_size-1); /* old_address % segment_size */
370
371 /*
372 * Adjust state variables
373 */
374 if (table->p > 0) {
375 table->p--;
376 } else {
377 table->maxp >>= 1;
378 table->p = table->maxp - 1;
379 }
380 table->bucket_count--;
381
382 /*
383 * Find the last bucket to merge back
384 */
385 if((current = old_segment[old_segment_index]) != NULL) {
386 new_address = hash(table, &old_segment[old_segment_index]->entry.key);
387 new_segment_dir = new_address >> table->segment_size_shift;
388 new_segment_index = new_address & (table->segment_size-1); /* new_address % segment_size */
389 new_segment = table->directory[new_segment_dir];
390
391 /*
392 * Relocate records to the new bucket by splicing the two chains
393 * together by inserting the old chain at the head of the new chain.
394 * First find the end of the old chain, then set its next pointer to
395 * point to the head of the new chain, set the head of the new chain to
396 * point to the old chain, then finally set the head of the old chain to
397 * NULL.
398 */
399
400 while (current->next != NULL) {
401 current = current->next;
402 }
403
404 current->next = new_segment[new_segment_index];
405 new_segment[new_segment_index] = old_segment[old_segment_index];
406 old_segment[old_segment_index] = NULL;
407 }
408 /*
409 * If we have removed the last of the chains in this segment then free the
410 * segment since its no longer in use.
411 */
412 if (old_segment_index == 0) {
413 table->segment_count--;
414 hfree(table, table->directory[old_segment_dir]);
415 }
416
417 #ifdef DEBUG
418 if (debug_level >= 2)
419 fprintf(stderr, "contract_table on exit: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n",
420 table->bucket_count, table->segment_count, table->p, table->maxp);
421 #endif
422
423 }
424 return HASH_SUCCESS;
425 }
426
427 static int lookup(hash_table_t *table, hash_key_t *key, element_t **element_arg, segment_t **chain_arg)
428 {
429 address_t h;
430 segment_t *current_segment;
431 unsigned long segment_index, segment_dir;
432 segment_t *chain, element;
433
434 *element_arg = NULL;
435 *chain_arg = NULL;
436
437 if (!table) return HASH_ERROR_BAD_TABLE;
438
439 #ifdef HASH_STATISTICS
440 table->statistics.hash_accesses++;
441 #endif
442 h = hash(table, key);
443 segment_dir = h >> table->segment_size_shift;
444 segment_index = h & (table->segment_size-1); /* h % segment_size */
445 /*
446 * valid segment ensured by hash()
447 */
448 current_segment = table->directory[segment_dir];
449
450 #ifdef DEBUG
451 if (debug_level >= 3)
452 fprintf(stderr, "lookup: h=%lu, segment_dir=%lu, segment_index=%lu current_segment=%p\n",
453 h, segment_dir, segment_index, current_segment);
454 #endif
455
456 if (current_segment == NULL) return EFAULT;
457 chain = &current_segment[segment_index];
458 element = *chain;
459 /*
460 * Follow collision chain
461 */
462 while (element != NULL && !key_equal(&element->entry.key, key)) {
463 chain = &element->next;
464 element = *chain;
465 #ifdef HASH_STATISTICS
466 table->statistics.hash_collisions++;
467 #endif
468 }
469 *element_arg = element;
470 *chain_arg = chain;
471
472 return HASH_SUCCESS;
473 }
474
475 static bool hash_keys_callback(hash_entry_t *item, void *user_data)
476 {
477 hash_keys_callback_data_t *data = (hash_keys_callback_data_t *)user_data;
478
479 data->keys[data->index++] = item->key;
480 return true;
481 }
482
483 static bool hash_values_callback(hash_entry_t *item, void *user_data)
484 {
485 hash_values_callback_data_t *data = (hash_values_callback_data_t *)user_data;
486
487 data->values[data->index++] = item->value;
488 return true;
489 }
490
491 /*****************************************************************************/
492 /**************************** Exported Functions ***************************/
493 /*****************************************************************************/
494
495 const char* hash_error_string(int error)
496 {
497 switch(error) {
498 case HASH_SUCCESS: return "Success";
499 case HASH_ERROR_BAD_KEY_TYPE: return "Bad key type";
500 case HASH_ERROR_BAD_VALUE_TYPE: return "Bad value type";
501 case HASH_ERROR_NO_MEMORY: return "No memory";
502 case HASH_ERROR_KEY_NOT_FOUND: return "Key not found";
503 case HASH_ERROR_BAD_TABLE: return "Bad table";
504 }
505 return NULL;
506 }
507
508
509 int hash_create(unsigned long count, hash_table_t **tbl,
510 hash_delete_callback *delete_callback,
511 void *delete_private_data)
512 {
513 return hash_create_ex(count, tbl, 0, 0, 0, 0,
514 NULL, NULL, NULL,
515 delete_callback,
516 delete_private_data);
517 }
518
519 int hash_create_ex(unsigned long count, hash_table_t **tbl,
520 unsigned int directory_bits,
521 unsigned int segment_bits,
522 unsigned long min_load_factor,
523 unsigned long max_load_factor,
524 hash_alloc_func *alloc_func,
525 hash_free_func *free_func,
526 void *alloc_private_data,
527 hash_delete_callback *delete_callback,
528 void *delete_private_data) {
529 unsigned long i;
530 unsigned int n_addr_bits, requested_bits;
531 unsigned int requested_directory_bits;
532 unsigned int requested_segment_bits;
533 address_t addr;
534 hash_table_t *table = NULL;
535
536 /* Initialize to NULL in case of an early return due to an error */
537 *tbl = NULL;
538
539 if (alloc_func == NULL) alloc_func = sys_malloc_wrapper;
540 if (free_func == NULL) free_func = sys_free_wrapper;
541
542 /* Compute directory and segment parameters */
543
544 /* compute power of 2 >= count; it's the number of requested buckets */
545 if (count > 1) {
546 for (requested_bits = 0; (1 << requested_bits) < count; requested_bits++);
547 } else {
548 requested_bits = 1;
549 }
550
551 /*
552 * If caller didn't specify directory & segment allocation then
553 * compute it from the requested count.
554 */
555 if (directory_bits == 0 || segment_bits == 0) {
556 /* Equally divide buckets between the directory and segments */
557 requested_directory_bits = MAX(requested_bits >> 1, 1);
558 requested_segment_bits = MAX(requested_bits - requested_directory_bits, 1);
559
560 /* If the requested count wasn't specified pick a default */
561 if (count == 0) {
562 directory_bits = MAX(requested_directory_bits, HASH_DEFAULT_DIRECTORY_BITS);
563 segment_bits = MAX(requested_segment_bits, HASH_DEFAULT_SEGMENT_BITS);
564 } else {
565 directory_bits = requested_directory_bits;
566 segment_bits = requested_segment_bits;
567 }
568 }
569
570 for (addr = ~0, n_addr_bits = 0; addr; addr >>= 1, n_addr_bits++);
571
572 if (directory_bits + segment_bits > n_addr_bits) return EINVAL;
573
574 table = (hash_table_t *)alloc_func(sizeof(hash_table_t),
575 alloc_private_data);
576 if (table == NULL) {
577 return HASH_ERROR_NO_MEMORY;
578 }
579 memset(table, 0, sizeof(hash_table_t));
580 table->halloc = alloc_func;
581 table->hfree = free_func;
582 table->halloc_pvt = alloc_private_data;
583
584 table->directory_size_shift = directory_bits;
585 table->directory_size = directory_bits ? 1 << directory_bits : 0;
586
587 table->segment_size_shift = segment_bits;
588 table->segment_size = segment_bits ? 1 << segment_bits : 0;
589
590 /* Allocate directory */
591 table->directory = (segment_t **)halloc(table, table->directory_size * sizeof(segment_t *));
592 if (table->directory == NULL) {
593 hash_destroy(table);
594 return HASH_ERROR_NO_MEMORY;
595 }
596 memset(table->directory, 0, table->directory_size * sizeof(segment_t *));
597
598 /*
599 * If one wanted to pre-allocate all the buckets necessary to meet the needs
600 * of the requested count it would be done like this:
601 *
602 * table->segment_count = MIN((count + table->segment_size-1) / table->segment_size,
603 * table->directory_size);
604 *
605 * But with dynamic hash tables there is little point to this, the whole idea is to
606 * allow the table to grow/shrink dynamically, therefore we start with just one
607 * segment of buckets, the minimum necessary.
608 */
609 table->segment_count = 1;
610 table->p = 0;
611 table->entry_count = 0;
612 table->delete_callback = delete_callback;
613 table->delete_pvt = delete_private_data;
614
615 /*
616 * Allocate initial segments of buckets
617 */
618 for (i = 0; i < table->segment_count; i++) {
619 table->directory[i] = (segment_t *)halloc(table, table->segment_size * sizeof(segment_t));
620 if (table->directory[i] == NULL) {
621 hash_destroy(table);
622 return HASH_ERROR_NO_MEMORY;
623 }
624 memset(table->directory[i], 0, table->segment_size * sizeof(segment_t));
625 }
626 table->bucket_count = table->segment_count << table->segment_size_shift;
627 table->maxp = table->bucket_count;
628 table->min_load_factor = min_load_factor == 0 ? HASH_DEFAULT_MIN_LOAD_FACTOR : min_load_factor;
629 table->max_load_factor = max_load_factor == 0 ? HASH_DEFAULT_MAX_LOAD_FACTOR : max_load_factor;
630
631 #ifdef DEBUG
632 if (debug_level >= 1) {
633 fprintf(stderr, "hash_create_ex: count=%lu available buckets=%lu bucket_count=%lu maxp=%lu\n",
634 count, table->directory_size*table->segment_size, table->bucket_count, table->maxp);
635 fprintf(stderr, " directory_bits=%u segment_bits=%u requested_bits=%u\n",
636 directory_bits, segment_bits, requested_bits);
637 fprintf(stderr, " directory_size=%lu segment_size=%lu segment_count=%lu\n",
638 table->directory_size, table->segment_size, table->segment_count);
639 fprintf(stderr, " min_load_factor=%lu max_load_factor=%lu\n",
640 table->min_load_factor, table->max_load_factor);
641 }
642 #endif
643 #ifdef HASH_STATISTICS
644 memset(&table->statistics, 0, sizeof(table->statistics));
645 #endif
646
647 *tbl = table;
648 return HASH_SUCCESS;
649 }
650
651 #ifdef HASH_STATISTICS
652 int hash_get_statistics(hash_table_t *table, hash_statistics_t *statistics)
653 {
654 if (!table) return HASH_ERROR_BAD_TABLE;
655 if (!statistics) return EINVAL;
656
657 *statistics = table->statistics;
658
659 return HASH_SUCCESS;
660 }
661 #endif
662
663 int hash_destroy(hash_table_t *table)
664 {
665 unsigned long i, j;
666 segment_t *s;
667 element_t *p, *q;
668
669 if (!table) return HASH_ERROR_BAD_TABLE;
670
671 if (table->directory) {
672 for (i = 0; i < table->segment_count; i++) {
673 /* test probably unnecessary */
674 if ((s = table->directory[i]) != NULL) {
675 for (j = 0; j < table->segment_size; j++) {
676 p = s[j];
677 while (p != NULL) {
678 q = p->next;
679 hdelete_callback(table, HASH_TABLE_DESTROY, &p->entry);
680 if (p->entry.key.type == HASH_KEY_STRING
681 || p->entry.key.type == HASH_KEY_CONST_STRING) {
682 /* Internally we do not use constant memory for keys
683 * in hash table elements. */
684 hfree(table, p->entry.key.str);
685 }
686 hfree(table, (char *)p);
687 p = q;
688 }
689 }
690 hfree(table, s);
691 }
692 }
693 hfree(table, table->directory);
694 }
695 hfree(table, table);
696 table = NULL;
697
698 return HASH_SUCCESS;
699 }
700
701 int hash_iterate(hash_table_t *table, hash_iterate_callback callback, void *user_data)
702 {
703 unsigned long i, j;
704 segment_t *s;
705 element_t *p;
706
707 if (!table) return HASH_ERROR_BAD_TABLE;
708
709 if (table != NULL) {
710 for (i = 0; i < table->segment_count; i++) {
711 /* test probably unnecessary */
712 if ((s = table->directory[i]) != NULL) {
713 for (j = 0; j < table->segment_size; j++) {
714 p = s[j];
715 while (p != NULL) {
716 if(!(*callback)(&p->entry, user_data)) return HASH_SUCCESS;
717 p = p->next;
718 }
719 }
720 }
721 }
722 }
723 return HASH_SUCCESS;
724 }
725
726 static hash_entry_t *hash_iter_next(struct hash_iter_context_t *iter_arg)
727 {
728 struct _hash_iter_context_t *iter = (struct _hash_iter_context_t *) iter_arg;
729 hash_entry_t *entry = NULL;
730 enum hash_iter_state state = HI_STATE_3A;
731
732 if (iter->table == NULL) return NULL;
733
734 while (state != HI_STATE_0) {
735
736 switch (state) {
737 case HI_STATE_1:
738 iter->i++;
739 if(iter->i >= iter->table->segment_count) return NULL;
740 /* test probably unnecessary */
741 iter->s = iter->table->directory[iter->i];
742 if (iter->s == NULL) {
743 state = HI_STATE_1;
744 break;
745 }
746 iter->j = 0;
747 state = HI_STATE_2;
748 /* FALLTHROUGH */
749
750 case HI_STATE_2:
751 if (iter->j >= iter->table->segment_size) {
752 state = HI_STATE_1;
753 break;
754 }
755 iter->p = iter->s[iter->j];
756 state = HI_STATE_3A;
757 /* FALLTHROUGH */
758
759 case HI_STATE_3A:
760 if (iter->p == NULL) {
761 state = HI_STATE_3B;
762 break;
763 }
764 entry = &iter->p->entry;
765 iter->p = iter->p->next;
766 state = HI_STATE_0;
767 break;
768
769 case HI_STATE_3B:
770 iter->j++;
771 state = HI_STATE_2;
772 break;
773
774 default:
775 /* Should never reach here */
776 fprintf(stderr, "ERROR hash_iter_next reached invalid state\n");
777 return NULL;
778 break;
779 }
780 }
781
782 return entry;
783 }
784
785 struct hash_iter_context_t *new_hash_iter_context(hash_table_t *table)
786 {
787 struct _hash_iter_context_t *iter;
788
789 if (!table) return NULL;;
790
791 iter = halloc(table, sizeof(struct _hash_iter_context_t));
792 if (iter == NULL) {
793 return NULL;
794 }
795
796
797 iter->iter.next = (hash_iter_next_t) hash_iter_next;
798
799 iter->table = table;
800 iter->i = 0;
801 iter->j = 0;
802 iter->s = table->directory[iter->i];
803 iter->p = iter->s[iter->j];
804
805 return (struct hash_iter_context_t *)iter;
806 }
807
808 unsigned long hash_count(hash_table_t *table)
809 {
810 return table->entry_count;
811 }
812
813
814 int hash_keys(hash_table_t *table, unsigned long *count_arg, hash_key_t **keys_arg)
815 {
816 unsigned long count;
817 hash_key_t *keys;
818 hash_keys_callback_data_t data;
819
820 if (!table) return HASH_ERROR_BAD_TABLE;
821
822 count = table->entry_count;
823 if (count == 0) {
824 *count_arg = 0;
825 *keys_arg = NULL;
826 return HASH_SUCCESS;
827 }
828
829 keys = halloc(table, sizeof(hash_key_t) * count);
830 if (keys == NULL) {
831 *count_arg = -1;
832 *keys_arg = NULL;
833 return HASH_ERROR_NO_MEMORY;
834 }
835
836 data.index = 0;
837 data.keys = keys;
838
839 hash_iterate(table, hash_keys_callback, &data);
840
841 *count_arg = count;
842 *keys_arg = keys;
843 return HASH_SUCCESS;
844 }
845
846 int hash_values(hash_table_t *table, unsigned long *count_arg, hash_value_t **values_arg)
847 {
848 unsigned long count;
849 hash_value_t *values;
850 hash_values_callback_data_t data;
851
852 if (!table) return HASH_ERROR_BAD_TABLE;
853
854 count = table->entry_count;
855 if (count == 0) {
856 *count_arg = 0;
857 *values_arg = NULL;
858 return HASH_SUCCESS;
859 }
860
861 values = halloc(table, sizeof(hash_value_t) * count);
862 if (values == NULL) {
863 *count_arg = -1;
864 *values_arg = NULL;
865 return HASH_ERROR_NO_MEMORY;
866 }
867
868 data.index = 0;
869 data.values = values;
870
871 hash_iterate(table, hash_values_callback, &data);
872
873 *count_arg = count;
874 *values_arg = values;
875 return HASH_SUCCESS;
876 }
877
878 typedef struct hash_entries_callback_data_t {
879 unsigned long index;
880 hash_entry_t *entries;
881 } hash_entries_callback_data_t;
882
883 static bool hash_entries_callback(hash_entry_t *item, void *user_data)
884 {
885 hash_entries_callback_data_t *data = (hash_entries_callback_data_t *)user_data;
886
887 data->entries[data->index++] = *item;
888 return true;
889 }
890
891 int hash_entries(hash_table_t *table, unsigned long *count_arg, hash_entry_t **entries_arg)
892 {
893 unsigned long count;
894 hash_entry_t *entries;
895 hash_entries_callback_data_t data;
896
897 if (!table) return HASH_ERROR_BAD_TABLE;
898
899 count = table->entry_count;
900 if (count == 0) {
901 *count_arg = 0;
902 *entries_arg = NULL;
903 return HASH_SUCCESS;
904 }
905
906 entries = halloc(table, sizeof(hash_entry_t) * count);
907 if (entries == NULL) {
908 *count_arg = -1;
909 *entries_arg = NULL;
910 return HASH_ERROR_NO_MEMORY;
911 }
912
913 data.index = 0;
914 data.entries = entries;
915
916 hash_iterate(table, hash_entries_callback, &data);
917
918 *count_arg = count;
919 *entries_arg = entries;
920 return HASH_SUCCESS;
921 }
922
923 bool hash_has_key(hash_table_t *table, hash_key_t *key)
924 {
925 hash_value_t value;
926
927 if (hash_lookup(table, key, &value) == HASH_SUCCESS)
928 return true;
929 else
930 return false;
931 }
932
933
934 int hash_get_default(hash_table_t *table, hash_key_t *key, hash_value_t *value, hash_value_t *default_value)
935 {
936 int error;
937
938 if (!table) return HASH_ERROR_BAD_TABLE;
939
940 error = hash_lookup(table, key, value);
941 if (error == HASH_ERROR_KEY_NOT_FOUND) {
942
943 error = hash_enter(table, key, default_value);
944 if (error != HASH_SUCCESS) {
945 return error;
946 }
947 *value = *default_value;
948 return HASH_SUCCESS;
949 } else {
950 if (error != HASH_SUCCESS) return error;
951 }
952
953 return HASH_SUCCESS;
954 }
955
956 int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value)
957 {
958 int error;
959 segment_t element, *chain;
960 size_t len;
961
962 if (!table) return HASH_ERROR_BAD_TABLE;
963
964 if (!is_valid_key_type(key->type))
965 return HASH_ERROR_BAD_KEY_TYPE;
966
967 if (!is_valid_value_type(value->type))
968 return HASH_ERROR_BAD_VALUE_TYPE;
969
970 lookup(table, key, &element, &chain);
971
972 if (element == NULL) { /* not found */
973 element = (element_t *)halloc(table, sizeof(element_t));
974 if (element == NULL) {
975 /* Allocation failed, return NULL */
976 return HASH_ERROR_NO_MEMORY;
977 }
978 memset(element, 0, sizeof(element_t));
979 /*
980 * Initialize new element
981 */
982 switch(element->entry.key.type = key->type) {
983 case HASH_KEY_ULONG:
984 element->entry.key.ul = key->ul;
985 break;
986 case HASH_KEY_STRING:
987 case HASH_KEY_CONST_STRING:
988 len = strlen(key->c_str) + 1;
989 element->entry.key.str = halloc(table, len);
990 if (element->entry.key.str == NULL) {
991 hfree(table, element);
992 return HASH_ERROR_NO_MEMORY;
993 }
994 memcpy(element->entry.key.str, key->str, len);
995 break;
996 }
997
998 *chain = element; /* link into chain */
999 element->next = NULL;
1000
1001 /*
1002 * Table over-full?
1003 */
1004 if (++table->entry_count / table->bucket_count > table->max_load_factor) {
1005 if ((error = expand_table(table)) != HASH_SUCCESS) { /* doesn't affect element */
1006 return error;
1007 }
1008 }
1009
1010 } else {
1011 hdelete_callback(table, HASH_ENTRY_DESTROY, &element->entry);
1012 }
1013
1014 switch(element->entry.value.type = value->type) {
1015 case HASH_VALUE_UNDEF:
1016 element->entry.value.ul = 0;
1017 break;
1018 case HASH_VALUE_PTR:
1019 element->entry.value.ptr = value->ptr;
1020 break;
1021 case HASH_VALUE_INT:
1022 element->entry.value.i = value->i;
1023 break;
1024 case HASH_VALUE_UINT:
1025 element->entry.value.ui = value->ui;
1026 break;
1027 case HASH_VALUE_LONG:
1028 element->entry.value.l = value->l;
1029 break;
1030 case HASH_VALUE_ULONG:
1031 element->entry.value.ul = value->ul;
1032 break;
1033 case HASH_VALUE_FLOAT:
1034 element->entry.value.f = value->f;
1035 break;
1036 case HASH_VALUE_DOUBLE:
1037 element->entry.value.d = value->d;
1038 break;
1039 }
1040
1041 return HASH_SUCCESS;
1042 }
1043
1044 int hash_lookup(hash_table_t *table, hash_key_t *key, hash_value_t *value)
1045 {
1046 segment_t element, *chain;
1047
1048 if (!table) return HASH_ERROR_BAD_TABLE;
1049
1050 if (!is_valid_key_type(key->type))
1051 return HASH_ERROR_BAD_KEY_TYPE;
1052
1053 lookup(table, key, &element, &chain);
1054
1055 if (element) {
1056 *value = element->entry.value;
1057 return HASH_SUCCESS;
1058 } else {
1059 return HASH_ERROR_KEY_NOT_FOUND;
1060 }
1061 }
1062
1063 int hash_delete(hash_table_t *table, hash_key_t *key)
1064 {
1065 int error;
1066 segment_t element, *chain;
1067
1068 if (!table) return HASH_ERROR_BAD_TABLE;
1069
1070 if (!is_valid_key_type(key->type))
1071 return HASH_ERROR_BAD_KEY_TYPE;
1072
1073 lookup(table, key, &element, &chain);
1074
1075 if (element) {
1076 hdelete_callback(table, HASH_ENTRY_DESTROY, &element->entry);
1077 *chain = element->next; /* remove from chain */
1078 /*
1079 * Table too sparse?
1080 */
1081 if (--table->entry_count / table->bucket_count < table->min_load_factor) {
1082 if ((error = contract_table(table)) != HASH_SUCCESS) { /* doesn't affect element */
1083 return error;
1084 }
1085 }
1086 if (element->entry.key.type == HASH_KEY_STRING
1087 || element->entry.key.type == HASH_KEY_CONST_STRING) {
1088 hfree(table, element->entry.key.str);
1089 }
1090 hfree(table, element);
1091 return HASH_SUCCESS;
1092 } else {
1093 return HASH_ERROR_KEY_NOT_FOUND;
1094 }
1095 }
1096
1097