SPH
Table.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
11 
12 class Table {
13 private:
14  using Row = Array<std::string>;
15 
16  Array<Row> rows;
17 
18  struct {
21  } params;
22 
23 public:
28  Table(const Size colSep = 1, const Size minColWidth = 5)
29  : params{ colSep, minColWidth } {}
30 
35  void setCell(const Size colIdx, const Size rowIdx, std::string text) {
36  // extend rows
37  for (Size i = rows.size(); i <= rowIdx; ++i) {
38  rows.emplaceBack(this->columnCnt());
39  }
40  // extend columns
41  if (colIdx >= this->columnCnt()) {
42  for (Row& row : rows) {
43  row.resize(colIdx + 1);
44  }
45  }
46  rows[rowIdx][colIdx] = std::move(text);
47  }
48 
49  Size rowCnt() const {
50  return rows.size();
51  }
52 
53  Size columnCnt() const {
54  return rows.empty() ? 0 : rows[0].size();
55  }
56 
58  std::string toString() const {
59  if (rows.empty()) {
60  return std::string();
61  }
62  Array<Size> colWidths(this->columnCnt());
63  colWidths.fill(0);
64  for (const Row& row : rows) {
65  for (Size colIdx = 0; colIdx < row.size(); ++colIdx) {
66  colWidths[colIdx] = max(colWidths[colIdx], Size(row[colIdx].size()));
67  }
68  }
69  for (Size colIdx = 0; colIdx < rows[0].size(); ++colIdx) {
70  colWidths[colIdx] = max(colWidths[colIdx] + params.colSep, params.minColWidth);
71  }
72  std::stringstream ss;
73  for (const Row& row : rows) {
74  for (Size colIdx = 0; colIdx < row.size(); ++colIdx) {
75  ss << std::setw(colWidths[colIdx]) << row[colIdx];
76  }
77  ss << std::endl;
78  }
79  return ss.str();
80  }
81 };
82 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
#define NAMESPACE_SPH_END
Definition: Object.h:12
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Definition: Array.h:332
void fill(const T &t)
Sets all elements of the array to given value.
Definition: Array.h:187
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
Definition: Table.h:12
Table(const Size colSep=1, const Size minColWidth=5)
Creates an empty table.
Definition: Table.h:28
Size columnCnt() const
Definition: Table.h:53
Size rowCnt() const
Definition: Table.h:49
Size minColWidth
Definition: Table.h:20
std::string toString() const
Creates the text representation of the table.
Definition: Table.h:58
Size colSep
Definition: Table.h:19
void setCell(const Size colIdx, const Size rowIdx, std::string text)
Sets the text in given cell.
Definition: Table.h:35