-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.h
53 lines (44 loc) · 1.09 KB
/
utils.h
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
#pragma once
#include <atomic>
#include <chrono>
#include <cstdint>
#include <string>
#include <sstream>
#include <thread>
#include <optional>
class Timer
{
public:
Timer()
{
start = clock::now();
}
double Seconds() const
{
clock::time_point now = clock::now();
int64_t microsecs = std::chrono::duration_cast<std::chrono::microseconds>(now - start).count();
return static_cast<double>(microsecs) / 1000000.;
}
private:
using clock = std::chrono::steady_clock;
clock::time_point start;
};
class ConsoleProgressBar
{
public:
ConsoleProgressBar(){};
ConsoleProgressBar(int64_t totalWork);
~ConsoleProgressBar() { Done(); }
void Start();
void Update(int64_t num = 1) { if (num == 0) return; workDone += num;}
double ElapsedSeconds() const { return finishTime ? *finishTime : timer.Seconds(); }
void Done();
private:
void Print();
int64_t totalWork;
Timer timer;
std::atomic<int64_t> workDone;
std::atomic<bool> exitThread;
std::thread updateThread;
std::optional<double> finishTime;
};