/** @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.
 *
 *  @bug No known bugs
 * */
#ifndef __UTILS__
#define __UTILS__

#include <armadillo>
#include <iomanip>
#include <sstream>
#include <string>
#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 ASSERT(expr)
 *  @brief A prettier assertion function.
 *
 *  This macro calls the m_assert function which is a more informative
 *  assertion function than the regular assert function from cassert.
 * */
#define ASSERT(expr, msg) m_assert(expr, #expr, __METHOD_NAME__, __FILE__, \
                                   __LINE__, msg)

#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)


/** Code stolen from https://github.com/anderkve/FYS3150
 *  Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp
 *  Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp
 * */

/** @brief Turns a double into a string written in scientific format.
 *
 *  @param d The number to stringify
 *  @param width The reserved width of the string
 *  @param prec The precision of the stringified number
 *
 *  @return 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.
 *
 *  @param v The vector to stringify
 *  @param width The reserved width of the string
 *  @param prec The precision of the stringified number
 *
 *  @return String
 * */
std::string scientific_format(const std::vector<double>& v, 
                              int width=20, 
                              int prec=10);

/** @brief Test an expression, confirm that test is ok, or abort execution.
 *
 *  This function takes in an expression and prints an OK message if it's
 *  true, or it prints a fail message and aborts execution if it fails.
 *
 *  @param expr The expression to be evaluated
 *  @param expr_str The stringified version of the expression
 *  @param func The function name of the caller
 *  @param file The file of the caller
 *  @param line The line number where this function is called from
 *  @param msg The message to be displayed
 * */
void m_assert(bool expr, 
              std::string expr_str, 
              std::string func, 
              std::string file, 
              int line,
              std::string msg);


/** @brief Test if two armadillo vectors are close to each other.
 *
 *  This function takes in 2 vectors and checks if they are approximately
 *  equal to each other given a tolerance.
 *
 *  @param a Vector a
 *  @param b Vector b
 *  @param tol The tolerance
 *
 *  @return Boolean
 * */
bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol=1e-8);


static inline std::string methodName(const std::string& prettyFunction)
{
    size_t colons = prettyFunction.find("::");
    size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
    size_t end = prettyFunction.rfind("(") - begin;

    return prettyFunction.substr(begin,end) + "()";
}

#endif
