diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/world.hpp | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/src/world.hpp b/src/world.hpp index 6321606e..24a77d11 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -12,15 +12,6 @@ struct chunk; struct world final { - std::shared_ptr<chunk> operator[](chunk_coords c) noexcept; - std::tuple<std::shared_ptr<chunk>, tile&> operator[](global_coords pt) noexcept; - bool contains(chunk_coords c) const noexcept; - void clear(); - void collect(); - - world(); - fm_DECLARE_DELETED_COPY_ASSIGNMENT(world); - private: void maybe_collect(); @@ -30,10 +21,33 @@ private: return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x); }; - std::size_t _last_collection = 0; - std::unordered_map<chunk_coords, std::shared_ptr<chunk>, decltype(hasher)> _chunks{initial_capacity, hasher}; mutable std::optional<std::tuple<std::shared_ptr<chunk>, chunk_coords>> _last_chunk; + std::size_t _last_collection = 0; + +public: + explicit world(); + + template<typename Hash, typename Alloc, typename Pred> + explicit world(std::unordered_map<chunk_coords, std::shared_ptr<chunk>, Hash, Alloc, Pred>&& chunks); + + std::shared_ptr<chunk> operator[](chunk_coords c) noexcept; + std::tuple<std::shared_ptr<chunk>, tile&> operator[](global_coords pt) noexcept; + bool contains(chunk_coords c) const noexcept; + void clear(); + void collect(); + + [[deprecated]] const auto& chunks() const noexcept { return _chunks; } // only for serialization + + fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world); + fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(world); }; +template<typename Hash, typename Alloc, typename Pred> +world::world(std::unordered_map<chunk_coords, std::shared_ptr<chunk>, Hash, Alloc, Pred>&& chunks) : + _chunks{chunks.begin(), chunks.end(), + std::max(initial_capacity, std::size_t(1/max_load_factor * 2 * chunks.size())), + hasher} +{} + } // namespace floormat |