Skip to content

Commit

Permalink
CLI: Rework indentation of the output
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Dec 22, 2023
1 parent b299ae9 commit e646d65
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
43 changes: 29 additions & 14 deletions src/cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,25 @@ namespace
return out;
}

void output_solution_grid(std::ostream& out, const picross::OutputGrid& grid, unsigned int indent = 0)
const stdutils::string::Indent CLI_INDENT(2);

void output_solution_grid(std::ostream& out, const picross::OutputGrid& grid, unsigned int indentation_level = 0)
{
const std::string ind(indent, ' ');
for (unsigned int y = 0u; y < grid.height(); y++)
out << ind << grid.get_line<picross::Line::ROW>(y) << std::endl;
{
out << CLI_INDENT(indentation_level) << grid.get_line<picross::Line::ROW>(y) << std::endl;
}
}

void output_solution_stats(std::ostream& out, const picross::GridStats& stats, unsigned int indentation_level = 0)
{
std::stringstream buf;
buf << stats;
buf.seekg(0);
for (std::string line; std::getline(buf, line);)
{
out << CLI_INDENT(line.empty() ? 0 : indentation_level) << line << std::endl;
}
}
} // namespace

Expand Down Expand Up @@ -265,7 +279,7 @@ int main(int argc, char *argv[])
if (!validation_mode)
{
std::cout << "GRID " << ++count_grids << ": " << input_grid.name() << std::endl;
std::cout << " Size: " << grid_data.size << std::endl;
std::cout << CLI_INDENT << "Size: " << grid_data.size << std::endl;
}

/* Sanity check of the input data */
Expand Down Expand Up @@ -339,14 +353,14 @@ int main(int argc, char *argv[])
if (solution.partial)
{
assert(!solution.grid.is_completed());
std::cout << " Partial solution:" << std::endl;
std::cout << CLI_INDENT << "Partial solution:" << std::endl;
}
else
{
assert(solution.grid.is_completed());
std::cout << " Solution nb " << ++nb_solutions << ": (branching depth: " << solution.branching_depth << ")" << std::endl;
std::cout << CLI_INDENT << "Solution nb " << ++nb_solutions << ": (branching depth: " << solution.branching_depth << ")" << std::endl;
}
output_solution_grid(std::cout, solution.grid, 2);
output_solution_grid(std::cout, solution.grid, 1);
std::cout << std::endl;
return max_nb_solutions == 0 || nb_solutions < max_nb_solutions;
};
Expand All @@ -364,17 +378,17 @@ int main(int argc, char *argv[])
break;
case picross::Solver::Status::ABORTED:
if (max_nb_solutions != 0 && nb_solutions == max_nb_solutions)
std::cout << " Reached max number of solutions" << std::endl;
std::cout << CLI_INDENT << "Reached max number of solutions" << std::endl;
else
std::cout << " Solver aborted" << std::endl;
std::cout << CLI_INDENT << "Solver aborted" << std::endl;
std::cout << std::endl;
break;
case picross::Solver::Status::CONTRADICTORY_GRID:
std::cout << " Not solvable" << std::endl;
std::cout << CLI_INDENT << "Not solvable" << std::endl;
std::cout << std::endl;
break;
case picross::Solver::Status::NOT_LINE_SOLVABLE:
std::cout << " Not line solvable" << std::endl;
std::cout << CLI_INDENT << "Not line solvable" << std::endl;
std::cout << std::endl;
break;
default:
Expand All @@ -383,18 +397,19 @@ int main(int argc, char *argv[])
}

/* Display stats */
std::cout << stats << std::endl;
output_solution_stats(std::cout, stats, 1);
std::cout << std::endl;

