在c中计算哈希集中的簇
我正在用 c 语言制作一个哈希集 ADT 以用于家庭作业。我一生都无法弄清楚为什么我的逻辑不适用于计算哈希集中簇的函数。
void printClusterStats (hashset_ref hashset) {
int **clusters = (int**)calloc (hashset->length, sizeof(int));
assert (clusters);
int ct = 0;
// i traverses hashset->array
// ct adds up words in each cluster
// this loop screws up vvv
for ( int i = 0; i < hashset->length; ++i) {
if (hashset->array[i] == NULL) {
clusters[ct] += 1;
ct = 0;
}else {
ct += 1;
}
}
clusters[ct] +=1; //catch an ending cluster
printf("%10d words in the hash set\n", hashset->load);
printf("%10d length of the hash array\n", hashset->length);
for ( int i = 0; i < hashset->length; i++){
if (clusters[i] == 0) continue;
else{
printf("%10d clusters of size %3d\n", clusters[i], i);
}
}
free(clusters);
}
该函数的输出如下所示:
26 words in the hash set
63 length of the hash array
96 clusters of size 0
32 clusters of size 1
16 clusters of size 2
4 clusters of size 4
4 clusters of size 6
305 clusters of size 33
-703256008 clusters of size 34
-703256008 clusters of size 35
对于我的输入哈希集,63 长的数组中有 26 个单词。然而,计数不知何故搞砸了。
编辑:我手动计算了簇数,发现每个计数都是应有的 4 倍。这意味着什么?
I'm making a hashset ADT in c for a homework assignment. I cannot figure out for the life of me why my logic isn't working for a function that counts clusters in a hashset.
void printClusterStats (hashset_ref hashset) {
int **clusters = (int**)calloc (hashset->length, sizeof(int));
assert (clusters);
int ct = 0;
// i traverses hashset->array
// ct adds up words in each cluster
// this loop screws up vvv
for ( int i = 0; i < hashset->length; ++i) {
if (hashset->array[i] == NULL) {
clusters[ct] += 1;
ct = 0;
}else {
ct += 1;
}
}
clusters[ct] +=1; //catch an ending cluster
printf("%10d words in the hash set\n", hashset->load);
printf("%10d length of the hash array\n", hashset->length);
for ( int i = 0; i < hashset->length; i++){
if (clusters[i] == 0) continue;
else{
printf("%10d clusters of size %3d\n", clusters[i], i);
}
}
free(clusters);
}
The output of this function looks like:
26 words in the hash set
63 length of the hash array
96 clusters of size 0
32 clusters of size 1
16 clusters of size 2
4 clusters of size 4
4 clusters of size 6
305 clusters of size 33
-703256008 clusters of size 34
-703256008 clusters of size 35
For my input hashset, there are 26 words in an array 63 long. However the counting screws up somehow.
EDIT: I've counted the clusters manually and discovered every count is 4 times what it should be. What does that mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行创建一个指向 int 的指针数组,
而不是您在存储簇计数时实际需要的 int 数组。
因此,当您执行
clusters[ct] += 1;
时,它将被视为指针算术,并且每次将簇计数加 4,因为您所在的系统具有 4 字节指针。This line creates an array of pointers to int
rather than an array of int that you actually want if you are storing cluster counts
Consequently, when you do
clusters[ct] += 1;
it will be treated as pointer arithmetic, and add 4 to the cluster count each time, since you are on a system with 4-byte pointers.应该是
我在 c 方面还不太称职,所以我无法解释为什么这解决了我的问题。但如果你好奇的话就去吧!
这是正确的输出
Should be
I'm not very competent at c yet, so I can't explain why this solved my problem. But there you go if you were curious!
Here's the correct output