Chi-Tech
print_iter_progress.cc
Go to the documentation of this file.
1#include "chi_utils.h"
2
3#include <cmath>
4#include <iomanip>
5#include <sstream>
6
7//###################################################################
8/**Print the percentage completed based on the given interval.
9 *
10 * The function divides 100% into `num_intvls` intervals. If an
11 * iteration passes an interval boundary then that interval percentage
12 * will be printed.
13 *
14 * Specifying 10 intervals will print after each 10% is completed.
15 * Specifying 4 intervals will print after each 25% is completed.*/
16std::string chi::
17 PrintIterationProgress(const size_t current_iteration,
18 const size_t total_num_iterations,
19 const unsigned int num_intvls/*=10*/)
20{
21 typedef unsigned int uint;
22
23 // Creating shorthand symbols for arguments
24 const auto& i = current_iteration;
25 const auto& I = num_intvls;
26 const auto& N = total_num_iterations;
27
28 // If on the first iteration then do nothing
29 if (i==0) return {};
30
31 // Prepare an output stream
32 std::stringstream output;
33 output << std::fixed << std::setprecision(2) << std::setw(7);
34
35 // If at the end, just print 100 and quit
36 if ((i+1)==N) { output << 100.0; return output.str(); }
37
38 const double dI = std::ceil(double(N)/I); //Interval size
39
40 // std::modf is used to get the integral part
41 // of a real value
42 double x1; std::modf(double(i-1)/dI,&x1);
43 double x2; std::modf(double(i )/dI,&x2);
44
45 if (uint(x2) != uint(x1)) { output << x2*(100.0/I); return output.str(); }
46
47 return {};
48}
std::string PrintIterationProgress(size_t current_iteration, size_t total_num_iterations, unsigned int num_intvls=10)
#define uint