diff options
Diffstat (limited to 'libprakpp/include')
-rw-r--r-- | libprakpp/include/praktable.hpp | 144 |
1 files changed, 94 insertions, 50 deletions
diff --git a/libprakpp/include/praktable.hpp b/libprakpp/include/praktable.hpp index 06519ee..daccedc 100644 --- a/libprakpp/include/praktable.hpp +++ b/libprakpp/include/praktable.hpp @@ -20,15 +20,15 @@ namespace prak { -/// truncate a string to n characters, or return in unmodified +// truncate a string to n characters, or return in unmodified inline std::string truncate_or(const std::string &str, size_t n) { if (str.size() >= n) return str.substr(0, n); return str; } -/// Unused for now. -/// TODO: Remove +// Unused for now. +// TODO: Remove template <typename T> struct opt_value { enum struct t: unsigned char { @@ -55,7 +55,7 @@ struct opt_value { }; -/// Class that can store, print, and apply operations to a table used by lab works +// Class that can store, print, and apply operations to a table used by lab works template <typename dtype> class table { std::vector<dtype> data; @@ -85,11 +85,14 @@ public: table *parent; size_t columns; size_t col_index = 0, data_index = 0; - using iterator_category = std::forward_iterator_tag; + using iterator_category = std::bidirectional_iterator_tag; using value_type = dtype; using difference_type = ptrdiff_t; using pointer = dtype *; using reference = dtype &; + iterator() = default; + iterator(const iterator& other) = default; + iterator& operator=(const iterator& other) = default; iterator(table *new_parent, const std::string &column, size_t row_idx = 0) : parent{new_parent}, columns(new_parent->columns) { @@ -97,21 +100,41 @@ public: data_index = col_index + row_idx * new_parent->columns; } iterator &operator++() { data_index += columns; return *this; } + iterator &operator--() { data_index -= columns; return *this; } iterator operator++(int) { iterator ret = *this; ++(*this); return ret; } + iterator operator--(int) { iterator ret = *this; --(*this); return ret; } iterator &operator+(int x) { data_index += columns * x; return *this; } - bool operator==(iterator other) { return data_index == other.data_index && parent == other.parent && col_index == other.col_index; } - bool operator!=(iterator other) { return data_index != other.data_index || parent != other.parent || col_index != other.col_index; } + iterator &operator-(int x) { data_index -= columns * x; return *this; } + std::strong_ordering operator<=>(const iterator& other) { + return parent == other.parent && col_index == other.col_index && data_index <=> other.data_index; + } + bool operator==(const iterator& other) { + return parent == other.parent && col_index == other.col_index && data_index == other.data_index; + } value_type &operator*() { return parent->data[data_index]; }; }; - /// Optional rownames: names of rows + // Optional rownames: names of rows std::vector<std::string> opt_rownames; - /// Mandatory columnnames: names of columns + // Mandatory columnnames: names of columns std::vector<std::string> names; - /// width used for printing, defaults to 8 + // width used for printing, defaults to 8 size_t column_width = 12; - /// default constructor + // default constructor table() = default; + + table(const std::vector<std::string> &cols, std::vector<iterator> iters, size_t rows) + : rows{rows}, columns{cols.size()} + { + names = cols; + data = std::vector<dtype>(rows * cols.size()); + for (size_t row = 0; row < rows; ++row) { + for (size_t col = 0; col < columns; ++col) { + data[row * columns + col] = *iters[col]; + ++iters[col]; + } + } + } explicit table(const std::vector<std::string> &columns, size_t rows, const dtype &deflt) : rows{rows}, columns{columns.size()} @@ -120,7 +143,7 @@ public: data = std::vector<dtype>(rows * columns.size(), deflt); } - /// Data: array of ararys, not freed automatically. + // Data: array of ararys, not freed automatically. explicit table(const std::vector<std::string> &columns, dtype **data, size_t rows /*size_t columns = strings.size()*/) : rows{rows}, columns{columns.size()} { @@ -132,9 +155,9 @@ public: data[i * columns.size() + j] = data[i][j]; } - /// Strings: names for columns - /// `new_data` format: { { entry1_a, entry2_a, ...} { entry1_b, entry2_b, ... }, ... } - /// where `a`, `b`, ... are columns + // Strings: names for columns + // `new_data` format: { { entry1_a, entry2_a, ...} { entry1_b, entry2_b, ... }, ... } + // where `a`, `b`, ... are columns explicit table(std::vector<std::string> &&strings, std::vector<std::vector<dtype>> &&new_data) : rows{new_data.size() ? new_data[0].size() : 0}, columns{strings.size()} { @@ -165,7 +188,7 @@ public: return data.at(names.size() * row + column); } - /// prints a table. defaults to using std::cout, but any std::ostream can be passed in it. + // prints a table. defaults to using std::cout, but any std::ostream can be passed in it. void print(std::ostream &stream = std::cout) const { stream << "columns: " << columns << ", rows: " << rows << std::endl; bool print_names = opt_rownames.size() == data.size() / columns; @@ -196,15 +219,15 @@ public: stream << '|' << std::endl << '|' << rowsep << '|' << std::endl; } - /// Returns whether the amount of names is correct - /// If it is incorrect, names won't be displayed during printing + // Returns whether the amount of names is correct + // If it is incorrect, names won't be displayed during printing bool set_rownames(std::vector<std::string> &&names) { opt_rownames = names; return opt_rownames.size() == data.size() / names.size(); } - /// apply a function to several columns and store result in another column - /// function must accept std::vector or arguments + // apply a function to several columns and store result in another column + // function must accept std::vector or arguments table &apply(function_type function, stringvec args, std::optional<std::string> result) { size_t result_index = result.has_value() ? index(*result) : 0; for (size_t i = 0; i < rows; ++i) { @@ -228,6 +251,22 @@ public: } return *this; } + + table &apply_function( + function_type function, + const std::vector<std::string> cols, + const std::vector<std::string> sgms, + const std::string& res, + const std::string& ress) { + std::vector<iterator> __cols(cols.size()), + __sgms(sgms.size()); + for (size_t i = 0; i < __cols.size() && i < __sgms.size(); ++i) { + __cols[i] = begin(cols[i]); + __sgms[i] = begin(sgms[i]); + } + + return apply_function_n(function, __cols, __sgms, rows, res, ress); + } table &apply_function_n( function_type function, @@ -253,7 +292,7 @@ public: } return *this; } - /// adds a column with name `name` and data `column_data` + // adds a column with name `name` and data `column_data` table &add_column(const std::string &name, std::vector<dtype> column_data) { if (column_data.size() == 0) column_data = std::vector<dtype>(rows, dtype{}); std::vector<dtype> data_new(rows * (++columns)); @@ -270,8 +309,13 @@ public: table &add_column(const std::string &name, dtype dflt = dtype{}) { return add_column(name, std::vector<dtype>(rows, dflt)); } + table &add_columns(const std::vector<std::string> &cols, dtype dflt = dtype{}) { + for (const auto &str : cols) + add_column(str, dflt); + return *this; + } - /// Deletes a column from a table. + // Deletes a column from a table. table &delete_col(const std::string &colname) { std::vector<dtype> data_new(rows * (--columns)); size_t idx = index(colname); @@ -286,7 +330,7 @@ public: return *this; } - /// Deletes several columns + // Deletes several columns table &delete_cols(const stringvec &cols) noexcept(false) { size_t columns_new = columns - cols.size(); std::vector<dtype> data_new(rows * columns_new); @@ -314,7 +358,7 @@ public: } - /// Appends a row to the table. if name is set, appends it to `opt_rownames` + // Appends a row to the table. if name is set, appends it to `opt_rownames` void add_row(std::vector<dtype> values, std::optional<std::string> name = std::nullopt) { if (values.size() == 0) values = std::vector<dtype>(columns, dtype{}); data.resize(columns * (++rows)); @@ -328,17 +372,17 @@ public: return os; } - /// Reads a table from a file in a format: - /// ``` - /// col1 col2 col3 ... - /// val1 val2 val3 ... - /// val4 val5 val6 ... - /// ... - /// ``` - /// Note tha `val` may either be a real number or a question mark, denoting that the value is unknown - /// `col` may be any string without whitespaeces. - /// if the first column is named "__name__" (as in python), first val in each row is a string used as - /// a row name. + // Reads a table from a file in a format: + // ``` + // col1 col2 col3 ... + // val1 val2 val3 ... + // val4 val5 val6 ... + // ... + // ``` + // Note tha `val` may either be a real number or a question mark, denoting that the value is unknown + // `col` may be any string without whitespaeces. + // if the first column is named "__name__" (as in python), first val in each row is a string used as + // a row name. void read(std::ifstream& f) { std::string header; std::getline(f >> std::ws, header); @@ -375,14 +419,14 @@ public: columns = names.size(); } - /// Reads a table from a file specified by `path`. - /// For details, refer to documentation of `void read(std::ifstream&)` overload + // Reads a table from a file specified by `path`. + // For details, refer to documentation of `void read(std::ifstream&)` overload void read(const std::string &path) { std::ifstream f(path); read(f); } - /// Fills a specified column with the same value `v` + // Fills a specified column with the same value `v` table &fill_column(const std::string &column, dtype v) { apply([&v](const std::vector<dtype>& _) -> dtype { return v; }, {}, column); return *this; @@ -403,7 +447,7 @@ public: return *this; } - /// returns an std::pair with coefficients A and B in that order + // returns an std::pair with coefficients A and B in that order std::pair<prak::pvalue<dtype>, prak::pvalue<dtype>> least_squares_linear(std::string x, std::string y, std::optional<std::string> sigma, std::optional<dtype> sigma_fixed) noexcept(false) { @@ -423,7 +467,7 @@ public: return ret; } - /// calculate an average of the column + // calculate an average of the column dtype col_avg(const std::string &column) { dtype accum = dtype{}; for (auto it = begin(column); it != end(column); ++it) @@ -431,7 +475,7 @@ public: return accum / rows; } - /// calculate standard deviation of the column + // calculate standard deviation of the column dtype col_stddev(const std::string &column) { dtype accum = dtype{}; dtype avg = col_avg(column); @@ -440,9 +484,9 @@ public: return std::sqrt(accum); } - /// takes columns [columns], calculates average and standard deviation for each row, puts them into `avg` and `stddev` and deleted original columns - /// if create_columns is true, creates columns avg and stddev - /// This is common thing to do, so might as well write a function for that + // takes columns [columns], calculates average and standard deviation for each row, puts them into `avg` and `stddev` and deleted original columns + // if create_columns is true, creates columns avg and stddev + // This is common thing to do, so might as well write a function for that table& into_avgstddev(const std::vector<std::string> &columns, const std::string &avg_out, const std::string &stddev_out, bool create_columns = false) { if (create_columns) { add_column(avg_out, std::vector<dtype>(rows, dtype{})); @@ -454,8 +498,8 @@ public: return *this; } - /// applies a function `func` to arguments in columns `args`, stores the result in column `result` and standard error in column `result_sigma`. - /// `sigmas` must be in a 1-to-1 correspondance with `args` + // applies a function `func` to arguments in columns `args`, stores the result in column `result` and standard error in column `result_sigma`. + // `sigmas` must be in a 1-to-1 correspondance with `args` table& apply_with_err(function_t<dtype> func, const stringvec &args, const stringvec &sigmas, const std::string &result, const std::string result_sigma) { if (args.size() != sigmas.size()) throw dimension_error("Args and Sigmas did not have the same dimentinons"); size_t result_index = index(result), @@ -481,7 +525,7 @@ public: return col_idx; } - /// Serialize data in format `data[args[0]][i] data[args[1]][i] data[args[2]][i]...` + // Serialize data in format `data[args[0]][i] data[args[1]][i] data[args[2]][i]...` void print_plot(const stringvec &args, std::ostream &out = std::cout) const { std::vector<size_t> offsets(args.size()); for (size_t i = 0; i < args.size(); ++i) { @@ -495,7 +539,7 @@ public: } } - /// Serialize data in format `data[xs][i] data[ys][i] <data[xss][i]> <data>[yss][i]>`, readable by gnuplot with xyerrorbars + // Serialize data in format `data[xs][i] data[ys][i] <data[xss][i]> <data>[yss][i]>`, readable by gnuplot with xyerrorbars table &write_plot_4(const std::string &file, const std::string &xs, const std::string &ys, const std::string &xss, const std::string &yss) { std::ofstream out(file); size_t xi = index(xs), yi = index(ys), xsi = index(xss), ysi = index(yss); @@ -507,7 +551,7 @@ public: return *this; } - /// Serialize data in format `data[xs][i] data[ys][i] <data[ss][i]>`, readable by gnuplot with yerrorbars + // Serialize data in format `data[xs][i] data[ys][i] <data[ss][i]>`, readable by gnuplot with yerrorbars table &write_plot(const std::string &xs, const std::string &ys, std::optional<std::string> yss = std::nullopt, std::ostream &out = std::cout) { size_t nosigma = std::numeric_limits<size_t>::max(); size_t xsi = index(xs), ysi = index(ys), ssi = nosigma; @@ -521,7 +565,7 @@ public: return *this; } - /// Serialize data into a file `file`. For details, refer to documentation for overload with std::ifstream as an argument + // Serialize data into a file `file`. For details, refer to documentation for overload with std::ifstream as an argument table &write_plot(const std::string &file, const std::string &xs, const std::string &ys, std::optional<std::string> yss = std::nullopt) { std::ofstream out(file); return write_plot(xs, ys, yss, out); |