From: Wolfgang Bangerth Date: Mon, 10 Apr 2017 01:24:29 +0000 (-0600) Subject: Implement TableHandler::start_new_row(). X-Git-Tag: v9.0.0-rc1~1705^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41ba7c4dd38ede23862879b51da66ebd04ce91b4;p=dealii.git Implement TableHandler::start_new_row(). --- diff --git a/include/deal.II/base/table_handler.h b/include/deal.II/base/table_handler.h index 62f6bcd19c..55687aec1f 100644 --- a/include/deal.II/base/table_handler.h +++ b/include/deal.II/base/table_handler.h @@ -356,6 +356,21 @@ public: void add_value (const std::string &key, const T value); + /** + * If a row is only partially filled, then set all elements of that + * row for which no elements exist in a particular column to the + * empty string. This is akin to the 'auto_fill_mode' described in + * the introduction, but more general because it allows you to start + * writing into a column for a new row without having to know that + * that column had been written to in the previous row. + * + * If all columns have been written into in the current row, then + * this function doesn't do anything at all. In other words, + * conceptually the function "completes" the current row, though its + * use case is to "start" a new row. + */ + void start_new_row (); + /** * Switch auto-fill mode on or off. See the general documentation of this * class for a description of what auto-fill mode does. @@ -818,6 +833,8 @@ namespace internal } } + + template void TableHandler::add_value (const std::string &key, const T value) @@ -834,11 +851,12 @@ void TableHandler::add_value (const std::string &key, { // follow the algorithm given in the introduction to this class // of padding columns as necessary - unsigned int n = 0; + unsigned int max_col_length = 0; for (std::map< std::string, Column >::iterator p = columns.begin(); p != columns.end(); ++p) - n = (n >= p->second.entries.size() ? n : p->second.entries.size()); + max_col_length = std::max(max_col_length, + static_cast(p->second.entries.size())); - while (columns[key].entries.size()+1 < n) + while (columns[key].entries.size()+1 < max_col_length) { columns[key].entries.push_back (internal::TableEntry(T())); internal::TableEntry &entry = columns[key].entries.back(); @@ -851,10 +869,12 @@ void TableHandler::add_value (const std::string &key, columns[key].entries.push_back (internal::TableEntry(value)); internal::TableEntry &entry = columns[key].entries.back(); entry.cache_string(columns[key].scientific, columns[key].precision); - columns[key].max_length = std::max(columns[key].max_length, static_cast(entry.get_cached_string().length())); + columns[key].max_length = std::max(columns[key].max_length, + static_cast(entry.get_cached_string().length())); } + template void TableHandler::Column::save(Archive &ar, const unsigned int /*version*/) const @@ -866,6 +886,8 @@ TableHandler::Column::save(Archive &ar, const unsigned int /*version*/) const & max_length; } + + template void TableHandler::Column::load(Archive &ar, const unsigned int /*version*/) diff --git a/source/base/table_handler.cc b/source/base/table_handler.cc index 78d8cbb655..5dc3b0708f 100644 --- a/source/base/table_handler.cc +++ b/source/base/table_handler.cc @@ -189,6 +189,29 @@ TableHandler::TableHandler() +void TableHandler::start_new_row () +{ + // figure out the longest current column + unsigned int max_col_length = 0; + for (std::map< std::string, Column >::iterator p = columns.begin(); p != columns.end(); ++p) + max_col_length = std::max(max_col_length, + static_cast(p->second.entries.size())); + + + // then pad all columns to that length with empty strings + for (std::map::iterator col=columns.begin(); col!=columns.end(); ++col) + while (col->second.entries.size() < max_col_length) + { + col->second.entries.push_back (internal::TableEntry("")); + internal::TableEntry &entry = col->second.entries.back(); + entry.cache_string(col->second.scientific, col->second.precision); + col->second.max_length = std::max(col->second.max_length, + static_cast(entry.get_cached_string().length())); + } +} + + + void TableHandler::set_auto_fill_mode (const bool state) {