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 {
93 TEUCHOSNUMERICS_LIB_DLL_EXPORT
bool
94 checkCommentLine (
const std::string& line,
97 const size_t lineNumber,
99 const bool maybeBannerLine =
false);
134 template<
class Ordinal>
136 readPatternData (std::istream& istr,
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>
235 readRealData (std::istream& istr,
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>
389 readPatternLine (
const std::string& line,
392 const size_t lineNumber,
396 std::istringstream istr (line);
397 return readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant);
400 template<
class Ordinal,
class Real>
402 readRealLine (
const std::string& line,
406 const size_t lineNumber,
410 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
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,
442 if (checkCommentLine (line, start, end, lineNumber, tolerant))
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> >
473 readPatternCoordinateData (std::istream& in,
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 while (getline (in, line)) {
485 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
488 const std::string theLine = line.substr (start, size);
490 Ordinal rowIndex, colIndex;
491 const bool localSuccess =
492 readPatternLine (theLine, rowIndex, colIndex,
493 lineNumber++, tolerant);
494 anySucceeded = anySucceeded || localSuccess;
495 allSucceeded = allSucceeded && localSuccess;
496 if (! localSuccess) {
497 badLineNumbers.push_back (lineNumber);
501 add(rowIndex, colIndex);
504 if (lineNumber == startingLineNumber) {
508 return std::make_pair (allSucceeded, badLineNumbers);
512 template<
class Ordinal>
514 readCoordinateDimensions (std::istream& in,
517 Ordinal& numNonzeros,
523 Ordinal the_numRows, the_numCols, the_numNonzeros;
531 bool commentLine =
true;
535 if (in.eof () || in.fail ()) {
540 std::ostringstream os;
541 os <<
"Unable to get coordinate dimensions line (at all) "
542 "from (line " << lineNumber <<
") of input stream; the "
543 "input stream claims that it is at \"end-of-file\" or has "
544 "an otherwise \"fail\"ed state.";
545 throw std::invalid_argument(os.str());
549 if (! getline (in, line)) {
554 std::ostringstream os;
555 os <<
"Failed to read coordinate dimensions line (at all) "
556 "from (line " << lineNumber <<
" from input stream. The "
557 "line should contain the coordinate matrix dimensions in "
558 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
559 throw std::invalid_argument(os.str());
566 size_t start = 0, size = 0;
567 commentLine = checkCommentLine (line, start, size,
568 lineNumber, tolerant);
578 std::istringstream istr (line);
579 if (istr.eof () || istr.fail ()) {
584 std::ostringstream os;
585 os <<
"Unable to read any coordinate dimensions data from line "
586 << lineNumber <<
" of input stream. Offending line:" << endl
588 throw std::invalid_argument(os.str());
597 std::ostringstream os;
598 os <<
"Failed to get number of rows from the coordinate "
599 "dimensions line (line " << lineNumber <<
" of input stream)."
600 " Offending line:" << endl << line;
601 throw std::invalid_argument(os.str());
604 else if (istr.eof ()) {
609 std::ostringstream os;
610 os <<
"No more data after number of rows, in the coordinate "
611 "dimensions line (line " << lineNumber <<
" of input stream)."
612 " Offending line:" << endl << line;
613 throw std::invalid_argument(os.str());
622 std::ostringstream os;
623 os <<
"Failed to get number of columns from the coordinate "
624 "dimensions line (line " << lineNumber <<
" of input stream)."
625 " Offending line:" << endl << line;
626 throw std::invalid_argument(os.str());
629 else if (istr.eof ()) {
634 std::ostringstream os;
635 os <<
"No more data after number of columns, in the coordinate "
636 "dimensions line (line " << lineNumber <<
" of input stream)."
637 " Offending line:" << endl << line;
638 throw std::invalid_argument (os.str ());
641 istr >> the_numNonzeros;
647 std::ostringstream os;
648 os <<
"Failed to get number of nonzeros from the coordinate "
649 "dimensions line (line " << lineNumber <<
" of input stream)."
650 " Offending line:" << endl << line;
651 throw std::invalid_argument (os.str ());
654 numRows = the_numRows;
655 numCols = the_numCols;
656 numNonzeros = the_numNonzeros;
667 #endif // __Teuchos_MatrixMarket_generic_hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers...