diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-17 13:28:37 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-17 13:28:37 +0200 |
commit | 7f01dc47b0fd322d8c2b3f27eb1a94cd450a26a5 (patch) | |
tree | ac6201f9179a0315449ad46d742d2db325775eff /src/world.hpp | |
parent | e65ebe30d3f2af77d57cb15e5e2b30efa454b6a8 (diff) |
a
Diffstat (limited to 'src/world.hpp')
-rw-r--r-- | src/world.hpp | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/src/world.hpp b/src/world.hpp index 1e8469d9..7c256efc 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -1,24 +1,10 @@ #pragma once #include "compat/int-hash.hpp" #include "global-coords.hpp" +#include "tile.hpp" #include <unordered_map> #include <memory> - -namespace std { - -template<typename> struct hash; - -template<> -struct hash<floormat::chunk_coords> final -{ - constexpr - std::size_t operator()(floormat::chunk_coords c) const noexcept - { - return floormat::int_hash((std::size_t)c.y << 16 | (std::size_t)c.x); - } -}; - -} // namespace std +#include <optional> namespace floormat { @@ -28,20 +14,23 @@ struct world final { world(); std::shared_ptr<chunk> operator[](chunk_coords c) noexcept; - std::shared_ptr<chunk> maybe_chunk(chunk_coords c) noexcept; - std::shared_ptr<const chunk> maybe_chunk(chunk_coords c) const noexcept; + std::tuple<std::shared_ptr<chunk>, tile&> operator[](global_coords pt) noexcept; bool contains(chunk_coords c) const noexcept; void clear(); void collect(); private: + void maybe_collect(); + static constexpr std::size_t initial_capacity = 64, collect_every = 32; static constexpr float max_load_factor = .5; - std::size_t _last_collection = 0; + static constexpr auto hasher = [](chunk_coords c) -> std::size_t { + return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x); + }; - void maybe_collect(); - - std::unordered_map<chunk_coords, std::shared_ptr<chunk>> _chunks{initial_capacity}; + std::size_t _last_collection = 0; + mutable std::optional<std::tuple<std::shared_ptr<chunk>, chunk_coords>> _last_chunk; + std::unordered_map<chunk_coords, std::shared_ptr<chunk>, decltype(hasher)> _chunks{initial_capacity, hasher}; }; } // namespace floormat |