Reference documentation for deal.II version Git cb0bd54b52 2019-09-21 16:31:22 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
table_handler.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/base/table_handler.h>
17 
18 #include <boost/io/ios_state.hpp>
19 
20 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23 
24 
25 DEAL_II_NAMESPACE_OPEN
26 
27 
28 /*---------------------------------------------------------------------*/
29 
30 // inline and template functions
31 namespace internal
32 {
33  double
35  {
36  // we don't quite know the data type in 'value', but
37  // it must be one of the ones in the type list of the
38  // boost::variant. Go through this list and return
39  // the value if this happens to be a number
40  //
41  // first try with int
42  try
43  {
44  return boost::get<int>(value);
45  }
46  catch (...)
47  {}
48 
49 
50  // ... then with unsigned int...
51  try
52  {
53  return boost::get<unsigned int>(value);
54  }
55  catch (...)
56  {}
57 
58  // ... then with unsigned long long int...
59  try
60  {
61  return boost::get<unsigned long long int>(value);
62  }
63  catch (...)
64  {}
65 
66  // ...and finally with double precision:
67  try
68  {
69  return boost::get<double>(value);
70  }
71  catch (...)
72  {
73  Assert(false,
74  ExcMessage("The number stored by this element of the "
75  "table is not a number."))
76  }
77 
78  return 0;
79  }
80 
81  void
82  TableEntry::cache_string(bool scientific, unsigned int precision) const
83  {
84  std::ostringstream ss;
85 
86  ss << std::setprecision(precision);
87 
88  if (scientific)
89  ss.setf(std::ios::scientific, std::ios::floatfield);
90  else
91  ss.setf(std::ios::fixed, std::ios::floatfield);
92 
93  ss << value;
94 
95  cached_value = ss.str();
96  if (cached_value.size() == 0)
97  cached_value = "\"\"";
98  }
99 
100  const std::string &
102  {
103  return cached_value;
104  }
105 
106 
107  namespace Local
108  {
109  // see which type we can cast to, then use this type to create
110  // a default constructed object
111  struct GetDefaultValue : public boost::static_visitor<>
112  {
113  template <typename T>
114  void
115  operator()(T &operand) const
116  {
117  operand = T();
118  }
119  };
120  } // namespace Local
121 
122  TableEntry
124  {
125  TableEntry new_entry = *this;
126  boost::apply_visitor(Local::GetDefaultValue(), new_entry.value);
127 
128  return new_entry;
129  }
130 
131 
132 } // namespace internal
133 
134 /* ------------------------------------------------ */
135 
136 TableHandler::Column::Column(const std::string &tex_caption)
137  : tex_caption(tex_caption)
138  , tex_format("c")
139  , precision(4)
140  , scientific(false)
141  , flag(0)
142  , max_length(0)
143 {}
144 
145 
146 
148  : tex_caption()
149  , tex_format("c")
150  , precision(4)
151  , scientific(false)
152  , flag(0)
153  , max_length(0)
154 {}
155 
156 
157 
158 void
160 {
161  // we should never have a column that is completely
162  // empty and that needs to be padded
163  Assert(entries.size() > 0, ExcInternalError());
164 
165  // add as many elements as necessary
166  while (entries.size() < size)
167  {
168  entries.push_back(entries.back().get_default_constructed_copy());
169  internal::TableEntry &entry = entries.back();
171  max_length =
172  std::max(max_length,
173  static_cast<unsigned int>(entry.get_cached_string().length()));
174  }
175 }
176 
177 
178 void
180 {
181  max_length = 0;
182 
183  for (const auto &entry : entries)
184  {
185  entry.cache_string(this->scientific, this->precision);
186  max_length =
187  std::max(max_length,
188  static_cast<unsigned int>(entry.get_cached_string().length()));
189  }
190 }
191 
192 
193 /*---------------------------------------------------------------------*/
194 
195 
197  : auto_fill_mode(false)
198 {}
199 
200 
201 
202 void
203 TableHandler::declare_column(const std::string &key)
204 {
205  // see if the column already exists; insert it if not
206  Assert(columns.find(key) == columns.end(),
207  ExcMessage("You are trying to declare a column with key <" + key +
208  "> but such a column already exists."));
209 
210  columns.insert(std::make_pair(key, Column(key)));
211  column_order.push_back(key);
212 }
213 
214 
215 
216 void
218 {
219  // figure out the longest current column
220  unsigned int max_col_length = 0;
221  for (const auto &column : columns)
222  max_col_length =
223  std::max(max_col_length,
224  static_cast<unsigned int>(column.second.entries.size()));
225 
226 
227  // then pad all columns to that length with empty strings
228  for (auto &column : columns)
229  while (column.second.entries.size() < max_col_length)
230  {
231  column.second.entries.emplace_back("");
232  internal::TableEntry &entry = column.second.entries.back();
233  entry.cache_string(column.second.scientific, column.second.precision);
234  column.second.max_length =
235  std::max(column.second.max_length,
236  static_cast<unsigned int>(
237  entry.get_cached_string().length()));
238  }
239 }
240 
241 
242 
243 void
245 {
246  auto_fill_mode = state;
247 }
248 
249 
250 void
252  const std::string &superkey)
253 {
254  Assert(columns.count(key), ExcColumnNotExistent(key));
255 
256  if (!supercolumns.count(superkey))
257  {
258  std::pair<std::string, std::vector<std::string>> new_column(
259  superkey, std::vector<std::string>());
260  supercolumns.insert(new_column);
261  // replace key in column_order
262  // by superkey
263  for (auto &column : column_order)
264  if (column == key)
265  {
266  column = superkey;
267  break;
268  }
269  }
270  else
271  {
272  // remove key from column_order
273  const auto order_iter =
274  std::find(column_order.begin(), column_order.end(), key);
275  if (order_iter != column_order.end())
276  column_order.erase(order_iter);
277  }
278 
279  if (supercolumns.count(superkey))
280  {
281  supercolumns[superkey].push_back(key);
282  // By default set the
283  // tex_supercaption to superkey
284  std::pair<std::string, std::string> new_tex_supercaption(superkey,
285  superkey);
286  tex_supercaptions.insert(new_tex_supercaption);
287  }
288  else
289  Assert(false, ExcInternalError());
290 }
291 
292 
293 
294 void
295 TableHandler::set_column_order(const std::vector<std::string> &new_order)
296 {
297  for (const auto &new_column : new_order)
298  {
299  (void)new_column;
300  Assert(supercolumns.count(new_column) || columns.count(new_column),
302  }
303 
304  column_order = new_order;
305 }
306 
307 
308 void
309 TableHandler::set_tex_caption(const std::string &key,
310  const std::string &tex_caption)
311 {
312  Assert(columns.count(key), ExcColumnNotExistent(key));
313  columns[key].tex_caption = tex_caption;
314 }
315 
316 
317 
318 void
319 TableHandler::set_tex_table_caption(const std::string &table_caption)
320 {
321  tex_table_caption = table_caption;
322 }
323 
324 
325 
326 void
327 TableHandler::set_tex_table_label(const std::string &table_label)
328 {
329  tex_table_label = table_label;
330 }
331 
332 
333 
334 void
335 TableHandler::set_tex_supercaption(const std::string &superkey,
336  const std::string &tex_supercaption)
337 {
338  Assert(supercolumns.count(superkey), ExcSuperColumnNotExistent(superkey));
339  Assert(tex_supercaptions.count(superkey), ExcInternalError());
340  tex_supercaptions[superkey] = tex_supercaption;
341 }
342 
343 
344 
345 void
346 TableHandler::set_tex_format(const std::string &key,
347  const std::string &tex_format)
348 {
349  Assert(columns.count(key), ExcColumnNotExistent(key));
350  Assert(tex_format == "l" || tex_format == "c" || tex_format == "r",
351  ExcUndefinedTexFormat(tex_format));
352  columns[key].tex_format = tex_format;
353 }
354 
355 
356 
357 void
358 TableHandler::set_precision(const std::string &key,
359  const unsigned int precision)
360 {
361  Assert(columns.count(key), ExcColumnNotExistent(key));
362  if (columns[key].precision != precision)
363  {
364  columns[key].precision = precision;
365  columns[key].invalidate_cache();
366  }
367 }
368 
369 
370 void
371 TableHandler::set_scientific(const std::string &key, const bool scientific)
372 {
373  Assert(columns.count(key), ExcColumnNotExistent(key));
374  if (columns[key].scientific != scientific)
375  {
376  columns[key].scientific = scientific;
377  columns[key].invalidate_cache();
378  }
379 }
380 
381 
382 void
383 TableHandler::write_text(std::ostream &out, const TextOutputFormat format) const
384 {
385  AssertThrow(out, ExcIO());
386  boost::io::ios_flags_saver restore_flags(out);
387 
388  // first pad the table from below if necessary
389  if (auto_fill_mode == true)
390  {
391  unsigned int max_rows = 0;
392  for (std::map<std::string, Column>::const_iterator p = columns.begin();
393  p != columns.end();
394  ++p)
395  max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
396 
397  for (auto &column : columns)
398  column.second.pad_column_below(max_rows);
399  }
400 
401  std::vector<std::string> sel_columns;
402  get_selected_columns(sel_columns);
403 
404  const unsigned int nrows = n_rows();
405  const unsigned int n_cols = sel_columns.size();
406 
407  // cache the columns and compute the widths of each column for alignment
408  std::vector<const Column *> cols;
409  std::vector<unsigned int> column_widths(n_cols, 0);
410  for (unsigned int j = 0; j < n_cols; ++j)
411  {
412  std::string key = sel_columns[j];
413  const std::map<std::string, Column>::const_iterator col_iter =
414  columns.find(key);
415  Assert(col_iter != columns.end(), ExcInternalError());
416  cols.push_back(&(col_iter->second));
417 
418  column_widths[j] = col_iter->second.max_length;
419  }
420 
421  switch (format)
422  {
423  case org_mode_table:
424  {
425  // write the captions
426  out << "| " << std::left;
427  for (unsigned int j = 0; j < n_cols; ++j)
428  {
429  const std::string &key = sel_columns[j];
430  column_widths[j] =
431  std::max(column_widths[j],
432  static_cast<unsigned int>(key.length()));
433  out << std::setw(column_widths[j]);
434  out << key << " | ";
435  }
436  out << std::endl;
437 
438  // write the body
439  for (unsigned int i = 0; i < nrows; ++i)
440  {
441  out << "| ";
442  for (unsigned int j = 0; j < n_cols; ++j)
443  {
444  const Column &column = *(cols[j]);
445 
446  out << std::setw(column_widths[j]);
447  out << column.entries[i].get_cached_string();
448  out << " | ";
449  }
450  out << '\n';
451  }
452 
453  out << std::flush;
454  return;
455  }
456 
458  {
459  // write the captions
460  for (unsigned int j = 0; j < n_cols; ++j)
461  {
462  const std::string &key = sel_columns[j];
463  out << "# " << j + 1 << ": " << key << '\n';
464  }
465 
466  // write the body
467  for (unsigned int i = 0; i < nrows; ++i)
468  {
469  for (unsigned int j = 0; j < n_cols; ++j)
470  {
471  const Column &column = *(cols[j]);
472 
473  out << column.entries[i].get_cached_string();
474  out << ' ';
475  }
476  out << '\n';
477  }
478 
479  out << std::flush;
480  return;
481  }
482 
484  {
485  // writing the captions for table_with_separate_column_description
486  // means that we ignore supercolumns and output the column
487  // header for each column. enumerate columns starting with 1
488  for (unsigned int j = 0; j < n_cols; ++j)
489  {
490  std::string key = sel_columns[j];
491  out << "# " << j + 1 << ": " << key << '\n';
492  }
493  break;
494  }
495 
496  case table_with_headers:
497  {
498  // This format output supercolumn headers and aligns them centered
499  // over all the columns that belong to it.
500  for (const auto &key : column_order)
501  {
502  unsigned int width = 0;
503  {
504  // compute the width of this column or supercolumn
505  const std::map<std::string,
506  std::vector<std::string>>::const_iterator
507  super_iter = supercolumns.find(key);
508  if (super_iter != supercolumns.end())
509  {
510  const unsigned int n_subcolumns = super_iter->second.size();
511  for (unsigned int k = 0; k < n_subcolumns; ++k)
512  {
513  const std::map<std::string, Column>::const_iterator
514  col_iter = columns.find(super_iter->second[k]);
515  Assert(col_iter != columns.end(), ExcInternalError());
516 
517  width += col_iter->second.max_length;
518  }
519  width += n_subcolumns - 1; // separators between subcolumns
520  }
521  else
522  {
523  const std::map<std::string, Column>::const_iterator
524  col_iter = columns.find(key);
525 
526  width = col_iter->second.max_length;
527  }
528  }
529 
530  // header is longer than the column(s) under it
531  if (width < key.length())
532  {
533  // make the column or the last column in this
534  // supercolumn wide enough
535  std::string colname;
536 
537  const std::map<std::string,
538  std::vector<std::string>>::const_iterator
539  super_iter = supercolumns.find(key);
540  if (super_iter != supercolumns.end())
541  colname = super_iter->second.back();
542  else
543  colname = key;
544 
545  // find column and change output width
546  for (unsigned int i = 0; i < n_cols; ++i)
547  {
548  if (sel_columns[i] == colname)
549  {
550  column_widths[i] += key.length() - width;
551  break;
552  }
553  }
554 
555  width = key.length();
556  }
557 
558  // now write key. try to center it somehow
559  const unsigned int front_padding = (width - key.length()) / 2,
560  rear_padding =
561  (width - key.length()) - front_padding;
562  for (unsigned int i = 0; i < front_padding; ++i)
563  out << ' ';
564  out << key;
565  for (unsigned int i = 0; i < rear_padding; ++i)
566  out << ' ';
567 
568  out << ' ';
569  }
570  out << '\n';
571  break;
572  }
573 
574  default:
575  Assert(false, ExcInternalError());
576  }
577 
578 
579  // finally output the data itself for
580  // table_with_headers or table_with_separate_column_description:
581  for (unsigned int i = 0; i < nrows; ++i)
582  {
583  for (unsigned int j = 0; j < n_cols; ++j)
584  {
585  const Column &column = *(cols[j]);
586  out << std::setw(column_widths[j]);
587  out << column.entries[i].get_cached_string();
588 
589  // pad after this column
590  out << ' ';
591  }
592  out << '\n';
593  }
594  out << std::flush;
595 }
596 
597 
598 void
599 TableHandler::write_tex(std::ostream &out, const bool with_header) const
600 {
601  // TODO[TH]: update code similar to
602  // write_text() to use the cache
603  AssertThrow(out, ExcIO());
604  if (with_header)
605  out << "\\documentclass[10pt]{report}" << std::endl
606  << "\\usepackage{float}" << std::endl
607  << std::endl
608  << std::endl
609  << "\\begin{document}" << std::endl;
610 
611  out << "\\begin{table}[H]" << std::endl
612  << "\\begin{center}" << std::endl
613  << "\\begin{tabular}{|";
614 
615  // first pad the table from below if necessary
616  if (auto_fill_mode == true)
617  {
618  unsigned int max_rows = 0;
619  for (std::map<std::string, Column>::const_iterator p = columns.begin();
620  p != columns.end();
621  ++p)
622  max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
623 
624  for (auto &column : columns)
625  column.second.pad_column_below(max_rows);
626  }
627 
628  std::vector<std::string> sel_columns;
629  get_selected_columns(sel_columns);
630 
631  // write the column formats
632  for (const auto &key : column_order)
633  {
634  // avoid `supercolumns[key]'
635  const std::map<std::string, std::vector<std::string>>::const_iterator
636  super_iter = supercolumns.find(key);
637 
638  if (super_iter != supercolumns.end())
639  {
640  const unsigned int n_subcolumns = super_iter->second.size();
641  for (unsigned int k = 0; k < n_subcolumns; ++k)
642  {
643  // avoid `columns[supercolumns[key]]'
644  const std::map<std::string, Column>::const_iterator col_iter =
645  columns.find(super_iter->second[k]);
646  Assert(col_iter != columns.end(), ExcInternalError());
647 
648  out << col_iter->second.tex_format << "|";
649  }
650  }
651  else
652  {
653  // avoid `columns[key]';
654  const std::map<std::string, Column>::const_iterator col_iter =
655  columns.find(key);
656  Assert(col_iter != columns.end(), ExcInternalError());
657  out << col_iter->second.tex_format << "|";
658  }
659  }
660  out << "} \\hline" << std::endl;
661 
662  // write the caption line of the table
663 
664  for (unsigned int j = 0; j < column_order.size(); ++j)
665  {
666  std::string key = column_order[j];
667  const std::map<std::string, std::vector<std::string>>::const_iterator
668  super_iter = supercolumns.find(key);
669 
670  if (super_iter != supercolumns.end())
671  {
672  const unsigned int n_subcolumns = super_iter->second.size();
673  // avoid use of `tex_supercaptions[key]'
674  std::map<std::string, std::string>::const_iterator
675  tex_super_cap_iter = tex_supercaptions.find(key);
676  out << std::endl
677  << "\\multicolumn{" << n_subcolumns << "}{|c|}{"
678  << tex_super_cap_iter->second << "}";
679  }
680  else
681  {
682  // col_iter->second=columns[col];
683  const std::map<std::string, Column>::const_iterator col_iter =
684  columns.find(key);
685  Assert(col_iter != columns.end(), ExcInternalError());
686  out << col_iter->second.tex_caption;
687  }
688  if (j < column_order.size() - 1)
689  out << " & ";
690  }
691  out << "\\\\ \\hline" << std::endl;
692 
693  // write the n rows
694  const unsigned int nrows = n_rows();
695  for (unsigned int i = 0; i < nrows; ++i)
696  {
697  const unsigned int n_cols = sel_columns.size();
698 
699  for (unsigned int j = 0; j < n_cols; ++j)
700  {
701  std::string key = sel_columns[j];
702  // avoid `column[key]'
703  const std::map<std::string, Column>::const_iterator col_iter =
704  columns.find(key);
705  Assert(col_iter != columns.end(), ExcInternalError());
706 
707  const Column &column = col_iter->second;
708 
709  out << std::setprecision(column.precision);
710 
711  if (col_iter->second.scientific)
712  out.setf(std::ios::scientific, std::ios::floatfield);
713  else
714  out.setf(std::ios::fixed, std::ios::floatfield);
715 
716  out << column.entries[i].value;
717 
718  if (j < n_cols - 1)
719  out << " & ";
720  }
721  out << "\\\\ \\hline" << std::endl;
722  }
723 
724  out << "\\end{tabular}" << std::endl << "\\end{center}" << std::endl;
725  if (!tex_table_caption.empty())
726  out << "\\caption{" << tex_table_caption << "}" << std::endl;
727  if (!tex_table_label.empty())
728  out << "\\label{" << tex_table_label << "}" << std::endl;
729  out << "\\end{table}" << std::endl;
730  if (with_header)
731  out << "\\end{document}" << std::endl;
732 }
733 
734 
735 void
737 {
738  columns.clear();
739  supercolumns.clear();
740  column_order.clear();
741  tex_supercaptions.clear();
742 
743  tex_table_label.clear();
744  tex_table_caption.clear();
745 }
746 
747 
748 unsigned int
750 {
751  if (columns.size() == 0)
752  return 0;
753 
754  std::map<std::string, Column>::const_iterator col_iter = columns.begin();
755  unsigned int n = col_iter->second.entries.size();
756 
757 #ifdef DEBUG
758  std::string first_name = col_iter->first;
759  for (++col_iter; col_iter != columns.end(); ++col_iter)
760  Assert(col_iter->second.entries.size() == n,
762  col_iter->first, col_iter->second.entries.size(), first_name, n));
763 #endif
764 
765  return n;
766 }
767 
768 
769 void
770 TableHandler::get_selected_columns(std::vector<std::string> &sel_columns) const
771 {
772  sel_columns.clear();
773 
774  for (const auto &key : column_order)
775  {
776  const std::map<std::string, std::vector<std::string>>::const_iterator
777  super_iter = supercolumns.find(key);
778 
779  if (super_iter != supercolumns.end())
780  {
781  // i.e. key is a supercolumn key
782  const unsigned int n_subcolumns = super_iter->second.size();
783  for (unsigned int k = 0; k < n_subcolumns; ++k)
784  {
785  const std::string subkey = super_iter->second[k];
786  Assert(columns.count(subkey), ExcInternalError());
787  sel_columns.push_back(subkey);
788  }
789  }
790  else
791  {
792  Assert(columns.count(key), ExcInternalError());
793  // i.e. key is a column key
794  sel_columns.push_back(key);
795  }
796  }
797 }
798 
799 
800 void
802 {
803  // Figure out what is the correct (max) length of the columns
804  // so that we "shave" one off.
805  std::vector<internal::TableEntry>::size_type n = 0;
806  for (const auto &column : columns)
807  n = std::max(n, column.second.entries.size());
808 
809  // shave the top most element
810  if (n != 0)
811  for (auto &column : columns)
812  if (column.second.entries.size() == n)
813  column.second.entries.pop_back();
814 }
815 
816 
817 DEAL_II_NAMESPACE_CLOSE
std::map< std::string, std::string > tex_supercaptions
void set_precision(const std::string &key, const unsigned int precision)
void declare_column(const std::string &key)
unsigned int max_length
static ::ExceptionBase & ExcIO()
void set_tex_supercaption(const std::string &superkey, const std::string &tex_supercaption)
std::map< std::string, Column > columns
static ::ExceptionBase & ExcColumnOrSuperColumnNotExistent(std::string arg1)
const std::string & get_cached_string() const
void set_tex_caption(const std::string &key, const std::string &tex_caption)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
void cache_string(bool scientific, unsigned int precision) const
void add_column_to_supercolumn(const std::string &key, const std::string &superkey)
void set_tex_format(const std::string &key, const std::string &format="c")
void get_selected_columns(std::vector< std::string > &sel_columns) const
static ::ExceptionBase & ExcSuperColumnNotExistent(std::string arg1)
static ::ExceptionBase & ExcWrongNumberOfDataEntries(std::string arg1, int arg2, std::string arg3, int arg4)
std::vector< std::string > column_order
std::string tex_format
static ::ExceptionBase & ExcMessage(std::string arg1)
std::string tex_table_caption
double get_numeric_value() const
TableEntry get_default_constructed_copy() const
void write_text(std::ostream &out, const TextOutputFormat format=table_with_headers) const
std::string cached_value
#define Assert(cond, exc)
Definition: exceptions.h:1407
void set_tex_table_label(const std::string &table_label)
unsigned int precision
void write_tex(std::ostream &file, const bool with_header=true) const
void clear_current_row()
static ::ExceptionBase & ExcUndefinedTexFormat(std::string arg1)
void set_column_order(const std::vector< std::string > &new_order)
void set_scientific(const std::string &key, const bool scientific)
void pad_column_below(const unsigned int length)
static ::ExceptionBase & ExcColumnNotExistent(std::string arg1)
std::vector< internal::TableEntry > entries
unsigned int n_rows() const
void start_new_row()
std::string tex_caption
void set_tex_table_caption(const std::string &table_caption)
std::map< std::string, std::vector< std::string > > supercolumns
std::string tex_table_label
static ::ExceptionBase & ExcInternalError()
void set_auto_fill_mode(const bool state)