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 
124 
128  DataRows const & getCellsData() const
129  { return m_rows; }
130 
136  inline bool operator==( TableData const & comparingTable ) const
137  {
138 
139  return getCellsData() == comparingTable.getCellsData();
140  }
141 
147  { return *m_errors; }
148 
154  { return *m_errors; }
155 
156 private:
158  DataRows m_rows;
159 
161  std::unique_ptr< geos::TableErrorListing > m_errors;
162 
163 };
164 
165 
170 {
171 public:
172 
174  using RowType = real64;
177 
180  {
186  };
187 
195  template< typename T >
196  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
197 
206  arrayView1d< real64 const > dim1AxisCoordinates,
208  bool columnMajorValues );
209 
222  string_view rowAxisDescription,
223  string_view columnAxisDescription,
224  arrayView1d< real64 const > const values,
225  bool columnMajorValues,
226  string_view valueDescription );
227 
238  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
239 
243  inline void clear()
244  {
245  m_data.clear();
246  m_columnValues.clear();
247  m_errors->clear();
248  }
249 
250 private:
252  std::map< RowType, std::map< ColumnType, string > > m_data;
254  std::set< real64 > m_columnValues;
256  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
257 };
258 
263 template< typename T >
264 constexpr bool isCellType = std::is_same_v< T, CellType >;
265 
266 template< typename ... Args >
267 void TableData::addRow( Args const &... args )
268 {
269  stdVector< CellData > cells;
270  ( [&] {
271  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" );
272  if constexpr (std::is_same_v< Args, CellType >) {
273  cells.push_back( { args, string() } );
274  }
275  else if constexpr (std::is_floating_point_v< std::decay_t< decltype(args) > >) {
276  if( !getErrorsList().hasErrors() && (std::isnan( args ) || std::isinf( args )))
277  {
278  m_errors->addError( "Warning : Invalid values detected (nan/inf)." );
279  }
280  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
281  }
282  else
283  {
284  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
285  }
286  } (), ...);
287  addRow( cells );
288 }
289 
290 template< typename T >
291 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
292 {
293  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
294  m_columnValues.insert( columnValue );
295  m_data[rowValue][columnValue] = GEOS_FMT( "{}", value );
296 }
297 
298 }
299 #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:170
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:174
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:243
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:291
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:176
Class for managing table data.
Definition: TableData.hpp:35
DataRows const & getCellsData() const
Definition: TableData.hpp:128
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:136
TableErrorListing & getErrorsList()
Get all error messages.
Definition: TableData.hpp:153
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:267
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:146
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:264
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:180
stdVector< string > headerNames
Definition: TableData.hpp:183
TableData tableData
TableData to be built.
Definition: TableData.hpp:185
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