Print CGO table (backported from Incentive)

This commit is contained in:
Gabriel Marques
2024-06-02 21:23:13 -04:00
committed by Jarrett Johnson
parent 878cdd54ba
commit 9cefa38079
3 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "PrintUtils.h"
display_table_t & display_table_t::begin_row() {
_table.push_back(std::vector<std::string>());
_current_row++;
return *this;
}
void display_table_t::display() {
// Dimensions of tables
const size_t num_rows = _table.size();
const size_t num_cols = ([&] () {
size_t largest = 0;
for (auto & v : _table) {
largest = std::max(largest, v.size());
}
return largest;
})();
// Pad
for (auto & row : _table) {
while (row.size() < num_cols) {
row.emplace_back(" ");
}
}
// Get the largest string for each col
std::vector<size_t> col_sizes(num_cols);
for (size_t j = 0; j < num_cols; ++j) {
size_t largest = 0;
for (size_t i = 0; i < num_rows; ++i) {
largest = std::max(largest, _table[i][j].size());
}
col_sizes[j] = largest;
}
// construct the output
std::stringstream ss;
ss << std::left;
auto insert_hr = [&]() {
for (size_t j = 0; j < num_cols; ++j) {
ss << std::setw(col_sizes[j] + 3)
<< std::setfill('-') << "+";
}
ss << "+" << std::setfill(' ') << std::endl;
};
{
int i = 0, j = 0;
for (auto & row : _table) {
insert_hr();
ss << "| ";
for (auto & col : row) {
ss << std::setw(col_sizes[j])
<< col << " | ";
j++;
}
ss << std::endl;
i++; j = 0;
}
insert_hr();
}
std::cout << ss.str();
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include <type_traits>
#include <vector>
#include <string>
#include <sstream>
/***********************************************************************
* Table pretty printing utility class
* ---------------------------------------------------------------------
* Will autofill to the row with the largest column
***********************************************************************/
class display_table_t {
public:
// both begins and ends a row
display_table_t & begin_row();
// inserts a new cell with some printable type in it
template <typename T>
display_table_t & insert_cell(T s) {
std::stringstream ss;
ss << s;
_table[_current_row].emplace_back(ss.str());
return *this;
}
// finalizes the table and prints it to std out
void display();
private:
int _current_row { -1 };
std::vector<std::vector<std::string>> _table;
};

View File

@@ -7981,6 +7981,32 @@ void CGO::add_to_cgo(int op, const float * pc) {
}
void CGO::print_table() const {
display_table_t table;
table.begin_row()
.insert_cell("#")
.insert_cell("OP_CODE")
.insert_cell("OP_SZ")
.insert_cell("DATA");
unsigned j = 0;
for (auto it = begin(); !it.is_stop(); ++it) {
const auto pc = it.data();
const auto op_code = it.op_code();
table.begin_row()
.insert_cell(++j)
.insert_cell(op_code)
.insert_cell(CGO_sz[op_code]);
int sz = CGO_sz[op_code];
std::stringstream ss;
for (int i = 0; i < sz; i++) {
ss << CGO_get_int(pc + i);
if (i != (sz - 1))
ss << ", ";
}
table.insert_cell(ss.str());
}
table.display();
}
CGO* CGOConvertSpheresToPoints(const CGO* I)