Skip to content

Commit

Permalink
Fix ODR violations via function inlining (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
M2-TE authored Nov 13, 2024
1 parent 60728a7 commit 370dde9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
40 changes: 20 additions & 20 deletions graphs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ namespace graphs
bool check = true;
};

const options defaultoptions;
options histogram_defaultoptions;

template <typename T>
constexpr size_t size(const T &array)
{
Expand All @@ -179,7 +176,7 @@ namespace graphs

// Number of columns needed to represent the string
// Adapted from: https://stackoverflow.com/a/31124065
int strcol(const char *const str)
inline int strcol(const char *const str)
{
size_t length = strlen(str);
for (size_t i = 0; i < length; ++i)
Expand Down Expand Up @@ -224,7 +221,7 @@ namespace graphs
// Word wrap
// Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0
// Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734
string wrap(const char *const str, const size_t line_length)
inline string wrap(const char *const str, const size_t line_length)
{
string words = str;
string wrapped;
Expand Down Expand Up @@ -272,7 +269,7 @@ namespace graphs

// Auto-scale number to unit
// Adapted from: https://github.com/coreutils/coreutils/blob/master/src/numfmt.c
void outputunit(long double number, const units_type scale, ostringstream &strm)
inline void outputunit(long double number, const units_type scale, ostringstream &strm)
{
unsigned x = 0;
long double val = number;
Expand Down Expand Up @@ -353,7 +350,7 @@ namespace graphs
}

// Convert fractions and constants to Unicode characters
void outputfraction(const long double number, ostringstream &strm)
inline void outputfraction(const long double number, ostringstream &strm)
{
bool output = false;

Expand Down Expand Up @@ -403,7 +400,7 @@ namespace graphs
strm << number;
}

size_t outputlabel(const long double label, const units_type units, ostringstream &strm)
inline size_t outputlabel(const long double label, const units_type units, ostringstream &strm)
{
strm.imbue(locale(""));

Expand Down Expand Up @@ -451,7 +448,7 @@ namespace graphs
}

// Output graph
int graph(const size_t height, const size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const vector<vector<unsigned short>> &array, const options &aoptions)
inline int graph(const size_t height, const size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const vector<vector<unsigned short>> &array, const options &aoptions)
{
if (!graphs::size(array))
return 1;
Expand Down Expand Up @@ -777,7 +774,7 @@ namespace graphs
}

template <typename T>
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, /* const */ options &aoptions = histogram_defaultoptions)
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = {})
{
if (!graphs::size(aarray))
return 1;
Expand Down Expand Up @@ -869,14 +866,17 @@ namespace graphs
for (size_t y = ay >= ymax ? 0 : yaxis - (ay / ystep); y < yaxis and y < height; ++y)
aaarray[x][y] = acolor;
}

aoptions.type = type_histogram;


if (aoptions.type != type_histogram) {
options hist_options = aoptions;
hist_options.type = type_histogram;
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, hist_options);
}
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
}

template <typename T>
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T *aarray, /* const */ options &aoptions = histogram_defaultoptions)
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T *aarray, const options &aoptions = {})
{
if (rows == 0)
return 1;
Expand All @@ -889,7 +889,7 @@ namespace graphs

// Convert one or more arrays to graph and output
template <typename T>
int plots(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const options &aoptions = defaultoptions)
int plots(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const options &aoptions = {})
{
if (!graphs::size(arrays))
return 1;
Expand Down Expand Up @@ -1012,7 +1012,7 @@ namespace graphs

// Convert single array to graph and output
template <typename T>
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = defaultoptions)
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = {})
{
const std::array<T, 1> aaarray = {aarray};

Expand All @@ -1021,7 +1021,7 @@ namespace graphs

// Convert single array to graph and output
template <typename T>
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T **aarray, const options &aoptions = defaultoptions)
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T **aarray, const options &aoptions = {})
{
if (rows == 0)
return 1;
Expand All @@ -1036,7 +1036,7 @@ namespace graphs

// Convert one or more functions to graph and output
template <typename T>
int functions(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = defaultoptions)
int functions(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = {})
{
const color_type color = aoptions.color;

Expand Down Expand Up @@ -1125,7 +1125,7 @@ namespace graphs

// Convert single function to function array and output
template <typename T>
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const function<T(T)> &afunction, const options &aoptions = defaultoptions)
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const function<T(T)> &afunction, const options &aoptions = {})
{
std::function<T(T)> afunctions[] = {afunction};

Expand All @@ -1134,7 +1134,7 @@ namespace graphs

// Convert single function to function array and output
template <typename T>
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, T afunction(T), const options &aoptions = defaultoptions)
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, T afunction(T), const options &aoptions = {})
{
std::function<T(T)> afunctions[] = {afunction};

Expand Down
16 changes: 7 additions & 9 deletions tables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ namespace tables
bool check = true;
};

const options defaultoptions;

template <typename T>
constexpr size_t size(const T &array)
{
Expand All @@ -75,7 +73,7 @@ namespace tables

// Number of columns needed to represent the string
// Adapted from: https://stackoverflow.com/a/31124065
int strcol(const char *str)
inline int strcol(const char *str)
{
const string astr = regex_replace(str, ansi, "");
str = astr.c_str();
Expand Down Expand Up @@ -123,7 +121,7 @@ namespace tables
// Word wrap
// Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0
// Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734
string wrap(const char *const str, const size_t line_length)
inline string wrap(const char *const str, const size_t line_length)
{
string words = str;
string wrapped;
Expand Down Expand Up @@ -352,7 +350,7 @@ namespace tables

// Convert array to char array and output as table
template <typename T1, typename T2>
int array(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const options &aoptions = defaultoptions)
int array(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const options &aoptions = {})
{
if (!tables::size(aarray))
return 1;
Expand Down Expand Up @@ -423,7 +421,7 @@ namespace tables
}

template <typename T>
int array(const size_t rows, const size_t columns, T **aarray, const char *const headerrow[] = nullptr, const char *const headercolumn[] = nullptr, const options &aoptions = defaultoptions)
int array(const size_t rows, const size_t columns, T **aarray, const char *const headerrow[] = nullptr, const char *const headercolumn[] = nullptr, const options &aoptions = {})
{
vector<vector<T>> aaarray(rows, vector<T>(columns));
for (size_t i = 0; i < rows; ++i)
Expand Down Expand Up @@ -460,7 +458,7 @@ namespace tables

// Convert one or more functions to array and output as table
template <typename T>
int functions(const long double xmin, const long double xmax, const long double xstep, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = defaultoptions)
int functions(const long double xmin, const long double xmax, const long double xstep, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = {})
{
if (numfunctions == 0)
return 1;
Expand Down Expand Up @@ -538,7 +536,7 @@ namespace tables

// Convert single function to array and output as table
template <typename T>
int function(const long double xmin, const long double xmax, const long double xstep, const function<T(T)> &afunction, const options &aoptions = defaultoptions)
int function(const long double xmin, const long double xmax, const long double xstep, const function<T(T)> &afunction, const options &aoptions = {})
{
std::function<T(T)> afunctions[] = {afunction};

Expand All @@ -547,7 +545,7 @@ namespace tables

// Convert single function to array and output as table
template <typename T>
int function(const long double xmin, const long double xmax, const long double xstep, T afunction(T), const options &aoptions = defaultoptions)
int function(const long double xmin, const long double xmax, const long double xstep, T afunction(T), const options &aoptions = {})
{
std::function<T(T)> afunctions[] = {afunction};

Expand Down

0 comments on commit 370dde9

Please sign in to comment.