10#ifndef __Teuchos_MatrixMarket_CoordDataReader_hpp
11#define __Teuchos_MatrixMarket_CoordDataReader_hpp
13#include "Teuchos_MatrixMarket_generic.hpp"
16#include "Teuchos_Tuple.hpp"
20 namespace MatrixMarket {
67 template<
class Callback,
class Ordinal>
138 const size_t lineNumber,
139 const bool tolerant) = 0;
170 virtual std::pair<bool, std::vector<size_t> >
172 const size_t startingLineNumber,
174 const bool debug =
false)
178 "Input stream is invalid.");
181 size_t lineNumber = startingLineNumber;
182 bool allSucceeded =
true;
183 std::vector<size_t> badLineNumbers;
184 while (getline (in, line)) {
186 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
190 const std::string theLine = line.substr (start, size);
192 const bool localSuccess =
readLine (theLine, lineNumber, tolerant);
194 allSucceeded = allSucceeded && localSuccess;
195 if (! localSuccess) {
196 badLineNumbers.push_back (lineNumber);
199 return std::make_pair (allSucceeded, badLineNumbers);
225 std::pair<Teuchos::Tuple<Ordinal, 3>,
bool>
228 const bool tolerant =
false)
245 bool commentLine =
true;
246 while (commentLine) {
248 if (in.eof() || in.fail()) {
250 return std::make_pair (dims,
false);
253 std::ostringstream os;
254 os <<
"Unable to get coordinate dimensions line (at all) "
255 "from (line " << lineNumber <<
") of input stream; the "
256 "input stream claims that it is at \"end-of-file\" or has "
257 "an otherwise \"fail\"ed state.";
258 throw std::invalid_argument(os.str());
262 if (getline(in, line)) {
267 return std::make_pair (dims,
false);
270 std::ostringstream os;
271 os <<
"Failed to read coordinate dimensions line (at all) "
272 "from (line " << lineNumber <<
" from input stream. The "
273 "line should contain the coordinate matrix dimensions in "
274 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
275 throw std::invalid_argument (os.str());
282 size_t start = 0, size = 0;
283 commentLine = checkCommentLine (line, start, size,
284 lineNumber, tolerant);
289 std::istringstream istr (line);
292 if (istr.eof() || istr.fail()) {
294 return std::make_pair (dims,
false);
296 std::ostringstream os;
297 os <<
"Unable to read any data from line " << lineNumber
298 <<
" of input; the line should contain the coordinate matrix "
299 <<
"dimensions \"<numRows> <numCols> <numNonzeros>\".";
300 throw std::invalid_argument(os.str());
304 Ordinal theNumRows = 0;
308 return std::make_pair (dims,
false);
310 std::ostringstream os;
311 os <<
"Failed to get number of rows from line " << lineNumber
312 <<
" of input; the line should contain the coordinate matrix "
313 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
314 throw std::invalid_argument(os.str());
317 dims[0] = theNumRows;
323 return std::make_pair (dims,
false);
325 std::ostringstream os;
326 os <<
"No more data after number of rows on line " << lineNumber
327 <<
" of input; the line should contain the coordinate matrix "
328 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
329 throw std::invalid_argument(os.str());
333 Ordinal theNumCols = 0;
337 return std::make_pair (dims,
false);
339 std::ostringstream os;
340 os <<
"Failed to get number of columns from line " << lineNumber
341 <<
" of input; the line should contain the coordinate matrix "
342 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
343 throw std::invalid_argument(os.str());
346 dims[1] = theNumCols;
352 return std::make_pair (dims,
false);
354 std::ostringstream os;
355 os <<
"No more data after number of columns on line " << lineNumber
356 <<
" of input; the line should contain the coordinate matrix "
357 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
358 throw std::invalid_argument(os.str());
362 Ordinal theNumNonzeros = 0;
363 istr >> theNumNonzeros;
366 return std::make_pair (dims,
false);
368 std::ostringstream os;
369 os <<
"Failed to get number of (structural) nonzeros from line "
371 <<
" of input; the line should contain the coordinate matrix "
372 <<
" dimensions \"<numRows> <numCols> <numNonzeros>\".";
373 throw std::invalid_argument(os.str());
376 dims[2] = theNumNonzeros;
383 return std::make_pair (dims,
true);
421 template<
class Callback,
456 const size_t lineNumber,
457 const bool tolerant);
460#ifdef HAVE_TEUCHOS_COMPLEX
462 template<
class Callback,
class Ordinal,
class Scalar>
478 readLine (
const std::string& theLine,
479 const size_t lineNumber,
482 typedef Teuchos::ScalarTraits<Scalar> STS;
483 typedef typename STS::magnitudeType Real;
489 Real realPart, imagPart;
490 const bool localSuccess =
491 readComplexLine (theLine, rowIndex, colIndex, realPart, imagPart,
492 lineNumber, tolerant);
501 value = std::complex<Real> (realPart, imagPart);
505 (*(this->
adder_)) (rowIndex, colIndex, value);
513 template<
class Callback,
class Ordinal,
class Scalar>
529 readLine (
const std::string& theLine,
530 const size_t lineNumber,
536 const bool localSuccess = readRealLine (theLine, rowIndex, colIndex,
537 value, lineNumber, tolerant);
541 (*(this->
adder_)) (rowIndex, colIndex, value);
570 template<
class Callback,
class Ordinal>
606 const size_t lineNumber,
611 const bool localSuccess =
612 readPatternLine (theLine, rowIndex, colIndex, lineNumber, tolerant);
616 (*(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.