tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
display.h
1 //addapted from boost progress.hpp, made c++11 only//
2 
3 
4 #ifndef PROGRESS_H
5 #define PROGRESS_H
6 
7 #include <iostream> // for ostream, cout, etc
8 #include <string> // for string
9 #include <chrono>
10 
11 namespace tiny_dnn {
12 
13 class timer
14 {
15  public:
16  timer(): t1(std::chrono::high_resolution_clock::now()){};
17  float_t elapsed(){return std::chrono::duration_cast<std::chrono::duration<float_t>>(std::chrono::high_resolution_clock::now() - t1).count();}
18  void restart(){t1 = std::chrono::high_resolution_clock::now();}
19  void start(){t1 = std::chrono::high_resolution_clock::now();}
20  void stop(){t2 = std::chrono::high_resolution_clock::now();}
21  float_t total(){stop();return std::chrono::duration_cast<std::chrono::duration<float_t>>(t2 - t1).count();}
22  ~timer(){}
23  private:
24  std::chrono::high_resolution_clock::time_point t1, t2;
25 };
26 
27 
28 // progress_display --------------------------------------------------------//
29 
30 // progress_display displays an appropriate indication of
31 // progress at an appropriate place in an appropriate form.
32 
34 {
35  public:
36  explicit progress_display( size_t expected_count_,
37  std::ostream & os = std::cout,
38  const std::string & s1 = "\n", //leading strings
39  const std::string & s2 = "",
40  const std::string & s3 = "" )
41  // os is hint; implementation may ignore, particularly in embedded systems
42  : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
43 
44  void restart( size_t expected_count_ )
45  // Effects: display appropriate scale
46  // Postconditions: count()==0, expected_count()==expected_count_
47  {
48  _count = _next_tic_count = _tic = 0;
49  _expected_count = expected_count_;
50 
51  m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
52  << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
53  << std::endl // endl implies flush, which ensures display
54  << m_s3;
55  if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
56  } // restart
57 
58  size_t operator+=( size_t increment )
59  // Effects: Display appropriate progress tic if needed.
60  // Postconditions: count()== original count() + increment
61  // Returns: count().
62  {
63  if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
64  return _count;
65  }
66 
67  size_t operator++() { return operator+=( 1 ); }
68  size_t count() const { return _count; }
69  size_t expected_count() const { return _expected_count; }
70 
71  private:
72  std::ostream & m_os; // may not be present in all imps
73  const std::string m_s1; // string is more general, safer than
74  const std::string m_s2; // const char *, and efficiency or size are
75  const std::string m_s3; // not issues
76 
77  size_t _count, _expected_count, _next_tic_count;
78  size_t _tic;
79  void display_tic()
80  {
81  // use of floating point ensures that both large and small counts
82  // work correctly. static_cast<>() is also used several places
83  // to suppress spurious compiler warnings.
84  size_t tics_needed =
85  static_cast<size_t>(
86  (static_cast<double>(_count)/_expected_count)*50.0 );
87  do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
88  _next_tic_count =
89  static_cast<size_t>((_tic/50.0)*_expected_count);
90  if ( _count == _expected_count ) {
91  if ( _tic < 51 ) m_os << '*';
92  m_os << std::endl;
93  }
94  } // display_tic
95 
96  progress_display &operator = (const progress_display &) = delete;
97 };
98 
99 } // namespace
100 
101 #endif
Definition: display.h:34
Definition: display.h:14