1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.6 KiB
C++

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#ifndef _UTILITIES_H_
#define _UTILITIES_H_
#include <chrono>
#include <cstdio>
#include <random>
#include <string>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <wincrypt.h>
#pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "user32.lib")
unsigned int __get_random_seed()
{
unsigned int r = 0;
HCRYPTPROV hCryptProv;
CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
CryptGenRandom(hCryptProv, sizeof(unsigned int), (BYTE *)&r);
CryptReleaseContext(hCryptProv, 0);
return r;
}
void __error_then_exit(const char *msg)
{
MessageBoxA(NULL, msg, "Error", MB_OK | MB_ICONERROR);
exit(1);
}
#else
unsigned int __get_random_seed()
{
unsigned int r = 0;
FILE *fd = fopen("/dev/random", "rb");
fread(&r, sizeof(unsigned int), 1, fd);
fclose(fd);
return r;
}
void __error_then_exit(const char *msg)
{
fputs(msg, stderr);
getchar(), getchar();
exit(1);
}
#endif
class Random
{
private:
std::mt19937 *rd;
public:
Random() { rd = new std::mt19937(__get_random_seed()); }
~Random() { delete rd; }
unsigned int next() { return (*rd)(); }
unsigned int next(unsigned int mn, unsigned int mx) { return next() % (mx - mn) + mn; }
};
class FileHandleExitOnFail
{
private:
FILE *fp;
public:
FileHandleExitOnFail(FILE *_fp) : fp(_fp) {}
FileHandleExitOnFail(const char *filename, const char *mode)
{
fp = fopen(filename, mode);
if (!fp)
{
std::string msg("Failed to open ");
msg += filename;
__error_then_exit(msg.c_str());
}
}
~FileHandleExitOnFail() { fclose(fp); }
operator FILE *() const { return fp; }
};
class Timer
{
public:
using fmilliseconds = std::chrono::duration<double, std::milli>;
void start()
{
t1 = std::chrono::steady_clock::now();
}
void stop()
{
t2 = std::chrono::steady_clock::now();
}
std::chrono::steady_clock::duration::rep difference()
{
return (t2 - t1).count();
}
double diff_in_ms()
{
return std::chrono::duration_cast<fmilliseconds>(t2 - t1).count();
}
private:
std::chrono::time_point<std::chrono::steady_clock> t1, t2;
};
template <typename T>
class Array2DView
{
private:
T *arr;
size_t w;
public:
Array2DView(size_t _w, T *ptr = nullptr) : arr(ptr), w(_w) {}
Array2DView<T> &operator=(T *ptr) { arr = ptr; }
void resize(size_t _w) { w = _w; }
T *operator[](size_t idx) { return arr + idx * w; }
T *data() const { return arr; }
};
#endif