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 
53  bool operator==( CellData const & other ) const
54  {
55  return value == other.value;
56  }
57  };
58 
61 
67  template< typename ... Args >
68  void addRow( Args const & ... args );
69 
74  void addRow( stdVector< CellData > const & row );
75 
80  void addSeparator();
81 
85  void clear();
86 
91 
97 
101  DataRows const & getCellsData() const
102  { return m_rows; }
103 
109  inline bool operator==( TableData const & comparingTable ) const
110  {
111 
112  return getCellsData() == comparingTable.getCellsData();
113  }
114 
115 private:
117  DataRows m_rows;
118 
119 };
120 
121 
126 {
127 public:
128 
130  using RowType = real64;
133 
136  {
142  };
143 
151  template< typename T >
152  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
153 
162  arraySlice1d< real64 const > dim1AxisCoordinates,
164  bool columnMajorValues );
165 
176  string_view rowAxisDescription,
177  string_view columnAxisDescription,
178  arrayView1d< real64 const > const values,
179  bool columnMajorValues,
180  string_view valueDescription );
181 
192  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
193 
194 private:
196  std::map< RowType, std::map< ColumnType, string > > m_data;
197 
199  std::set< real64 > m_columnValues;
200 };
201 
206 template< typename T >
207 constexpr bool isCellType = std::is_same_v< T, CellType >;
208 
209 template< typename ... Args >
210 void TableData::addRow( Args const &... args )
211 {
212  stdVector< CellData > cells;
213  ( [&] {
214  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" );
215  if constexpr (std::is_same_v< Args, CellType >) {
216  cells.push_back( { args, string() } );
217  }
218  else
219  {
220  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
221  }
222  } (), ...);
223  addRow( cells );
224 }
225 
226 template< typename T >
227 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
228 {
229  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
230  m_columnValues.insert( columnValue );
231  m_data[rowValue][columnValue] = GEOS_FMT( "{}", value );
232 }
233 
234 }
235 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEDATA_HPP */
Enumerates the Units that are in use in GEOS and regroups useful conversion and formatting functions.
Class for managing 2D table m_data.
Definition: TableData.hpp:126
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:130
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:227
void collectTableValues(arraySlice1d< real64 const > dim0AxisCoordinates, arraySlice1d< real64 const > dim1AxisCoordinates, arrayView1d< real64 const > values, bool columnMajorValues)
Collects all the values needed to build the table.
TableDataHolder buildTableData(string_view dataDescription, string_view rowFmt="{}", string_view columnFmt="{}") const
TableData2D::TableDataHolder convertTable2D(ArrayOfArraysView< real64 const > const coordinates, string_view rowAxisDescription, string_view columnAxisDescription, arrayView1d< real64 const > const values, bool columnMajorValues, string_view valueDescription)
real64 ColumnType
Type real64 for a column.
Definition: TableData.hpp:132
Class for managing table data.
Definition: TableData.hpp:35
DataRows const & getCellsData() const
Definition: TableData.hpp:101
stdVector< string > const & getErrorMsgs() const
Get all error messages.
void clear()
Reset data in the table.
void addRow(stdVector< CellData > const &row)
Add a row to the table.
bool operator==(TableData const &comparingTable) const
Comparison operator for data rows.
Definition: TableData.hpp:109
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:210
void addSeparator()
Add a line separator to the table You must have filled values in TableData before using it.
stdVector< stdVector< CellData > > DataRows
Alias for table data rows with cells values.
Definition: TableData.hpp:60
stdVector< stdVector< CellData > > const & getTableDataRows() const
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:188
std::string string
String type.
Definition: DataTypes.hpp:90
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:294
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
constexpr bool isCellType
Trait to check is the args is a special type of cell.
Definition: TableData.hpp:207
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:192
std::vector< T, Allocator > stdVector
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:33
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
Struct containing conversion informations.
Definition: TableData.hpp:136
stdVector< string > headerNames
Definition: TableData.hpp:139
TableData tableData
TableData to be built.
Definition: TableData.hpp:141
Representing a data in TableData.
Definition: TableData.hpp:42
bool operator==(CellData const &other) const
Comparison operator for cell value.
Definition: TableData.hpp:53
string value
The cell value.
Definition: TableData.hpp:46
CellType type
The cell type.
Definition: TableData.hpp:44