"Fossies" - the Fresh Open Source Software Archive

Member "dlib-19.18/docs/dlib/console_progress_indicator.h.html" (22 Sep 2019, 15121 Bytes) of package /linux/misc/dlib-19.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - console_progress_indicator.h</title></head><body bgcolor='white'><pre>
    2 <font color='#009900'>// Copyright (C) 2010  Davis E. King (davis@dlib.net)
    3 </font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
    4 </font><font color='#0000FF'>#ifndef</font> DLIB_CONSOLE_PROGRESS_INDiCATOR_Hh_
    5 <font color='#0000FF'>#define</font> DLIB_CONSOLE_PROGRESS_INDiCATOR_Hh_
    6 
    7 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>ctime<font color='#5555FF'>&gt;</font>
    8 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cmath<font color='#5555FF'>&gt;</font>
    9 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>limits<font color='#5555FF'>&gt;</font>
   10 <font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
   11 
   12 <font color='#0000FF'>namespace</font> dlib
   13 <b>{</b>
   14 
   15 <font color='#009900'>// ----------------------------------------------------------------------------------------
   16 </font>
   17     <font color='#0000FF'>class</font> <b><a name='console_progress_indicator'></a>console_progress_indicator</b>
   18     <b>{</b>
   19         <font color='#009900'>/*!
   20             WHAT THIS OBJECT REPRESENTS
   21                 This object is a tool for reporting how long a task will take
   22                 to complete.  
   23 
   24                 For example, consider the following bit of code:
   25 
   26                     console_progress_indicator pbar(100)
   27                     for (int i = 1; i &lt;= 100; ++i)
   28                     {
   29                         pbar.print_status(i);
   30                         long_running_operation();
   31                     }
   32 
   33                 The above code will print a message to the console each iteration
   34                 which shows how much time is remaining until the loop terminates.
   35         !*/</font>
   36 
   37     <font color='#0000FF'>public</font>:
   38 
   39         <font color='#0000FF'>inline</font> <font color='#0000FF'>explicit</font> <b><a name='console_progress_indicator'></a>console_progress_indicator</b> <font face='Lucida Console'>(</font>
   40             <font color='#0000FF'><u>double</u></font> target_value 
   41         <font face='Lucida Console'>)</font>; 
   42         <font color='#009900'>/*!
   43             ensures
   44                 - #target() == target_value
   45         !*/</font>
   46 
   47         <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='reset'></a>reset</b> <font face='Lucida Console'>(</font>
   48             <font color='#0000FF'><u>double</u></font> target_value
   49         <font face='Lucida Console'>)</font>;
   50         <font color='#009900'>/*!
   51             ensures
   52                 - #target() == target_value
   53                 - performs the equivalent of:
   54                     *this = console_progress_indicator(target_value)
   55                     (i.e. resets this object with a new target value)
   56 
   57         !*/</font>
   58 
   59         <font color='#0000FF'>inline</font> <font color='#0000FF'><u>double</u></font> <b><a name='target'></a>target</b> <font face='Lucida Console'>(</font>
   60         <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;
   61         <font color='#009900'>/*!
   62             ensures
   63                 - This object attempts to measure how much time is
   64                   left until we reach a certain targeted value.  This
   65                   function returns that targeted value.
   66         !*/</font>
   67 
   68         <font color='#0000FF'>inline</font> <font color='#0000FF'><u>bool</u></font> <b><a name='print_status'></a>print_status</b> <font face='Lucida Console'>(</font>
   69             <font color='#0000FF'><u>double</u></font> cur,
   70             <font color='#0000FF'><u>bool</u></font> always_print <font color='#5555FF'>=</font> <font color='#979000'>false</font>
   71         <font face='Lucida Console'>)</font>;
   72         <font color='#009900'>/*!
   73             ensures
   74                 - print_status() assumes it is called with values which are linearly 
   75                   approaching target().  It will attempt to predict how much time is 
   76                   remaining until cur becomes equal to target().
   77                 - prints a status message to the screen which indicates how much
   78                   more time is left until cur is equal to target()
   79                 - if (always_print) then
   80                     - This function prints to the screen each time it is called.
   81                 - else
   82                     - This function throttles the printing so that at most 1 message is
   83                       printed each second.  Note that it won't print anything to the screen
   84                       until about one second has elapsed.  This means that the first call
   85                       to print_status() never prints to the screen.
   86                 - This function returns true if it prints to the screen and false
   87                   otherwise. 
   88         !*/</font>
   89 
   90     <font color='#0000FF'>private</font>:
   91 
   92         <font color='#0000FF'><u>double</u></font> target_val;
   93 
   94         time_t start_time;
   95         <font color='#0000FF'><u>double</u></font> first_val;
   96         <font color='#0000FF'><u>double</u></font> seen_first_val;
   97         time_t last_time;
   98 
   99     <b>}</b>;
  100 
  101 <font color='#009900'>// ----------------------------------------------------------------------------------------
  102 </font><font color='#009900'>// ----------------------------------------------------------------------------------------
  103 </font><font color='#009900'>//                               IMPLEMENTATION DETAILS
  104 </font><font color='#009900'>// ----------------------------------------------------------------------------------------
  105 </font><font color='#009900'>// ----------------------------------------------------------------------------------------
  106 </font>
  107     console_progress_indicator::
  108     <b><a name='console_progress_indicator'></a>console_progress_indicator</b> <font face='Lucida Console'>(</font>
  109         <font color='#0000FF'><u>double</u></font> target_value 
  110     <font face='Lucida Console'>)</font> :
  111         target_val<font face='Lucida Console'>(</font>target_value<font face='Lucida Console'>)</font>,
  112         start_time<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
  113         first_val<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
  114         seen_first_val<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
  115         last_time<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
  116     <b>{</b>
  117     <b>}</b>
  118 
  119 <font color='#009900'>// ----------------------------------------------------------------------------------------
  120 </font>
  121     <font color='#0000FF'><u>bool</u></font> console_progress_indicator::
  122     <b><a name='print_status'></a>print_status</b> <font face='Lucida Console'>(</font>
  123         <font color='#0000FF'><u>double</u></font> cur,
  124         <font color='#0000FF'><u>bool</u></font> always_print
  125     <font face='Lucida Console'>)</font>
  126     <b>{</b>
  127         <font color='#0000FF'>const</font> time_t cur_time <font color='#5555FF'>=</font> std::<font color='#BB00BB'>time</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
  128 
  129         <font color='#009900'>// if this is the first time print_status has been called
  130 </font>        <font color='#009900'>// then collect some information and exit.  We will print status
  131 </font>        <font color='#009900'>// on the next call.
  132 </font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>seen_first_val<font face='Lucida Console'>)</font>
  133         <b>{</b>
  134             start_time <font color='#5555FF'>=</font> cur_time;
  135             last_time <font color='#5555FF'>=</font> cur_time;
  136             first_val <font color='#5555FF'>=</font> cur;
  137             seen_first_val <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
  138             <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
  139         <b>}</b>
  140 
  141         <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>cur_time <font color='#5555FF'>!</font><font color='#5555FF'>=</font> last_time <font color='#5555FF'>|</font><font color='#5555FF'>|</font> always_print<font face='Lucida Console'>)</font>
  142         <b>{</b>
  143             last_time <font color='#5555FF'>=</font> cur_time;
  144             <font color='#0000FF'><u>double</u></font> delta_t <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>cur_time <font color='#5555FF'>-</font> start_time<font face='Lucida Console'>)</font>;
  145             <font color='#0000FF'><u>double</u></font> delta_val <font color='#5555FF'>=</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>cur <font color='#5555FF'>-</font> first_val<font face='Lucida Console'>)</font>;
  146 
  147             <font color='#009900'>// don't do anything if cur is equal to first_val
  148 </font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>delta_val <font color='#5555FF'>&lt;</font> std::numeric_limits<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font>::<font color='#BB00BB'>epsilon</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
  149                 <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
  150 
  151             <font color='#0000FF'><u>double</u></font> seconds <font color='#5555FF'>=</font> delta_t<font color='#5555FF'>/</font>delta_val <font color='#5555FF'>*</font> std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font>target_val <font color='#5555FF'>-</font> cur<font face='Lucida Console'>)</font>;
  152 
  153             std::ios::fmtflags oldflags <font color='#5555FF'>=</font> std::cout.<font color='#BB00BB'>flags</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;  
  154 
  155             std::cout.<font color='#BB00BB'>setf</font><font face='Lucida Console'>(</font>std::ios::fixed,std::ios::floatfield<font face='Lucida Console'>)</font>;
  156             std::streamsize ss;
  157 
  158             <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>seconds <font color='#5555FF'>&lt;</font> <font color='#979000'>60</font><font face='Lucida Console'>)</font>
  159             <b>{</b>
  160                 ss <font color='#5555FF'>=</font> std::cout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>; 
  161                 std::cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Time remaining: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> seconds <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> seconds.                 \r</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> std::flush;
  162             <b>}</b>
  163             <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>seconds <font color='#5555FF'>&lt;</font> <font color='#979000'>60</font><font color='#5555FF'>*</font><font color='#979000'>60</font><font face='Lucida Console'>)</font>
  164             <b>{</b>
  165                 ss <font color='#5555FF'>=</font> std::cout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>; 
  166                 std::cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Time remaining: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> seconds<font color='#5555FF'>/</font><font color='#979000'>60</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> minutes.                 \r</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> std::flush;
  167             <b>}</b>
  168             <font color='#0000FF'>else</font> 
  169             <b>{</b>
  170                 ss <font color='#5555FF'>=</font> std::cout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>; 
  171                 std::cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Time remaining: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> seconds<font color='#5555FF'>/</font><font color='#979000'>60</font><font color='#5555FF'>/</font><font color='#979000'>60</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> hours.                 \r</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> std::flush;
  172             <b>}</b>
  173 
  174             <font color='#009900'>// restore previous output flags and precision settings
  175 </font>            std::cout.<font color='#BB00BB'>flags</font><font face='Lucida Console'>(</font>oldflags<font face='Lucida Console'>)</font>; 
  176             std::cout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font>ss<font face='Lucida Console'>)</font>; 
  177 
  178             <font color='#0000FF'>return</font> <font color='#979000'>true</font>;
  179         <b>}</b>
  180 
  181         <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
  182     <b>}</b>
  183 
  184 <font color='#009900'>// ----------------------------------------------------------------------------------------
  185 </font>
  186     <font color='#0000FF'><u>double</u></font> console_progress_indicator::
  187     <b><a name='target'></a>target</b> <font face='Lucida Console'>(</font>
  188     <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
  189     <b>{</b>
  190         <font color='#0000FF'>return</font> target_val;
  191     <b>}</b>
  192 
  193 <font color='#009900'>// ----------------------------------------------------------------------------------------
  194 </font>
  195     <font color='#0000FF'><u>void</u></font> console_progress_indicator::
  196     <b><a name='reset'></a>reset</b> <font face='Lucida Console'>(</font>
  197         <font color='#0000FF'><u>double</u></font> target_value
  198     <font face='Lucida Console'>)</font> 
  199     <b>{</b>
  200         <font color='#5555FF'>*</font><font color='#0000FF'>this</font> <font color='#5555FF'>=</font> <font color='#BB00BB'>console_progress_indicator</font><font face='Lucida Console'>(</font>target_value<font face='Lucida Console'>)</font>;
  201     <b>}</b>
  202 
  203 <font color='#009900'>// ----------------------------------------------------------------------------------------
  204 </font>
  205 <b>}</b>
  206 
  207 <font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_CONSOLE_PROGRESS_INDiCATOR_Hh_
  208 </font>
  209 
  210 </pre></body></html>