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 
62  bool operator==( TableData const & other ) const;
63 
67  struct CellData
68  {
72  string value;
73 
77  size_t getSerializedSize() const;
78 
84  };
85 
90  size_t getSerializedSize() const;
91 
94 
99  void serialize( stdVector< buffer_unit_type > & serializedTableData ) const;
100 
106  template< typename ... Args >
107  void addRow( Args const & ... args );
108 
113  void addRow( stdVector< CellData > const & row );
114 
119  void addSeparator();
120 
124  void clear();
125 
129  void clearErrors()
130  { m_errors->clear(); }
131 
137 
141  DataRows const & getCellsData() const
142  { return m_rows; }
143 
148  { return m_rows; }
149 
155  { return *m_errors; }
156 
162  { return *m_errors; }
163 
169  template< typename SortingFunc >
170  void sort( SortingFunc sortingFunctor )
171  {
172  std::sort( m_rows.begin(), m_rows.end(), sortingFunctor );
173  }
174 
175 private:
177  DataRows m_rows;
178 
180  std::unique_ptr< geos::TableErrorListing > m_errors;
181 
182 };
183 
184 
189 {
190 public:
191 
193  using RowType = real64;
196 
199  {
205  };
206 
214  template< typename T >
215  void addCell( RowType rowValue, ColumnType columnValue, T const & value );
216 
225  arrayView1d< real64 const > dim1AxisCoordinates,
227  bool columnMajorValues );
228 
241  string_view rowAxisDescription,
242  string_view columnAxisDescription,
243  arrayView1d< real64 const > const values,
244  bool columnMajorValues,
245  string_view valueDescription );
246 
257  string_view rowFmt = "{}", string_view columnFmt = "{}" ) const;
258 
262  inline void clear()
263  {
264  m_data.clear();
265  m_columnValues.clear();
266  m_errors->clear();
267  }
268 
269 private:
272 
274  std::set< real64 > m_columnValues;
276  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
277 };
278 
283 template< typename T >
284 constexpr bool isCellType = std::is_same_v< T, CellType >;
285 
286 template< typename ... Args >
287 void TableData::addRow( Args const &... args )
288 {
289  stdVector< CellData > cells;
290  ( [&] {
291  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" );
292  if constexpr (std::is_same_v< Args, CellType >) {
293  cells.push_back( { args, string() } );
294  }
295  else if constexpr (std::is_floating_point_v< std::decay_t< decltype(args) > >) {
296  if( !getErrorsList().hasErrors() && (std::isnan( args ) || std::isinf( args )))
297  {
298  m_errors->addError( "Warning : Invalid values detected (nan/inf)." );
299  }
300  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
301  }
302  else
303  {
304  cells.push_back( {CellType::Value, GEOS_FMT( "{}", args )} );
305  }
306  } (), ...);
307  addRow( cells );
308 }
309 
318 template< typename T >
319 void TableData2D::addCell( real64 const rowValue, real64 const columnValue, T const & value )
320 {
321  static_assert( has_formatter_v< decltype(value) >, "Argument passed in addCell cannot be converted to string" );
322  m_columnValues.insert( columnValue );
323  m_data.get_inserted( rowValue ).get_inserted( columnValue ) = GEOS_FMT( "{}", value );
324 }
325 
326 // Serialisation/ Deserialisation utils for common
327 namespace basicSerialization
328 {
329 
334 template< typename T >
335 inline unsigned long sizeOfPrimitive( T )
336 { return sizeof(T); }
337 
343 inline unsigned long sizeOfString( string const & str )
344 { return sizeof(string::size_type) + str.size(); }
345 
352 template< typename T,
353  std::enable_if_t< std::is_trivially_copyable_v< T >, bool > = true >
354 void serializePrimitive ( T const data, stdVector< buffer_unit_type > & out );
355 
361 void serializeString ( string const & data, stdVector< buffer_unit_type > & out );
362 
370 template< typename T,
371  std::enable_if_t< std::is_trivially_copyable_v< T >, bool > = true >
372 void deserializePrimitive( T & data, buffer_unit_type const * & ptr, buffer_unit_type const * end );
373 
380 void deserializeString( string & str, buffer_unit_type const * & ptr, buffer_unit_type const * end );
381 }
382 
383 // Custom Comp function;
384 namespace tableDataSorting
385 {
393 }
394 
399 
400 template< typename T,
401  std::enable_if_t< std::is_trivially_copyable_v< T >, bool > >
402 void basicSerialization::serializePrimitive( T const data,
404 {
405  buffer_unit_type const * begin = reinterpret_cast< buffer_unit_type const * >( &data );
406  buffer_unit_type const * end = begin + sizeof(data);
407  out.insert( out.end(), begin, end );
408 }
409 
410 template< typename T,
411  std::enable_if_t< std::is_trivially_copyable_v< T >, bool > >
412 void basicSerialization::deserializePrimitive( T & data,
413  buffer_unit_type const * & ptr,
414  buffer_unit_type const * end )
415 {
416  if( ptr + sizeof(T)> end )
417  throw std::runtime_error( "Buffer overflow" );
418 
419  data = *reinterpret_cast< T const * >(ptr);
420  ptr += sizeof(T);
421 }
422 
424 
425 }
426 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEDATA_HPP */
bool positiveNumberStringComp(string_view a, string_view b)
Compare two string number string by in ascending numerical order.
unsigned long sizeOfPrimitive(T)
Definition: TableData.hpp:335
void serializeString(string const &data, stdVector< buffer_unit_type > &out)
Write a string value to the buffer.
void deserializeString(string &str, buffer_unit_type const *&ptr, buffer_unit_type const *end)
Reads a string value from the buffer and advances the pointer.
void deserializePrimitive(T &data, buffer_unit_type const *&ptr, buffer_unit_type const *end)
Reads the data from the buffer and advances the pointer.
Definition: TableData.hpp:412
unsigned long sizeOfString(string const &str)
Returns the size of a string (header size + content).
Definition: TableData.hpp:343
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:189
real64 RowType
Type real64 for a row.
Definition: TableData.hpp:193
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:262
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:195
Class for managing table data.
Definition: TableData.hpp:35
DataRows const & getCellsData() const
Definition: TableData.hpp:141
size_t getSerializedSize() const
Returns the total serialized byte size of all rows in the table.
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:129
TableErrorListing & getErrorsList()
Get all error messages.
Definition: TableData.hpp:161
void serialize(stdVector< buffer_unit_type > &serializedTableData) const
Serializes the tableData into the output buffer.
void addRow(Args const &... args)
Add a row to the table. The values passed to addRow (can be any type).
Definition: TableData.hpp:287
DataRows & getCellsData()
Definition: TableData.hpp:147
void sort(SortingFunc sortingFunctor)
Sorts the rows using a custom comparator functor.
Definition: TableData.hpp:170
bool operator<(TableData const &other) const
Lexicographic comparison operator, can be used for ordering in containers.
bool operator==(TableData const &other) const
Equality comparison of all cells.
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:93
TableErrorListing const & getErrorsList() const
Get all error messages.
Definition: TableData.hpp:154
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:284
internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdMap
signed char buffer_unit_type
Type stored in communication buffers.
Definition: DataTypes.hpp:108
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:199
stdVector< string > headerNames
Definition: TableData.hpp:202
TableData tableData
TableData to be built.
Definition: TableData.hpp:204
Representing a single cell's data within a TableData row.
Definition: TableData.hpp:68
void serialize(stdVector< buffer_unit_type > &out) const
Serializes the cell type and value into the output buffer.
string value
The cell value.
Definition: TableData.hpp:72
size_t getSerializedSize() const
CellType type
The cell type.
Definition: TableData.hpp:70