36 #include <string_view>
39 #include <cereal/archives/binary.hpp>
40 #include <cereal/types/variant.hpp>
41 #include <cereal/types/vector.hpp>
53 template <
typename DataType_>
55 std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
60 cereal::BinaryOutputArchive out_archive(ofs);
69 template <
typename DataType_>
71 std::ifstream ifs(filepath, std::ios::in | std::ios::binary);
76 cereal::BinaryInputArchive in_archive(ifs);
88 using namespace std::chrono;
89 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
106 auto x = std::chrono::duration<std::size_t, std::milli>(ts);
107 std::chrono::time_point<std::chrono::system_clock> tp{ x };
108 auto tts{ std::chrono::system_clock::to_time_t(tp) };
110 std::stringstream ss;
111 ss << std::put_time(std::localtime(&tts), fmt.data());
127 auto conv_res = std::from_chars(str.data(), str.data() + str.size(), result);
129 if (conv_res.ptr != (str.data() + str.size())) {
143 template <
typename T_,
typename S_>
144 static inline T_ str2(
const S_& s) {
145 std::stringstream ss(s);
157 template <
typename T_>
158 inline static T_ square(T_ a) {
169 template <
typename T_>
171 std::random_device rd;
172 std::mt19937 gen(rd());
173 std::uniform_int_distribution<T_> dist(from, to);
185 template <
typename T_>
187 std::random_device rd;
188 std::mt19937 gen(rd());
189 std::uniform_real_distribution<T_> dist(from, to);
201 inline std::vector<std::string>
split(
const std::string& str,
char delim) {
202 std::vector<std::string> result;
203 std::stringstream ss(str);
206 while (getline(ss, item, delim)) {
207 result.emplace_back(item);
220 template <
typename T_>
222 return ((mask >> i) & 0x01) == 1;
231 template <
typename Container_>
233 for (
auto&& row : mat) {
244 template <
typename Container_>
246 for (
auto&& v : row) {
247 std::cout <<
"\t" << std::fixed << std::setprecision(4) << v;
249 std::cout << std::endl;
259 std::string transformed;
261 std::transform(old.begin(), old.end(), std::back_inserter(transformed), ::tolower);
274 third_party::SHA256 hash;
276 std::ifstream f(filepath, std::ios::binary);
281 constexpr
const std::size_t buff_size = 4096;
282 std::vector<char> buffer(buff_size);
285 f.read(buffer.data(), buff_size);
286 size_t bytes_read = size_t(f.gcount());
288 hash.add(buffer.data(), bytes_read);
291 return hash.getHash();
301 std::ifstream ifs{ filepath };
302 if (!ifs.is_open()) {
307 ifs.seekg(0, std::ios::end);
308 std::streamsize size = ifs.tellg();
310 std::string file_content(size,
' ');
313 ifs.read(&file_content[0], size);
325 std::string res{ s };
326 res.erase(res.begin(),
327 std::find_if(res.begin(), res.end(), [](
unsigned char ch) { return (std::isspace(ch) == 0); }));
338 std::string res{ s };
339 res.erase(std::find_if(res.rbegin(), res.rend(), [](
unsigned char ch) { return (std::isspace(ch) == 0); }).base(),
355 template <
typename DType_>
356 void to_file(
const std::vector<DType_>& vec,
const std::string filepath) {
357 std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
362 for (
auto&& d : vec) {
363 const char* p_d{
reinterpret_cast<const char*
>(&d) };
364 ofs.write(p_d,
sizeof(DType_));
371 template <
typename DType_>
372 void to_file(
const std::vector<std::vector<DType_>>& mat,
const std::string filepath) {
373 std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
378 for (
auto&& vec : mat) {
379 for (
auto&& d : vec) {
380 const char* p_d{
reinterpret_cast<const char*
>(&d) };
381 ofs.write(p_d,
sizeof(DType_));
389 template <
typename T_>
392 for (std::size_t i = 0; i < p; ++i) {
401 template <
typename T_>
403 std::size_t b =
ipow(10, places);
404 return static_cast<T_
>(std::round(b * x) / b);
416 string_view p = __PRETTY_FUNCTION__;
417 return string_view(p.data() + 34, p.size() - 34 - 1);
418 #elif defined(__GNUC__)
419 string_view p = __PRETTY_FUNCTION__;
420 # if __cplusplus < 201402
421 return string_view(p.data() + 36, p.size() - 36 - 1);
423 return string_view(p.data() + 49, p.find(
';', 49) - 49);
425 #elif defined(_MSC_VER)
426 string_view p = __FUNCSIG__;
427 return string_view(p.data() + 84, p.size() - 84 - 7);
std::vector< std::string > split(const std::string &str, char delim)
Splits the string with the provided character delimiter.
Definition: utils.hpp:201
std::string trim_left(const std::string &s)
Left trim.
Definition: utils.hpp:324
int str_to_int(const std::string &str)
Tries to parse the integer from the string.
Definition: utils.hpp:123
std::string get_formated_timestamp(const std::string &fmt, UnixTimestamp ts=timestamp())
Returns string representing current time and date in formated string based on provided format.
Definition: utils.hpp:105
std::string trim(const std::string &s)
Trim.
Definition: utils.hpp:350
std::string to_lowercase(const std::string &old)
Returns a copy of old string but all lowercase.
Definition: utils.hpp:258
std::string read_whole_file(const std::string &filepath)
Reads the whole file into string that it will return.
Definition: utils.hpp:300
constexpr std::string_view type_name()
Returns a string view describing the type T_.
Definition: utils.hpp:413
std::string trim_right(const std::string &s)
Right trim.
Definition: utils.hpp:337
void print_matrix(const Container_ &mat)
Prints out any two-dimensional iterable container.
Definition: utils.hpp:232
void print_vector(const Container_ &row)
Prints out any one-dimensional iterable container.
Definition: utils.hpp:245
void to_file(const std::vector< DType_ > &vec, const std::string filepath)
Writes the given vector into the given file.
Definition: utils.hpp:356
void serialize_to_file(DataType_ data, const std::string filepath)
Serializes the given data into the file using cereal lib.
Definition: utils.hpp:54
DataType_ deserialize_from_file(const std::string filepath)
Deserializes the data from the provided file using cereal lib.
Definition: utils.hpp:70
T_ irand(T_ from, T_ to)
Returns a pseudorandom integral number sampled from the uniform distribution [from,...
Definition: utils.hpp:170
T_ round_decimal(T_ x, std::size_t places)
Rounds the number to the specified number of decimal places.
Definition: utils.hpp:402
T_ frand(T_ from, T_ to)
Returns a pseudorandom floating point number sampled from the uniform distribution [from,...
Definition: utils.hpp:186
std::string sha256_sum(const std::string &filepath)
Computes the SHA256 hash for the given file and returns it.
Definition: utils.hpp:272
int64_t timestamp()
Returns the actual UNIX timestamp (ms).
Definition: utils.hpp:87
T_ ipow(T_ b, std::size_t p)
Unsigned integer power function.
Definition: utils.hpp:390
bool is_set(T_ mask, size_t i)
Tests whether the i-th lowest significant bit is set.
Definition: utils.hpp:221
Definition: common-types.h:33
std::int64_t UnixTimestamp
Definition: common-types.h:54
#define SHLOG_E_THROW(x)
Definition: static-logger.hpp:158