work on settings (set and show), perf improvement when adding row into table
This commit is contained in:
34
table.cpp
34
table.cpp
@@ -17,7 +17,7 @@ Table::Table(const Table &other) {
|
||||
m_col_defs = other.m_col_defs;
|
||||
m_rows.reserve(other.m_rows.size());
|
||||
for(const Row& orig_row : other.m_rows) {
|
||||
add_copy_of_row(orig_row);
|
||||
commit_copy_of_row(orig_row);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +39,9 @@ ColDefNode Table::get_column_def(int col_index) {
|
||||
}
|
||||
}
|
||||
|
||||
Row Table::create_empty_row() const {
|
||||
return {columns_count()};
|
||||
Row& Table::create_empty_row() {
|
||||
m_rows.emplace_back(columns_count());
|
||||
return m_rows.back();
|
||||
}
|
||||
|
||||
std::string Table::csv_string() {
|
||||
@@ -76,10 +77,10 @@ int Table::load_csv_string(const std::string &content) {
|
||||
|
||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||
for (auto it = csv.begin() + 1; it != csv.end(); ++it) {
|
||||
std::vector<std::string> csv_line = *it;
|
||||
std::vector<std::string> &csv_line = *it;
|
||||
|
||||
// prepare empty new_row
|
||||
Row new_row = create_empty_row();
|
||||
Row& new_row = create_empty_row();
|
||||
|
||||
// copy values
|
||||
for (size_t i = 0; i < columns_count(); i++) {
|
||||
@@ -102,7 +103,7 @@ int Table::load_csv_string(const std::string &content) {
|
||||
}
|
||||
|
||||
// append new_row
|
||||
add_row(new_row);
|
||||
commit_row(new_row);
|
||||
|
||||
row_cnt++;
|
||||
}
|
||||
@@ -144,23 +145,23 @@ void Table::print() {
|
||||
std::cout << out << std::endl;
|
||||
std::cout << out2 << std::endl;
|
||||
|
||||
for(auto row : m_rows) {
|
||||
for(auto& row : m_rows) {
|
||||
row.print(col_char_sizes);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void Table::add_row(const Row &row) {
|
||||
// PERF, here it is performance botleneck, because
|
||||
// m_rows.push_back(row) calls Row::Row(const Row &other) constructor
|
||||
// it would be much more performant to add row directly to m_rows in create_new_row
|
||||
// and here in case of failed validation only remove it
|
||||
validate_row(row);
|
||||
m_rows.push_back(row);
|
||||
void Table::commit_row(const Row &row) {
|
||||
try {
|
||||
validate_row(row);
|
||||
} catch (Exception &e) {
|
||||
m_rows.erase(m_rows.end() - 1);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
void Table::add_copy_of_row(const Row &row) {
|
||||
Row new_row = create_empty_row();
|
||||
void Table::commit_copy_of_row(const Row &row) {
|
||||
Row& new_row = create_empty_row();
|
||||
|
||||
for(int i = 0; i < m_col_defs.size(); i++) {
|
||||
ColValue *ct = row.ith_column(i);
|
||||
@@ -184,7 +185,6 @@ void Table::add_copy_of_row(const Row &row) {
|
||||
}
|
||||
|
||||
validate_row(new_row);
|
||||
m_rows.push_back(new_row);
|
||||
}
|
||||
|
||||
void Table::validate_column(const ColDefNode *col_def, ValueNode *col_val) {
|
||||
|
||||
Reference in New Issue
Block a user