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 
119 
123  DataRows const & getCellsData() const
124  { return m_rows; }
125 
130  { return m_rows; }
131 
137  inline bool operator==( TableData const & comparingTable ) const
138  { return getCellsData() == comparingTable.getCellsData(); }
139 
145  { return *m_errors; }
146 
152  { return *m_errors; }
153 
154 private:
156  DataRows m_rows;
157 
159  std::unique_ptr< geos::TableErrorListing > m_errors;
160 
161 };
162 
163 
168 {
169 public:
170 
172  using RowType = real64;
175 
178  {
184  };
185 
193  template< typename T >
194  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
195 
204  arrayView1d< real64 const > dim1AxisCoordinates,
206  bool columnMajorValues );
207 
220  string_view rowAxisDescription,
221  string_view columnAxisDescription,
222  arrayView1d< real64 const > const values,
223  bool columnMajorValues,
224  string_view valueDescription );
225 
236  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
237 
241  inline void clear()
242  {
243  m_data.clear();
244  m_columnValues.clear();
245  m_errors->clear();
246  }
247 
248 private:
251 
253  std::set< real64 > m_columnValues;
255  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
256 };
257 
262 template< typename T >
263 constexpr bool isCellType = std::is_same_v< T, CellType >;
264 
265 template< typename ... Args >
266 void TableData::addRow( Args const &... args )
267 {
268  stdVector< CellData > cells;
269  ( [&] {
270  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" );
271  if constexpr (std::is_same_v< Args, CellType >) {
272  cells.push_back( { args, string() } );
273  }
274  else if constexpr (std::is_floating_point_v< std::decay_t< decltype(args) > >) {
275  if( !getErrorsList().hasErrors() && (std::isnan( args ) || std::isinf( args )))
276  {
277  m_errors->addError( "Warning : Invalid values detected (nan/inf)." );
278  }
279  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
280  }
281  else
282  {
283  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
284  }
285  } (), ...);
286  addRow( cells );
287 }
288 
297 template< typename T >
298 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
299 {
300  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
301  m_columnValues.insert( columnValue );
302  m_data.get_inserted( rowValue ).get_inserted( columnValue ) = GEOS_FMT( "{}", value );
303 }
304 
305 }
306 #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:168
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:172
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:241
void addCell(RowType rowValue, ColumnType columnValue, T const &value)
Add a cell to the table. If necessary, create automatically the containing column & row.
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:174
Class for managing table data.
Definition: TableData.hpp:35
DataRows const & getCellsData() const
Definition: TableData.hpp:123
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:137
TableErrorListing & getErrorsList()
Get all error messages.
Definition: TableData.hpp:151
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:266
DataRows & getCellsData()
Definition: TableData.hpp:129
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:144
Class for retrieving errors in the table classes.
Definition: TableTypes.hpp:47
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:263
internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdMap
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:35
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:178
stdVector< string > headerNames
Definition: TableData.hpp:181
TableData tableData
TableData to be built.
Definition: TableData.hpp:183
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