Skip to content

Commit

Permalink
add hash() member function to global_coords
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Jun 8, 2024
1 parent 53d61d8 commit 461481e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/global-coords.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "global-coords.hpp"
#include "point.inl"
#include "compat/hash.hpp"
#include <array>
#include <algorithm>

Expand Down Expand Up @@ -32,6 +33,12 @@ Debug& operator<<(Debug& dbg, const chunk_coords_& c)
return dbg;
}

size_t global_coords::hash() const noexcept
{
static_assert(sizeof *this == 4 + 4);
return hash_buf(this, sizeof *this);
}

namespace {

static_assert(sizeof(decltype(local_coords::x))*8 == 8);
Expand Down
2 changes: 2 additions & 0 deletions src/global-coords.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ struct global_coords final
constexpr global_coords& operator+=(Vector2i vec) noexcept;
constexpr global_coords& operator-=(Vector2i vec) noexcept;
constexpr Vector2i operator-(global_coords other) const noexcept;

size_t hash() const noexcept;
};

constexpr local_coords global_coords::local() const noexcept
Expand Down
2 changes: 1 addition & 1 deletion test/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ template<int16_t CoordMax, int8_t ZMax, uint32_t MaxCollisions>
for (int8_t k = 0; k <= ZMax; k++)
{
auto p = global_coords{i, j, k};
auto h = hash_buf(&p, sizeof p);
auto h = p.hash();
h %= BucketCount;
auto ref = bitset[h];
if (ref) [[unlikely]]
Expand Down

0 comments on commit 461481e

Please sign in to comment.