【发布时间】:2021-09-24 18:10:12
【问题描述】:
这是我对 hashmap 的分析中的日志(截断):
unsigned nbuckets = octree->Nodes.bucket_count();
LOG(Error, "bucket size = {0}, kk = {1}", nbuckets, octree->Nodes.max_load_factor());
for (auto& x : octree->Nodes) {
LOG(Warning, "Element [{0}:{1}] is in bucket {2}", x.first.morton, x.second.position, octree->Nodes.bucket(x.first));
}
[ 00:19:26.169 ]: [Error] bucket size = 512, kk = 1.0
[ 00:19:26.169 ]: [Warning] Element [132120576:X:384 Y:384 Z:384] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [115343360:X:128 Y:384 Z:384] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [98566144:X:384 Y:128 Z:384] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [81788928:X:128 Y:128 Z:384] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [65011712:X:384 Y:384 Z:128] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [48234496:X:128 Y:384 Z:128] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [31457280:X:384 Y:128 Z:128] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [16515072:X:192 Y:192 Z:192] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [14417920:X:64 Y:192 Z:192] is in bucket 0
[ 00:19:26.169 ]: [Warning] Element [12320768:X:192 Y:64 Z:192] is in bucket
....
地图是这样定义的:
std::unordered_map<Int3Pos, OctreeNode, Int3Pos::HashFunction> Nodes;
其中 OctreeNode 是具有一些值的结构。和 Int3Pos:
struct Int3Pos // TODO rename
{
public:
Int3_64 position;
uint64 morton;
Int3Pos() : position(Int3_64(0, 0, 0)), morton(0) { }
Int3Pos(Int3_64 c)
{
this->position = c;
this->morton = mrtn(this->position);
}
bool operator==(const Int3Pos& otherPos) const
{
if (this->morton == otherPos.morton) return true;
else return false;
}
struct HashFunction
{
size_t operator()(const Int3Pos& pos) const
{
return pos.morton;
}
};
};
它告诉了 512 个桶,但都变成了 0。或者我误解了什么?
【问题讨论】:
-
可能你的散列函数总是为每个对象返回相同的值。您确定每个对象的
morton都不同吗?否则,您应该发布minimal reproducible example。 -
if(condition) return true; else return false;可以是return condition;。 -
@FrançoisAndrieux 是的,您可以在 LOG 中看到不同的整数。我确定。我添加了日志打印代码。
-
什么是
nbuckets?和Nodes有关系吗? -
这里没有足够的信息来帮助您解决问题。显示的行为很奇怪。您需要分享minimal reproducible example 以提供更多信息。