Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ typedef std::unordered_map<ID, Ball *> DictOfBalls;
typedef std::vector<Ball *> VectorOfBalls;
typedef std::vector<StaticCollidable *> VectorOfStaticCollidables;
typedef std::pair<ID, Ball*> DictEntry;
typedef std::unordered_map<Key,Box *,std::hash<size_t>> MapOfBoxes;
typedef std::vector<Box *> VectorOfBoxes;
typedef std::back_insert_iterator< VectorOfBoxes > BoxVectorInsertor;

Expand Down Expand Up @@ -80,12 +79,6 @@ class Key
if (iz > k.iz) return false;
return n<k.n;
}
//hash key
operator size_t () const {
return std::hash<int64_t>()((ix<<3) + (iy<<2) + (iz<<1) + n);
}


int64_t ix;
int64_t iy;
int64_t iz;
Expand All @@ -108,6 +101,27 @@ class Key
}
};

struct KeyHash
{
size_t operator()(const Key& key) const noexcept
{
size_t seed = 0;
const auto combine = [&seed](int64_t value)
{
const size_t valueHash = std::hash<int64_t>{}(value);
seed ^= valueHash + static_cast<size_t>(0x9e3779b9U) + (seed << 6) + (seed >> 2);
};

combine(key.ix);
combine(key.iy);
combine(key.iz);
combine(key.n);
return seed;
}
};

typedef std::unordered_map<Key, Box*, KeyHash> MapOfBoxes;


class Partition
{
Expand Down Expand Up @@ -204,4 +218,4 @@ class Partition
BoxAllocator mBoxAllocator;
};

#endif
#endif
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target_sources(DestinyTest PRIVATE
TestAABB.cpp
TestBoxShape.cpp
TestCollision.cpp
TestPartition.cpp
TestPlane.cpp
TestTriangle.cpp
TestVector3d.cpp
Expand Down
44 changes: 44 additions & 0 deletions tests/TestPartition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2026 CCP ehf.

#include "StdAfx.h"

TEST(PartitionKeyHash, SeparatesCoordinatesThatPreviouslyCollided)
{
const Key first(1, 0, 0, 16);
const Key second(0, 2, 0, 16);

// The previous linear hash produced 24 for both keys:
// (1 << 3) + 16 == (2 << 2) + 16.
EXPECT_NE(KeyHash{}(first), KeyHash{}(second));
}

TEST(PartitionKeyHash, RetainsEveryDistinctBoxKey)
{
MapOfBoxes boxes;
constexpr int gridSize = 16;

for (int64_t ix = 0; ix < gridSize; ++ix)
{
for (int64_t iy = 0; iy < gridSize; ++iy)
{
for (int64_t iz = 0; iz < gridSize; ++iz)
{
boxes.emplace(Key(ix, iy, iz, gridSize), nullptr);
}
}
}

EXPECT_EQ(boxes.size(), static_cast<size_t>(gridSize * gridSize * gridSize));

for (int64_t ix = 0; ix < gridSize; ++ix)
{
for (int64_t iy = 0; iy < gridSize; ++iy)
{
for (int64_t iz = 0; iz < gridSize; ++iz)
{
const auto found = boxes.find(Key(ix, iy, iz, gridSize));
ASSERT_NE(found, boxes.end());
}
}
}
}