10#ifndef __Teuchos_MatrixMarket_CoordDataReader_hpp
11#define __Teuchos_MatrixMarket_CoordDataReader_hpp
13#include "Teuchos_MatrixMarket_generic.hpp"
16#include "Teuchos_Tuple.hpp"
21 namespace MatrixMarket {
68 template<
class Callback,
class Ordinal>
139 const size_t lineNumber,
140 const bool tolerant) = 0;
171 virtual std::pair<bool, std::vector<size_t> >
173 const size_t startingLineNumber,
175 const bool debug =
false)
179 "Input stream is invalid.");
182 size_t lineNumber = startingLineNumber;
183 bool allSucceeded =
true;
184 std::vector<size_t> badLineNumbers;
185 while (getline (in, line)) {
187 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
191 const std::string theLine = line.substr (start, size);
193 const bool localSuccess =
readLine (theLine, lineNumber, tolerant);
195 allSucceeded = allSucceeded && localSuccess;
196 if (! localSuccess) {
197 badLineNumbers.push_back (lineNumber);
200 return std::make_pair (allSucceeded, badLineNumbers);
226 std::pair<Teuchos::Tuple<Ordinal, 3>,
bool>
229 const bool tolerant =
false)
246 bool commentLine =
true;
247 while (commentLine) {
249 if (in.eof() || in.fail()) {
251 return std::make_pair (dims,
false);
254 std::ostringstream os;
255 os <<
"Unable to get coordinate dimensions line (at all) "
256 "from (line " << lineNumber <<
") of input stream; the "
257 "input stream claims that it is at \"end-of-file\" or has "
258 "an otherwise \"fail\"ed state.";
259 throw std::invalid_argument(os.str());
263 if (getline(in, line)) {
268 return std::make_pair (dims,
false);
271 std::ostringstream os;
272 os <<
"Failed to read coordinate dimensions line (at all) "
273 "from (line " << lineNumber <<
" from input stream. The "
274 "line should contain the coordinate matrix dimensions in "
275 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
276 throw std::invalid_argument (os.str());
283 size_t start = 0, size = 0;
284 commentLine = checkCommentLine (line, start, size,
285 lineNumber, tolerant);
290 std::istringstream istr (line);
293 if (istr.eof() || istr.fail()) {
295 return std::make_pair (dims,
false);
297 std::ostringstream os;
298 os <<
"Unable to read any data from line " << lineNumber
299 <<
" of input; the line should contain the coordinate matrix "
300 <<
"dimensions \"<numRows> <numCols> <numNonzeros>\".";
301 throw std::invalid_argument(os.str());
305 Ordinal theNumRows = 0;
309 return std::make_pair (dims,
false);
311 std::ostringstream os;
312 os <<
"Failed to get number of rows from line " << lineNumber
313 <<
" of input; the line should contain the coordinate matrix "
314 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
315 throw std::invalid_argument(os.str());
318 dims[0] = theNumRows;
324 return std::make_pair (dims,
false);
326 std::ostringstream os;
327 os <<
"No more data after number of rows on line " << lineNumber
328 <<
" of input; the line should contain the coordinate matrix "
329 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
330 throw std::invalid_argument(os.str());
334 Ordinal theNumCols = 0;
338 return std::make_pair (dims,
false);
340 std::ostringstream os;
341 os <<
"Failed to get number of columns from line " << lineNumber
342 <<
" of input; the line should contain the coordinate matrix "
343 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
344 throw std::invalid_argument(os.str());
347 dims[1] = theNumCols;
353 return std::make_pair (dims,
false);
355 std::ostringstream os;
356 os <<
"No more data after number of columns on line " << lineNumber
357 <<
" of input; the line should contain the coordinate matrix "
358 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
359 throw std::invalid_argument(os.str());
363 Ordinal theNumNonzeros = 0;
364 istr >> theNumNonzeros;
367 return std::make_pair (dims,
false);
369 std::ostringstream os;
370 os <<
"Failed to get number of (structural) nonzeros from line "
372 <<
" of input; the line should contain the coordinate matrix "
373 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
374 throw std::invalid_argument(os.str());
377 dims[2] = theNumNonzeros;
383 if constexpr (std::is_signed_v<Ordinal>) {
384 if (dims[0] < 0 || dims[1] < 0 || dims[2] < 0) {
386 return std::make_pair (dims,
false);
388 std::ostringstream os;
389 os <<
"Invalid Matrix Market dimensions on line " << lineNumber
390 <<
": dimensions must be non-negative, got "
391 << dims[0] <<
" " << dims[1] <<
" " << dims[2];
392 throw std::invalid_argument(os.str());
395 return std::make_pair (dims,
true);
433 template<
class Callback,
468 const size_t lineNumber,
469 const bool tolerant);
472#ifdef HAVE_TEUCHOS_COMPLEX
474 template<
class Callback,
class Ordinal,
class Scalar>
490 readLine (
const std::string& theLine,
491 const size_t lineNumber,
494 typedef Teuchos::ScalarTraits<Scalar> STS;
495 typedef typename STS::magnitudeType Real;
501 Real realPart, imagPart;
502 const bool localSuccess =
503 readComplexLine (theLine, rowIndex, colIndex, realPart, imagPart,
504 lineNumber, tolerant);
513 value = std::complex<Real> (realPart, imagPart);
517 (*(this->
adder_)) (rowIndex, colIndex, value);
525 template<
class Callback,
class Ordinal,
class Scalar>
541 readLine (
const std::string& theLine,
542 const size_t lineNumber,
548 const bool localSuccess = readRealLine (theLine, rowIndex, colIndex,
549 value, lineNumber, tolerant);
553 (*(this->
adder_)) (rowIndex, colIndex, value);
582 template<
class Callback,
class Ordinal>
618 const size_t lineNumber,
623 const bool localSuccess =
624 readPatternLine (theLine, rowIndex, colIndex, lineNumber, tolerant);
628 (*(this->
adder_)) (rowIndex, colIndex);
Reference-counted pointer class and non-member templated function implementations.
Defines basic traits for the scalar field type.
Common functionality of a coordinate-format sparse matrix or graph data reader.
virtual ~CoordDataReaderBase()
Virtual destructor for safety and happy compilers.
CoordDataReaderBase()
No-argument constructor.
virtual std::pair< bool, std::vector< size_t > > read(std::istream &in, const size_t startingLineNumber, const bool tolerant, const bool debug=false)
Read in all the data from the given input stream.
std::pair< Teuchos::Tuple< Ordinal, 3 >, bool > readDimensions(std::istream &in, size_t &lineNumber, const bool tolerant=false)
Read (numRows, numCols, numNonzeros).
Teuchos::RCP< Callback > adder_
Closure that knows how to add entries to the sparse graph or matrix.
CoordDataReaderBase(const Teuchos::RCP< Callback > &adder)
Constructor with "adder" argument.
virtual bool readLine(const std::string &theLine, const size_t lineNumber, const bool tolerant)=0
Read in the data from a single line of the input stream.
void setAdder(const Teuchos::RCP< Callback > &adder)
Set the Adder object.
Coordinate-format sparse matrix data reader.
CoordDataReader()
No-argument constructor.
virtual ~CoordDataReader()
Virtual destructor for safety and happy compilers.
CoordDataReader(const Teuchos::RCP< Callback > &adder)
Constructor with "adder" argument.
bool readLine(const std::string &theLine, const size_t lineNumber, const bool tolerant)
Read in the data from a single line of the input stream.
CoordPatternReader(const Teuchos::RCP< Callback > &adder)
Constructor with "adder" argument.
virtual ~CoordPatternReader()
Virtual destructor for safety and happy compilers.
bool readLine(const std::string &theLine, const size_t lineNumber, const bool tolerant)
Read in the data from a single line of the input stream.
CoordPatternReader()
No-argument constructor.
Smart reference counting pointer class for automatic garbage collection.
Statically sized simple array (tuple) class.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
static const bool isComplex
Determines if scalar type is std::complex.