- hash->name = name;
- hash->entries = NULL;
- hash->n_entries = 0;
- hash->hash_entry_key = NULL;
-
- picker = primes_table;
- while(*picker && (*picker++ < len));
- if(!*picker)
- fprintf(stderr, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__, len);
- --picker;
-
- hash->n_entries = *picker;
- hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t));
- if (hash->entries == NULL) {
- fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
- return ENOMEM;
- }
- return 0;
+ memset(hash, 0, sizeof(hash_table_t));
+
+ hash->name = name;
+ hash->n_buckets = len;
+ hash->entries = xcalloc(hash->n_buckets, sizeof(hash_entry_t));
+}
+
+void
+hash_print_stats(hash_table_t *hash)
+{
+ printf("hash_table: %s, %d bytes\n"
+ "\tn_buckets=%d, n_elements=%d, n_collisions=%d\n"
+ "\tmax_bucket_len=%d, n_used_buckets=%d, ave_bucket_len=%.2f\n"
+ "\tn_hits=%d, n_misses=%d\n",
+ hash->name,
+ hash->n_buckets*(int)sizeof(hash_entry_t),
+ hash->n_buckets,
+ hash->n_elements,
+ hash->n_collisions,
+ hash->max_bucket_len,
+ hash->n_used_buckets,
+ (hash->n_used_buckets ?
+ ((float)hash->n_elements)/hash->n_used_buckets : 0.0f),
+ hash->n_hits,
+ hash->n_misses);