10 #ifndef __Teuchos_MatrixMarket_generic_hpp
11 #define __Teuchos_MatrixMarket_generic_hpp
14 #ifdef HAVE_TEUCHOS_COMPLEX
16 #endif // HAVE_TEUCHOS_COMPLEX
20 #include <type_traits>
24 namespace MatrixMarket {
61 TEUCHOSNUMERICS_LIB_DLL_EXPORT
bool
62 checkCommentLine (
const std::string& line,
65 const size_t lineNumber,
67 const bool maybeBannerLine =
false);
102 template<
class Ordinal>
104 readPatternData (std::istream& istr,
107 const size_t lineNumber,
110 Ordinal the_rowIndex, the_colIndex;
112 if (istr.eof() || istr.fail())
118 std::ostringstream os;
119 os <<
"Unable to read any data from line " << lineNumber <<
" of input";
120 throw std::invalid_argument(os.str());
123 istr >> the_rowIndex;
130 std::ostringstream os;
131 os <<
"Failed to get row index from line " << lineNumber <<
" of input";
132 throw std::invalid_argument(os.str());
141 std::ostringstream os;
142 os <<
"No more data after row index on line " << lineNumber <<
" of input";
143 throw std::invalid_argument(os.str());
146 istr >> the_colIndex;
153 std::ostringstream os;
154 os <<
"Failed to get column index from line " << lineNumber <<
" of input";
155 throw std::invalid_argument(os.str());
158 rowIndex = the_rowIndex;
159 colIndex = the_colIndex;
201 template<
class Ordinal,
class Real>
203 readRealData (std::istream& istr,
207 const size_t lineNumber,
211 if (! readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant))
217 std::ostringstream os;
218 os <<
"Failed to read pattern data from line " << lineNumber <<
" of input";
219 throw std::invalid_argument(os.str());
228 std::ostringstream os;
229 os <<
"No more data after pattern data on line " << lineNumber <<
" of input";
230 throw std::invalid_argument(os.str());
234 if (std::is_same<Real, float>::value) {
237 the_realValue =
static_cast<Real
> (dblVal);
240 istr >> the_realValue;
249 std::ostringstream os;
250 os <<
"Failed to get real value from line " << lineNumber <<
" of input";
251 throw std::invalid_argument(os.str());
254 realValue = the_realValue;
258 #ifdef HAVE_TEUCHOS_COMPLEX
301 template<
class Ordinal,
class Real>
303 readComplexData (std::istream& istr,
308 const size_t lineNumber,
311 Real the_realPart, the_imagPart;
312 if (! readRealData (istr, rowIndex, colIndex, the_realPart, lineNumber, tolerant))
318 std::ostringstream os;
319 os <<
"Failed to read pattern data and/or real value from line "
320 << lineNumber <<
" of input";
321 throw std::invalid_argument(os.str());
330 std::ostringstream os;
331 os <<
"No more data after real value on line "
332 << lineNumber <<
" of input";
333 throw std::invalid_argument(os.str());
336 istr >> the_imagPart;
343 std::ostringstream os;
344 os <<
"Failed to get imaginary value from line "
345 << lineNumber <<
" of input";
346 throw std::invalid_argument(os.str());
349 realPart = the_realPart;
350 imagPart = the_imagPart;
353 #endif // HAVE_TEUCHOS_COMPLEX
355 template<
class Ordinal>
357 readPatternLine (
const std::string& line,
360 const size_t lineNumber,
364 std::istringstream istr (line);
365 return readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant);
368 template<
class Ordinal,
class Real>
370 readRealLine (
const std::string& line,
374 const size_t lineNumber,
378 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
386 throw std::logic_error(
"Should never get here! checkCommentLine() "
387 "is supposed to catch empty lines.");
394 std::istringstream istr (line.substr (start, size));
395 return readRealData (istr, rowIndex, colIndex, realValue, lineNumber, tolerant);
398 #ifdef HAVE_TEUCHOS_COMPLEX
399 template<
class Ordinal,
class Real>
401 readComplexLine (
const std::string& line,
406 const size_t lineNumber,
410 if (checkCommentLine (line, start, end, lineNumber, tolerant))
418 throw std::logic_error(
"Should never get here! checkCommentLine() "
419 "is supposed to catch empty lines.");
424 std::istringstream istr (line.substr (start, end));
426 Real the_realPart, the_imagPart;
428 readComplexData (istr, rowIndex, colIndex, the_realPart, the_imagPart,
429 lineNumber, tolerant);
432 realPart = the_realPart;
433 imagPart = the_imagPart;
437 #endif // HAVE_TEUCHOS_COMPLEX
439 template<
class Ordinal,
class PatternCallback>
440 std::pair<bool, std::vector<size_t> >
441 readPatternCoordinateData (std::istream& in,
443 const size_t startingLineNumber,
447 size_t lineNumber = startingLineNumber;
448 bool anySucceeded =
false;
449 bool allSucceeded =
true;
450 std::vector<size_t> badLineNumbers;
451 while (getline (in, line)) {
453 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
456 const std::string theLine = line.substr (start, size);
458 Ordinal rowIndex, colIndex;
459 const bool localSuccess =
460 readPatternLine (theLine, rowIndex, colIndex,
461 lineNumber++, tolerant);
462 anySucceeded = anySucceeded || localSuccess;
463 allSucceeded = allSucceeded && localSuccess;
464 if (! localSuccess) {
465 badLineNumbers.push_back (lineNumber);
469 add(rowIndex, colIndex);
472 if (lineNumber == startingLineNumber) {
476 return std::make_pair (allSucceeded, badLineNumbers);
480 template<
class Ordinal>
482 readCoordinateDimensions (std::istream& in,
485 Ordinal& numNonzeros,
491 Ordinal the_numRows, the_numCols, the_numNonzeros;
499 bool commentLine =
true;
503 if (in.eof () || in.fail ()) {
508 std::ostringstream os;
509 os <<
"Unable to get coordinate dimensions line (at all) "
510 "from (line " << lineNumber <<
") of input stream; the "
511 "input stream claims that it is at \"end-of-file\" or has "
512 "an otherwise \"fail\"ed state.";
513 throw std::invalid_argument(os.str());
517 if (! getline (in, line)) {
522 std::ostringstream os;
523 os <<
"Failed to read coordinate dimensions line (at all) "
524 "from (line " << lineNumber <<
" from input stream. The "
525 "line should contain the coordinate matrix dimensions in "
526 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
527 throw std::invalid_argument(os.str());
534 size_t start = 0, size = 0;
535 commentLine = checkCommentLine (line, start, size,
536 lineNumber, tolerant);
546 std::istringstream istr (line);
547 if (istr.eof () || istr.fail ()) {
552 std::ostringstream os;
553 os <<
"Unable to read any coordinate dimensions data from line "
554 << lineNumber <<
" of input stream. Offending line:" << endl
556 throw std::invalid_argument(os.str());
565 std::ostringstream os;
566 os <<
"Failed to get number of rows from the coordinate "
567 "dimensions line (line " << lineNumber <<
" of input stream)."
568 " Offending line:" << endl << line;
569 throw std::invalid_argument(os.str());
572 else if (istr.eof ()) {
577 std::ostringstream os;
578 os <<
"No more data after number of rows, in the coordinate "
579 "dimensions line (line " << lineNumber <<
" of input stream)."
580 " Offending line:" << endl << line;
581 throw std::invalid_argument(os.str());
590 std::ostringstream os;
591 os <<
"Failed to get number of columns from the coordinate "
592 "dimensions line (line " << lineNumber <<
" of input stream)."
593 " Offending line:" << endl << line;
594 throw std::invalid_argument(os.str());
597 else if (istr.eof ()) {
602 std::ostringstream os;
603 os <<
"No more data after number of columns, in the coordinate "
604 "dimensions line (line " << lineNumber <<
" of input stream)."
605 " Offending line:" << endl << line;
606 throw std::invalid_argument (os.str ());
609 istr >> the_numNonzeros;
615 std::ostringstream os;
616 os <<
"Failed to get number of nonzeros from the coordinate "
617 "dimensions line (line " << lineNumber <<
" of input stream)."
618 " Offending line:" << endl << line;
619 throw std::invalid_argument (os.str ());
622 numRows = the_numRows;
623 numCols = the_numCols;
624 numNonzeros = the_numNonzeros;
635 #endif // __Teuchos_MatrixMarket_generic_hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers...