Updated C++ libraries to use namespaces.

This commit is contained in:
Teal Dulcet 2022-07-14 01:44:17 -07:00
parent 274f949079
commit 0540e54213
5 changed files with 1220 additions and 1212 deletions

View File

@ -46,11 +46,11 @@ int main()
// Allocate and set array // Allocate and set array
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
table(rows, columns, array, nullptr, nullptr, aoptions); tables::array(rows, columns, array, nullptr, nullptr, aoptions);
// Deallocate array // Deallocate array
@ -77,11 +77,11 @@ int main()
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
return 0; return 0;
} }
@ -112,11 +112,11 @@ int main()
// Allocate and set array // Allocate and set array
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
table(rows, columns, array, headerrow, headercolumn, aoptions); tables::array(rows, columns, array, headerrow, headercolumn, aoptions);
// Deallocate array // Deallocate array
@ -143,13 +143,13 @@ int main()
// Set array // Set array
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
// or with C++20: // or with C++20:
// tableoptions aoptions{.headerrow = true, .headercolumn = true}; // tables::options aoptions{.headerrow = true, .headercolumn = true};
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
return 0; return 0;
} }
@ -175,7 +175,7 @@ int main()
// Allocate and set array // Allocate and set array
table(rows, columns, array); tables::array(rows, columns, array);
// Deallocate array // Deallocate array
@ -199,7 +199,7 @@ int main()
// Set array // Set array
table(array); tables::array(array);
return 0; return 0;
} }
@ -245,7 +245,7 @@ int main()
sort(array, array + rows, compare<int *>); sort(array, array + rows, compare<int *>);
table(rows, columns, array); tables::array(rows, columns, array);
// Deallocate array // Deallocate array
@ -287,7 +287,7 @@ int main()
sort(array.begin(), array.end(), compare<vector<int>>); sort(array.begin(), array.end(), compare<vector<int>>);
table(array); tables::array(array);
return 0; return 0;
} }
@ -315,10 +315,10 @@ int main()
double xmax = 10; double xmax = 10;
double xscl = 2; double xscl = 2;
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
table(xmin, xmax, xscl, afunction, aoptions); tables::function(xmin, xmax, xscl, afunction, aoptions);
return 0; return 0;
} }
@ -340,10 +340,10 @@ int main()
function<double(double)> afunction = [](auto x) function<double(double)> afunction = [](auto x)
{ return x + 1; }; { return x + 1; };
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
table(xmin, xmax, xscl, afunction, aoptions); tables::function(xmin, xmax, xscl, afunction, aoptions);
return 0; return 0;
} }
@ -382,10 +382,10 @@ int main()
// Function parameter and return value can be any data type, as long as they are the same // Function parameter and return value can be any data type, as long as they are the same
function<double(double)> functions[] = {function1, function2}; function<double(double)> functions[] = {function1, function2};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
table(xmin, xmax, xscl, numfunctions, functions, aoptions); tables::functions(xmin, xmax, xscl, numfunctions, functions, aoptions);
return 0; return 0;
} }
@ -413,10 +413,10 @@ int main()
[](auto x) [](auto x)
{ return pow(x, 2); }}; { return pow(x, 2); }};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
table(xmin, xmax, xscl, numfunctions, functions, aoptions); tables::functions(xmin, xmax, xscl, numfunctions, functions, aoptions);
return 0; return 0;
} }
@ -549,7 +549,7 @@ int main()
// Allocate and set array // Allocate and set array
graph(height, width, xmin, xmax, ymin, ymax, rows, array); graphs::array(height, width, xmin, xmax, ymin, ymax, rows, array);
// Deallocate array // Deallocate array
@ -580,7 +580,7 @@ int main()
// Set array // Set array
graph(height, width, xmin, xmax, ymin, ymax, array); graphs::array(height, width, xmin, xmax, ymin, ymax, array);
return 0; return 0;
} }
@ -590,7 +590,7 @@ If `xmin` and `xmax` are both `0`, they will be set to the respective minimum an
![](images/array%20to%20plot.png) ![](images/array%20to%20plot.png)
Use `graph()` to plot multiple arrays, which can be of different sizes. Use `graphs::arrays()` to plot multiple arrays, which can be of different sizes.
#### Output single function as graph #### Output single function as graph
@ -616,7 +616,7 @@ int main()
long double ymin = -20; long double ymin = -20;
long double ymax = 20; long double ymax = 20;
graph(height, width, xmin, xmax, ymin, ymax, afunction); graphs::function(height, width, xmin, xmax, ymin, ymax, afunction);
return 0; return 0;
} }
@ -642,7 +642,7 @@ int main()
function<double(double)> afunction = [](auto x) function<double(double)> afunction = [](auto x)
{ return x + 1; }; { return x + 1; };
graph(height, width, xmin, xmax, ymin, ymax, afunction); graphs::function(height, width, xmin, xmax, ymin, ymax, afunction);
return 0; return 0;
} }
@ -684,7 +684,7 @@ int main()
// Function parameter and return value can be any data type, as long as they are the same // Function parameter and return value can be any data type, as long as they are the same
function<double(double)> functions[] = {function1, function2}; function<double(double)> functions[] = {function1, function2};
graph(height, width, xmin, xmax, ymin, ymax, numfunctions, functions); graphs::functions(height, width, xmin, xmax, ymin, ymax, numfunctions, functions);
return 0; return 0;
} }
@ -715,7 +715,7 @@ int main()
[](auto x) [](auto x)
{ return pow(x, 2); }}; { return pow(x, 2); }};
graph(height, width, xmin, xmax, ymin, ymax, numfunctions, functions); graphs::functions(height, width, xmin, xmax, ymin, ymax, numfunctions, functions);
return 0; return 0;
} }

