1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_SIMPLE_READER_H_
2 #define GENESIS_UTILS_CONTAINERS_MATRIX_SIMPLE_READER_H_
95 return separator_char_;
100 return skip_first_col_;
105 return skip_first_row_;
110 separator_char_ = value;
116 skip_first_col_ = value;
122 skip_first_row_ = value;
134 parse_value_ = functor;
140 convert_value_ = functor;
152 auto& it = input_stream;
155 std::vector<T> table;
164 if( skip_first_row_ ) {
172 if( skip_first_col_ ) {
173 while( it && *it != separator_char_ && *it !=
'\n' ) {
176 assert( !it || *it == separator_char_ || *it ==
'\n' );
181 auto const line_length = parse_line_( it, table );
187 if( line_length == 0 ) {
193 }
else if( cols != line_length ) {
194 throw std::runtime_error(
195 "In " + it.source_name() +
" at " + it.at() +
": " +
196 "Different line lengths. Stareted with " +
std::to_string( cols ) +
203 if( table.size() == 0 ) {
209 if( table.size() % cols != 0 ) {
212 throw std::runtime_error(
"Matrix is not rectangluar." );
216 size_t const rows = table.size() / cols;
217 return Matrix<T>( rows, cols, std::move(table) );
220 size_t parse_line_( utils::InputStream& input_stream, std::vector<T>& table )
const
222 auto& it = input_stream;
224 while( it && *it !=
'\n' ) {
226 table.push_back( parse_field_( it ));
230 if( it && *it != separator_char_ && *it !=
'\n' ) {
231 throw std::runtime_error(
232 "In " + it.source_name() +
" at " + it.at() +
": " +
236 assert( !it || *it == separator_char_ || *it ==
'\n' );
237 if( it && *it == separator_char_ ) {
241 assert( !it || *it ==
'\n' );
246 T parse_field_( utils::InputStream& input_stream )
const
249 auto& it = input_stream;
252 result = parse_value_( it );
256 while( it && *it != separator_char_ && *it !=
'\n' ) {
262 if( convert_value_ ) {
264 result = convert_value_( value );
269 result = convert_from_string<T>( value );
271 throw std::runtime_error(
272 "In " + it.source_name() +
" at " + it.at() +
": " +
273 "Invalid value \"" + value +
"\""
287 char separator_char_ =
'\t';
288 bool skip_first_row_ =
false;
289 bool skip_first_col_ =
false;
291 std::function<T( utils::InputStream& )> parse_value_;
292 std::function<T( std::string
const& )> convert_value_;
299 #endif // include guard