From c06af49068ec0f4d3ac5702b8c6a74626d68ddd2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 13 Feb 2021 00:34:33 +0100 Subject: [PATCH] Info: restructure mipmap level data structure --- src/Info.cpp | 10 ++++++---- src/Info.hpp | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Info.cpp b/src/Info.cpp index 55c1156..9d9d751 100644 --- a/src/Info.cpp +++ b/src/Info.cpp @@ -46,12 +46,14 @@ void Info::writeJSON(const char *filename) const { std::fprintf(f, "{"); std::fprintf(f, "\"mipmaps\":["); - for (size_t level = 0; level < regions.size(); level++) { - if (level != 0) + bool first_level = true; + for (const auto &level : levels) { + if (!first_level) std::fprintf(f, ","); + first_level = false; int minX, maxX, minZ, maxZ; - std::tie(minX, maxX, minZ, maxZ) = getBounds(level); + std::tie(minX, maxX, minZ, maxZ) = level.bounds; std::fprintf(f, "{"); std::fprintf(f, "\"bounds\":{"); @@ -63,7 +65,7 @@ void Info::writeJSON(const char *filename) const { std::fprintf(f, "\"regions\":{"); bool first_z = true; - for (const auto &item : regions[level]) { + for (const auto &item : level.regions) { if (!first_z) std::fprintf(f, ","); first_z = false; diff --git a/src/Info.hpp b/src/Info.hpp index bccabdb..fe4950a 100644 --- a/src/Info.hpp +++ b/src/Info.hpp @@ -39,9 +39,14 @@ namespace MinedMap { class Info { +public: + struct Level { + std::map> regions; + std::tuple bounds; + }; + private: - std::vector>> regions; - std::vector> bounds; + std::vector levels; int32_t spawnX, spawnZ; @@ -51,18 +56,18 @@ public: } std::tuple getBounds(size_t level) const { - return bounds[level]; + return levels[level].bounds; } void addRegion(int x, int z, size_t level) { - auto &level_r = regions[level]; - auto z_regions = level_r.emplace( + auto &the_level = levels[level]; + auto z_regions = the_level.regions.emplace( std::piecewise_construct, std::make_tuple(z), std::make_tuple()).first; z_regions->second.insert(x); - std::tuple &b = bounds[level]; + std::tuple &b = the_level.bounds; if (x < std::get<0>(b)) std::get<0>(b) = x; if (x > std::get<1>(b)) std::get<1>(b) = x; @@ -71,12 +76,14 @@ public: } void addMipmapLevel() { - regions.emplace_back(); - bounds.emplace_back(INT_MAX, INT_MIN, INT_MAX, INT_MIN); + levels.emplace_back(Level { + .regions = {}, + .bounds = {INT_MAX, INT_MIN, INT_MAX, INT_MIN}, + }); } size_t getMipmapLevel() const { - return regions.size()-1; + return levels.size()-1; } void setSpawn(const std::pair &v) {