LOOS  v2.3.2
ProgressCounters.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2009, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 #include <ProgressCounters.hpp>
24 #include <utils.hpp>
25 #include <cmath>
26 
27 
28 namespace loos {
29 
30  void SimpleCounter::attach(ObsT* obs) { observers.push_back(obs); }
31  void SimpleCounter::detach(ObsT* obs) {
32  Observers::iterator i = std::find(observers.begin(), observers.end(), obs);
33  if (i == observers.end())
34  throw(std::logic_error("Attempting to detach an observer that was never attached"));
35  observers.erase(i);
36  }
37 
38  void SimpleCounter::notify(void) {
39  Observers::iterator i;
40  for (i = observers.begin(); i != observers.end(); ++i)
41  (*i)->update(this);
42  }
43 
44  void SimpleCounter::finish(void) {
45  timer_.stop();
46  Observers::iterator i;
47  for (i = observers.begin(); i != observers.end(); ++i)
48  (*i)->finish(this);
49  }
50 
51  void SimpleCounter::start(void) {
52  count_ = 0;
53  timer_.start();
54  Observers::iterator i;
55  for (i = observers.begin(); i != observers.end(); ++i)
56  (*i)->start(this);
57  }
58 
59 
60  uint SimpleCounter::count(void) const { return(count_); }
61  double SimpleCounter::elapsed(void) { return(timer_.elapsed()); }
62 
63  uint SimpleCounter::remaining(void) { throw(std::logic_error("remaining() is unimplemented")); }
64 
65  double SimpleCounter::timeRemaining(void) { throw(std::logic_error("timeRemaining() is unimplemented")); }
66 
67  double SimpleCounter::fractionComplete(void) { throw(std::logic_error("fractionComplete() is unimplemented")); }
68 
69 
70 
71 
72  // ----------------------------------------------------------
73 
74  void EstimatingCounter::setExpected(uint n) { expected = n; }
75  uint EstimatingCounter::remaining(void) { return(expected - count_); }
77  double avg = timer_.elapsed() / count_;
78  return(remaining() * avg);
79  }
80 
81  double EstimatingCounter::fractionComplete(void) { return(static_cast<double>(count_) / expected); }
82 
83 
84  // ----------------------------------------------------------
85 
86  void BasicProgress::start(SimpleCounter*) { os_ << prefix_; }
87  void BasicProgress::update(SimpleCounter*) { os_ << msg_; }
88  void BasicProgress::finish(SimpleCounter*) { os_ << suffix_; }
89 
90  // ----------------------------------------------------------
91 
92  void PercentProgress::update(SimpleCounter* s) {
93  uint i = static_cast<uint>(floor(s->fractionComplete() * 100.0));
94  os_ << i << "% " << msg_ << std::endl;
95  }
96 
97  void PercentProgress::finish(SimpleCounter* s) {
98  os_ << suffix_;
99  os_ << "Total elapsed time was " << timeAsString(s->elapsed()) << std::endl;
100  }
101 
102  // -----------------------------------------------------------
103 
104  void PercentProgressWithTime::update(SimpleCounter* s) {
105  uint i = static_cast<uint>(floor(s->fractionComplete() * 100.0));
106  os_ << i << "% " << msg_ << " (" << timeAsString(s->timeRemaining()) << " remaining)\n";
107  }
108 
109  void PercentProgressWithTime::finish(SimpleCounter* s) {
110  os_ << suffix_;
111  os_ << "Total elapsed time was " << timeAsString(s->elapsed()) << std::endl;
112  }
113 
114 }
115 
virtual void finish(void)
Notify observers that we've finished with our calculation.
void setExpected(uint)
Alter the expected count.
virtual double elapsed(void)
Total elapsed wall-time.
virtual double fractionComplete(void)
Percent complete (if applicable)
virtual void start(void)
Notify observers that we're starting a calculation.
virtual uint remaining(void)
Remaining iterations (if applicable)
double fractionComplete(void)
Returns the percent completed so far.
Basic progress counter object, defining the interface...
virtual double timeRemaining(void)
Remaining time (estimated, again if applicable)
std::string timeAsString(const double t, const uint precision)
Convert t (seconds) into a string, converting to hours and minutes as necessary.
Definition: utils.cpp:212
uint count(void) const
Number of iterations we've seen so far.
virtual void notify(void)
Notify observers that an update should occur.
Namespace for most things not already encapsulated within a class.
uint remaining(void)
Returns the number of iterations left.