1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_READER_H_ 2 #define GENESIS_UTILS_CONTAINERS_MATRIX_READER_H_ 95 return skip_first_col_;
100 return skip_first_row_;
105 skip_first_col_ = value;
111 skip_first_row_ = value;
127 parse_value_ = functor;
139 std::vector<T> table;
143 if( ! input_stream ) {
148 if( skip_first_row_ ) {
152 while( input_stream ) {
153 auto const line = reader_.
parse_line( input_stream );
157 auto len = line.size();
158 if( len > 0 && skip_first_col_ ) {
175 }
else if( cols != len ) {
176 throw std::runtime_error(
"Matrix has different line lengths." );
181 for(
size_t i = first; i < line.size(); ++i ) {
182 table.push_back( parse_value_( line[i] ) );
185 for(
size_t i = first; i < line.size(); ++i ) {
186 table.push_back( parse_value_stringstream_( line[i] ) );
192 if( table.size() == 0 ) {
198 if( table.size() % cols != 0 ) {
201 throw std::runtime_error(
"Matrix is not rectangluar." );
205 size_t const rows = table.size() / cols;
206 return Matrix<T>( rows, cols, std::move(table) );
209 inline T parse_value_stringstream_( std::string
const& cell )
const 211 std::stringstream ss( cell );
223 bool skip_first_row_ =
false;
224 bool skip_first_col_ =
false;
228 std::function<T( std::string const& )> parse_value_;
235 #endif // include guard Matrix< T > read(std::shared_ptr< BaseInputSource > source) const
MatrixReader & parse_value_functor(std::function< T(std::string const &)> functor)
MatrixReader & operator=(MatrixReader const &)=default
CsvReader & separator_chars(std::string const &chars)
Set the chars used to separate fields of the CSV data.
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
MatrixReader & skip_first_col(bool value)
Provides some valuable additions to STD.
MatrixReader & skip_first_row(bool value)
bool skip_first_col() const
bool skip_first_row() const
MatrixReader(char separator_char='\t')
std::vector< std::string > parse_line(utils::InputStream &input_stream) const
Parse one line of the CSV data and return it.
Read Comma/Character Separated Values (CSV) data and other delimiter-separated formats.
MatrixReader(CsvReader const &reader)
CsvReader const & csv_reader() const