-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.hpp
111 lines (79 loc) · 2.11 KB
/
utils.hpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* =====================================================================================
*
* Filename: utils.hpp
*
* Description:
*
* Version: 1.0
* Created: 2013-01-31 17:16:06
* Revision: none
* Compiler: gcc
*
* Author: Didzis Gosko (dg), [email protected]
* Organization:
*
* =====================================================================================
*/
#ifndef __UTILS_HPP__
#define __UTILS_HPP__
#include <string>
#include <ctime>
//
// Klase ļauj aprēķināt izmantot laiku
//
class Timing
{
public:
void start() { _duration = 0; _stop = _start = clock(); }
double stop() { _stop = clock(); _duration = (double)(_stop-_start)/(double)CLOCKS_PER_SEC; return _duration; }
double duration() const { return _duration; }
operator double() const { return _duration; }
private:
clock_t _start;
clock_t _stop;
double _duration;
};
void outputDuration(double duration);
//
// Standarta ANSI termināļos ļauj attīt atpakaļ izvadi.
// Skat: http://ascii-table.com/ansi-escape-sequences.php
//
class RollbackOutput
{
public:
RollbackOutput() { rollbackSize = 0; fillSymbol = ' '; }
RollbackOutput(char fillSymbol) { rollbackSize = 0; this->fillSymbol = fillSymbol; }
~RollbackOutput() { rollback(); }
const std::string& operator+=(const std::string& s) { append(s); return s; }
const std::string& operator=(const std::string& s) { replace(s); return s; }
void append(const std::string& s);
void replace(const std::string& s);
void rollback();
char fillSymbol;
private:
int rollbackSize;
};
class ProgressIndicator
{
public:
ProgressIndicator(int total, int start = 0)
{ _total = total == 0 ? 1 : total; _current = start; _prevDisplay = 0; }
int operator=(int value) { update(value); return value; }
void display();
void clear();
std::string postfix;
private:
const double displayUnit = 0.001;
void update(int value)
{
_current = value;
if((double)(_current - _prevDisplay)/_total >= displayUnit)
display();
}
RollbackOutput output;
double _total;
int _current;
int _prevDisplay;
};
#endif // __UTILS_HPP__