Skip to content

Commit

Permalink
Issue #7 | Fix state insolvency, add more debug code, add YO_ prefix …
Browse files Browse the repository at this point in the history
…to internal macros
  • Loading branch information
Mr-S-Mirzoev committed Jul 2, 2023
1 parent c789968 commit 352c037
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
11 changes: 4 additions & 7 deletions src/inc/debug/assert.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#pragma once

#include "debug/exception.hpp"
#include "debug/throw.hpp"

#include <sstream>

#define DEBUG_ASSERT(condition) DEBUG_ASSERT_WITH_MSG(condition, "");
#define YO_DEBUG_ASSERT(condition) YO_DEBUG_ASSERT_WITH_MSG(condition, "");

#define DEBUG_ASSERT_WITH_MSG(condition, description) \
#define YO_DEBUG_ASSERT_WITH_MSG(condition, description, ...) \
do \
{ \
if (!(condition)) \
{ \
std::ostringstream oss; \
oss << "Assertion failed: " description " in file " __FILE__ \
" at line " \
<< __LINE__; \
throw YamlOptimizerError(oss.str()); \
YO_THROW(YamlOptimizerError, "Assertion failed: {} in file " __FILE__ " at line {}", fmt::format(description, ##__VA_ARGS__), __LINE__) \
} \
} while (false);
4 changes: 2 additions & 2 deletions src/inc/debug/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#ifndef YO_DEBUG

#define DEBUG_PRINT(...)
#define YO_DEBUG_PRINT(...)

#else

#include <fmt/format.h>

#define DEBUG_PRINT(message, ...) \
#define YO_DEBUG_PRINT(message, ...) \
fmt::print("[DEBUG] " message "\n", ##__VA_ARGS__)

#endif // YO_DEBUG
1 change: 1 addition & 0 deletions src/inc/optimizer/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class YamlOptimizer
*/
void dump(std::string const& filename);

private:
NAMED_PRIVATE_SECTION(Input data)

const OptimizationSettings settings_;
Expand Down
23 changes: 23 additions & 0 deletions src/inc/utils/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ void set_reference(ryml::NodeRef& node, ryml::csubstr anchor)
node.set_val_ref(anchor);
}

std::size_t get_next_valid_id_after_content_removal(ryml::NodeRef node, std::size_t max_possible_id)
{
std::size_t next_valid_id = ryml::NONE;

while (next_valid_id == ryml::NONE && node.has_parent())
{
if (node == node.last_sibling())
{
node = node.parent();
}
else
{
next_valid_id = node.next_sibling().id();
break;
}
}

if (next_valid_id == ryml::NONE)
next_valid_id = max_possible_id;

return next_valid_id;
}

std::string to_string(const ryml::ConstNodeRef& node)
{
std::ostringstream oss;
Expand Down
54 changes: 28 additions & 26 deletions src/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void YamlOptimizer::optimize()
{
for (std::size_t i = 0; i < data_.size() - 1; ++i)
{
DEBUG_PRINT("i={}", i);
YO_DEBUG_PRINT("i={}", i);
if (i >= data_.size())
break;

Expand All @@ -55,7 +55,13 @@ void YamlOptimizer::optimize()

for (std::size_t j = i + 1; j < data_.size(); ++j)
{
DEBUG_PRINT("i={}; j={}", i, j);
YO_DEBUG_PRINT("i={}; j={}", i, j);
if (data_.size() != tree_.size())
YO_DEBUG_ASSERT_WITH_MSG(
data_.size() == tree_.size(),
"Node count must be solvent at all times: {} != {}",
data_.size(), tree_.size());

if (i >= data_.size() || j >= data_.size())
break;

Expand All @@ -74,7 +80,7 @@ void YamlOptimizer::optimize()

if (!nodes_equal(a, b))
{
DEBUG_PRINT(
YO_DEBUG_PRINT(
"Diff nodes: \nlhs:\n\"\"\"\n{}\"\"\"\n "
"\nrhs:\n\"\"\"\n{}\"\"\"\n \ndiff:\n\"\"\"\n{}\"\"\"\n",
node_utils::to_string(a), node_utils::to_string(b),
Expand All @@ -95,25 +101,21 @@ void YamlOptimizer::optimize()
a.set_val_anchor(anchor);
}

auto next_valid_id = b.next_sibling().id();
if (next_valid_id == ryml::NONE && b.has_parent())
next_valid_id = b.parent().next_sibling().id();
if (next_valid_id == ryml::NONE)
next_valid_id = data_.size();
auto next_valid_id =
node_utils::get_next_valid_id_after_content_removal(
b, data_.size());

node_utils::set_reference(b, anchor);

DEBUG_PRINT("tree_.size={}", tree_.size());

// Reorder the tree nodes (fix indexes to match [0 .. sz - 1] range)
tree_.reorder();

DEBUG_PRINT("next_valid_id={}; b.id = {}", next_valid_id, b.id());
YO_DEBUG_PRINT("Removing elements from {} to {}. Removing: \n{}\n",
b.id() + 1, next_valid_id, node_utils::to_string(a));

// Erase the redundant nodes from the data vector
data_.erase(data_.begin() + b.id() + 1,
data_.begin() + next_valid_id);
DEBUG_PRINT("data_.size={}", data_.size());
}
}

Expand Down Expand Up @@ -165,14 +167,14 @@ bool YamlOptimizer::nodes_equal(const ryml::ConstNodeRef& a,
{
if (data_[a.id()].size != data_[b.id()].size)
{
DEBUG_PRINT("Size mismatch: {} vs {}", data_[a.id()].size,
data_[b.id()].size);
YO_DEBUG_PRINT("Size mismatch: {} vs {}", data_[a.id()].size,
data_[b.id()].size);
return false;
}

if (a.type() != b.type())
{
DEBUG_PRINT("Type mismatch: {} vs {}", a.type_str(), b.type_str());
YO_DEBUG_PRINT("Type mismatch: {} vs {}", a.type_str(), b.type_str());

if (!long_types_equal(a, b))
return false;
Expand All @@ -182,40 +184,40 @@ bool YamlOptimizer::nodes_equal(const ryml::ConstNodeRef& a,
{
if (a.has_key() && a.key() != b.key())
{
DEBUG_PRINT("Key mismatch");
YO_DEBUG_PRINT("Key mismatch");
return false;
}

if (a.val() != b.val())
{
DEBUG_PRINT("Val mismatch");
YO_DEBUG_PRINT("Val mismatch");
return false;
}

if (a.has_val_tag() != b.has_val_tag())
{
DEBUG_PRINT("Val tag mismatch 1");
YO_DEBUG_PRINT("Val tag mismatch 1");
return false;
}

if (a.has_val_tag() && a.val_tag() != b.val_tag())
{
DEBUG_PRINT("Val tag mismatch 2");
YO_DEBUG_PRINT("Val tag mismatch 2");
return false;
}
}
else
{
if (a.has_children() != b.has_children())
{
DEBUG_PRINT("No child mismatch");
YO_DEBUG_PRINT("No child mismatch");
return false;
}

if (a.num_children() != b.num_children())
{
DEBUG_PRINT("Num children mismatch: {} vs. {}", a.num_children(),
b.num_children());
YO_DEBUG_PRINT("Num children mismatch: {} vs. {}", a.num_children(),
b.num_children());
return false;
}

Expand All @@ -224,7 +226,7 @@ bool YamlOptimizer::nodes_equal(const ryml::ConstNodeRef& a,
{
if (!nodes_equal(*a_it, *b_it))
{
DEBUG_PRINT("Child mismatch");
YO_DEBUG_PRINT("Child mismatch");
return false;
}
}
Expand Down Expand Up @@ -255,8 +257,8 @@ std::size_t YamlOptimizer::get_info_impl(const ryml::ConstNodeRef& node)
{
// Store the size of the current node in the data vector
std::size_t nodeId{node.id()};
DEBUG_ASSERT_WITH_MSG(nodeId < data_.size(),
"Node id must never exceed nodes count");
YO_DEBUG_ASSERT_WITH_MSG(nodeId < data_.size(),
"Node id must never exceed nodes count");

auto& node_size = data_[nodeId].size;

Expand Down Expand Up @@ -287,7 +289,7 @@ void YamlOptimizer::debug_print_data() const
std::string val;
if (tree_.ref(i).has_val())
val = {tree_.ref(i).val().data(), tree_.ref(i).val().size()};
DEBUG_PRINT("{}({} -> {}) : {}", i, key, val, data_[i].size);
YO_DEBUG_PRINT("{}({} -> {}) : {}", i, key, val, data_[i].size);
}
}
#endif // YO_DEBUG

0 comments on commit 352c037

Please sign in to comment.