Skip to content

Commit

Permalink
ReaderMapping::write_compressed: Remove custom width support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankata453 committed Dec 5, 2024
1 parent 12bdec2 commit ea9c537
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/object/tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ TileMap::write_tiles(Writer& writer) const
{
writer.write("width", m_width);
writer.write("height", m_height);
writer.write_compressed("tiles", m_tiles, m_width);
writer.write_compressed("tiles", m_tiles);
}

void
Expand Down
39 changes: 11 additions & 28 deletions src/util/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,54 +249,37 @@ Writer::write(const std::string& name, const sexp::Value& value)
}

void
Writer::write_compressed(const std::string& name, const std::vector<unsigned int>& value, int width)
Writer::write_compressed(const std::string& name, const std::vector<unsigned int>& value)
{
indent();
*out << '(' << name;
if (width > 0)
{
*out << "\n";
indent();
}

int count = 0;
int repeater = 0;
unsigned int repeated_value = 0;
for (const auto& i : value)
for (size_t i = 0; i < value.size(); ++i)
{
const bool width_limit = (width > 0 && count >= width);

++count;
if (repeater > 0 && i == repeated_value)
if (repeater > 0 && value[i] == repeated_value)
{
++repeater;
}
else
{
if (repeater > 1)
*out << -repeater << " " << repeated_value << (width_limit ? "" : " ");
else if (repeater == 1)
*out << repeated_value << (width_limit ? "" : " ");

repeater = 1;
repeated_value = i;
}

if (width > 0 && count >= width)
{
if (repeater > 1)
*out << -repeater << " " << repeated_value;
else if (repeater == 1)
*out << repeated_value;

count = 0;
repeater = 0;
repeated_value = 0;
if (i != value.size() - 1)
*out << " ";

*out << "\n";
indent();
repeater = 1;
repeated_value = value[i];
}
}
if (repeater > 1)
*out << " " << -repeater << " " << repeated_value;
else if (repeater == 1)
*out << " " << repeated_value;

*out << ")\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Writer final
// add more write-functions when needed...

// Writes vector by using negative integer values as repeaters for repeating values.
void write_compressed(const std::string& name, const std::vector<unsigned int>& value, int width = 0);
void write_compressed(const std::string& name, const std::vector<unsigned int>& value);

void end_list(const std::string& listname);

Expand Down

0 comments on commit ea9c537

Please sign in to comment.