mirror of
https://github.com/NVIDIA/nvbench.git
synced 2026-03-14 20:27:24 +00:00
Split table_builder out from markdown_table.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <nvbench/detail/transform_reduce.cuh>
|
||||
|
||||
#include <nvbench/internal/table_builder.cuh>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -23,33 +25,9 @@ namespace nvbench::internal
|
||||
* std::string table_str = table.to_string();
|
||||
* ```
|
||||
*/
|
||||
struct markdown_table
|
||||
struct markdown_table : private table_builder
|
||||
{
|
||||
void add_cell(std::size_t row,
|
||||
const std::string &column_key,
|
||||
const std::string &header,
|
||||
std::string value)
|
||||
{
|
||||
auto iter = std::find_if(m_columns.begin(),
|
||||
m_columns.end(),
|
||||
[&column_key](const column &col) {
|
||||
return col.key == column_key;
|
||||
});
|
||||
|
||||
auto &col = iter == m_columns.end()
|
||||
? m_columns.emplace_back(column{column_key,
|
||||
header,
|
||||
std::vector<std::string>{},
|
||||
header.size()})
|
||||
: *iter;
|
||||
|
||||
col.max_width = std::max(col.max_width, value.size());
|
||||
if (col.rows.size() <= row)
|
||||
{
|
||||
col.rows.resize(row + 1);
|
||||
col.rows[row] = std::move(value);
|
||||
}
|
||||
}
|
||||
using table_builder::add_cell;
|
||||
|
||||
std::string to_string()
|
||||
{
|
||||
@@ -67,28 +45,6 @@ struct markdown_table
|
||||
}
|
||||
|
||||
private:
|
||||
struct column
|
||||
{
|
||||
std::string key;
|
||||
std::string header;
|
||||
std::vector<std::string> rows;
|
||||
std::size_t max_width;
|
||||
};
|
||||
|
||||
void fix_row_lengths()
|
||||
{ // Ensure that each row is the same length:
|
||||
m_num_rows = nvbench::detail::transform_reduce(
|
||||
m_columns.cbegin(),
|
||||
m_columns.cend(),
|
||||
0ll,
|
||||
[](const auto &a, const auto &b) { return a > b ? a : b; },
|
||||
[](const column &col) { return col.rows.size(); });
|
||||
std::for_each(m_columns.begin(),
|
||||
m_columns.end(),
|
||||
[num_rows = m_num_rows](column &col) {
|
||||
col.rows.resize(num_rows);
|
||||
});
|
||||
}
|
||||
|
||||
void print_header(fmt::memory_buffer &buffer)
|
||||
{
|
||||
@@ -122,9 +78,6 @@ private:
|
||||
fmt::format_to(buffer, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<column> m_columns;
|
||||
std::size_t m_num_rows{};
|
||||
};
|
||||
|
||||
} // namespace nvbench::internal
|
||||
|
||||
69
nvbench/internal/table_builder.cuh
Normal file
69
nvbench/internal/table_builder.cuh
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nvbench::internal
|
||||
{
|
||||
|
||||
/*!
|
||||
* State for a text table (rows and columns of cells). Tracks column widths.
|
||||
*/
|
||||
struct table_builder
|
||||
{
|
||||
struct column
|
||||
{
|
||||
std::string key;
|
||||
std::string header;
|
||||
std::vector<std::string> rows;
|
||||
std::size_t max_width;
|
||||
};
|
||||
|
||||
std::vector<column> m_columns;
|
||||
std::size_t m_num_rows{};
|
||||
|
||||
void add_cell(std::size_t row,
|
||||
const std::string &column_key,
|
||||
const std::string &header,
|
||||
std::string value)
|
||||
{
|
||||
auto iter = std::find_if(m_columns.begin(),
|
||||
m_columns.end(),
|
||||
[&column_key](const column &col) {
|
||||
return col.key == column_key;
|
||||
});
|
||||
|
||||
auto &col = iter == m_columns.end()
|
||||
? m_columns.emplace_back(column{column_key,
|
||||
header,
|
||||
std::vector<std::string>{},
|
||||
header.size()})
|
||||
: *iter;
|
||||
|
||||
col.max_width = std::max(col.max_width, value.size());
|
||||
if (col.rows.size() <= row)
|
||||
{
|
||||
col.rows.resize(row + 1);
|
||||
col.rows[row] = std::move(value);
|
||||
}
|
||||
}
|
||||
|
||||
void fix_row_lengths()
|
||||
{ // Ensure that each row is the same length:
|
||||
m_num_rows = nvbench::detail::transform_reduce(
|
||||
m_columns.cbegin(),
|
||||
m_columns.cend(),
|
||||
0ll,
|
||||
[](const auto &a, const auto &b) { return a > b ? a : b; },
|
||||
[](const column &col) { return col.rows.size(); });
|
||||
std::for_each(m_columns.begin(),
|
||||
m_columns.end(),
|
||||
[num_rows = m_num_rows](column &col) {
|
||||
col.rows.resize(num_rows);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nvbench::internal
|
||||
Reference in New Issue
Block a user