LCOV - code coverage report
Current view: top level - utils - tablecsv.hpp (source / functions) Coverage Total Hit
Test: Palace Coverage Report Lines: 100.0 % 8 8
Test Date: 2025-10-23 22:45:05 Functions: 81.2 % 16 13
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       2              : // SPDX-License-Identifier: Apache-2.0
       3              : 
       4              : #ifndef PALACE_UTILS_TABLECSV_HPP
       5              : #define PALACE_UTILS_TABLECSV_HPP
       6              : 
       7              : #include <cstddef>
       8              : #include <optional>
       9              : #include <string>
      10              : #include <string_view>
      11              : #include <vector>
      12              : 
      13              : namespace palace
      14              : {
      15              : 
      16            2 : struct ColumnOptions
      17              : {
      18              :   // Options that individual cols can overwrite.
      19              :   std::size_t min_left_padding = 8;
      20              :   std::size_t float_precision = 12;
      21              :   std::string fmt_sign = {"+"};
      22              : 
      23              :   // Common options
      24              :   std::string empty_cell_val = {"NULL"};
      25              : };
      26              : 
      27              : class Column
      28              : {
      29              :   friend class Table;
      30              : 
      31              : public:
      32              :   // Map-like index, to interface via Table class.
      33              :   std::string name;
      34              : 
      35              :   [[nodiscard]] std::size_t col_width(const ColumnOptions &defaults = {}) const;
      36              : 
      37              :   [[nodiscard]] auto format_header(const ColumnOptions &defaults = {},
      38              :                                    const std::optional<std::size_t> &width = {}) const;
      39              : 
      40              :   [[nodiscard]] auto format_row(std::size_t i, const ColumnOptions &defaults = {},
      41              :                                 const std::optional<std::size_t> &width = {}) const;
      42              : 
      43            1 :   Column(std::string name, std::string header_text = "", long column_group_idx = 0,
      44              :          std::optional<std::size_t> min_left_padding = {},
      45              :          std::optional<std::size_t> float_precision = {},
      46              :          std::optional<std::string> fmt_sign = {});
      47              : 
      48              :   // Actual Data in Column.
      49              :   std::vector<double> data;
      50              : 
      51              :   // Pretty text to print in file for column header.
      52              :   std::string header_text;
      53              : 
      54              :   // Index to group column into blocks, to verify common cursor.
      55              :   // We assume that column groups are contiguous in table.
      56              :   long column_group_idx = 0;
      57              : 
      58              :   // Column-wise printing options that overwrite default.
      59              :   std::optional<std::size_t> min_left_padding;
      60              :   std::optional<std::size_t> float_precision;
      61              :   std::optional<std::string> fmt_sign;
      62              : 
      63              :   // Quick-fix since leading column of eig is int stored as double (rather then implementing
      64              :   // a templated & type erased solution).
      65              :   bool print_as_int = false;
      66              : 
      67              :   [[nodiscard]] inline std::size_t n_rows() const { return data.size(); }
      68              : 
      69              :   // Convenience operator at higher level.
      70              :   inline auto &operator<<(double val)
      71              :   {
      72          764 :     data.emplace_back(val);
      73          720 :     return *this;
      74              :   }
      75              : };
      76              : 
      77              : class Table
      78              : {
      79              :   // Column-wise mini-table for storing data and and printing to csv file for doubles.
      80              :   // Future: allow int and other output, allow non-owning memory via span.
      81              :   std::vector<Column> cols;
      82              : 
      83              :   // Cache value to reserve vector space by default.
      84              :   std::size_t reserve_n_rows = 0;
      85              : 
      86              : public:
      87          115 :   Table() = default;
      88              :   Table(std::string_view table_str,
      89              :         std::optional<std::string_view> print_col_separator_ = std::nullopt,
      90              :         std::optional<std::string_view> print_row_separator_ = std::nullopt);
      91              : 
      92              :   // Default column options; can be overwritten column-wise.
      93              :   ColumnOptions col_options = {};
      94              : 
      95              :   // Global printing options.
      96              :   std::string_view print_col_separator{",", 1};
      97              :   std::string_view print_row_separator{"\n", 1};
      98              : 
      99              :   // Table properties.
     100              : 
     101              :   [[nodiscard]] bool empty() const { return cols.empty(); }
     102              :   [[nodiscard]] std::size_t n_cols() const { return cols.size(); }
     103              :   [[nodiscard]] std::size_t n_rows() const;
     104              : 
     105              :   void reserve(std::size_t n_rows, std::size_t n_cols);
     106              : 
     107              :   // Insert columns: map like interface.
     108              :   bool insert(Column &&column);
     109              :   template <typename... Args>
     110          688 :   bool insert(Args &&...args)
     111              :   {
     112         1418 :     return insert(Column(std::forward<Args>(args)...));
     113              :   }
     114              : 
     115              :   // Access columns via vector position or column name.
     116              :   inline Column &operator[](std::size_t idx) { return cols.at(idx); }
     117              :   Column &operator[](std::string_view name);
     118              : 
     119              :   inline auto begin() { return cols.begin(); }
     120              :   inline auto end() { return cols.end(); }
     121              :   inline auto cbegin() const { return cols.begin(); }
     122              :   inline auto cend() const { return cols.end(); }
     123              : 
     124              :   // Formatting and Printing Options.
     125              :   template <typename T>
     126              :   void append_header(T &buf) const;
     127              : 
     128              :   template <typename T>
     129              :   void append_row(T &buf, std::size_t row_j) const;
     130              : 
     131              :   [[nodiscard]] std::string format_header() const;
     132              : 
     133              :   [[nodiscard]] std::string format_row(std::size_t j) const;
     134              : 
     135              :   [[nodiscard]] std::string format_table() const;
     136              : };
     137              : 
     138              : // Wrapper for storing Table to csv file.
     139              : 
     140          220 : class TableWithCSVFile
     141              : {
     142              :   std::string csv_file_fullpath_;
     143              : 
     144              : public:
     145              :   Table table = {};
     146              : 
     147              :   TableWithCSVFile() = default;
     148              :   explicit TableWithCSVFile(std::string csv_file_fullpath, bool load_existing_file = false);
     149              : 
     150              :   std::string_view get_csv_filepath() const { return {csv_file_fullpath_}; }
     151              : 
     152              :   void WriteFullTableTrunc();
     153              : };
     154              : 
     155              : }  // namespace palace
     156              : 
     157              : #endif  // PALACE_UTILS_TABLECSV_HPP
        

Generated by: LCOV version 2.0-1