-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariadicTable.h
382 lines (325 loc) · 10.5 KB
/
VariadicTable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#pragma once
#include <iostream>
#include <iomanip>
#include <ios>
#include <vector>
#include <tuple>
#include <type_traits>
#include <cassert>
#include <cmath>
#include <algorithm>
#include <limits>
/**
* Used to specify the column format
*/
enum class VariadicTableColumnFormat
{
AUTO,
SCIENTIFIC,
FIXED,
PERCENT
};
/**
* A class for "pretty printing" a table of data.
*
* Requries C++11 (and nothing more)
*
* It's templated on the types that will be in each column
* (all values in a column must have the same type)
*
* For instance, to use it with data that looks like: "Fred", 193.4, 35, "Sam"
* with header names: "Name", "Weight", "Age", "Brother"
*
* You would invoke the table like so:
* VariadicTable<std::string, double, int, std::string> vt("Name", "Weight", "Age", "Brother");
*
* Then add the data to the table:
* vt.addRow("Fred", 193.4, 35, "Sam");
*
* And finally print it:
* vt.print();
*/
template <class... Ts>
class VariadicTable
{
public:
/// The type stored for each row
typedef std::tuple<Ts...> DataTuple;
/**
* Construct the table with headers
*
* @param headers The names of the columns
* @param static_column_size The size of columns that can't be found automatically
*/
VariadicTable(std::vector<std::string> headers,
unsigned int static_column_size = 0,
unsigned int cell_padding = 1)
: _headers(headers),
_num_columns(std::tuple_size<DataTuple>::value),
_static_column_size(static_column_size),
_cell_padding(cell_padding)
{
assert(headers.size() == _num_columns);
}
/**
* Add a row of data
*
* Easiest to use like:
* table.addRow({data1, data2, data3});
*
* @param data A Tuple of data to add
*/
void addRow(Ts... entries) { _data.emplace_back(std::make_tuple(entries...)); }
/**
* Pretty print the table of data
*/
template <typename StreamType>
void print(StreamType & stream)
{
size_columns();
// Start computing the total width
// First - we will have _num_columns + 1 "|" characters
unsigned int total_width = _num_columns + 1;
// Now add in the size of each colum
for (auto & col_size : _column_sizes)
total_width += col_size + (2 * _cell_padding);
// Print out the top line
stream << std::string(total_width, '-') << "\n";
// Print out the headers
stream << "|";
for (unsigned int i = 0; i < _num_columns; i++)
{
// Must find the center of the column
auto half = _column_sizes[i] / 2;
half -= _headers[i].size() / 2;
stream << std::string(_cell_padding, ' ') << std::setw(_column_sizes[i]) << std::left
<< std::string(half, ' ') + _headers[i] << std::string(_cell_padding, ' ') << "|";
}
stream << "\n";
// Print out the line below the header
stream << std::string(total_width, '-') << "\n";
// Now print the rows of the table
for (auto & row : _data)
{
stream << "|";
print_each(row, stream);
stream << "\n";
}
// Print out the line below the header
stream << std::string(total_width, '-') << "\n";
}
/**
* Set how to format numbers for each column
*
* Note: this is ignored for std::string columns
*
* @column_format The format for each column: MUST be the same length as the number of columns.
*/
void setColumnFormat(const std::vector<VariadicTableColumnFormat> & column_format)
{
assert(column_format.size() == std::tuple_size<DataTuple>::value);
_column_format = column_format;
}
/**
* Set how many digits of precision to show for floating point numbers
*
* Note: this is ignored for std::string columns
*
* @column_format The precision for each column: MUST be the same length as the number of columns.
*/
void setColumnPrecision(const std::vector<int> & precision)
{
assert(precision.size() == std::tuple_size<DataTuple>::value);
_precision = precision;
}
protected:
// Just some handy typedefs for the following two functions
typedef decltype(&std::right) right_type;
typedef decltype(&std::left) left_type;
// Attempts to figure out the correct justification for the data
// If it's a floating point value
template <typename T,
typename = typename std::enable_if<
std::is_arithmetic<typename std::remove_reference<T>::type>::value>::type>
static right_type justify(int /*firstchoice*/)
{
return std::right;
}
// Otherwise
template <typename T>
static left_type justify(long /*secondchoice*/)
{
return std::left;
}
/**
* These three functions print out each item in a Tuple into the table
*
* Original Idea From From https://stackoverflow.com/a/26908596
*
* BTW: This would all be a lot easier with generic lambdas
* there would only need to be one of this sequence and then
* you could pass in a generic lambda. Unfortunately, that's C++14
*/
/**
* This ends the recursion
*/
template <typename TupleType, typename StreamType>
void print_each(TupleType &&,
StreamType & /*stream*/,
std::integral_constant<
size_t,
std::tuple_size<typename std::remove_reference<TupleType>::type>::value>)
{
}
/**
* This gets called on each item
*/
template <std::size_t I,
typename TupleType,
typename StreamType,
typename = typename std::enable_if<
I != std::tuple_size<typename std::remove_reference<TupleType>::type>::value>::type>
void print_each(TupleType && t, StreamType & stream, std::integral_constant<size_t, I>)
{
auto & val = std::get<I>(t);
// Set the precision
if (!_precision.empty())
{
assert(_precision.size() ==
std::tuple_size<typename std::remove_reference<TupleType>::type>::value);
stream << std::setprecision(_precision[I]);
}
// Set the format
if (!_column_format.empty())
{
assert(_column_format.size() ==
std::tuple_size<typename std::remove_reference<TupleType>::type>::value);
if (_column_format[I] == VariadicTableColumnFormat::SCIENTIFIC)
stream << std::scientific;
if (_column_format[I] == VariadicTableColumnFormat::FIXED)
stream << std::fixed;
if (_column_format[I] == VariadicTableColumnFormat::PERCENT)
stream << std::fixed << std::setprecision(2);
}
stream << std::string(_cell_padding, ' ') << std::setw(_column_sizes[I])
<< justify<decltype(val)>(0) << val << std::string(_cell_padding, ' ') << "|";
// Unset the format
if (!_column_format.empty())
{
// Because "stream << std::defaultfloat;" won't compile with old GCC or Clang
stream.unsetf(std::ios_base::floatfield);
}
// Recursive call to print the next item
print_each(std::forward<TupleType>(t), stream, std::integral_constant<size_t, I + 1>());
}
/**
* his is what gets called first
*/
template <typename TupleType, typename StreamType>
void print_each(TupleType && t, StreamType & stream)
{
print_each(std::forward<TupleType>(t), stream, std::integral_constant<size_t, 0>());
}
/**
* Try to find the size the column will take up
*
* If the datatype has a size() member... let's call it
*/
template <class T>
size_t sizeOfData(const T & data, decltype(((T *)nullptr)->size()) * /*dummy*/ = nullptr)
{
return data.size();
}
/**
* Try to find the size the column will take up
*
* If the datatype is an integer - let's get it's length
*/
template <class T>
size_t sizeOfData(const T & data,
typename std::enable_if<std::is_integral<T>::value>::type * /*dummy*/ = nullptr)
{
if (data == 0)
return 1;
return data < 0 ? std::log10(data * (-1)) + 1 : std::log10(data) + 1;
}
/**
* If it doesn't... let's just use a statically set size
*/
size_t sizeOfData(...) { return _static_column_size; }
/**
* These three functions iterate over the Tuple, find the printed size of each element and set it
* in a vector
*/
/**
* End the recursion
*/
template <typename TupleType>
void size_each(TupleType &&,
std::vector<size_t> & /*sizes*/,
std::integral_constant<
size_t,
std::tuple_size<typename std::remove_reference<TupleType>::type>::value>)
{
}
/**
* Recursively called for each element
*/
template <std::size_t I,
typename TupleType,
typename = typename std::enable_if<
I != std::tuple_size<typename std::remove_reference<TupleType>::type>::value>::type>
void size_each(TupleType && t, std::vector<size_t> & sizes, std::integral_constant<size_t, I>)
{
sizes[I] = sizeOfData(std::get<I>(t));
// Override for Percent
if (!_column_format.empty())
if (_column_format[I] == VariadicTableColumnFormat::PERCENT)
sizes[I] = 6; // 100.00
// Continue the recursion
size_each(std::forward<TupleType>(t), sizes, std::integral_constant<size_t, I + 1>());
}
/**
* The function that is actually called that starts the recursion
*/
template <typename TupleType>
void size_each(TupleType && t, std::vector<size_t> & sizes)
{
size_each(std::forward<TupleType>(t), sizes, std::integral_constant<size_t, 0>());
}
/**
* Finds the size each column should be and set it in _column_sizes
*/
void size_columns()
{
_column_sizes.resize(_num_columns);
// Temporary for querying each row
std::vector<size_t> column_sizes(_num_columns);
// Start with the size of the headers
for (unsigned int i = 0; i < _num_columns; i++)
_column_sizes[i] = _headers[i].size();
// Grab the size of each entry of each row and see if it's bigger
for (auto & row : _data)
{
size_each(row, column_sizes);
for (unsigned int i = 0; i < _num_columns; i++)
_column_sizes[i] = std::max(_column_sizes[i], column_sizes[i]);
}
}
/// The column headers
std::vector<std::string> _headers;
/// Number of columns in the table
unsigned int _num_columns;
/// Size of columns that we can't get the size of
unsigned int _static_column_size;
/// Size of the cell padding
unsigned int _cell_padding;
/// The actual data
std::vector<DataTuple> _data;
/// Holds the printable width of each column
std::vector<size_t> _column_sizes;
/// Column Format
std::vector<VariadicTableColumnFormat> _column_format;
/// Precision For each column
std::vector<int> _precision;
};