From c077f0f904cce505e15543b855b4330ca547b53d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 14 Sep 2023 09:02:23 +0200 Subject: replace hash with fnv1 --- src/world.cpp | 22 ++++++++-------------- src/world.hpp | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/world.cpp b/src/world.cpp index 46a65f64..fd6e4cbb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -6,21 +6,15 @@ using namespace floormat; -size_t std::hash::operator()(const chunk_coords_& coord) const noexcept -{ - std::size_t x = 0; - - x |= size_t(uint16_t(coord.y)) << 16; - x |= size_t(uint16_t(coord.x)); - if constexpr(sizeof(size_t) > 4) - x |= size_t(uint8_t(coord.z- chunk_z_min) & 0xf) << 32; - else - x ^= size_t(uint8_t(coord.z- chunk_z_min) & 0xf) * size_t(1664525); +size_t world::object_id_hasher::operator()(object_id id) const noexcept { return (size_t)int_hash(id); } - if constexpr(sizeof(size_t) > 4) - return int_hash(uint64_t(x)); - else - return int_hash(uint32_t(x)); +size_t world::chunk_coords_hasher::operator()(const chunk_coords_& coord) const noexcept +{ + uint64_t x = 0; + x |= uint64_t((uint16_t)coord.x) << 0; + x |= uint64_t((uint16_t)coord.y) << 16; + x |= uint64_t( (uint8_t)coord.z) << 32; + return (size_t)int_hash(x); } namespace floormat { diff --git a/src/world.hpp b/src/world.hpp index 288d69ca..bda8d3a4 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -3,24 +3,15 @@ #include "chunk.hpp" #include "global-coords.hpp" #include "object-type.hpp" -#include "compat/int-hash.hpp" #include #include #include #include -template<> -struct std::hash final { - floormat::size_t operator()(const floormat::chunk_coords_& coord) const noexcept; -}; - namespace floormat { struct object; template struct object_type_; -struct object_id_hasher { - size_t operator()(object_id id) const noexcept { return int_hash(id); } -}; struct world final { @@ -36,7 +27,16 @@ private: chunk_coords_ pos = invalid_coords; } _last_chunk; - std::unordered_map _chunks; + struct object_id_hasher + { + size_t operator()(object_id id) const noexcept; + }; + + struct chunk_coords_hasher { + size_t operator()(const chunk_coords_& coord) const noexcept; + }; + + std::unordered_map _chunks; tsl::robin_map, object_id_hasher> _objects; size_t _last_collection = 0; size_t _collect_every = initial_collect_every; -- cgit v1.2.3