-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathTableData.cpp
More file actions
316 lines (267 loc) · 9.19 KB
/
Copy pathTableData.cpp
File metadata and controls
316 lines (267 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
/**
* @file TableData.cpp
*/
#include "TableData.hpp"
#include "common/logger/Logger.hpp"
namespace geos
{
TableData::TableData():
m_errors( std::make_unique< TableErrorListing >() )
{}
TableData::TableData( TableData const & other ):
m_rows( other.m_rows ),
m_errors( std::make_unique< TableErrorListing >( *other.m_errors ) )
{}
TableData::TableData( TableData && other ):
m_rows( std::move( other.m_rows )),
m_errors( std::move( other.m_errors ))
{}
TableData & TableData::operator=( TableData && other )
{
if( this != &other )
{
m_rows = std::move( other.m_rows );
m_errors = std::move( other.m_errors );
}
return *this;
}
TableData & TableData::operator=( TableData const & other )
{
if( this != &other )
{
m_rows = other.m_rows;
*m_errors = *other.m_errors;
}
return *this;
}
bool TableData::operator<( TableData const & other ) const
{
if( other.getCellsData().size()!= getCellsData().size())
return false;
for( size_t i = 0; i < getCellsData().size(); i++ )
{
if( getCellsData()[i].data()->value > other.getCellsData()[i].data()->value )
return false;
}
return true;
}
bool TableData::operator==( TableData const & comparingTable ) const
{
if( comparingTable.getCellsData().size()!= getCellsData().size())
return false;
for( size_t i = 0; i < getCellsData().size(); i++ )
{
if( getCellsData()[i].data()->value != comparingTable.getCellsData()[i].data()->value )
return false;
}
return true;
}
void TableData::CellData::serialize( stdVector< buffer_unit_type > & out ) const
{
basicSerialization::serializePrimitive( type, out );
basicSerialization::serializeString( value, out );
}
size_t TableData::CellData::getSerializedSize() const
{
return basicSerialization::sizeOfPrimitive( type ) + basicSerialization::sizeOfString( value );
}
size_t TableData::getSerializedSize() const
{
size_t totalSize =0;
if( m_rows.empty())
return totalSize;
for( auto & row : m_rows )
{
size_t rowSize = 0;
for( auto & cell : row )
{
rowSize += cell.getSerializedSize();
}
totalSize += sizeof(size_t) + rowSize;
}
return totalSize;
}
void TableData::serialize( stdVector< buffer_unit_type > & serializedTableData ) const
{
if( m_rows.empty())
return;
for( auto & row : m_rows )
{
{ // pack row size;
size_t rowSize = 0;
for( auto const & cell : row )
rowSize += cell.getSerializedSize();
basicSerialization::serializePrimitive( rowSize, serializedTableData );
}
{ // pack cells
for( auto const & cell : row )
{
cell.serialize( serializedTableData );
}
}
}
}
void TableData::addRow( stdVector< TableData::CellData > const & row )
{
m_rows.push_back( row );
}
void TableData::addSeparator()
{
if( m_rows.empty())
{
m_errors->addError( "Warning : Bad use of a Tabledata::addSeparator(). Make sure you have added values in TableData" );
}
else
{
integer rowSize = m_rows.front().size();
m_rows.emplace_back( stdVector< TableData::CellData >( rowSize, { CellType::Separator, "-" } ));
}
}
void TableData::clear()
{
m_rows.clear();
getErrorsList().clear();
}
void TableData2D::collectTableValues( arrayView1d< real64 const > dim0AxisCoordinates,
arrayView1d< real64 const > dim1AxisCoordinates,
arrayView1d< real64 const > values,
bool columnMajorInputValues )
{
arrayView1d< real64 const > rowAxisCoordinates = columnMajorInputValues ? dim1AxisCoordinates : dim0AxisCoordinates;
arrayView1d< real64 const > columAxisCoordinates = columnMajorInputValues ? dim0AxisCoordinates : dim1AxisCoordinates;
integer const nCol = columAxisCoordinates.size();
integer const nRow = rowAxisCoordinates.size();
array1d< real64 > wellFormedValues( values.size() );
wellFormedValues = values;
if( values.size() < nRow * nCol )
{
m_errors->addError( GEOS_FMT( "Warning: Not enough for the number of columns & rows:\n"
" - Expected {} values ({} columns x {} rows),\n - Found {} values",
nRow * nCol, nCol, nRow, values.size() ) );
wellFormedValues.resizeDefault( nRow * nCol, 0 );
}
else if( values.size() > nRow * nCol )
{
m_errors->addError( GEOS_FMT( "Warning: Too much data for the number of columns & rows:\n"
" - Expected {} values ({} columns x {} rows),\n - Found {} values."
" Data may be misaligned",
nRow * nCol, nCol, nRow, values.size() ) );
}
for( integer y = 0; y < nRow; y++ )
{
for( integer x = 0; x < nCol; x++ )
{
addCell( rowAxisCoordinates[y], columAxisCoordinates[x], wellFormedValues[ x + y*nCol ] );
}
}
}
TableData2D::TableDataHolder TableData2D::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 )
{
string const rowFmt = GEOS_FMT( "{} = {{}}", rowAxisDescription );
string const columnFmt = GEOS_FMT( "{} = {{}}", columnAxisDescription );
collectTableValues( coordX, coordY, values, columnMajorValues );
return buildTableData( valueDescription, rowFmt, columnFmt );
}
TableData2D::TableDataHolder TableData2D::buildTableData( string_view targetUnit,
string_view rowFmt,
string_view columnFmt ) const
{
TableData2D::TableDataHolder tableData1D;
tableData1D.headerNames.push_back( string( targetUnit ) );
for( auto const & columnValue : m_columnValues )
{
tableData1D.headerNames.push_back( GEOS_FMT( columnFmt, columnValue ) );
}
for( auto const & error : *m_errors )
{
tableData1D.tableData.getErrorsList().addError( error );
}
// insert row value and row cell values
for( auto const & [rowValue, rowMap] : m_data )
{
stdVector< TableData::CellData > currentRowValues;
currentRowValues.reserve( rowMap.size() );
currentRowValues.push_back( {CellType::Value, GEOS_FMT( rowFmt, rowValue )} );
std::set< real64 >::const_iterator columnIt = m_columnValues.begin();
for( auto const & [columnValue, cellValue] : rowMap )
{
// if a column value(s) is/are missing, insert empty entry(ies)
while( columnValue > *( columnIt++ ) && columnIt != m_columnValues.end() )
{
currentRowValues.push_back( {CellType::Value, ""} );
}
currentRowValues.push_back( {CellType::Value, GEOS_FMT( "{}", cellValue )} );
}
tableData1D.tableData.addRow( currentRowValues );
}
return tableData1D;
}
void basicSerialization::serializeString ( string const & data, stdVector< buffer_unit_type > & out )
{
basicSerialization::serializePrimitive( data.size(), out );
auto * begin = data.data();
auto * end = begin + data.size();
out.insert( out.end(), begin, end );
}
void basicSerialization::deserializeString( string & str, buffer_unit_type const * & ptr, buffer_unit_type const * end )
{
string::size_type strSize = 0;
basicSerialization::deserializePrimitive( strSize, ptr, end );
if( static_cast< long >(strSize) > std::distance( ptr, end ) )
{
throw std::runtime_error( "buffer overflow reading string" );
}
str.assign( ptr, ptr + strSize );
ptr += str.size();
}
bool tableDataSorting::positiveNumberStringComp( string_view s1, string_view s2 )
{
auto split = []( string_view s, string & intPart, string & decPart )
{
size_t dotPos = s.find( '.' );
if( dotPos == string::npos )
{
intPart = s;
decPart = "";
}
else
{
intPart = s.substr( 0, dotPos );
decPart = s.substr( dotPos + 1 );
}
};
string s1Int, s1Dec, s2Int, s2Dec;
split( s1, s1Int, s1Dec );
split( s2, s2Int, s2Dec );
if( s1Int.length() != s2Int.length())
return s1Int.length() < s2Int.length();
if( s1Int != s2Int )
return s1Int < s2Int;
size_t minLen = std::min( s1Dec.length(), s2Dec.length());
for( size_t i = 0; i < minLen; ++i )
{
if( s1Dec[i] != s2Dec[i] )
return s1Dec[i] < s2Dec[i];
}
return false;
}
}