Commit 8e2edfe9 authored by daniel.bub's avatar daniel.bub
Browse files

[ADD #132] add method to check if values of a column can be converted to int

parent 14a0386f
......@@ -72,9 +72,9 @@ void WProtonData::setCSVData( WDataSetCSV::ContentSPtr csvData )
throw WException( "Can not set data! No data content found!" );
}
detectColumnTypesFromCsvData( csvData );
m_csvData = csvData;
detectColumnTypesFromCsvData( csvData );
}
WDataSetCSV::ContentSPtr WProtonData::getCSVData()
......@@ -134,6 +134,17 @@ void WProtonData::detectColumnTypesFromCsvData( WDataSetCSV::ContentSPtr csvData
assert( m_columnTypes != nullptr );
assert( !m_columnTypes->empty() );
assert( m_columnTypes->size() == m_csvHeader->at( 0 ).size() );
for( size_t idx = 0; idx < m_columnTypes->size(); idx++ )
{
if( m_columnTypes->at( idx ) == WDatatype::getDouble() )
{
if( checkIfDoubleColumnCanBeInteger( idx ) )
{
m_columnTypes->at( idx ) = WDatatype::getInt();
}
}
}
}
std::string WProtonData::determineColumnTypeByString( std::string cellValue )
......@@ -155,6 +166,25 @@ std::string WProtonData::determineColumnTypeByString( std::string cellValue )
}
}
bool WProtonData::checkIfDoubleColumnCanBeInteger( int columnNumber )
{
double doubleValue;
int intValue;
for( auto row : *m_csvData )
{
doubleValue = boost::lexical_cast< double >( row.at( columnNumber ) );
intValue = ( int )doubleValue;
if( doubleValue - intValue != 0 )
{
return false;
}
}
return true;
}
std::vector< std::string > WProtonData::getHeaderFromType( std::string typeName )
{
std::vector< std::string > header = m_csvHeader->at( 0 );
......
......@@ -168,6 +168,13 @@ private:
* \return either "int", "double" or "string"
*/
std::string determineColumnTypeByString( std::string cellValue );
/**
* Checks, if values of a column, containing double values, can be converted to integers
* \param columnNumber the column number within m_csvHeader
* \return true, if all double values of a column ends with ".0"; false otherwise
*/
bool checkIfDoubleColumnCanBeInteger( int columnNumber );
};
#endif // WPROTONDATA_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment