From 0292669a5e9d906810ddeff86d39a957181e6286 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 8 Apr 2018 07:27:35 +0200 Subject: [PATCH] Use vector instead of ustring --- Minetestmapper/TileGenerator.cpp | 14 +++++++------- Minetestmapper/TileGenerator.h | 4 ++-- Minetestmapper/ZlibDecompressor.cpp | 12 ++++++------ Minetestmapper/ZlibDecompressor.h | 4 ++-- Minetestmapper/db-sqlite3.cpp | 5 ++--- Minetestmapper/db-sqlite3.h | 5 ++--- Minetestmapper/db.h | 3 +-- Minetestmapper/types.h | 10 ---------- 8 files changed, 22 insertions(+), 35 deletions(-) delete mode 100644 Minetestmapper/types.h diff --git a/Minetestmapper/TileGenerator.cpp b/Minetestmapper/TileGenerator.cpp index ccae845..6179a12 100644 --- a/Minetestmapper/TileGenerator.cpp +++ b/Minetestmapper/TileGenerator.cpp @@ -1607,8 +1607,8 @@ void TileGenerator::createImage() void TileGenerator::processMapBlock(const DB::Block &block) { const BlockPos &pos = block.first; - const unsigned char *data = block.second.c_str(); - size_t length = block.second.length(); + const unsigned char *data = block.second.data(); + size_t length = block.second.size(); uint8_t version = readU8(data, 0, length); //uint8_t flags = readU8(data, 1, length); @@ -1628,8 +1628,8 @@ void TileGenerator::processMapBlock(const DB::Block &block) checkDataLimit("zlib", dataOffset, 3, length); ZlibDecompressor decompressor(data, length); decompressor.setSeekPos(dataOffset); - ustring mapData = decompressor.decompress(); - ustring mapMetadata = decompressor.decompress(); + auto mapData = decompressor.decompress(); + auto mapMetadata = decompressor.decompress(); dataOffset = decompressor.seekPos(); // Skip unused data @@ -2021,12 +2021,12 @@ Color TileGenerator::computeMapHeightColor(int height) return Color(int(r / n + 0.5), int(g / n + 0.5), int(b / n + 0.5)); } -inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPos &pos, int version) +inline void TileGenerator::renderMapBlock(const std::vector &mapBlock, const BlockPos &pos, int version) { - checkBlockNodeDataLimit(version, mapBlock.length()); + checkBlockNodeDataLimit(version, mapBlock.size()); int xBegin = worldBlockX2StoredX(pos.x()); int zBegin = worldBlockZ2StoredY(pos.z()); - const unsigned char *mapData = mapBlock.c_str(); + const unsigned char *mapData = mapBlock.data(); int minY = (pos.y() < m_reqYMin) ? 16 : (pos.y() > m_reqYMin) ? 0 : m_reqYMinNode; int maxY = (pos.y() > m_reqYMax) ? -1 : (pos.y() < m_reqYMax) ? 15 : m_reqYMaxNode; bool renderedAnything = false; diff --git a/Minetestmapper/TileGenerator.h b/Minetestmapper/TileGenerator.h index 266db72..b098424 100644 --- a/Minetestmapper/TileGenerator.h +++ b/Minetestmapper/TileGenerator.h @@ -25,7 +25,7 @@ #include #include #include -#include "types.h" + #include "PixelAttributes.h" #include "BlockPos.h" #include "Color.h" @@ -186,7 +186,7 @@ private: void pushPixelRows(PixelAttributes &pixelAttributes, int zPosLimit); void scalePixelRows(PixelAttributes &pixelAttributes, PixelAttributes &pixelAttributesScaled, int zPosLimit); void processMapBlock(const DB::Block &block); - void renderMapBlock(const ustring &mapBlock, const BlockPos &pos, int version); + void renderMapBlock(const std::vector &mapBlock, const BlockPos &pos, int version); void renderScale(); void renderHeightScale(); void renderOrigin(); diff --git a/Minetestmapper/ZlibDecompressor.cpp b/Minetestmapper/ZlibDecompressor.cpp index 5697667..3a112fa 100644 --- a/Minetestmapper/ZlibDecompressor.cpp +++ b/Minetestmapper/ZlibDecompressor.cpp @@ -32,14 +32,14 @@ std::size_t ZlibDecompressor::seekPos() const return m_seekPos; } -ustring ZlibDecompressor::decompress() +std::vector ZlibDecompressor::decompress() { const unsigned char *data = m_data + m_seekPos; const std::size_t size = m_size - m_seekPos; - ustring buffer; - const size_t BUFSIZE = 128 * 1024; - uint8_t temp_buffer[BUFSIZE]; + std::vector buffer; + const size_t BUFSIZE = 256 * 1024; + unsigned char temp_buffer[BUFSIZE]; z_stream strm; strm.zalloc = Z_NULL; @@ -56,9 +56,9 @@ ustring ZlibDecompressor::decompress() int ret = 0; do { strm.avail_out = BUFSIZE; - strm.next_out = temp_buffer; + strm.next_out = &temp_buffer[0]; ret = inflate(&strm, Z_NO_FLUSH); - buffer += ustring(reinterpret_cast(temp_buffer), BUFSIZE - strm.avail_out); + buffer.insert(buffer.end(), &temp_buffer[0], &temp_buffer[BUFSIZE - strm.avail_out]); } while (ret == Z_OK); if (ret != Z_STREAM_END) { throw DecompressError(strm.msg); diff --git a/Minetestmapper/ZlibDecompressor.h b/Minetestmapper/ZlibDecompressor.h index 8813c81..44f3460 100644 --- a/Minetestmapper/ZlibDecompressor.h +++ b/Minetestmapper/ZlibDecompressor.h @@ -12,7 +12,7 @@ #include #include -#include "types.h" +#include class ZlibDecompressor @@ -27,7 +27,7 @@ public: ~ZlibDecompressor(); void setSeekPos(std::size_t seekPos); std::size_t seekPos() const; - ustring decompress(); + std::vector decompress(); private: const unsigned char *m_data; diff --git a/Minetestmapper/db-sqlite3.cpp b/Minetestmapper/db-sqlite3.cpp index bf4eddb..d4312c4 100644 --- a/Minetestmapper/db-sqlite3.cpp +++ b/Minetestmapper/db-sqlite3.cpp @@ -8,7 +8,6 @@ #include #include #include "porting.h" -#include "types.h" #define DATAVERSION_STATEMENT "PRAGMA data_version" #define BLOCKPOSLIST_STATEMENT "SELECT pos, rowid FROM blocks" @@ -211,7 +210,7 @@ int DBSQLite3::getBlockPosListRows() DB::Block DBSQLite3::getBlockOnPos(const BlockPos &pos) { - Block block(pos,reinterpret_cast("")); + Block block(pos, {}); int result = 0; m_blocksQueriedCount++; @@ -234,7 +233,7 @@ DB::Block DBSQLite3::getBlockOnPos(const BlockPos &pos) if(result == SQLITE_ROW) { const unsigned char *data = reinterpret_cast(sqlite3_column_blob(statement, 1)); int size = sqlite3_column_bytes(statement, 1); - block = Block(pos, ustring(data, size)); + block.second.assign(&data[0], &data[size]); m_blocksReadCount++; break; } else if (result == SQLITE_BUSY) { // Wait some time and try again diff --git a/Minetestmapper/db-sqlite3.h b/Minetestmapper/db-sqlite3.h index 169aa2e..5004cce 100644 --- a/Minetestmapper/db-sqlite3.h +++ b/Minetestmapper/db-sqlite3.h @@ -15,14 +15,13 @@ #include #include -#include "types.h" class DBSQLite3 : public DB { #if __cplusplus >= 201103L - typedef std::unordered_map BlockCache; + typedef std::unordered_map> BlockCache; typedef std::unordered_set BlockIdSet; #else - typedef std::map BlockCache; + typedef std::map> BlockCache; typedef std::set BlockIdSet; #endif public: diff --git a/Minetestmapper/db.h b/Minetestmapper/db.h index 9dd077d..cd63565 100644 --- a/Minetestmapper/db.h +++ b/Minetestmapper/db.h @@ -6,14 +6,13 @@ #include #include -#include "types.h" #include "BlockPos.h" class DB { public: virtual ~DB() {} - typedef std::pair Block; + typedef std::pair> Block; typedef std::vector BlockPosList; virtual const BlockPosList &getBlockPosList()=0; virtual const BlockPosList &getBlockPosList(BlockPos, BlockPos) { return getBlockPosList(); } diff --git a/Minetestmapper/types.h b/Minetestmapper/types.h deleted file mode 100644 index c6e3a10..0000000 --- a/Minetestmapper/types.h +++ /dev/null @@ -1,10 +0,0 @@ - -#ifndef TYPES_H -#define TYPES_H - -#include - -typedef std::basic_string ustring; - -#endif // TYPES_H -