SOMHunter Core
sh::utils Namespace Reference

Functions

template<typename DataType_ >
void serialize_to_file (DataType_ data, const std::string filepath)
 Serializes the given data into the file using cereal lib. More...
 
template<typename DataType_ >
DataType_ deserialize_from_file (const std::string filepath)
 Deserializes the data from the provided file using cereal lib. More...
 
int64_t timestamp ()
 Returns the actual UNIX timestamp (ms). More...
 
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. More...
 
int str_to_int (const std::string &str)
 Tries to parse the integer from the string. More...
 
template<typename T_ >
T_ irand (T_ from, T_ to)
 Returns a pseudorandom integral number sampled from the uniform distribution [from, to]. More...
 
template<typename T_ >
T_ frand (T_ from, T_ to)
 Returns a pseudorandom floating point number sampled from the uniform distribution [from, to). More...
 
std::vector< std::string > split (const std::string &str, char delim)
 Splits the string with the provided character delimiter. More...
 
template<typename T_ >
bool is_set (T_ mask, size_t i)
 Tests whether the i-th lowest significant bit is set. More...
 
template<typename Container_ >
void print_matrix (const Container_ &mat)
 Prints out any two-dimensional iterable container. More...
 
template<typename Container_ >
void print_vector (const Container_ &row)
 Prints out any one-dimensional iterable container. More...
 
std::string to_lowercase (const std::string &old)
 Returns a copy of old string but all lowercase. More...
 
std::string sha256_sum (const std::string &filepath)
 Computes the SHA256 hash for the given file and returns it. More...
 
std::string read_whole_file (const std::string &filepath)
 Reads the whole file into string that it will return. More...
 
std::string trim_left (const std::string &s)
 Left trim. More...
 
std::string trim_right (const std::string &s)
 Right trim. More...
 
std::string trim (const std::string &s)
 Trim. More...
 
template<typename DType_ >
void to_file (const std::vector< DType_ > &vec, const std::string filepath)
 Writes the given vector into the given file. More...
 
template<typename DType_ >
void to_file (const std::vector< std::vector< DType_ >> &mat, const std::string filepath)
 Writes the given matrix into the given file. More...
 
template<typename T_ >
T_ ipow (T_ b, std::size_t p)
 Unsigned integer power function. More...
 
template<typename T_ >
T_ round_decimal (T_ x, std::size_t places)
 Rounds the number to the specified number of decimal places. More...
 
template<class T_ >
constexpr std::string_view type_name ()
 Returns a string view describing the type T_. More...
 

Function Documentation

◆ deserialize_from_file()

template<typename DataType_ >
DataType_ sh::utils::deserialize_from_file ( const std::string  filepath)

Deserializes the data from the provided file using cereal lib.

https://uscilab.github.io/cereal/

◆ frand()

template<typename T_ >
T_ sh::utils::frand ( T_  from,
T_  to 
)

Returns a pseudorandom floating point number sampled from the uniform distribution [from, to).

Parameters
fromLower distribution bound.
toUpper distribution bound (exclusive).
Returns
The sampled number.

◆ get_formated_timestamp()

std::string sh::utils::get_formated_timestamp ( const std::string &  fmt,
UnixTimestamp  ts = timestamp() 
)
inline

Returns string representing current time and date in formated string based on provided format.

Remarks
Use format string as for put_time method (https://en.cppreference.com/w/cpp/io/manip/put_time)

"%d-%m-%Y_%H-%M-%S" => e.g. "16-11-2019_13-26-45:

Parameters
fmtFormat string using the same rules as put_time method.
Returns
String representing current date and time in desired format.

◆ ipow()

template<typename T_ >
T_ sh::utils::ipow ( T_  b,
std::size_t  p 
)

Unsigned integer power function.

◆ irand()

template<typename T_ >
T_ sh::utils::irand ( T_  from,
T_  to 
)

Returns a pseudorandom integral number sampled from the uniform distribution [from, to].

Parameters
fromLower distribution bound.
toUpper distribution bound (inclusive).
Returns
The sampled number.

◆ is_set()

template<typename T_ >
bool sh::utils::is_set ( T_  mask,
size_t  i 
)

Tests whether the i-th lowest significant bit is set.

Parameters
maskBitfield to test bit in.
iIndex of the LSb in question.
Returns
True if the bit is high.

◆ print_matrix()

template<typename Container_ >
void sh::utils::print_matrix ( const Container_ &  mat)

Prints out any two-dimensional iterable container.

Parameters
matContainer to be printed.
Returns
void
Here is the call graph for this function:

◆ print_vector()

template<typename Container_ >
void sh::utils::print_vector ( const Container_ &  row)

Prints out any one-dimensional iterable container.

Parameters
matContainer to be printed.
Returns
void

◆ read_whole_file()

std::string sh::utils::read_whole_file ( const std::string &  filepath)
inline

Reads the whole file into string that it will return.

Parameters
filepathFilepath of the file to be read.
Returns
Contents of the file in the string.

◆ round_decimal()

template<typename T_ >
T_ sh::utils::round_decimal ( T_  x,
std::size_t  places 
)

Rounds the number to the specified number of decimal places.

Here is the call graph for this function:

◆ serialize_to_file()

template<typename DataType_ >
void sh::utils::serialize_to_file ( DataType_  data,
const std::string  filepath 
)

Serializes the given data into the file using cereal lib.

https://uscilab.github.io/cereal/

◆ sha256_sum()

std::string sh::utils::sha256_sum ( const std::string &  filepath)
inline

Computes the SHA256 hash for the given file and returns it.

Parameters
filepathFilepath of the file to compute hash for.
Returns
Computed hash for the file.

◆ split()

std::vector<std::string> sh::utils::split ( const std::string &  str,
char  delim 
)
inline

Splits the string with the provided character delimiter.

Parameters
strString to be split.
delimDelimiter to split with.
Returns
Vector of resulting strings.

◆ str_to_int()

int sh::utils::str_to_int ( const std::string &  str)
inline

Tries to parse the integer from the string.

Exceptions
std::runtime_errorIf unable to parse from the string.
Parameters
strString to be parsed as an int.
Returns
Parsed integer.

◆ timestamp()

int64_t sh::utils::timestamp ( )
inline

Returns the actual UNIX timestamp (ms).

◆ to_file() [1/2]

template<typename DType_ >
void sh::utils::to_file ( const std::vector< DType_ > &  vec,
const std::string  filepath 
)

Writes the given vector into the given file.

◆ to_file() [2/2]

template<typename DType_ >
void sh::utils::to_file ( const std::vector< std::vector< DType_ >> &  mat,
const std::string  filepath 
)

Writes the given matrix into the given file.

◆ to_lowercase()

std::string sh::utils::to_lowercase ( const std::string &  old)
inline

Returns a copy of old string but all lowercase.

Parameters
oldString to be transformed. \raturns New copy of lowercase string.

◆ trim()

std::string sh::utils::trim ( const std::string &  s)
inline

Trim.

/param s A reference to the string to be trimmed. /returns A new copy of trimmed string.

Here is the call graph for this function:

◆ trim_left()

std::string sh::utils::trim_left ( const std::string &  s)
inline

Left trim.

/param s A reference to the string to be trimmed. /returns A new copy of trimmed string.

◆ trim_right()

std::string sh::utils::trim_right ( const std::string &  s)
inline

Right trim.

/param s A reference to the string to be trimmed. /returns A new copy of trimmed string.

◆ type_name()

template<class T_ >
constexpr std::string_view sh::utils::type_name ( )
constexpr

Returns a string view describing the type T_.

Code from: https://stackoverflow.com/a/20170989/5481153