This repository was archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
185 lines (163 loc) · 4.04 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* utils.h
*
* Created on: Apr 15, 2017
* Author: paul.rigor
*/
#ifndef UTILS_H_
#define UTILS_H_
#include <windows.h>
#include <ctime>
#include <sstream>
#include <random>
// Returns the amount of milliseconds elapsed since the UNIX epoch.
// Source copied from http://stackoverflow.com/a/1861337/6592879
typedef long long int64; typedef unsigned long long uint64;
uint64 GetTimeMs64()
{
/* Windows */
FILETIME ft;
LARGE_INTEGER li;
/* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
* to a LARGE_INTEGER structure. */
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
uint64 ret = li.QuadPart;
ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
return ret;
}
//Class encapsulating the measurement of elapsed time.
class SimpleTimer
{
private:
double _PCFreq_us;
__int64 _startTicks;
__int64 _accumulatedTicks;
bool _timerStarted;
__int64 GetCounter()
{
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return counter.QuadPart;
}
public:
SimpleTimer(bool startTimer = false)
{
_accumulatedTicks = 0;
startTimer ? Start() : Reset();
}
void Start()
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
_PCFreq_us = double(freq.QuadPart)/1000000.0;
_startTicks = GetCounter();
_timerStarted = true;
}
void Reset()
{
_startTicks = 0;
_timerStarted = false;
_accumulatedTicks = 0;
}
void Pause()
{
if (_timerStarted)
{
_accumulatedTicks += (GetCounter() - _startTicks);
_timerStarted = false;
}
}
void Restart()
{
Reset();
Start();
}
bool IsRunning() { return _timerStarted; }
double Elapsed_us()
{
if (_timerStarted)
return (_accumulatedTicks + (GetCounter() - _startTicks)) / _PCFreq_us;
else
return _accumulatedTicks / _PCFreq_us;
}
std::string Elapsed_us_str(bool includeUnit = true)
{
std::stringstream elapsedTime;
elapsedTime<<Elapsed_us();
if (includeUnit)
return (elapsedTime.str() + " us");
else
return (elapsedTime.str());
}
};
class RandomNumGen
{
private:
std::mt19937 mersenne_rng;
public:
RandomNumGen()
{
mersenne_rng.seed(GetTimeMs64());
}
int Next(int min, int max)
{
std::uniform_int_distribution<int> dist(min, max);
return dist(mersenne_rng);
}
};
// Source copied from: http://www.cplusplus.com/articles/4z18T05o/#Windows
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
void SetConsoleBufferHeight(int height)
{
COORD coord;
coord.X = 80;
coord.Y = height;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleScreenBufferSize(Handle, coord);
}
bool IsKeyDown(int vKey)
{
return (GetAsyncKeyState(vKey) & 0x8000);
}
uint64 GetRandomNumber(uint64 seedValue = GetTimeMs64())
{
return seedValue+rand();
}
uint64 GetRandomNumber(uint64 maxValue, uint64 seedValue = GetTimeMs64())
{
return seedValue+rand() % (maxValue + 1);
}
#endif /* UTILS_H_ */