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 
39  TableData();
40 
41  TableData( TableData const & other );
42 
43  TableData( TableData && other );
44 
45  TableData & operator=( TableData const & other );
46 
47  TableData & operator=( TableData && other );
49 
55  bool operator<( TableData const & other ) const;
56 
60  struct CellData
61  {
65  string value;
66 
68  bool operator==( CellData const & other ) const
69  {
70  return value == other.value;
71  }
72 
73  bool operator<( CellData const & other ) const
74  {
75  return value < other.value;
76  }
78  };
79 
82 
88  template< typename ... Args >
89  void addRow( Args const & ... args );
90 
95  void addRow( stdVector< CellData > const & row );
96 
101  void addSeparator();
102 
106  void clear();
107 
111  void clearErrors()
112  { m_errors->clear(); }
113 
118 
123 
129 
133  DataRows const & getCellsData() const
134  { return m_rows; }
135 
141  inline bool operator==( TableData const & comparingTable ) const
142  {
143 
144  return getCellsData() == comparingTable.getCellsData();
145  }
146 
152  { return *m_errors; }
153 
159  { return *m_errors; }
160 
161 private:
163  DataRows m_rows;
164 
166  std::unique_ptr< geos::TableErrorListing > m_errors;
167 
168 };
169 
170 
175 {
176 public:
177 
179  using RowType = real64;
182 
185  {
191  };
192 
200  template< typename T >
201  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
202 
211  arrayView1d< real64 const > dim1AxisCoordinates,
213  bool columnMajorValues );
214 
227  string_view rowAxisDescription,
228  string_view columnAxisDescription,
229  arrayView1d< real64 const > const values,
230  bool columnMajorValues,
231  string_view valueDescription );
232 
243  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
244 
248  inline void clear()
249  {
250  m_data.clear();
251  m_columnValues.clear();
252  m_errors->clear();
253  }
254 
255 private:
257  std::map< RowType, std::map< ColumnType, string > > m_data;
259  std::set< real64 > m_columnValues;
261  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
262 };
263 
268 template< typename T >
269 constexpr bool isCellType = std::is_same_v< T, CellType >;
270 
271 template< typename ... Args >
272 void TableData::addRow( Args const &... args )
273 {
274  stdVector< CellData > cells;
275  ( [&] {
276  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" );
277  if constexpr (std::is_same_v< Args, CellType >) {
278  cells.push_back( { args, string() } );
279  }
280  else if constexpr (std::is_floating_point_v< std::decay_t< decltype(args) > >) {
281  if( !getErrorsList().hasErrors() && (std::isnan( args ) || std::isinf( args )))
282  {
283  m_errors->addError( "Warning : Invalid values detected (nan/inf)." );
284  }
285  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
286  }
287  else
288  {
289  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
290  }
291  } (), ...);
292  addRow( cells );
293 }
294 
295 template< typename T >
296 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
297 {
298  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
299  m_columnValues.insert( columnValue );
300  m_data[rowValue][columnValue] = GEOS_FMT( "{}", value );
301 }
302 
303 }
304 #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:175
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:179
void collectTableValues(arrayView1d< real64 const > dim0AxisCoordinates, arrayView1d< real64 const > dim1AxisCoordinates, arrayView1d< real64 const > values, bool columnMajorValues)
Collects all the values needed to build the table.
void clear()
Clear all data stored in TableData.
Definition: TableData.hpp:248
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:296
TableData2D::TableDataHolder convertTable2D(arrayView1d< real64 const > coordX, arrayView1d< real64 const > coordY, string_view rowAxisDescription, string_view columnAxisDescription, arrayView1d< real64 const > const values, bool columnMajorValues, string_view valueDescription)
Convert from 2D axis/values a structure the information needed to build a TableFormatter.
TableDataHolder buildTableData(string_view dataDescription, string_view rowFmt="{}", string_view columnFmt="{}") const
real64 ColumnType
Type real64 for a column.
Definition: TableData.hpp:181
Class for managing table data.
Definition: TableData.hpp:35
stdVector< stdVector< CellData > > & getTableDataRows()
DataRows const & getCellsData() const
Definition: TableData.hpp:133
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.
void clearErrors()
Remove all errors.
Definition: TableData.hpp:111
bool operator==(TableData const &comparingTable) const
Comparison operator for data rows.
Definition: TableData.hpp:141
TableErrorListing & getErrorsList()
Get all error messages.
Definition: TableData.hpp:158
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:272
bool operator<(TableData const &other) const
Lexicographic sorting.
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:81
TableErrorListing const & getErrorsList() const
Get all error messages.
Definition: TableData.hpp:151
stdVector< stdVector< CellData > > const & getTableDataRows() const
Class for retrieving errors in the table classes.
Definition: TableTypes.hpp:45
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:179
std::string string
String type.
Definition: DataTypes.hpp:90
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:269
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:33
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector
Struct containing conversion informations.
Definition: TableData.hpp:185
stdVector< string > headerNames
Definition: TableData.hpp:188
TableData tableData
TableData to be built.
Definition: TableData.hpp:190
Representing a data in TableData.
Definition: TableData.hpp:61
string value
The cell value.
Definition: TableData.hpp:65
CellType type
The cell type.
Definition: TableData.hpp:63