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>
137 readLine (
const std::string& theLine,
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,
455 readLine (
const std::string& theLine,
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,
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);
510 #endif // HAVE_TEUCHOS_COMPLEX
513 template<
class Callback,
class Ordinal,
class Scalar>
514 class CoordDataReader<Callback, Ordinal, Scalar, false> :
515 public CoordDataReaderBase<Callback, Ordinal> {
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);
625 #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.