GEDLIB  1.0
progress_bar.ipp
Go to the documentation of this file.
1 /***************************************************************************
2 * *
3 * Copyright (C) 2018 by David B. Blumenthal *
4 * *
5 * This file is part of GEDLIB. *
6 * *
7 * GEDLIB is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as published *
9 * by the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * GEDLIB is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with GEDLIB. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21 
27 #ifndef SRC_ENV_PROGRESS_BAR_IPP_
28 #define SRC_ENV_PROGRESS_BAR_IPP_
29 
30 namespace ged {
31 
33 ProgressBar(std::size_t num_tasks) :
34 num_solved_tasks_{0},
35 num_tasks_{num_tasks},
36 start_time_{std::chrono::high_resolution_clock::now()} {}
37 
38 void
41  num_solved_tasks_++;
42 }
43 
44 void
46 reset() {
47  num_solved_tasks_ = 0;
48 }
49 
50 std::ostream &
51 operator<<(std::ostream & os, const ProgressBar & progress_bar) {
52 
53  std::streamsize precision{os.precision()};
54  os.precision(2);
55  os.setf(std::ios::fixed, std::ios::floatfield);
56  double progress_in_percent{100.0 * static_cast<double>(progress_bar.num_solved_tasks_) / static_cast<double>(progress_bar.num_tasks_)};
57  os << "[";
58  for (std::size_t i{1}; i <= 10; i++) {
59  if (static_cast<double>(i) <= progress_in_percent / 10.0) {
60  os << "=";
61  }
62  else if (static_cast<double>(i - 1) < progress_in_percent / 10.0) {
63  os << ">";
64  }
65  else {
66  os << " ";
67  }
68  }
69  os << "] ";
70  if (progress_in_percent < 100) {
71  os << " ";
72  }
73  if (progress_in_percent < 10) {
74  os << " ";
75  }
76  os << progress_in_percent << " %";
77  if (progress_in_percent > 0) {
78  std::size_t max_num_digits{7};
79  std::size_t num_digits{4};
80  std::chrono::duration<double> runtime_so_far = std::chrono::high_resolution_clock::now() - progress_bar.start_time_;
81  double estimated_remaining_runtime{(runtime_so_far.count() * ((100.0 / progress_in_percent) - 1.0)) / (60.0 * 60.0)};
82  if (estimated_remaining_runtime >= 10) {
83  num_digits++;
84  }
85  if (estimated_remaining_runtime >= 100) {
86  num_digits++;
87  }
88  if (estimated_remaining_runtime >= 1000) {
89  num_digits++;
90  }
91  while(num_digits++ < max_num_digits) {
92  os << " ";
93  }
94  os << " " << estimated_remaining_runtime;
95  }
96  else {
97  os << " ?";
98  }
99  os << " h rem.";
100  os.unsetf(std::ios::floatfield);
101  os.precision(precision);
102  return os;
103 }
104 
105 
106 }
107 
108 #endif /* SRC_ENV_PROGRESS_BAR_IPP_ */
ProgressBar(std::size_t num_tasks)
Constructs a progress bar for given number of tasks.
A progress bar class.
Global namespace for GEDLIB.
void increment()
Increments the number of solved tasks.
void reset()
Sets the number of solved tasks to 0.