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 size_t validDataLines = 0;
217 while (getline (in, line)) {
219 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
223 const std::string theLine = line.substr (start, size);
225 const bool localSuccess =
readLine (theLine, lineNumber, tolerant);
227 allSucceeded = allSucceeded && localSuccess;
228 if (! localSuccess) {
229 badLineNumbers.push_back (lineNumber);
235 return std::make_pair (allSucceeded, badLineNumbers);
261 std::pair<Teuchos::Tuple<Ordinal, 3>,
bool>
264 const bool tolerant =
false)
281 bool commentLine =
true;
282 while (commentLine) {
284 if (in.eof() || in.fail()) {
286 return std::make_pair (dims,
false);
289 std::ostringstream os;
290 os <<
"Unable to get coordinate dimensions line (at all) "
291 "from (line " << lineNumber <<
") of input stream; the "
292 "input stream claims that it is at \"end-of-file\" or has "
293 "an otherwise \"fail\"ed state.";
294 throw std::invalid_argument(os.str());
298 if (getline(in, line)) {
303 return std::make_pair (dims,
false);
306 std::ostringstream os;
307 os <<
"Failed to read coordinate dimensions line (at all) "
308 "from (line " << lineNumber <<
" from input stream. The "
309 "line should contain the coordinate matrix dimensions in "
310 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
311 throw std::invalid_argument (os.str());
318 size_t start = 0, size = 0;
319 commentLine = checkCommentLine (line, start, size,
320 lineNumber, tolerant);
325 std::istringstream istr (line);
328 if (istr.eof() || istr.fail()) {
330 return std::make_pair (dims,
false);
332 std::ostringstream os;
333 os <<
"Unable to read any data from line " << lineNumber
334 <<
" of input; the line should contain the coordinate matrix "
335 <<
"dimensions \"<numRows> <numCols> <numNonzeros>\".";
336 throw std::invalid_argument(os.str());
340 Ordinal theNumRows = 0;
344 return std::make_pair (dims,
false);
346 std::ostringstream os;
347 os <<
"Failed to get number of rows from line " << lineNumber
348 <<
" of input; the line should contain the coordinate matrix "
349 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
350 throw std::invalid_argument(os.str());
353 dims[0] = theNumRows;
359 return std::make_pair (dims,
false);
361 std::ostringstream os;
362 os <<
"No more data after number of rows on line " << lineNumber
363 <<
" of input; the line should contain the coordinate matrix "
364 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
365 throw std::invalid_argument(os.str());
369 Ordinal theNumCols = 0;
373 return std::make_pair (dims,
false);
375 std::ostringstream os;
376 os <<
"Failed to get number of columns from line " << lineNumber
377 <<
" of input; the line should contain the coordinate matrix "
378 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
379 throw std::invalid_argument(os.str());
382 dims[1] = theNumCols;
388 return std::make_pair (dims,
false);
390 std::ostringstream os;
391 os <<
"No more data after number of columns on line " << lineNumber
392 <<
" of input; the line should contain the coordinate matrix "
393 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
394 throw std::invalid_argument(os.str());
398 Ordinal theNumNonzeros = 0;
399 istr >> theNumNonzeros;
402 return std::make_pair (dims,
false);
404 std::ostringstream os;
405 os <<
"Failed to get number of (structural) nonzeros from line "
407 <<
" of input; the line should contain the coordinate matrix "
408 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
409 throw std::invalid_argument(os.str());
412 dims[2] = theNumNonzeros;
419 return std::make_pair (dims,
true);
457 template<
class Callback,
491 readLine (
const std::string& theLine,
492 const size_t lineNumber,
493 const bool tolerant);
496 #ifdef HAVE_TEUCHOS_COMPLEX
498 template<
class Callback,
class Ordinal,
class Scalar>
514 readLine (
const std::string& theLine,
515 const size_t lineNumber,
519 typedef typename STS::magnitudeType Real;
525 Real realPart, imagPart;
526 const bool localSuccess =
527 readComplexLine (theLine, rowIndex, colIndex, realPart, imagPart,
528 lineNumber, tolerant);
537 value = std::complex<Real> (realPart, imagPart);
541 (*(this->
adder_)) (rowIndex, colIndex, value);
546 #endif // HAVE_TEUCHOS_COMPLEX
549 template<
class Callback,
class Ordinal,
class Scalar>
550 class CoordDataReader<Callback, Ordinal, Scalar, false> :
551 public CoordDataReaderBase<Callback, Ordinal> {
565 readLine (
const std::string& theLine,
566 const size_t lineNumber,
572 const bool localSuccess = readRealLine (theLine, rowIndex, colIndex,
573 value, lineNumber, tolerant);
577 (*(this->
adder_)) (rowIndex, colIndex, value);
606 template<
class Callback,
class Ordinal>
642 const size_t lineNumber,
647 const bool localSuccess =
648 readPatternLine (theLine, rowIndex, colIndex, lineNumber, tolerant);
652 (*(this->
adder_)) (rowIndex, colIndex);
661 #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.