View File

@ -66,13 +66,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = i + j; // rand(); array[i][j] = i + j; // rand();
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, rows, array, aoptions); graphs::array(height, width, xmin, xmax, ymin, ymax, rows, array, aoptions);
} }
if (array != nullptr) if (array != nullptr)
@ -90,13 +90,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = i + j; // rand(); aarray[i][j] = i + j; // rand();
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, aarray, aoptions); graphs::array(height, width, xmin, xmax, ymin, ymax, aarray, aoptions);
} }
} }
{ {
@ -106,38 +106,38 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = i + j; // rand(); array[i][j] = i + j; // rand();
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, array, aoptions); graphs::array(height, width, xmin, xmax, ymin, ymax, array, aoptions);
} }
} }
// Output single function as graph // Output single function as graph
cout << "\nOutput single function as graph\n\n"; cout << "\nOutput single function as graph\n\n";
{ {
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, afunction, aoptions); graphs::function(height, width, xmin, xmax, ymin, ymax, afunction, aoptions);
} }
} }
{ {
function<long double(long double)> afunction = [](auto x) function<long double(long double)> afunction = [](auto x)
{ return x + 1; }; { return x + 1; };
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, afunction, aoptions); graphs::function(height, width, xmin, xmax, ymin, ymax, afunction, aoptions);
} }
} }
// Output multiple functions as graph // Output multiple functions as graph
@ -145,13 +145,13 @@ int main()
{ {
function<long double(long double)> functions[] = {function1, function2}; function<long double(long double)> functions[] = {function1, function2};
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, 2, functions, aoptions); graphs::functions(height, width, xmin, xmax, ymin, ymax, 2, functions, aoptions);
} }
} }
{ {
@ -160,13 +160,13 @@ int main()
[](auto x) [](auto x)
{ return pow(x, 2); }}; { return pow(x, 2); }};
graphoptions aoptions; graphs::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, 2, functions, aoptions); graphs::functions(height, width, xmin, xmax, ymin, ymax, 2, functions, aoptions);
} }
} }
{ {
@ -177,15 +177,15 @@ int main()
function<long double(long double)> functions[] = {function3, function4, function5}; function<long double(long double)> functions[] = {function3, function4, function5};
graphoptions aoptions; graphs::options aoptions;
aoptions.axisunitslabel = false; aoptions.axisunitslabel = false;
// graphoptions aoptions{.axisunitslabel = false}; // graphs::options aoptions{.axisunitslabel = false};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(graphs::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
graph(height, width, xmin, xmax, ymin, ymax, 3, functions, aoptions); graphs::functions(height, width, xmin, xmax, ymin, ymax, 3, functions, aoptions);
} }
/* aoptions.style = 2; /* aoptions.style = 2;
@ -195,7 +195,7 @@ int main()
cout << "\e[1;1H" cout << "\e[1;1H"
<< "\e[2J"; << "\e[2J";
graph(k, k, xmin, xmax, ymin, ymax, 3, functions, aoptions); graphs::functions(k, k, xmin, xmax, ymin, ymax, 3, functions, aoptions);
usleep(200000); usleep(200000);
} */ } */

