GEOS
TableData.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_FORMAT_TABLE_TABLEDATA_HPP
21 #define GEOS_COMMON_FORMAT_TABLE_TABLEDATA_HPP
22 
23 #include "common/Units.hpp"
24 #include "common/DataTypes.hpp"
25 #include "common/format/Format.hpp"
26 #include "TableTypes.hpp"
27 
28 namespace geos
29 {
30 
34 class TableData
35 {
36 public:
37 
41  struct CellData
42  {
46  string value = "";
47  };
48 
54  template< typename ... Args >
55  void addRow( Args const & ... args );
56 
61  void addRow( std::vector< CellData > const & row );
62 
67  void addSeparator();
68 
72  void clear();
73 
77  std::vector< std::vector< CellData > > const & getTableDataRows() const;
78 
83  std::vector< string > const & getErrorMsgs() const;
84 
85 private:
86 
88  std::vector< std::vector< CellData > > m_rows;
89 
90 };
91 
96 {
97 public:
98 
100  using RowType = real64;
103 
106  {
109  std::vector< string > headerNames;
112  };
113 
121  template< typename T >
122  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
123 
131  arraySlice1d< real64 const > columnAxisValues,
133 
143  units::Unit const valueUnit,
144  ArrayOfArraysView< real64 const > const coordinates,
145  string_view rowAxisDescription,
146  string_view columnAxisDescription );
147 
158  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
159 
160 private:
162  std::map< RowType, std::map< ColumnType, string > > m_data;
163 
165  std::set< real64 > m_columnValues;
166 };
167 
172 template< typename T >
173 constexpr bool isCellType = std::is_same_v< T, CellType >;
174 
175 template< typename ... Args >
176 void TableData::addRow( Args const &... args )
177 {
178  std::vector< CellData > cells;
179  ( [&] {
180  static_assert( has_formatter_v< decltype(args) > || isCellType< std::decay_t< decltype(args) > >, "Argument passed in addRow cannot be converted to string nor a CellType" );
181  if constexpr (std::is_same_v< Args, CellType >) {
182  if( args == CellType::Separator )
183  {
184  cells.push_back( {CellType::Separator} );
185  }
186  else
187  {
188  cells.push_back( {CellType::MergeNext} );
189  }
190  }
191  else
192  {
193  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
194  }
195  } (), ...);
196  addRow( cells );
197 }
198 
199 template< typename T >
200 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
201 {
202  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
203  m_columnValues.insert( columnValue );
204  m_data[rowValue][columnValue] = GEOS_FMT( "{}", value );
205 }
206 
207 }
208 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEDATA_HPP */
Enumerates the Units that are in use in GEOS and regroups useful conversion and formatting functions.
Unit
Enumerator of available unit types. Units are in SI by default.
Definition: Units.hpp:56
Class for managing 2D table m_data.
Definition: TableData.hpp:96
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:100
TableData2D::TableDataHolder convertTable2D(arrayView1d< real64 const > const values, units::Unit const valueUnit, ArrayOfArraysView< real64 const > const coordinates, string_view rowAxisDescription, string_view columnAxisDescription)
void addCell(RowType rowValue, ColumnType columnValue, T const &value)
Add a cell to the table. If necessary, create automatically the containing column & row.
Definition: TableData.hpp:200
void collectTableValues(arraySlice1d< real64 const > rowAxisValues, arraySlice1d< real64 const > columnAxisValues, arrayView1d< real64 const > values)
Collects all the values needed to build the table.
TableDataHolder buildTableData(string_view dataDescription, string_view rowFmt="{}", string_view columnFmt="{}") const
real64 ColumnType
Type real64 for a column.
Definition: TableData.hpp:102
Class for managing table data.
Definition: TableData.hpp:35
void clear()
Reset data in the table.
std::vector< std::vector< CellData > > const & getTableDataRows() const
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:176
std::vector< string > const & getErrorMsgs() const
Get all error messages.
void addSeparator()
Add a line separator to the table You must have filled values in TableData before using it.
void addRow(std::vector< CellData > const &row)
Add a row to the table.
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:180
LvArray::ArrayOfArraysView< T, INDEX_TYPE const, CONST_SIZES, LvArray::ChaiBuffer > ArrayOfArraysView
View of array of variable-sized arrays. See LvArray::ArrayOfArraysView for details.
Definition: DataTypes.hpp:286
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
constexpr bool isCellType
Trait to check is the args is a special type of cell.
Definition: TableData.hpp:173
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:184
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:33
std::string_view string_view
String type.
Definition: DataTypes.hpp:94
Struct containing conversion informations.
Definition: TableData.hpp:106
TableData tableData
TableData to be built.
Definition: TableData.hpp:111
std::vector< string > headerNames
Definition: TableData.hpp:109
Representing a data in TableData.
Definition: TableData.hpp:42
string value
The cell value.
Definition: TableData.hpp:46
CellType type
The cell type.
Definition: TableData.hpp:44