42 #ifndef __Teuchos_MatrixMarket_generic_hpp
43 #define __Teuchos_MatrixMarket_generic_hpp
46 #ifdef HAVE_TEUCHOS_COMPLEX
48 #endif // HAVE_TEUCHOS_COMPLEX
52 #include <type_traits>
56 namespace MatrixMarket {
97 const size_t lineNumber,
99 const bool maybeBannerLine =
false);
134 template<
class Ordinal>
139 const size_t lineNumber,
142 Ordinal the_rowIndex, the_colIndex;
144 if (istr.eof() || istr.fail())
150 std::ostringstream os;
151 os <<
"Unable to read any data from line " << lineNumber <<
" of input";
152 throw std::invalid_argument(os.str());
155 istr >> the_rowIndex;
162 std::ostringstream os;
163 os <<
"Failed to get row index from line " << lineNumber <<
" of input";
164 throw std::invalid_argument(os.str());
173 std::ostringstream os;
174 os <<
"No more data after row index on line " << lineNumber <<
" of input";
175 throw std::invalid_argument(os.str());
178 istr >> the_colIndex;
185 std::ostringstream os;
186 os <<
"Failed to get column index from line " << lineNumber <<
" of input";
187 throw std::invalid_argument(os.str());
190 rowIndex = the_rowIndex;
191 colIndex = the_colIndex;
233 template<
class Ordinal,
class Real>
239 const size_t lineNumber,
243 if (!
readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant))
249 std::ostringstream os;
250 os <<
"Failed to read pattern data from line " << lineNumber <<
" of input";
251 throw std::invalid_argument(os.str());
260 std::ostringstream os;
261 os <<
"No more data after pattern data on line " << lineNumber <<
" of input";
262 throw std::invalid_argument(os.str());
266 if (std::is_same<Real, float>::value) {
269 the_realValue =
static_cast<Real> (dblVal);
272 istr >> the_realValue;
281 std::ostringstream os;
282 os <<
"Failed to get real value from line " << lineNumber <<
" of input";
283 throw std::invalid_argument(os.str());
286 realValue = the_realValue;
290 #ifdef HAVE_TEUCHOS_COMPLEX
333 template<
class Ordinal,
class Real>
335 readComplexData (std::istream& istr,
340 const size_t lineNumber,
343 Real the_realPart, the_imagPart;
344 if (!
readRealData (istr, rowIndex, colIndex, the_realPart, lineNumber, tolerant))
350 std::ostringstream os;
351 os <<
"Failed to read pattern data and/or real value from line "
352 << lineNumber <<
" of input";
353 throw std::invalid_argument(os.str());
362 std::ostringstream os;
363 os <<
"No more data after real value on line "
364 << lineNumber <<
" of input";
365 throw std::invalid_argument(os.str());
368 istr >> the_imagPart;
375 std::ostringstream os;
376 os <<
"Failed to get imaginary value from line "
377 << lineNumber <<
" of input";
378 throw std::invalid_argument(os.str());
381 realPart = the_realPart;
382 imagPart = the_imagPart;
385 #endif // HAVE_TEUCHOS_COMPLEX
387 template<
class Ordinal>
392 const size_t lineNumber,
396 std::istringstream istr (line);
397 return readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant);
400 template<
class Ordinal,
class Real>
406 const size_t lineNumber,
418 throw std::logic_error(
"Should never get here! checkCommentLine() "
419 "is supposed to catch empty lines.");
426 std::istringstream istr (line.substr (start, size));
427 return readRealData (istr, rowIndex, colIndex, realValue, lineNumber, tolerant);
430 #ifdef HAVE_TEUCHOS_COMPLEX
431 template<
class Ordinal,
class Real>
433 readComplexLine (
const std::string& line,
438 const size_t lineNumber,
450 throw std::logic_error(
"Should never get here! checkCommentLine() "
451 "is supposed to catch empty lines.");
456 std::istringstream istr (line.substr (start, end));
458 Real the_realPart, the_imagPart;
460 readComplexData (istr, rowIndex, colIndex, the_realPart, the_imagPart,
461 lineNumber, tolerant);
464 realPart = the_realPart;
465 imagPart = the_imagPart;
469 #endif // HAVE_TEUCHOS_COMPLEX
471 template<
class Ordinal,
class PatternCallback>
472 std::pair<bool, std::vector<size_t> >
475 const size_t startingLineNumber,
479 size_t lineNumber = startingLineNumber;
480 bool anySucceeded =
false;
481 bool allSucceeded =
true;
482 std::vector<size_t> badLineNumbers;
483 size_t validDataLines = 0;
484 while (getline (in, line)) {
489 const std::string theLine = line.substr (start, size);
491 Ordinal rowIndex, colIndex;
492 const bool localSuccess =
494 lineNumber++, tolerant);
495 anySucceeded = anySucceeded || localSuccess;
496 allSucceeded = allSucceeded && localSuccess;
497 if (! localSuccess) {
498 badLineNumbers.push_back (lineNumber);
502 add (rowIndex, colIndex);
506 if (lineNumber == startingLineNumber) {
510 return std::make_pair (allSucceeded, badLineNumbers);
514 template<
class Ordinal>
519 Ordinal& numNonzeros,
525 Ordinal the_numRows, the_numCols, the_numNonzeros;
533 bool commentLine =
true;
537 if (in.eof () || in.fail ()) {
542 std::ostringstream os;
543 os <<
"Unable to get coordinate dimensions line (at all) "
544 "from (line " << lineNumber <<
") of input stream; the "
545 "input stream claims that it is at \"end-of-file\" or has "
546 "an otherwise \"fail\"ed state.";
547 throw std::invalid_argument(os.str());
551 if (! getline (in, line)) {
556 std::ostringstream os;
557 os <<
"Failed to read coordinate dimensions line (at all) "
558 "from (line " << lineNumber <<
" from input stream. The "
559 "line should contain the coordinate matrix dimensions in "
560 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
561 throw std::invalid_argument(os.str());
568 size_t start = 0, size = 0;
570 lineNumber, tolerant);
580 std::istringstream istr (line);
581 if (istr.eof () || istr.fail ()) {
586 std::ostringstream os;
587 os <<
"Unable to read any coordinate dimensions data from line "
588 << lineNumber <<
" of input stream. Offending line:" << endl
590 throw std::invalid_argument(os.str());
599 std::ostringstream os;
600 os <<
"Failed to get number of rows from the coordinate "
601 "dimensions line (line " << lineNumber <<
" of input stream)."
602 " Offending line:" << endl << line;
603 throw std::invalid_argument(os.str());
606 else if (istr.eof ()) {
611 std::ostringstream os;
612 os <<
"No more data after number of rows, in the coordinate "
613 "dimensions line (line " << lineNumber <<
" of input stream)."
614 " Offending line:" << endl << line;
615 throw std::invalid_argument(os.str());
624 std::ostringstream os;
625 os <<
"Failed to get number of columns from the coordinate "
626 "dimensions line (line " << lineNumber <<
" of input stream)."
627 " Offending line:" << endl << line;
628 throw std::invalid_argument(os.str());
631 else if (istr.eof ()) {
636 std::ostringstream os;
637 os <<
"No more data after number of columns, in the coordinate "
638 "dimensions line (line " << lineNumber <<
" of input stream)."
639 " Offending line:" << endl << line;
640 throw std::invalid_argument (os.str ());
643 istr >> the_numNonzeros;
649 std::ostringstream os;
650 os <<
"Failed to get number of nonzeros from the coordinate "
651 "dimensions line (line " << lineNumber <<
" of input stream)."
652 " Offending line:" << endl << line;
653 throw std::invalid_argument (os.str ());
656 numRows = the_numRows;
657 numCols = the_numCols;
658 numNonzeros = the_numNonzeros;
669 #endif // __Teuchos_MatrixMarket_generic_hpp
bool checkCommentLine(const std::string &line, size_t &start, size_t &size, const size_t lineNumber, const bool tolerant, const bool maybeBannerLine)
True if the line is a comment line, false otherwise.
bool readCoordinateDimensions(std::istream &in, Ordinal &numRows, Ordinal &numCols, Ordinal &numNonzeros, size_t &lineNumber, const bool tolerant)
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
#define TEUCHOSNUMERICS_LIB_DLL_EXPORT
bool readPatternData(std::istream &istr, Ordinal &rowIndex, Ordinal &colIndex, const size_t lineNumber, const bool tolerant)
bool readPatternLine(const std::string &line, Ordinal &rowIndex, Ordinal &colIndex, const size_t lineNumber, const bool tolerant)
bool readRealData(std::istream &istr, Ordinal &rowIndex, Ordinal &colIndex, Real &realValue, const size_t lineNumber, const bool tolerant)
std::pair< bool, std::vector< size_t > > readPatternCoordinateData(std::istream &in, PatternCallback add, const size_t startingLineNumber, const bool tolerant)
bool readRealLine(const std::string &line, Ordinal &rowIndex, Ordinal &colIndex, Real &realValue, const size_t lineNumber, const bool tolerant)
int maxLineLength()
Maximum line length for a Matrix Market file.