137 lines
4.1 KiB
C++
137 lines
4.1 KiB
C++
/** @file utils.hpp
|
|
*
|
|
* @author Cory Alexander Balaton (coryab)
|
|
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
|
|
*
|
|
* @version 1.0
|
|
*
|
|
* @brief Function prototypes and macros that are useful.
|
|
*
|
|
* These utility function are mainly for convenience and aren't directly
|
|
* related to the project. Anything that is in the details namespace should
|
|
* not be used directly, or else it might cause undefined behavior if not used
|
|
* correctly.
|
|
*
|
|
* @bug No known bugs
|
|
* */
|
|
#ifndef __UTILS__
|
|
#define __UTILS__
|
|
|
|
#include <armadillo>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <sys/stat.h>
|
|
#include <vector>
|
|
|
|
/** @def DEBUG(msg)
|
|
* @brief Writes a debug message
|
|
*
|
|
* This macro writes a debug message that includes the filename,
|
|
* line number, and a custom message. The function is wrapped in an ifdef
|
|
* that checks if DBG is defined, so one can choose to display the debug
|
|
* messages by adding the -DDBG flag when compiling.
|
|
* */
|
|
#ifdef DBG
|
|
#define DEBUG(msg) \
|
|
std::cout << __FILE__ << " " << __LINE__ << ": " << msg << std::endl
|
|
#else
|
|
#define DEBUG(msg)
|
|
#endif
|
|
|
|
/** @def __METHOD_NAME__
|
|
* @brief Get the name of the current method/function without the return type.
|
|
* */
|
|
#define __METHOD_NAME__ details::methodName(__PRETTY_FUNCTION__)
|
|
|
|
namespace details {
|
|
/** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type.
|
|
*
|
|
* @details This function should only be used for the __METHOD_NAME__ macro,
|
|
* since it takes the output from __PRETTY_FUNCTION__ and strips the return
|
|
* type.
|
|
*
|
|
* @param pretty_function The string from __PRETTY_FUNCTION__
|
|
*
|
|
* @return std::string
|
|
* */
|
|
inline std::string methodName(const std::string &pretty_function)
|
|
{
|
|
size_t colons = pretty_function.find("::");
|
|
size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1;
|
|
size_t end = pretty_function.rfind("(") - begin;
|
|
|
|
return pretty_function.substr(begin, end) + "()";
|
|
}
|
|
|
|
} // namespace details
|
|
|
|
namespace utils {
|
|
|
|
/** @brief Turns a double into a string written in scientific format.
|
|
*
|
|
* @details The code is stolen from https://github.com/anderkve/FYS3150.
|
|
*
|
|
* @param d The number to stringify
|
|
* @param width The reserved width of the string
|
|
* @param prec The precision of the stringified number
|
|
*
|
|
* @return std::string
|
|
* */
|
|
std::string scientific_format(double d, int width = 20, int prec = 10);
|
|
|
|
/** @brief Turns a vector of doubles into a string written in scientific
|
|
* format.
|
|
*
|
|
* @details The code is stolen from https://github.com/anderkve/FYS3150.
|
|
*
|
|
* @param v The vector to stringify
|
|
* @param width The reserved width of the string
|
|
* @param prec The precision of the stringified number
|
|
*
|
|
* @return std::string
|
|
* */
|
|
std::string scientific_format(const std::vector<double> &v, int width = 20,
|
|
int prec = 10);
|
|
|
|
/** @brief Make path given.
|
|
*
|
|
* @details This tries to be the equivalent to "mkdir -p" and creates a new
|
|
* directory whenever it needs to.
|
|
*
|
|
* @param path The path to be created
|
|
* @param mode The mode/permissions for all the new directories
|
|
*
|
|
* @return bool Success/Fail
|
|
* */
|
|
bool mkpath(std::string path, int mode = 0777);
|
|
|
|
/** @brief Get the directory name of the path
|
|
*
|
|
* @param path The path to use.
|
|
*
|
|
* @return string
|
|
* */
|
|
std::string dirname(const std::string &path);
|
|
|
|
/** @brief Take 2 strings and concatenate them and make sure there is a
|
|
* directory separator (/) between them.
|
|
*
|
|
* @details This function doesn't care whether or not the values given as
|
|
* parameters are valid path strings. It is the responsibility of the user to
|
|
* make sure that the values given are valid path strings. The function only
|
|
* guarantees that the output string is a valid path string.
|
|
*
|
|
* @param left The left hand side of the result string
|
|
* @param right The right hand side of the result string
|
|
*
|
|
* @return string
|
|
* */
|
|
std::string concatpath(const std::string &left, const std::string &right);
|
|
|
|
// A function that prints the structure of a sparse matrix to screen.
|
|
void print_sp_matrix_structure(const arma::sp_cx_mat &A);
|
|
} // namespace utils
|
|
|
|
#endif
|