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.
135 lines
2.9 KiB
C++
135 lines
2.9 KiB
C++
#pragma once
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
#ifndef _UTILITIES_H_
|
|
#define _UTILITIES_H_
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <random>
|
|
#include <string>
|
|
|
|
void __strconcat_impl(std::string &res) {}
|
|
template <typename T, typename... TArgs>
|
|
void __strconcat_impl(std::string &res, const T &arg, const TArgs &... args)
|
|
{
|
|
res.append(arg);
|
|
__strconcat_impl(res, args...);
|
|
}
|
|
|
|
template <typename... TArgs>
|
|
std::string strconcat(TArgs... args)
|
|
{
|
|
std::string res;
|
|
__strconcat_impl(res, args...);
|
|
return res;
|
|
}
|
|
|
|
#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)
|
|
{
|
|
auto msg = strconcat("Failed to open ", 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 |