From 320bf2f0155edd67557a3042403eeb843b90a41d Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Sun, 9 Mar 2025 23:15:32 +0300 Subject: added 219 --- libprakpp/include/praktable.hpp | 59 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'libprakpp/include/praktable.hpp') diff --git a/libprakpp/include/praktable.hpp b/libprakpp/include/praktable.hpp index daccedc..2cfa500 100644 --- a/libprakpp/include/praktable.hpp +++ b/libprakpp/include/praktable.hpp @@ -111,7 +111,39 @@ public: 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]; }; + reference operator*() { return parent->data[data_index]; }; + }; + struct const_iterator { + const table *parent; + size_t columns; + size_t col_index = 0, data_index = 0; + using iterator_category = std::bidirectional_iterator_tag; + using value_type = dtype; + using difference_type = ptrdiff_t; + using pointer = const dtype *; + using reference = const dtype &; + const_iterator() = default; + const_iterator(const const_iterator& other) = default; + const_iterator& operator=(const const_iterator& other) = default; + const_iterator(const table *new_parent, const std::string &column, size_t row_idx = 0) + : parent{new_parent}, columns(new_parent->columns) + { + col_index = parent->index(column); + data_index = col_index + row_idx * new_parent->columns; + } + const_iterator &operator++() { data_index += columns; return *this; } + const_iterator &operator--() { data_index -= columns; return *this; } + const_iterator operator++(int) { const_iterator ret = *this; ++(*this); return ret; } + const_iterator operator--(int) { const_iterator ret = *this; --(*this); return ret; } + const_iterator &operator+(int x) { data_index += columns * x; return *this; } + const_iterator &operator-(int x) { data_index -= columns * x; return *this; } + std::strong_ordering operator<=>(const const_iterator& other) const { + return parent == other.parent && col_index == other.col_index && data_index <=> other.data_index; + } + bool operator==(const const_iterator& other) const { + return parent == other.parent && col_index == other.col_index && data_index == other.data_index; + } + reference operator*() const { return parent->data[data_index]; }; }; // Optional rownames: names of rows std::vector opt_rownames; @@ -177,7 +209,9 @@ public: } iterator begin(std::string column) { return iterator(this, column); } + const_iterator cbegin(std::string column) const { return const_iterator(this, column); } iterator end(std::string column) { return iterator(this, column, rows); } + const_iterator cend(std::string column) const { return const_iterator(this, column, rows); } dtype & SUBSCR_OPRTR (const std::string &column, size_t row) noexcept(false) { size_t i = index(column); @@ -240,6 +274,16 @@ public: return *this; } + table& apply(function_type function, const std::string& arg, std::optional result) { + size_t result_index = result.has_value() ? index(*result) : 0; + for (size_t i = 0; i < rows; ++i) { + const std::vector v(1, SUBSCR_OPRTR(arg, i)); + if (result.has_value()) data[columns * i + result_index] = function(v); + else (void)function(v); + } + return *this; + } + table &apply_n(function_type function, std::vector cols, size_t n, std::optional result) { size_t result_index = result.has_value() ? index(*result) : 0; for (size_t i = 0; i < n; ++i) { @@ -475,6 +519,19 @@ public: return accum / rows; } + dtype col_max(const std::string &column) { + dtype max = dtype{}; + for (auto it = begin(column); it != end(column); ++it) + max = max < *it ? *it : max; + return max; + } + dtype col_min(const std::string &column) { + dtype min = dtype{}; + for (auto it = begin(column); it != end(column); ++it) + min = min > *it ? *it : min; + return min; + } + // calculate standard deviation of the column dtype col_stddev(const std::string &column) { dtype accum = dtype{}; -- cgit v1.2.3-70-g09d2