View File

@ -17,9 +17,11 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
using namespace std; namespace graphs
{
using namespace std;
const char *const styles[][11] = { const char *const styles[][11] = {
{"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // ASCII {"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // ASCII
{"", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // Basic {"", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // Basic
{"", "", "", "", "", "", "", "", "", "", ""}, // Light {"", "", "", "", "", "", "", "", "", "", ""}, // Light
@ -27,35 +29,35 @@ const char *const styles[][11] = {
{"", "", "", "", "", "", "", "", "", "", ""}, // Double {"", "", "", "", "", "", "", "", "", "", ""}, // Double
{"", "", "", "", "", "", "", "", "", "", ""}, // Light Dashed {"", "", "", "", "", "", "", "", "", "", ""}, // Light Dashed
{"", "", "", "", "", "", "", "", "", "", ""} // Heavy Dashed {"", "", "", "", "", "", "", "", "", "", ""} // Heavy Dashed
}; };
// {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border
const char *const colors[] = {"\e[39m", "\e[30m", "\e[31m", "\e[32m", "\e[33m", "\e[34m", "\e[35m", "\e[36m", "\e[37m", "\e[90m", "\e[91m", "\e[92m", "\e[93m", "\e[94m", "\e[95m", "\e[96m", "\e[97m"}; const char *const colors[] = {"\e[39m", "\e[30m", "\e[31m", "\e[32m", "\e[33m", "\e[34m", "\e[35m", "\e[36m", "\e[37m", "\e[90m", "\e[91m", "\e[92m", "\e[93m", "\e[94m", "\e[95m", "\e[96m", "\e[97m"};
const char *const dots[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}; const char *const dots[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
const int values[][4] = {{0x1, 0x2, 0x4, 0x40}, {0x8, 0x10, 0x20, 0x80}}; const int values[][4] = {{0x1, 0x2, 0x4, 0x40}, {0x8, 0x10, 0x20, 0x80}};
const char *const fractions[] = {"¼", "½", "¾", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}; const char *const fractions[] = {"¼", "½", "¾", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
const long double fractionvalues[] = {1.0L / 4.0L, 1.0L / 2.0L, 3.0L / 4.0L, 1.0L / 7.0L, 1.0L / 9.0L, 1.0L / 10.0L, 1.0L / 3.0L, 2.0L / 3.0L, 1.0L / 5.0L, 2.0L / 5.0L, 3.0L / 5.0L, 4.0L / 5.0L, 1.0L / 6.0L, 5.0L / 6.0L, 1.0L / 8.0L, 3.0L / 8.0L, 5.0L / 8.0L, 7.0L / 8.0L}; const long double fractionvalues[] = {1.0L / 4.0L, 1.0L / 2.0L, 3.0L / 4.0L, 1.0L / 7.0L, 1.0L / 9.0L, 1.0L / 10.0L, 1.0L / 3.0L, 2.0L / 3.0L, 1.0L / 5.0L, 2.0L / 5.0L, 3.0L / 5.0L, 4.0L / 5.0L, 1.0L / 6.0L, 5.0L / 6.0L, 1.0L / 8.0L, 3.0L / 8.0L, 5.0L / 8.0L, 7.0L / 8.0L};
struct graphoptions struct options
{ {
bool border = true; bool border = true;
bool axislabel = true; bool axislabel = true;
bool axisunitslabel = true; bool axisunitslabel = true;
const char *title = nullptr; const char *title = nullptr;
unsigned int style = 2; unsigned int style = 2;
unsigned int color = 2; unsigned int color = 2;
}; };
const graphoptions defaultoptions; const options defaultoptions;
// Number of columns needed to represent the string // Number of columns needed to represent the string
// Adapted from: https://stackoverflow.com/a/31124065 // Adapted from: https://stackoverflow.com/a/31124065
int strcol(const char *const str) int strcol(const char *const str)
{ {
size_t length = strlen(str); size_t length = strlen(str);
for (size_t i = 0; i < length; ++i) for (size_t i = 0; i < length; ++i)
if (iscntrl(str[i])) if (iscntrl(str[i]))
@ -94,13 +96,13 @@ int strcol(const char *const str)
delete[] wcstring; delete[] wcstring;
return width; return width;
} }
// Word wrap // Word wrap
// Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0 // Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0
// Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734 // Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734
string wrap(const char *const str, const size_t line_length) string wrap(const char *const str, const size_t line_length)
{ {
char words[strlen(str) + 1]; char words[strlen(str) + 1];
strcpy(words, str); strcpy(words, str);
string wrapped; string wrapped;
@ -146,11 +148,11 @@ string wrap(const char *const str, const size_t line_length)
} }
wrapped = words; wrapped = words;
return wrapped; return wrapped;
} }
// Convert fractions and constants to Unicode characters // Convert fractions and constants to Unicode characters
size_t outputlabel(const long double label, ostringstream &strm) size_t outputlabel(const long double label, ostringstream &strm)
{ {
bool output = false; bool output = false;
long double intpart = 0; long double intpart = 0;
@ -209,11 +211,11 @@ size_t outputlabel(const long double label, ostringstream &strm)
size_t length = strcol(strm.str().c_str()); size_t length = strcol(strm.str().c_str());
return length; return length;
} }
// Output graph // 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 graphoptions &aoptions) 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 (!size(array)) if (!size(array))
return 1; return 1;
@ -483,12 +485,12 @@ int graph(const size_t height, const size_t width, const long double xmin, const
cout << endl; cout << endl;
return 0; return 0;
} }
// Convert one or more arrays to graph and output // Convert one or more arrays to graph and output
template <typename T> template <typename T>
int graphs(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const graphoptions &aoptions = defaultoptions) int arrays(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)
{ {
if (!size(arrays)) if (!size(arrays))
return 1; return 1;
@ -592,21 +594,21 @@ int graphs(size_t height, size_t width, long double xmin, long double xmax, long
} }
return graph(height, width, xmin, xmax, ymin, ymax, aarray, aoptions); return graph(height, width, xmin, xmax, ymin, ymax, aarray, aoptions);
} }
// Convert single array to graph and output // Convert single array to graph and output
template <typename T> template <typename T>
int graph(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const graphoptions &aoptions = defaultoptions) int array(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)
{ {
const std::array<T, 1> aaarray = {aarray}; const std::array<T, 1> aaarray = {aarray};
return graphs(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions); return arrays(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
} }
// Convert single array to graph and output // Convert single array to graph and output
template <typename T> template <typename T>
int graph(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 graphoptions &aoptions = defaultoptions) int array(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)
{ {
if (rows == 0) if (rows == 0)
return 1; return 1;
@ -615,13 +617,13 @@ int graph(size_t height, size_t width, long double xmin, long double xmax, long
for (unsigned int i = 0; i < rows; ++i) for (unsigned int i = 0; i < rows; ++i)
copy(aarray[i], aarray[i] + columns, aaarray[i].begin()); copy(aarray[i], aarray[i] + columns, aaarray[i].begin());
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions); return array(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
} }
// Convert one or more functions to graph and output // Convert one or more functions to graph and output
template <typename T> template <typename T>
int graph(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 graphoptions &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 = defaultoptions)
{ {
const unsigned int color = aoptions.color; const unsigned int color = aoptions.color;
if (color >= size(colors)) if (color >= size(colors))
@ -701,22 +703,24 @@ int graph(size_t height, size_t width, const long double xmin, const long double
} }
return graph(height, width, xmin, xmax, ymin, ymax, array, aoptions); return graph(height, width, xmin, xmax, ymin, ymax, array, aoptions);
} }
// Convert single function to function array and output // Convert single function to function array and output
template <typename T> template <typename T>
int graph(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 graphoptions &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 = defaultoptions)
{ {
std::function<T(T)> afunctions[] = {afunction}; std::function<T(T)> afunctions[] = {afunction};
return graph(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions); return functions(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions);
} }
// Convert single function to function array and output // Convert single function to function array and output
template <typename T> template <typename T>
int graph(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 graphoptions &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 = defaultoptions)
{ {
std::function<T(T)> afunctions[] = {afunction}; std::function<T(T)> afunctions[] = {afunction};
return graph(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions); return functions(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions);
}
} }

View File

@ -94,13 +94,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand(); array[i][j] = rand();
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(rows, columns, array, nullptr, nullptr, aoptions); tables::array(rows, columns, array, nullptr, nullptr, aoptions);
} }
if (array != nullptr) if (array != nullptr)
@ -118,13 +118,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = rand(); aarray[i][j] = rand();
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -134,13 +134,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand(); array[i][j] = rand();
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -153,13 +153,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX); array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(rows, columns, array, nullptr, nullptr, aoptions); tables::array(rows, columns, array, nullptr, nullptr, aoptions);
} }
if (array != nullptr) if (array != nullptr)
@ -177,13 +177,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX); aarray[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -193,13 +193,13 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX); array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
} }
} }
// Output char array as table // Output char array as table
@ -212,16 +212,16 @@ int main()
{"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"}, {"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"},
{"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}}; {"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
// tableoptions aoptions{.headerrow = true, .headercolumn = true}; // tables::options aoptions{.headerrow = true, .headercolumn = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -231,16 +231,16 @@ int main()
{"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"}, {"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"},
{"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}}}; {"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}}};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
// tableoptions aoptions{.headerrow = true, .headercolumn = true}; // tables::options aoptions{.headerrow = true, .headercolumn = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
} }
// Output array as table with separate header row and column // Output array as table with separate header row and column
@ -258,16 +258,16 @@ int main()
const char *const headerrow[] = {"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"}; const char *const headerrow[] = {"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"};
const char *const headercolumn[] = {"Header column 2", "Header column 3", "Header column 4", "Header column 5"}; const char *const headercolumn[] = {"Header column 2", "Header column 3", "Header column 4", "Header column 5"};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
// tableoptions aoptions{.headerrow = true, .headercolumn = true}; // tables::options aoptions{.headerrow = true, .headercolumn = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -286,116 +286,116 @@ int main()
vector<string> aheadercolumn(headerrow, headerrow + 1); vector<string> aheadercolumn(headerrow, headerrow + 1);
aheadercolumn.insert(aheadercolumn.end(), headercolumn, headercolumn + columns - 1); aheadercolumn.insert(aheadercolumn.end(), headercolumn, headercolumn + columns - 1);
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
{ {
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
aoptions.cellborder = true; aoptions.cellborder = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.headerrow = true, .headercolumn = true, .cellborder = true, .style = k}; // tables::options aoptions{.headerrow = true, .headercolumn = true, .cellborder = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.headerrow = true, .headercolumn = true, .style = k}; // tables::options aoptions{.headerrow = true, .headercolumn = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = aheaderrow.data(); string *headerrow = aheaderrow.data();
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.headerrow = true, .style = k}; // tables::options aoptions{.headerrow = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = aheadercolumn.data(); string *headercolumn = aheadercolumn.data();
tableoptions aoptions; tables::options aoptions;
aoptions.headercolumn = true; aoptions.headercolumn = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.headercolumn = true, .style = k}; // tables::options aoptions{.headercolumn = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.cellborder = true; aoptions.cellborder = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.cellborder = true, .style = k}; // tables::options aoptions{.cellborder = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.tableborder = false; aoptions.tableborder = false;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.tableborder = false, .style = k}; // tables::options aoptions{.tableborder = false, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
tableoptions aoptions; tables::options aoptions;
aoptions.tableborder = false; aoptions.tableborder = false;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.headercolumn = true; aoptions.headercolumn = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.tableborder = false, .headerrow = true, .headercolumn = true, .style = k}; // tables::options aoptions{.tableborder = false, .headerrow = true, .headercolumn = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = aheaderrow.data(); string *headerrow = aheaderrow.data();
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.tableborder = false; aoptions.tableborder = false;
aoptions.headerrow = true; aoptions.headerrow = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.tableborder = false, .headerrow = true, .style = k}; // tables::options aoptions{.tableborder = false, .headerrow = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = aheadercolumn.data(); string *headercolumn = aheadercolumn.data();
tableoptions aoptions; tables::options aoptions;
aoptions.tableborder = false; aoptions.tableborder = false;
aoptions.headercolumn = true; aoptions.headercolumn = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.tableborder = false, .headercolumn = true, .style = k}; // tables::options aoptions{.tableborder = false, .headercolumn = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
{ {
string *headerrow = nullptr; string *headerrow = nullptr;
string *headercolumn = nullptr; string *headercolumn = nullptr;
tableoptions aoptions; tables::options aoptions;
aoptions.tableborder = false; aoptions.tableborder = false;
aoptions.cellborder = true; aoptions.cellborder = true;
aoptions.style = k; aoptions.style = k;
// tableoptions aoptions{.tableborder = false, .cellborder = true, .style = k}; // tables::options aoptions{.tableborder = false, .cellborder = true, .style = k};
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
} }
} }
@ -409,15 +409,15 @@ int main()
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand() % 2; array[i][j] = rand() % 2;
tableoptions aoptions; tables::options aoptions;
aoptions.boolalpha = true; aoptions.boolalpha = true;
// tableoptions aoptions{.boolalpha = true}; // tables::options aoptions{.boolalpha = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(rows, columns, array, nullptr, nullptr, aoptions); tables::array(rows, columns, array, nullptr, nullptr, aoptions);
} }
if (array != nullptr) if (array != nullptr)
@ -446,13 +446,13 @@ int main()
// qsort(array, rows, sizeof(array[0]), compare<int *>); // qsort(array, rows, sizeof(array[0]), compare<int *>);
sort(array, array + rows, compare<int *>); sort(array, array + rows, compare<int *>);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(rows, columns, array, nullptr, nullptr, aoptions); tables::array(rows, columns, array, nullptr, nullptr, aoptions);
} }
if (array != nullptr) if (array != nullptr)
@ -475,13 +475,13 @@ int main()
sort(aarray.begin(), aarray.end(), compare<array<int, columns>>); sort(aarray.begin(), aarray.end(), compare<array<int, columns>>);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(aarray, headerrow, headercolumn, aoptions); tables::array(aarray, headerrow, headercolumn, aoptions);
} }
} }
{ {
@ -496,42 +496,42 @@ int main()
sort(array.begin(), array.end(), compare<vector<int>>); sort(array.begin(), array.end(), compare<vector<int>>);
tableoptions aoptions; tables::options aoptions;
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(array, headerrow, headercolumn, aoptions); tables::array(array, headerrow, headercolumn, aoptions);
} }
} }
// Output single function as table // Output single function as table
cout << "\nOutput single function as table\n\n"; cout << "\nOutput single function as table\n\n";
{ {
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
// tableoptions aoptions{.headerrow = true}; // tables::options aoptions{.headerrow = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(xmin, xmax, xscl, afunction, aoptions); tables::function(xmin, xmax, xscl, afunction, aoptions);
} }
} }
{ {
function<long double(long double)> afunction = [](auto x) function<long double(long double)> afunction = [](auto x)
{ return x + 1; }; { return x + 1; };
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
// tableoptions aoptions{.headerrow = true}; // tables::options aoptions{.headerrow = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(xmin, xmax, xscl, afunction, aoptions); tables::function(xmin, xmax, xscl, afunction, aoptions);
} }
} }
// Output multiple functions as table // Output multiple functions as table
@ -539,15 +539,15 @@ int main()
{ {
function<long double(long double)> functions[] = {function1, function2}; function<long double(long double)> functions[] = {function1, function2};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
// tableoptions aoptions{.headerrow = true}; // tables::options aoptions{.headerrow = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(xmin, xmax, xscl, 2, functions, aoptions); tables::functions(xmin, xmax, xscl, 2, functions, aoptions);
} }
} }
{ {
@ -556,15 +556,15 @@ int main()
[](auto x) [](auto x)
{ return pow(x, 2); }}; { return pow(x, 2); }};
tableoptions aoptions; tables::options aoptions;
aoptions.headerrow = true; aoptions.headerrow = true;
// tableoptions aoptions{.headerrow = true}; // tables::options aoptions{.headerrow = true};
for (unsigned int k = 0; k < size(styles); ++k) for (unsigned int k = 0; k < size(tables::styles); ++k)
{ {
aoptions.style = k; aoptions.style = k;
table(xmin, xmax, xscl, 2, functions, aoptions); tables::functions(xmin, xmax, xscl, 2, functions, aoptions);
} }
} }

