/
benchmark_table.hpp
225 lines (198 loc) · 5.8 KB
/
benchmark_table.hpp
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
/* CirKit: A circuit toolkit
* Copyright (C) 2009-2015 University of Bremen
* Copyright (C) 2015-2017 EPFL
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file benchmark_table.hpp
*
* @brief Data-structures for producing benchmark tables
*
* @author Mathias Soeken
* @since 2.0
*/
#ifndef BENCHMARK_TABLE_HPP
#define BENCHMARK_TABLE_HPP
#include <iostream>
#include <iomanip>
#include <boost/format.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/range/counting_range.hpp>
namespace cirkit
{
/* this is to format a cell */
using length_t = boost::optional<unsigned>;
template<typename T>
std::string print_cell( const T& value, const length_t& length = length_t() )
{
assert( 0 );
}
template<>
std::string print_cell<>( const std::string& value, const length_t& length )
{
if ( length )
{
return boost::str( boost::format( "%-" + std::to_string( *length ) + "s" ) % value );
}
else
{
return value;
}
}
template<>
std::string print_cell<>( const int& value, const length_t& length )
{
if ( length )
{
return boost::str( boost::format( "%" + std::to_string( *length ) + "d" ) % value );
}
else
{
return std::to_string( value );
}
}
template<>
std::string print_cell<>( const unsigned& value, const length_t& length )
{
if ( length )
{
return boost::str( boost::format( "%" + std::to_string( *length ) + "d" ) % value );
}
else
{
return std::to_string( value );
}
}
template<>
std::string print_cell<>( const double& value, const length_t& length )
{
if ( length )
{
return boost::str( boost::format( "%" + std::to_string( *length ) + ".2f" ) % value );
}
else
{
return boost::str( boost::format( "%.2f" ) % value );
}
}
template<>
std::string print_cell<>( const boost::optional<unsigned>& value, const length_t& length )
{
std::string timeout = "T/O";
return value ? print_cell( *value, length ) : print_cell( timeout );
}
/* this is to display one row of the result set */
template<class TypleT, int N, typename... Arguments> struct print_row_t;
template<class TupleT, int N, class T, typename... Arguments> struct print_row_t<TupleT, N, T, Arguments...>
{
static void print( const TupleT& t, const std::vector<unsigned>& column_lengths )
{
std::cout << "| " << print_cell( std::get<N>( t ), column_lengths[N] ) << " ";
print_row_t<TupleT, N + 1, Arguments...>::print( t, column_lengths );
}
};
template<class TupleT, int N, class T> struct print_row_t<TupleT, N, T>
{
static void print( const TupleT& t, const std::vector<unsigned>& column_lengths )
{
std::cout << "| " << print_cell( std::get<N>( t ), column_lengths[N] ) << " |" << std::endl;
}
};
template<class TupleT, typename... Arguments>
void print_row( const TupleT& t, const std::vector<unsigned>& column_lengths )
{
print_row_t<TupleT, 0, Arguments...>::print( t, column_lengths );
};
/* the actual benchmark table */
template<class... Arguments>
class benchmark_table
{
public:
using benchmark = std::tuple<Arguments...>;
benchmark_table( std::initializer_list<std::string> column_names, bool verbose = false ) : verbose( verbose )
{
using boost::adaptors::transformed;
boost::push_back( _column_names, column_names );
boost::push_back( lengths, _column_names | transformed( []( const std::string& s ) { return s.size(); } ) );
}
template<typename T>
void add_length( const T& value )
{
unsigned l = print_cell( value ).size();
if ( l > lengths[length_i] )
{
lengths[length_i] = l;
}
++length_i;
}
template<typename T>
void compute_lengths(const T& value)
{
add_length( value );
}
template<typename U, typename... T>
void compute_lengths(const U& head, const T&... tail)
{
add_length( head );
compute_lengths(tail...);
}
template<class... Args>
void add(Args&&... args)
{
length_i = 0u;
compute_lengths( args... );
results.push_back(std::make_tuple(std::forward<Args>(args)...));
if ( verbose )
{
print_row<benchmark, Arguments...>( results.back(), lengths );
}
}
void print() const
{
for ( unsigned n : boost::counting_range( 0u, (unsigned)_column_names.size() ) )
{
std::cout.setf( std::ios::left );
std::cout << "| " << std::setw( lengths[n] ) << _column_names[n] << " ";
}
std::cout << "|" << std::endl;
for ( const auto& result : results )
{
print_row<benchmark, Arguments...>( result, lengths );
}
}
private:
bool verbose = false;
std::vector<std::string> _column_names;
std::vector<benchmark> results;
std::vector<unsigned> lengths;
unsigned length_i;
};
}
#endif
// Local Variables:
// c-basic-offset: 2
// eval: (c-set-offset 'substatement-open 0)
// eval: (c-set-offset 'innamespace 0)
// End: