diff options
Diffstat (limited to 'src/world.cpp')
-rw-r--r-- | src/world.cpp | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/src/world.cpp b/src/world.cpp index 4e47f912..dcb1b4a3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -3,37 +3,32 @@ namespace floormat { -struct chunk_pointer_maker final -{ - inline operator std::shared_ptr<chunk>() const { return std::make_shared<chunk>(); } -}; - world::world() { _chunks.max_load_factor(max_load_factor); } -std::shared_ptr<chunk> world::operator[](chunk_coords c) noexcept +chunk& world::operator[](chunk_coords coord) noexcept { maybe_collect(); - if (_last_chunk) + if (auto& [c, coord2] = _last_chunk; c && coord == coord2) { - auto& [ret, pos] = *_last_chunk; - if (pos == c) - return ret; + return *c; } - auto [it, inserted] = _chunks.try_emplace(c, chunk_pointer_maker{}); - auto ret = it->second; - _last_chunk = { ret, c }; + auto [it, inserted] = _chunks.try_emplace(coord); + auto& ret = it->second; + auto& [_c, _coord] = _last_chunk; + _c = &ret; + _coord = coord; return ret; } -std::tuple<std::shared_ptr<chunk>, tile&> world::operator[](global_coords pt) noexcept +std::tuple<chunk&, tile&> world::operator[](global_coords pt) noexcept { - auto c = operator[](pt.chunk()); - return { c, (*c)[pt.local()] }; + auto& c = operator[](pt.chunk()); + return { c, c[pt.local()] }; } bool world::contains(chunk_coords c) const noexcept @@ -44,9 +39,10 @@ bool world::contains(chunk_coords c) const noexcept void world::clear() { _last_collection = 0; - _last_chunk = std::nullopt; _chunks.clear(); _chunks.rehash(initial_capacity); + auto& [c, _] = _last_chunk; + c = nullptr; } void world::maybe_collect() @@ -60,14 +56,15 @@ void world::collect() for (auto it = _chunks.begin(); it != _chunks.end(); (void)0) { const auto& [_, c] = *it; - if (c->empty()) + if (c.empty()) it = _chunks.erase(it); else ++it; } _last_collection = _chunks.size(); - _last_chunk = std::nullopt; + auto& [c, _] = _last_chunk; + c = nullptr; } } // namespace floormat |