Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 95 lines (81 sloc) 2.661 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
open source routing machine
Copyright (C) Dennis Luxen, others 2010

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/

#ifndef PERCENT_H
#define PERCENT_H

#include <iostream>


class Percent
{
public:
    /**
* Constructor.
* @param maxValue the value that corresponds to 100%
* @param step the progress is shown in steps of 'step' percent
*/
    Percent(unsigned maxValue, unsigned step = 5) {
        reinit(maxValue, step);
    }

    /** Reinitializes this object. */
    void reinit(unsigned maxValue, unsigned step = 5) {
        _maxValue = maxValue;
        _current_value = 0;
        _intervalPercent = _maxValue / 100;
        _nextThreshold = _intervalPercent;
        _lastPercent = 0;
        _step = step;
    }

    /** If there has been significant progress, display it. */
    void printStatus(unsigned currentValue) {
        if (currentValue >= _nextThreshold) {
            _nextThreshold += _intervalPercent;
            printPercent( currentValue / (double)_maxValue * 100 );
        }
        if (currentValue + 1 == _maxValue)
            std::cout << " 100%" << std::endl;
    }

    void printIncrement()
    {
#pragma omp atomic
        ++_current_value;
        printStatus(_current_value);
    }

    void printAddition(const unsigned addition) {
#pragma omp atomic
        _current_value += addition;
        printStatus(_current_value);
    }
private:
    unsigned _current_value;
    unsigned _maxValue;
    unsigned _intervalPercent;
    unsigned _nextThreshold;
    unsigned _lastPercent;
    unsigned _step;

    /** Displays the new progress. */
    void printPercent(double percent) {
        while (percent >= _lastPercent+_step) {
            _lastPercent+=_step;
            if (_lastPercent % 10 == 0) {
                std::cout << " " << _lastPercent << "% ";
            }
            else {
                std::cout << ".";
            }
            std::cout.flush();
        }
    }
};

#endif // PERCENT_H
Something went wrong with that request. Please try again.