use vector instead of map for great memory savings

develop
myk002 2021-09-10 20:16:37 -07:00 committed by Myk
parent 8c8365abec
commit afc7096329
1 changed files with 22 additions and 14 deletions

@ -630,12 +630,9 @@ static bool get_filename(string &fname,
return true; return true;
} }
typedef map<int16_t /* x */, const char *> bp_row; typedef vector<const char *> bp_row; // index is x coordinate
typedef map<int16_t /* y */, bp_row> bp_area; typedef map<int16_t, bp_row> bp_area; // key is y coordinate
typedef map<int16_t /* z */, bp_area> bp_volume; typedef map<int16_t, bp_area> bp_volume; // key is z coordinate
static const bp_area NEW_AREA;
static const bp_row NEW_ROW;
typedef const char * (get_tile_fn)(const df::coord &pos, typedef const char * (get_tile_fn)(const df::coord &pos,
const tile_context &ctx); const tile_context &ctx);
@ -666,11 +663,15 @@ static void write_minimal(ofstream &ofile, const blueprint_options &opts,
for (auto row : area.second) { for (auto row : area.second) {
for ( ; yprev < row.first; ++yprev) for ( ; yprev < row.first; ++yprev)
ofile << endl; ofile << endl;
int16_t xprev = 0; size_t xprev = 0;
for (auto tile : row.second) { auto &tiles = row.second;
for ( ; xprev < tile.first; ++xprev) size_t rowsize = tiles.size();
for (size_t x = 0; x < rowsize; ++x) {
if (!tiles[x])
continue;
for ( ; xprev < x; ++xprev)
ofile << ","; ofile << ",";
ofile << tile.second; ofile << tiles[x];
} }
} }
ofile << endl; ofile << endl;
@ -692,7 +693,7 @@ static void write_pretty(ofstream &ofile, const blueprint_options &opts,
row = &area->at(y); row = &area->at(y);
for (int16_t x = 0; x < opts.width; ++x) { for (int16_t x = 0; x < opts.width; ++x) {
const char *tile = NULL; const char *tile = NULL;
if (row && row->count(x)) if (row)
tile = row->at(x); tile = row->at(x);
ofile << (tile ? tile : " ") << ","; ofile << (tile ? tile : " ") << ",";
} }
@ -742,6 +743,10 @@ static bool do_transform(color_ostream &out,
const df::coord &start, const df::coord &end, const df::coord &start, const df::coord &end,
const blueprint_options &opts, const blueprint_options &opts,
vector<string> &filenames) { vector<string> &filenames) {
// empty map instances to pass to emplace() below
static const bp_area EMPTY_AREA;
static const bp_row EMPTY_ROW;
vector<blueprint_processor> processors; vector<blueprint_processor> processors;
if (opts.auto_phase || opts.dig) if (opts.auto_phase || opts.dig)
@ -779,10 +784,13 @@ static bool do_transform(color_ostream &out,
if (tile_str) { if (tile_str) {
// ensure our z-index is in the order we want to write // ensure our z-index is in the order we want to write
auto area = processor.mapdata.emplace(abs(z - start.z), auto area = processor.mapdata.emplace(abs(z - start.z),
NEW_AREA); EMPTY_AREA);
auto row = area.first->second.emplace(y - start.y, auto row = area.first->second.emplace(y - start.y,
NEW_ROW); EMPTY_ROW);
row.first->second[x - start.x] = tile_str; auto &tiles = row.first->second;
if (row.second)
tiles.resize(opts.width);
tiles[x - start.x] = tile_str;
} }
} }
} }