|
17 | 17 | #include "shambase/aliases_float.hpp" |
18 | 18 | #include "shambase/aliases_int.hpp" |
19 | 19 | #include "shambase/string.hpp" |
| 20 | +#include "shambase/tabulate.hpp" |
20 | 21 | #include "shamalgs/collective/reduction.hpp" |
21 | 22 | #include "shamcomm/collectives.hpp" |
22 | 23 | #include "shamcomm/logs.hpp" |
|
25 | 26 | #include <string> |
26 | 27 | #include <variant> |
27 | 28 |
|
28 | | -namespace shammodels { |
29 | | - |
30 | | - template<size_t cols_count> |
31 | | - struct table { |
32 | | - struct rule {}; |
33 | | - struct double_rule {}; |
34 | | - struct rulled_data { |
35 | | - std::array<std::string, cols_count> colnames; |
36 | | - }; |
37 | | - enum positionning { |
38 | | - left, |
39 | | - right, |
40 | | - center, |
41 | | - }; |
42 | | - struct data { |
43 | | - std::array<std::string, cols_count> cols; |
44 | | - positionning position; |
45 | | - }; |
46 | | - |
47 | | - std::vector<std::variant<rule, double_rule, rulled_data, data>> table_lines; |
48 | | - |
49 | | - void add_rule() { table_lines.push_back(rule{}); } |
50 | | - void add_double_rule() { table_lines.push_back(double_rule{}); } |
51 | | - void add_rulled_data(std::array<std::string, cols_count> colnames) { |
52 | | - table_lines.push_back(rulled_data{colnames}); |
53 | | - } |
54 | | - void add_data(std::array<std::string, cols_count> cols, positionning position) { |
55 | | - table_lines.push_back(data{cols, position}); |
56 | | - } |
57 | | - |
58 | | - std::array<size_t, cols_count> compute_widths() { |
59 | | - std::array<size_t, cols_count> widths{}; |
60 | | - for (auto &line : table_lines) { |
61 | | - if (data *data_line = std::get_if<data>(&line)) { |
62 | | - for (u32 i = 0; i < cols_count; i++) { |
63 | | - widths[i] = std::max(widths[i], data_line->cols[i].size()); |
64 | | - } |
65 | | - } else if (rulled_data *head_and_ruller_line = std::get_if<rulled_data>(&line)) { |
66 | | - for (u32 i = 0; i < cols_count; i++) { |
67 | | - widths[i] = std::max(widths[i], head_and_ruller_line->colnames[i].size()); |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - return widths; |
72 | | - } |
73 | | - |
74 | | - std::string render() { |
75 | | - |
76 | | - std::array<size_t, cols_count> widths = compute_widths(); |
77 | | - |
78 | | - std::string print = ""; |
79 | | - for (auto &line : table_lines) { |
80 | | - if (data *data_line = std::get_if<data>(&line)) { |
81 | | - print += "\n|"; |
82 | | - for (u32 i = 0; i < cols_count; i++) { |
83 | | - if (data_line->position == left) { |
84 | | - print += shambase::format(" {:<{}} |", data_line->cols[i], widths[i]); |
85 | | - } else if (data_line->position == right) { |
86 | | - print += shambase::format(" {:>{}} |", data_line->cols[i], widths[i]); |
87 | | - } else if (data_line->position == center) { |
88 | | - print += shambase::format(" {:^{}} |", data_line->cols[i], widths[i]); |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - } else if (rulled_data *head_and_ruller_line = std::get_if<rulled_data>(&line)) { |
93 | | - std::string tmp = "+"; |
94 | | - for (u32 i = 0; i < cols_count; i++) { |
95 | | - tmp += shambase::format( |
96 | | - " {:^{}} +", head_and_ruller_line->colnames[i], widths[i]); |
97 | | - } |
98 | | - |
99 | | - auto neigh_char_is_good = [&](char c) -> bool { |
100 | | - return c == ' ' || c == '-' || c == '<' || c == '>' || c == '+'; |
101 | | - }; |
102 | | - |
103 | | - std::vector<bool> set_to_space = std::vector<bool>(tmp.size(), false); |
104 | | - // if the next and previous chars are space and i am a space then set me to true |
105 | | - for (size_t i = 1; i < tmp.size() - 1; i++) { |
106 | | - if (tmp[i] == ' ' && neigh_char_is_good(tmp[i - 1]) |
107 | | - && neigh_char_is_good(tmp[i + 1])) { |
108 | | - set_to_space[i] = true; |
109 | | - } |
110 | | - } |
111 | | - // replace the spaces by '-' |
112 | | - for (size_t i = 0; i < tmp.size() - 1; i++) { |
113 | | - if (set_to_space[i]) { |
114 | | - tmp[i] = '-'; |
115 | | - } |
116 | | - } |
117 | | - print += "\n" + tmp; |
118 | | - } else if (rule *rule_line = std::get_if<rule>(&line)) { |
119 | | - print += "\n+"; |
120 | | - for (u32 i = 0; i < cols_count; i++) { |
121 | | - print += shambase::format( |
122 | | - "-{:<{}}-+", std::string(widths[i], '-'), widths[i]); |
123 | | - } |
124 | | - } else if (double_rule *double_rule_line = std::get_if<double_rule>(&line)) { |
125 | | - print += "\n+"; |
126 | | - for (u32 i = 0; i < cols_count; i++) { |
127 | | - print += shambase::format( |
128 | | - "={:<{}}=+", std::string(widths[i], '='), widths[i]); |
129 | | - } |
130 | | - } |
131 | | - } |
132 | | - return print; |
133 | | - } |
134 | | - }; |
135 | | -} // namespace shammodels |
136 | | - |
137 | 29 | std::string shammodels::report_perf_timestep( |
138 | 30 | f64 rate, |
139 | 31 | u64 nobj, |
@@ -176,7 +68,7 @@ std::string shammodels::report_perf_timestep( |
176 | 68 |
|
177 | 69 | static constexpr u32 cols_count = 9; |
178 | 70 |
|
179 | | - using Table = table<cols_count>; |
| 71 | + using Table = shambase::table<cols_count>; |
180 | 72 |
|
181 | 73 | Table table; |
182 | 74 |
|
|
0 commit comments