diff options
Diffstat (limited to 'chunk.hpp')
-rw-r--r-- | chunk.hpp | 64 |
1 files changed, 4 insertions, 60 deletions
@@ -47,13 +47,12 @@ struct chunk final constexpr tile& operator[](std::size_t i); constexpr const tile& operator[](std::size_t i) const; - // TODO use local_coords template<typename F> - requires std::invocable<F, tile&, local_coords, std::size_t> + requires std::invocable<F, tile&, std::size_t, local_coords> constexpr inline void foreach_tile(F&& fun) { foreach_tile_<F, chunk&>(std::forward<F>(fun)); } template<typename F> - requires std::invocable<F, const tile&, local_coords, std::size_t> + requires std::invocable<F, const tile&, std::size_t, local_coords> constexpr inline void foreach_tile(F&& fun) const { foreach_tile_<F, const chunk&>(std::forward<F>(fun)); } private: @@ -90,63 +89,8 @@ constexpr void chunk::foreach_tile_(F&& fun) std::size_t k = 0; for (std::size_t j = 0; j < N; j++) for (std::size_t i = 0; i < N; i++, k++) - fun(const_cast<Self>(*this).tiles[k], - local_coords{(std::uint8_t)i, (std::uint8_t)j}, - k); + fun(const_cast<Self>(*this).tiles[k], k, + local_coords{(std::uint8_t)i, (std::uint8_t)j}); } -#if 0 -constexpr std::size_t chunk_coords::to_index() const noexcept -{ - using unsigned_type = std::make_unsigned_t<decltype(x)>; - using limits = std::numeric_limits<unsigned_type>; - constexpr auto N = limits::max() + std::size_t{1}; - static_assert(sizeof(unsigned_type) <= sizeof(UnsignedInt)/2); - return (std::size_t)(unsigned_type)y * N + (std::size_t)(unsigned_type)x; -} -#endif - -#if 0 -struct hash_chunk final { - constexpr std::size_t operator()(chunk_coords xy) const noexcept { - return hash<sizeof(std::size_t)*8>{}(xy.to_index()); - } -}; - -struct world final -{ - static_assert(sizeof(chunk_coords::x) <= sizeof(std::size_t)/2); - - explicit world() = default; - template<typename F> std::shared_ptr<chunk> ensure_chunk(chunk_coords xy, F&& fun); - -private: - std::unordered_map<chunk_coords, std::shared_ptr<chunk>, hash_chunk> chunks; -}; - -template<typename F> -std::shared_ptr<chunk> world::ensure_chunk(chunk_coords xy, F&& fun) -{ - ASSERT(xy.x < 1 << chunk_coords::max_bits); - ASSERT(xy.y < 1 << chunk_coords::max_bits); - - auto it = chunks.find(xy); - if (it != chunks.end()) - return it->second; - else - { - std::shared_ptr<chunk> ptr{fun()}; - ASSERT(ptr); - return chunks[xy] = std::move(ptr); - } -} - -constexpr global_coords::global_coords(chunk_coords c, local_coords tile) noexcept : - x{tile.x + ((std::uint32_t)(std::make_unsigned_t<decltype(c.x)>)c.x << chunk_coords::max_bits)}, - y{tile.y + ((std::uint32_t)(std::make_unsigned_t<decltype(c.y)>)c.y << chunk_coords::max_bits)} -{ -} - -#endif - } // namespace Magnum::Examples |