/* Display timings */
if (!args["no-timing"])
{
std::cout << " Wall time: " << time_ms.count() << "ms" << std::endl;
std::cout << CLI_INDENT << "Wall time: " << time_ms.count() << "ms" << std::endl;
}
}
}
else if (!validation_mode)
{
std::cout << " Invalid grid. Error message: " << grid_data.misc << std::endl;
std::cout << CLI_INDENT << "Invalid grid. Error message: " << grid_data.misc << std::endl;
}
}
catch (std::exception& e)
Expand Down
30 changes: 15 additions & 15 deletions src/picross/src/picross_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,54 +58,54 @@ void merge_branching_grid_stats(GridStats& stats, const GridStats& branching_sta

std::ostream& operator<<(std::ostream& out, const GridStats& stats)
{
out << " Difficulty: " << str_difficulty_code(difficulty_code(stats)) << std::endl;
out << " Number of solutions found: " << stats.nb_solutions << std::endl;
out << " Max K: " << stats.max_k << std::endl;
out << " Max branching depth: " << stats.max_branching_depth << std::endl;
out << "Difficulty: " << str_difficulty_code(difficulty_code(stats)) << std::endl;
out << "Number of solutions found: " << stats.nb_solutions << std::endl;
out << "Max K: " << stats.max_k << std::endl;
out << "Max branching depth: " << stats.max_branching_depth << std::endl;

if (stats.max_branching_depth > 0u)
{
out << " > Hypothesis (probing/branching) on " << stats.nb_probing_calls << "/" << stats.nb_branching_calls << " lines" << std::endl;
out << " > Total number of alternatives being tested (probing/branching): " << stats.total_nb_probing_alternatives << "/" << stats.total_nb_branching_alternatives << std::endl;
out << " > Hypothesis (probing/branching) on " << stats.nb_probing_calls << "/" << stats.nb_branching_calls << " lines" << std::endl;
out << " > Total number of alternatives being tested (probing/branching): " << stats.total_nb_probing_alternatives << "/" << stats.total_nb_branching_alternatives << std::endl;
assert(stats.max_nb_alternatives_by_branching_depth.size() == stats.max_branching_depth);
out << " > Max number of alternatives by branching depth:";
out << " > Max number of alternatives by branching depth:";
for (const auto& max_alternatives : stats.max_nb_alternatives_by_branching_depth)
{
out << " " << max_alternatives;
}
out << std::endl;
}

out << " Max number of alternatives on an empty line (initial grid pass): " << stats.max_initial_nb_alternatives;
out << "Max number of alternatives on an empty line (initial grid pass): " << stats.max_initial_nb_alternatives;
if (stats.max_initial_nb_alternatives == BinomialCoefficients::overflowValue()) { out << " (overflow!)"; }
out << std::endl;

if (stats.max_nb_alternatives_partial_w_change > 0 || stats.max_nb_alternatives_partial)
{
out << " Max number of alternatives after a partial line reduction (change/all): " << stats.max_nb_alternatives_partial_w_change << "/" << stats.max_nb_alternatives_partial << std::endl;
out << "Max number of alternatives after a partial line reduction (change/all): " << stats.max_nb_alternatives_partial_w_change << "/" << stats.max_nb_alternatives_partial << std::endl;
}
if (stats.max_nb_alternatives_linear_w_change > 0 || stats.max_nb_alternatives_linear)
{
out << " Max number of alternatives after a linear line reduction (change/all): " << stats.max_nb_alternatives_linear_w_change << "/" << stats.max_nb_alternatives_linear << std::endl;
out << "Max number of alternatives after a linear line reduction (change/all): " << stats.max_nb_alternatives_linear_w_change << "/" << stats.max_nb_alternatives_linear << std::endl;
}
if (stats.max_nb_alternatives_full_w_change > 0 || stats.max_nb_alternatives_full)
{
out << " Max number of alternatives after a full line reduction (change/all): " << stats.max_nb_alternatives_full_w_change << "/" << stats.max_nb_alternatives_full << std::endl;
out << "Max number of alternatives after a full line reduction (change/all): " << stats.max_nb_alternatives_full_w_change << "/" << stats.max_nb_alternatives_full << std::endl;
}

out << " Number of full grid pass: " << stats.nb_full_grid_pass << std::endl;
out << "Number of full grid pass: " << stats.nb_full_grid_pass << std::endl;

if (stats.nb_single_line_partial_reduction_w_change > 0 || stats.nb_single_line_partial_reduction > 0)
{
out << " Number of single line partial reduction (change/all): " << stats.nb_single_line_partial_reduction_w_change << "/" << stats.nb_single_line_partial_reduction << std::endl;
out << "Number of single line partial reduction (change/all): " << stats.nb_single_line_partial_reduction_w_change << "/" << stats.nb_single_line_partial_reduction << std::endl;
}
if (stats.nb_single_line_linear_reduction_w_change > 0 || stats.nb_single_line_linear_reduction > 0)
{
out << " Number of single line linear reduction (change/all): " << stats.nb_single_line_linear_reduction_w_change << "/" << stats.nb_single_line_linear_reduction << std::endl;
out << "Number of single line linear reduction (change/all): " << stats.nb_single_line_linear_reduction_w_change << "/" << stats.nb_single_line_linear_reduction << std::endl;
}
if (stats.nb_single_line_full_reduction_w_change > 0 || stats.nb_single_line_full_reduction > 0)
{
out << " Number of single line full reduction (change/all): " << stats.nb_single_line_full_reduction_w_change << "/" << stats.nb_single_line_full_reduction << std::endl;
out << "Number of single line full reduction (change/all): " << stats.nb_single_line_full_reduction_w_change << "/" << stats.nb_single_line_full_reduction << std::endl;
}

return out;
Expand Down

0 comments on commit e646d65

Please sign in to comment.