41 const_iterator
end()
const {
50 template <
typename T_Other>
52 return m_size == other_range.m_size;
96 template <
typename T_Other>
98 return m_rows == other_range.m_size;
104 template <
class T_Stream,
typename T_Counter>
108 const std::string &label_prefix,
110 const std::size_t min_label_width,
111 const bool dump_footer,
112 const bool separate_last_row,
113 const unsigned int precision) {
115 std::size_t max_size =min_label_width;
119 const std::size_t total_size =max_size+3+2*2+
column_width;
120 std::string
line(total_size,
'-');
121 std::array<std::size_t,3> vertical_line_pos{0
u, max_size+3,
line.size()-1};
122 for (std::size_t
pos : vertical_line_pos) {
128 auto default_precision =
out.precision();
129 for (
const T_Counter &
a :
counter) {
130 if (separate_last_row &&
idx+1 ==
label.size()) {
134 out <<
"| " << (label_prefix.empty() ? std::left : std::right)
135 << std::setw(label_prefix.size()) << (
idx==0 ? label_prefix : empty)
136 << std::setw(max_size-label_prefix.size()) <<
label[
idx] << std::right
138 <<
" | " << std::setw(
column_width) <<
a <<
" |" << std::endl;
141 out << std::setprecision(default_precision);
152 template <
class T_Stream,
typename T_Counter>
157 const std::string &top_left_label,
158 const std::string &label_prefix,
160 const std::size_t min_label_width,
161 const bool dump_header,
162 const bool dump_footer,
163 const bool separate_last_row,
164 const std::vector<unsigned int> &precision) {
165 if (
counter && row_label && column_label
166 &&
counter.equalSize(row_label)
168 std::size_t max_size =
std::max(top_left_label.size(),
static_cast<std::size_t
>(min_label_width));
169 for (
const std::string &
name : row_label ) {
170 max_size =
std::max(max_size,
name.size() + label_prefix.size());
173 for (
const std::string &
name : column_label ) {
176 std::size_t total_size =max_size+2*2;
177 for (std::size_t column_i=0; column_i<column_label.size(); ++column_i) {
178 total_size += the_width + 3;
180 std::string
line(total_size,
'-');
184 for (std::size_t column_i=0; column_i<column_label.size(); ++column_i) {
186 pos += the_width + 3;
190 out <<
line << std::endl <<
"| " << std::setw(max_size) << top_left_label <<
" |" << std::left;
191 for (
const std::string &
header : column_label ) {
192 out <<
" " << std::setw(the_width) <<
header <<
" |";
194 out << std::right << std::endl;
199 auto default_precision =
out.precision();
201 if (separate_last_row &&
idx+1 == row_label.size()) {
204 assert(
idx < row_label.size());
205 out <<
"| " << (label_prefix.empty() ? std::left : std::right)
206 << std::setw(label_prefix.size()) << (
idx==0 ? label_prefix : empty)
207 << std::setw(max_size-label_prefix.size()) << row_label[
idx] << std::right <<
" |";
208 unsigned int col_i=0;
209 for (
const T_Counter &
a : a_row) {
211 << std::setprecision( (col_i < precision.size()
215 << std::setw(the_width) <<
a <<
" |";
221 out << std::setprecision(default_precision);
230 template <
typename T>
251 template <
typename T>
274 template <
typename T_index,
class T_
string>
276 std::initializer_list<std::pair<T_index, T_string> > a_list)
278 std::vector<std::string>
labels;
279 labels.resize( n_entries );
280 if (a_list.size() != n_entries) {
281 throw std::logic_error(
"Expected number of entries and elements in the initializer lists do not match.");
283 for (
auto elm : a_list) {
284 labels.at(elm.first) = std::move(elm.second);
289 template<
class T_Collection>
291 std::size_t max_width=0
u;
292 for (
const auto &elm : col ) {
293 max_width =
std::max( max_width, elm.size());
300 [[maybe_unused]]
const std::size_t sub_categories,
301 [[maybe_unused]]
const std::size_t n_counter) {
305 [[maybe_unused]]
const std::size_t sub_categories,
306 [[maybe_unused]]
const std::size_t n_counter) {
310 [[maybe_unused]]
const std::size_t sub_categories,
311 [[maybe_unused]]
const std::size_t n_counter) {
321 template<
typename T_Output,
typename T_Input, const std::
size_t N>
323 const std::size_t sub_categories,
324 const std::vector< std::array<T_Input, N> > &input_counts) {
325 if (
categories*sub_categories!= input_counts.size()) {
326 std::stringstream
msg;
327 msg <<
"Category dimensions (" <<
categories <<
" * " << sub_categories <<
"="
328 << (
categories * sub_categories) <<
") and input counter container size "
329 << input_counts.size() <<
" do not match.";
330 throw std::logic_error(
msg.str());
332 std::vector<T_Output> output_counts;
333 output_counts.resize((
categories+1) * (sub_categories+1) *
N);
338 for (std::size_t sub_category_i=0;
339 sub_category_i < sub_categories;
341 for (std::size_t category_i=0; category_i<
categories; ++category_i) {
342 std::size_t src_idx = category_i * sub_categories + sub_category_i;
343 std::size_t dest_idx_base = sub_category_i * sub_category_stride + 0 * counter_stride;
344 std::size_t dest_idx_project_categories_base = dest_idx_base;
345 dest_idx_base += category_i * category_stride;
346 dest_idx_project_categories_base +=
categories * category_stride;
348 for (std::size_t counter_i=0; counter_i<
N; ++counter_i) {
349 std::size_t dest_idx=dest_idx_base + counter_i * counter_stride;
350 assert( src_idx < input_counts.size() && counter_i < input_counts[src_idx].size());
351 assert( dest_idx < output_counts.size());
352 output_counts[dest_idx] = input_counts[src_idx][counter_i];
353 assert( dest_idx_project_categories_base + counter_i * counter_stride < output_counts.size());
354 output_counts[dest_idx_project_categories_base + counter_i * counter_stride] += output_counts[dest_idx];
359 for (std::size_t category_i=0; category_i<=
categories; ++category_i) {
360 for (std::size_t counter_i=0; counter_i<
N; ++counter_i) {
361 std::size_t dest_idx_base = 0 * sub_category_stride + counter_i * counter_stride + category_i;
362 std::size_t dest_idx_project_sub_categories = sub_categories * sub_category_stride + dest_idx_base;
363 assert( dest_idx_project_sub_categories < output_counts.size() );
364 for (std::size_t sub_category_i=0;
365 sub_category_i<sub_categories;
367 std::size_t sub_category_idx = dest_idx_base + sub_category_i * sub_category_stride;
368 assert( sub_category_idx < output_counts.size() );
369 output_counts[dest_idx_project_sub_categories] += output_counts[sub_category_idx];
373 return output_counts;
378 std::vector< SummandDefinition > >;
382 template <
typename T>
385 return std::make_pair( std::vector< SummandDefinition > { std::make_pair(
static_cast<std::size_t
>(numerator),1)},
386 std::vector< SummandDefinition > { std::make_pair(
static_cast<std::size_t
>(
denominator),1)});
390 template <
typename T>
391 inline std::tuple< std::string, RatioDefinition >
401 template <
typename T>
403 return std::make_pair(
static_cast<std::size_t
>(counter_idx), multiplier) ;
408 std::size_t
computeSum(
const std::vector< SummandDefinition > &sum_def,
409 std::size_t eta_offset,
410 std::size_t row_stride,
412 const std::vector<std::size_t> &
stat);
415 return numerator!=0 ?
static_cast<float>(numerator/
static_cast<double>(
denominator)) : 0.f;
425 std::size_t eta_offset,
426 std::size_t row_stride,
428 const std::vector<std::size_t> &
stat) {
429 std::size_t numerator=
computeSum(ratio_def.first, eta_offset, row_stride, seed_i,
stat);
431 ?
computeSum(ratio_def.second, eta_offset, row_stride, seed_i,
stat)
439 std::vector< SummandDefinition >&&numerator,
441 return std::make_tuple(std::move(
name),
442 std::make_pair(std::move(numerator),
447 inline std::tuple<std::vector<std::string>, std::vector<RatioDefinition> >
450 std::tuple< std::vector<std::string>, std::vector<RatioDefinition> > splitted;
451 std::get<0>(splitted).reserve( a_ratio_list.size() );
452 for (
auto a_ratio : a_ratio_list) {
453 std::get<0>(splitted).emplace_back( std::move(std::get<0>(a_ratio)) );
455 std::get<1>(splitted).reserve( a_ratio_list.size() );
456 for (
auto a_ratio : a_ratio_list) {
457 std::get<1>(splitted).emplace_back( std::move(std::get<1>(a_ratio)) );
464 [[maybe_unused]]
const std::size_t sub_categories,
465 [[maybe_unused]]
const std::vector<RatioDefinition> &ratio_def) {
469 [[maybe_unused]]
const std::size_t sub_categories,
470 [[maybe_unused]]
const std::vector<RatioDefinition> &ratio_def) {
474 [[maybe_unused]]
const std::size_t sub_categories,
475 [[maybe_unused]]
const std::vector<RatioDefinition> &ratio_def) {
480 std::vector<float>
computeRatios(
const std::vector<RatioDefinition> &ratio_def,
482 const std::size_t sub_categories,
483 const std::vector< std::size_t> &
counter);
486 const std::vector<float> &
bins,
488 bool abs_value=
false,
490 std::stringstream range_label;
491 range_label << std::fixed << std::setprecision(precision);
492 if (bin_i==
bins.size()+1) {
493 range_label <<
" All " << variable_name;
497 std::stringstream value_str;
498 value_str << std::fixed << std::setprecision(precision) << 0.;
499 range_label << std::setw(4) << (abs_value ? value_str.str().c_str() :
"-inf") <<
"-";
502 range_label << std::setw(4) <<
bins.at(bin_i-1) <<
"-";
504 if (bin_i>=
bins.size()) {
505 range_label << std::setw(4) <<
"+inf";
508 range_label << std::setw(4) <<
bins.at(bin_i);
511 return range_label.str();
515 std::size_t eta_bin_i,
516 bool abs_eta=
false) {
522 template <
typename T, std::
size_t N>
524 const std::array<std::string, N> &
label) {
532 template <
typename T, std::
size_t Nrows, std::
size_t Ncolumns>
534 const std::array<std::string, Nrows> &row_label,
535 const std::array<std::string, Ncolumns> &column_label,
536 const std::string &top_left_label=
"") {
539 counter.size(), column_label.size(),
550 template <
typename T>
552 std::size_t start_idx,
553 std::size_t row_stride,
554 const std::vector<std::string> &row_label,
555 const std::vector<std::string> &column_label,
556 const std::string &top_left_label=
"") {
557 if (start_idx + (row_label.size()-1) * row_stride >=
counter.size() || row_stride < column_label.size()) {
558 std::stringstream
msg;
559 msg <<
"Counter dimension and label dimensions (" << row_label.size() <<
" * " << column_label.size()
560 <<
") do not match: [" << start_idx <<
", "
561 << start_idx <<
" + " << (row_label.size()-1) <<
" * " << row_stride <<
" = "
562 << (start_idx + (row_label.size()-1) * row_stride)
564 msg <<
" [row_labels:";
565 for (
const std::string &
label : row_label) {
568 msg <<
"; column_labels:";
569 for (
const std::string &
label : column_label) {
573 throw std::logic_error(
msg.str());
588 template <
typename T, std::
size_t N>
590 std::size_t start_row_idx,
591 std::size_t row_stride,
592 std::size_t start_column_idx,
593 std::size_t column_stride,
594 const std::vector<std::string> &row_label,
595 const std::vector<std::string> &column_label,
596 const std::string &top_left_label=
"") {
597 if (start_row_idx + (row_label.size()-1) * row_stride >=
counter.size()*
N
598 || start_column_idx + (column_label.size()-1) * column_stride >=
counter.size()*
N
599 || (row_stride*row_label.size()>column_stride && column_stride*column_label.size()>row_stride) ) {
600 std::stringstream
msg;
601 msg <<
"Counter dimension and label dimensions (" << row_label.size() <<
" * " << column_label.size()
602 <<
") do not match: [" << start_row_idx <<
", "
603 << start_row_idx <<
" + " << (row_label.size()-1) <<
" * " << row_stride <<
" = "
604 << (start_row_idx + (row_label.size()-1) * row_stride)
606 << start_column_idx <<
" + " << (column_label.size()-1) <<
" * " << column_stride <<
" = "
607 << (start_column_idx + (column_label.size()-1) * column_stride)
610 << (start_row_idx + (row_label.size()-1) * row_stride) <<
" >= " << (
counter.size()*
N)
611 <<
" || " << (start_column_idx + (column_label.size()-1) * column_stride) <<
" >= " << (
counter.size()*
N)
612 <<
" || ( " << (row_stride*row_label.size()) <<
" > " << column_stride <<
" && " << (column_stride*column_label.size()) <<
" > " << (row_stride)
614 msg <<
" [row_labels:";
615 for (
const std::string &
label : row_label) {
618 msg <<
"; column_labels:";
619 for (
const std::string &
label : column_label) {
623 throw std::logic_error(
msg.str());
639 template <
typename T>
641 const std::vector<std::string> &row_label,
642 const std::vector<std::string> &column_label,
643 const std::string &top_left_label=
"") {
644 return makeTable(
counter, 0
u, column_label.size(), row_label, column_label, top_left_label);
651 template <
typename T>
660 stat.m_minLabelWidth,
662 stat.m_separateLastRow,
669 template <
typename T>
680 stat.m_minLabelWidth,
683 stat.m_separateLastRow,
690 template <
typename T>
699 stat.m_minLabelWidth,
701 stat.m_separateLastRow,
707 template <
typename T>
718 stat.m_minLabelWidth,
721 stat.m_separateLastRow,