Exemple #1
0
void hash_stat_print(HashArray *pHash)
{
#define STAT_MAX_NUM  64
	HashStat hs;
	int stats[STAT_MAX_NUM];

	if (hash_stat(pHash, &hs, stats, STAT_MAX_NUM) != 0)
	{
		printf("hash max length exceeds %d!\n", STAT_MAX_NUM);
		return;
	}

	/*
	printf("collision stat:\n");
	for (i=0; i<last; i++)
	{
		if (stats[i] > 0) printf("%d: %d\n", i+1, stats[i]);
	}
	if (stats[i] > 0) printf(">=%d: %d\n", i+1, stats[i]);
	*/

	printf("capacity: %d, item_count=%d, bucket_used: %d, " \
		"avg length: %.4f, max length: %d, bucket / item = %.2f%%\n", 
		hs.capacity, hs.item_count, hs.bucket_used,
		hs.bucket_avg_length, hs.bucket_max_length, 
		(double)hs.bucket_used*100.00/(double)hs.capacity);
}
Exemple #2
0
int
verify(hash_t *hash, elt *elts, int nelts, char *msg) {
    int i, err;
    hash_node_t *node=0, tmp;
    elt *p;

    memset(&tmp, 0, sizeof(tmp));

    printf("%s: ", msg);

    err = 0;
    for(i=0, p=elts; i<nelts; i++, p++) {
	p->got_foreach = 0;
	node = hash_get(hash, p);
	if( !p->in_hash && node ) {
	    printf("error: p found in hash"
		   " at %d: p=%p, in_hash=%d, key=%p val=%p",
		   i, p, p->in_hash, node->node_key, node->node_val);
	    err = 1;
	}
	if( p->in_hash && (!node || node->node_val != p) ) {
	    node = &tmp;
	    err = 1;
	    printf("error: p not found in hash"
		   " at %d: p=%p, in_hash=%d, key=%p val=%p\n",
		   i, p, p->in_hash, node->node_key, node->node_val);
	}
    }
    
    hash_foreach(hash, verify_foreach, elts);
    for(i=0, p=elts; i<nelts; i++, p++) {
	if( p->in_hash && !p->got_foreach ) {
	    printf("error in foreach: elt missed"
		   " at i=%d, key=%p, val=%p, got_foreach=%d\n",
		   i, node->node_key, node->node_val, 
		   p->got_foreach);
	    err = 1;
	}
    }

    if( !err ) {
	hash_stat_t st;
	i = hash_stat(hash, &st);
	printf("ok\n  count=%d buckets=%d used=%d(%0.1f%%) max=%d avg=%.1f",
	       hash_count(hash), st.count, st.used, 
	       (float)100.0*st.used/st.count, 
	       st.max, st.avg);
    }
    printf("\n");

    return err;
}