42 #ifndef __Teuchos_MatrixMarket_CoordDataReader_hpp
43 #define __Teuchos_MatrixMarket_CoordDataReader_hpp
45 #include "Teuchos_MatrixMarket_generic.hpp"
48 #include "Teuchos_Tuple.hpp"
52 namespace MatrixMarket {
99 template<
class Callback,
class Ordinal>
169 readLine (
const std::string& theLine,
170 const size_t lineNumber,
171 const bool tolerant) = 0;
202 virtual std::pair<bool, std::vector<size_t> >
204 const size_t startingLineNumber,
206 const bool debug =
false)
210 "Input stream is invalid.");
213 size_t lineNumber = startingLineNumber;
214 bool allSucceeded =
true;
215 std::vector<size_t> badLineNumbers;
216 while (getline (in, line)) {
218 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
222 const std::string theLine = line.substr (start, size);
224 const bool localSuccess =
readLine (theLine, lineNumber, tolerant);
226 allSucceeded = allSucceeded && localSuccess;
227 if (! localSuccess) {
228 badLineNumbers.push_back (lineNumber);
231 return std::make_pair (allSucceeded, badLineNumbers);
257 std::pair<Teuchos::Tuple<Ordinal, 3>,
bool>
260 const bool tolerant =
false)
277 bool commentLine =
true;
278 while (commentLine) {
280 if (in.eof() || in.fail()) {
282 return std::make_pair (dims,
false);
285 std::ostringstream os;
286 os <<
"Unable to get coordinate dimensions line (at all) "
287 "from (line " << lineNumber <<
") of input stream; the "
288 "input stream claims that it is at \"end-of-file\" or has "
289 "an otherwise \"fail\"ed state.";
290 throw std::invalid_argument(os.str());
294 if (getline(in, line)) {
299 return std::make_pair (dims,
false);
302 std::ostringstream os;
303 os <<
"Failed to read coordinate dimensions line (at all) "
304 "from (line " << lineNumber <<
" from input stream. The "
305 "line should contain the coordinate matrix dimensions in "
306 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
307 throw std::invalid_argument (os.str());
314 size_t start = 0, size = 0;
315 commentLine = checkCommentLine (line, start, size,
316 lineNumber, tolerant);
321 std::istringstream istr (line);
324 if (istr.eof() || istr.fail()) {
326 return std::make_pair (dims,
false);
328 std::ostringstream os;
329 os <<
"Unable to read any data from line " << lineNumber
330 <<
" of input; the line should contain the coordinate matrix "
331 <<
"dimensions \"<numRows> <numCols> <numNonzeros>\".";
332 throw std::invalid_argument(os.str());
336 Ordinal theNumRows = 0;
340 return std::make_pair (dims,
false);
342 std::ostringstream os;
343 os <<
"Failed to get number of rows from line " << lineNumber
344 <<
" of input; the line should contain the coordinate matrix "
345 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
346 throw std::invalid_argument(os.str());
349 dims[0] = theNumRows;
355 return std::make_pair (dims,
false);
357 std::ostringstream os;
358 os <<
"No more data after number of rows on line " << lineNumber
359 <<
" of input; the line should contain the coordinate matrix "
360 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
361 throw std::invalid_argument(os.str());
365 Ordinal theNumCols = 0;
369 return std::make_pair (dims,
false);
371 std::ostringstream os;
372 os <<
"Failed to get number of columns from line " << lineNumber
373 <<
" of input; the line should contain the coordinate matrix "
374 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
375 throw std::invalid_argument(os.str());
378 dims[1] = theNumCols;
384 return std::make_pair (dims,
false);
386 std::ostringstream os;
387 os <<
"No more data after number of columns on line " << lineNumber
388 <<
" of input; the line should contain the coordinate matrix "
389 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
390 throw std::invalid_argument(os.str());
394 Ordinal theNumNonzeros = 0;
395 istr >> theNumNonzeros;
398 return std::make_pair (dims,
false);
400 std::ostringstream os;
401 os <<
"Failed to get number of (structural) nonzeros from line "
403 <<
" of input; the line should contain the coordinate matrix "
404 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
405 throw std::invalid_argument(os.str());
408 dims[2] = theNumNonzeros;
415 return std::make_pair (dims,
true);
453 template<
class Callback,
487 readLine (
const std::string& theLine,
488 const size_t lineNumber,
489 const bool tolerant);
492 #ifdef HAVE_TEUCHOS_COMPLEX
494 template<
class Callback,
class Ordinal,
class Scalar>
510 readLine (
const std::string& theLine,
511 const size_t lineNumber,
515 typedef typename STS::magnitudeType Real;
521 Real realPart, imagPart;
522 const bool localSuccess =
523 readComplexLine (theLine, rowIndex, colIndex, realPart, imagPart,
524 lineNumber, tolerant);
533 value = std::complex<Real> (realPart, imagPart);
537 (*(this->
adder_)) (rowIndex, colIndex, value);
542 #endif // HAVE_TEUCHOS_COMPLEX
545 template<
class Callback,
class Ordinal,
class Scalar>
546 class CoordDataReader<Callback, Ordinal, Scalar, false> :
547 public CoordDataReaderBase<Callback, Ordinal> {
561 readLine (
const std::string& theLine,
562 const size_t lineNumber,
568 const bool localSuccess = readRealLine (theLine, rowIndex, colIndex,
569 value, lineNumber, tolerant);
573 (*(this->
adder_)) (rowIndex, colIndex, value);
602 template<
class Callback,
class Ordinal>
638 const size_t lineNumber,
643 const bool localSuccess =
644 readPatternLine (theLine, rowIndex, colIndex, lineNumber, tolerant);
648 (*(this->
adder_)) (rowIndex, colIndex);
657 #endif // __Teuchos_MatrixMarket_CoordDataReader_hpp
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.
CoordDataReader()
No-argument constructor.
CoordDataReaderBase()
No-argument constructor.
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.
std::pair< Teuchos::Tuple< Ordinal, 3 >, bool > readDimensions(std::istream &in, size_t &lineNumber, const bool tolerant=false)
Read (numRows, numCols, numNonzeros).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
virtual ~CoordPatternReader()
Virtual destructor for safety and happy compilers.
CoordPatternReader()
No-argument constructor.
CoordDataReaderBase(const Teuchos::RCP< Callback > &adder)
Constructor with "adder" argument.
Coordinate-format sparse matrix data reader.
This structure defines some basic traits for a scalar field type.
Coordinate-format sparse graph data reader.
Statically sized simple array (tuple) class.
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 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.
virtual ~CoordDataReader()
Virtual destructor for safety and happy compilers.
void setAdder(const Teuchos::RCP< Callback > &adder)
Set the Adder object.
Defines basic traits for the scalar field type.
Teuchos::RCP< Callback > adder_
Closure that knows how to add entries to the sparse graph or matrix.
Common functionality of a coordinate-format sparse matrix or graph data reader.
virtual ~CoordDataReaderBase()
Virtual destructor for safety and happy compilers.
Reference-counted pointer class and non-member templated function implementations.