1 /* 2 open source routing machine 3 Copyright (C) Dennis Luxen, others 2010 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU AFFERO General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 or see http://www.gnu.org/licenses/agpl.txt. 19 */ 20 21 #ifndef PERCENT_H 22 #define PERCENT_H 23 24 #include <iostream> 25 #ifdef _OPENMP 26 #include <omp.h> 27 #endif 28 29 class Percent 30 { 31 public: 32 /** 33 * Constructor. 34 * @param maxValue the value that corresponds to 100% 35 * @param step the progress is shown in steps of 'step' percent 36 */ 37 Percent(unsigned maxValue, unsigned step = 5) { 38 reinit(maxValue, step); 39 } 40 41 /** Reinitializes this object. */ 42 void reinit(unsigned maxValue, unsigned step = 5) { 43 _maxValue = maxValue; 44 _current_value = 0; 45 _intervalPercent = _maxValue / 100; 46 _nextThreshold = _intervalPercent; 47 _lastPercent = 0; 48 _step = step; 49 } 50 51 /** If there has been significant progress, display it. */ printStatus(unsigned currentValue)52 void printStatus(unsigned currentValue) { 53 if (currentValue >= _nextThreshold) { 54 _nextThreshold += _intervalPercent; 55 printPercent( currentValue / (double)_maxValue * 100 ); 56 } 57 if (currentValue + 1 == _maxValue) 58 std::cout << " 100%" << std::endl; 59 } 60 printIncrement()61 void printIncrement() 62 { 63 #pragma omp atomic 64 _current_value++; 65 printStatus(_current_value); 66 } 67 private: 68 unsigned _current_value; 69 unsigned _maxValue; 70 unsigned _intervalPercent; 71 unsigned _nextThreshold; 72 unsigned _lastPercent; 73 unsigned _step; 74 75 /** Displays the new progress. */ printPercent(double percent)76 void printPercent(double percent) { 77 while (percent >= _lastPercent+_step) { 78 _lastPercent+=_step; 79 if (_lastPercent % 10 == 0) { 80 std::cout << " " << _lastPercent << "% "; 81 } 82 else { 83 std::cout << "."; 84 } 85 std::cout.flush(); 86 } 87 } 88 }; 89 90 #endif // PERCENT_H 91