View File

@ -13,9 +13,11 @@
#include <unistd.h> #include <unistd.h>
#include <regex> #include <regex>
using namespace std; namespace tables
{
using namespace std;
const char *const styles[][11] = { const char *const styles[][11] = {
{"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // ASCII {"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // ASCII
{"", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // Basic {"", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // Basic
{"", "", "", "", "", "", "", "", "", "", ""}, // Light {"", "", "", "", "", "", "", "", "", "", ""}, // Light
@ -23,13 +25,13 @@ const char *const styles[][11] = {
{"", "", "", "", "", "", "", "", "", "", ""}, // Double {"", "", "", "", "", "", "", "", "", "", ""}, // Double
{"", "", "", "", "", "", "", "", "", "", ""}, // Light Dashed {"", "", "", "", "", "", "", "", "", "", ""}, // Light Dashed
{"", "", "", "", "", "", "", "", "", "", ""} // Heavy Dashed {"", "", "", "", "", "", "", "", "", "", ""} // Heavy Dashed
}; };
// {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border
regex ansi(R"(\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m)"); regex ansi(R"(\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m)");
struct tableoptions struct options
{ {
bool headerrow = false; bool headerrow = false;
bool headercolumn = false; bool headercolumn = false;
bool tableborder = true; bool tableborder = true;
@ -39,14 +41,14 @@ struct tableoptions
bool boolalpha = false; bool boolalpha = false;
const char *title = nullptr; const char *title = nullptr;
unsigned int style = 2; unsigned int style = 2;
}; };
const tableoptions defaultoptions; const options defaultoptions;
// Number of columns needed to represent the string // Number of columns needed to represent the string
// Adapted from: https://stackoverflow.com/a/31124065 // Adapted from: https://stackoverflow.com/a/31124065
int strcol(const char *str) int strcol(const char *str)
{ {
const string astr = regex_replace(str, ansi, ""); const string astr = regex_replace(str, ansi, "");
str = astr.c_str(); str = astr.c_str();
@ -88,13 +90,13 @@ int strcol(const char *str)
delete[] wcstring; delete[] wcstring;
return width; return width;
} }
// Word wrap // Word wrap
// Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0 // Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0
// Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734 // Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734
string wrap(const char *const str, const size_t line_length) string wrap(const char *const str, const size_t line_length)
{ {
char words[strlen(str) + 1]; char words[strlen(str) + 1];
strcpy(words, str); strcpy(words, str);
string wrapped; string wrapped;
@ -140,12 +142,12 @@ string wrap(const char *const str, const size_t line_length)
} }
wrapped = words; wrapped = words;
return wrapped; return wrapped;
} }
// Output char array as table // Output char array as table
template <typename T> template <typename T>
int table(const vector<vector<basic_string<T>>> &array, const tableoptions &aoptions) int table(const vector<vector<basic_string<T>>> &array, const options &aoptions)
{ {
if (!size(array)) if (!size(array))
return 1; return 1;
@ -314,12 +316,12 @@ int table(const vector<vector<basic_string<T>>> &array, const tableoptions &aopt
cout << endl; cout << endl;
return 0; return 0;
} }
// Convert array to char array and output as table // Convert array to char array and output as table
template <typename T1, typename T2> template <typename T1, typename T2>
int table(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const tableoptions &aoptions = defaultoptions) int array(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const options &aoptions = defaultoptions)
{ {
if (!size(aarray)) if (!size(aarray))
return 1; return 1;
@ -386,11 +388,11 @@ int table(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullpt
} }
return table(aaarray, aoptions); return table(aaarray, aoptions);
} }
template <typename T> template <typename T>
int table(const size_t rows, const size_t columns, T **aarray, const char *const headerrow[] = nullptr, const char *const headercolumn[] = nullptr, const tableoptions &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 = defaultoptions)
{ {
vector<vector<T>> aaarray(rows, vector<T>(columns)); vector<vector<T>> aaarray(rows, vector<T>(columns));
for (unsigned int i = 0; i < rows; ++i) for (unsigned int i = 0; i < rows; ++i)
copy(aarray[i], aarray[i] + columns, aaarray[i].begin()); copy(aarray[i], aarray[i] + columns, aaarray[i].begin());
@ -421,13 +423,13 @@ int table(const size_t rows, const size_t columns, T **aarray, const char *const
aheadercolumn = aaheadercolumn.data(); aheadercolumn = aaheadercolumn.data();
} }
return table(aaarray, aheaderrow, aheadercolumn, aoptions); return array(aaarray, aheaderrow, aheadercolumn, aoptions);
} }
// Convert one or more functions to array and output as table // Convert one or more functions to array and output as table
template <typename T> template <typename T>
int table(const long double xmin, const long double xmax, const long double xscl, const size_t numfunctions, function<T(T)> functions[], const tableoptions &aoptions = defaultoptions) int functions(const long double xmin, const long double xmax, const long double xscl, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = defaultoptions)
{ {
if (numfunctions == 0) if (numfunctions == 0)
return 1; return 1;
@ -487,7 +489,7 @@ int table(const long double xmin, const long double xmax, const long double xscl
aarray[i][j + 1] = (functions[j])(aarray[i][0]); aarray[i][j + 1] = (functions[j])(aarray[i][0]);
} }
int code = table(aarray, headerrow, headercolumn, aoptions); int code = array(aarray, headerrow, headercolumn, aoptions);
if (headerrow != nullptr) if (headerrow != nullptr)
{ {
@ -500,22 +502,24 @@ int table(const long double xmin, const long double xmax, const long double xscl
// } // }
return code; return code;
} }
// Convert single function to array and output as table // Convert single function to array and output as table
template <typename T> template <typename T>
int table(const long double xmin, const long double xmax, const long double xscl, const function<T(T)> &afunction, const tableoptions &aoptions = defaultoptions) int function(const long double xmin, const long double xmax, const long double xscl, const function<T(T)> &afunction, const options &aoptions = defaultoptions)
{ {
std::function<T(T)> afunctions[] = {afunction}; std::function<T(T)> afunctions[] = {afunction};
return table(xmin, xmax, xscl, 1, afunctions, aoptions); return functions(xmin, xmax, xscl, 1, afunctions, aoptions);
} }
// Convert single function to array and output as table // Convert single function to array and output as table
template <typename T> template <typename T>
int table(const long double xmin, const long double xmax, const long double xscl, T afunction(T), const tableoptions &aoptions = defaultoptions) int function(const long double xmin, const long double xmax, const long double xscl, T afunction(T), const options &aoptions = defaultoptions)
{ {
std::function<T(T)> afunctions[] = {afunction}; std::function<T(T)> afunctions[] = {afunction};
return table(xmin, xmax, xscl, 1, afunctions, aoptions); return functions(xmin, xmax, xscl, 1, afunctions, aoptions);
}
} }