- 论坛徽章:
- 0
|
编制 hash 测试程序,怎么得到系统保存的 value 值不对
/*
** 这是一个针对 dfshash 函数进行测试的程序。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dfshash.h"
int hash_print(HashArray *pHash)
{
HashData **ppBucket;
HashData **bucket_end;
HashData *hash_data;
int count = 0;
bucket_end = pHash->buckets + (*pHash->capacity);
printf("hash capacity = [%d] \n", (*pHash->capacity) );
for (ppBucket=pHash->buckets; ppBucket<bucket_end; ppBucket++)
{
count ++;
if (*ppBucket == NULL)
{
continue;
}
hash_data = *ppBucket;
printf("hash_code[%03d] %12d %s XXX", count-1, hash_data->hash_code, hash_data->key);
while (hash_data != NULL)
{
printf("%s ", hash_data->value);
hash_data = hash_data->next;
}
printf("\n");
}
return 0;
}
int main(int ac , char *av[])
{
HashArray pHash;
HashData *Ph;
int i1;
char key[32];
char vale[32];
hash_init(&pHash, DFH_Time33Hash, 10, 0.8);
for(i1=5; i1< 15; i1 ++ ){
sprintf(key, "key %02d", i1);
sprintf(vale, " 保存键值 %02d ", i1);
hash_insert_ex(&pHash, key, 20, vale, 20);
}
// 将需要处理的数据保存到 hash 桶中
hash_print(&pHash);
hash_stat_print(&pHash);
// 打印基本 hash 桶的分布情况
hash_best_op(&pHash, 10);
// 进行 hash 桶扩展,该扩展后将可以保证 hash 数据的充分分布
hash_print(&pHash);
hash_stat_print(&pHash);
// 打印新的 hash 桶的分布情况
sprintf(key, "key %02d", 20);
Ph = hash_find(&pHash, key, 20);
if(Ph) printf("测试 value [%s]\n", Ph->value);
hash_destroy(&pHash);
return 0;
} |
程序的测试结果为
hash capacity = [17]
hash_code[000] -128244380 key 06 XXX 保存键值 14
hash_code[004] -1107202109 key 14 XXX 保存键值 14 保存键值 14 保存键值 14
hash_code[008] -500076889 key 09 XXX 保存键值 14 保存键值 14 保存键值 14
hash_code[012] 448397826 key 13 XXX 保存键值 14
hash_code[013] -735369600 key 11 XXX 保存键值 14
hash_code[016] 1055523046 key 08 XXX 保存键值 14
capacity: 17, item_count=10, bucket_used: 6, avg length: 1.6667, max length: 3, bucket / item = 35.29%
hash capacity = [23]
hash_code[002] -500076889 key 09 XXX 保存键值 14
hash_code[003] -1683844315 key 07 XXX 保存键值 14
hash_code[004] 1427355555 key 05 XXX 保存键值 14
hash_code[005] -735369600 key 11 XXX 保存键值 14
hash_code[008] 1055523046 key 08 XXX 保存键值 14
hash_code[009] -1107202109 key 14 XXX 保存键值 14
hash_code[010] 2003997761 key 12 XXX 保存键值 14
hash_code[011] 820230335 key 10 XXX 保存键值 14
hash_code[015] 448397826 key 13 XXX 保存键值 14
hash_code[021] -128244380 key 06 XXX 保存键值 14
capacity: 23, item_count=10, bucket_used: 10, avg length: 1.0000, max length: 1, bucket / item = 43.48%
怎么 hash 中保存的 value 数值是一致的,我感觉后面保存的参数应该与 key 中的参数一致,为什么不成功? |
|