42 #ifndef __Teuchos_MatrixMarket_generic_hpp
43 #define __Teuchos_MatrixMarket_generic_hpp
46 #ifdef HAVE_TEUCHOS_COMPLEX
48 #endif // HAVE_TEUCHOS_COMPLEX
55 namespace MatrixMarket {
92 TEUCHOSNUMERICS_LIB_DLL_EXPORT
bool
93 checkCommentLine (
const std::string& line,
96 const size_t lineNumber,
98 const bool maybeBannerLine =
false);
133 template<
class Ordinal>
135 readPatternData (std::istream& istr,
138 const size_t lineNumber,
141 Ordinal the_rowIndex, the_colIndex;
143 if (istr.eof() || istr.fail())
149 std::ostringstream os;
150 os <<
"Unable to read any data from line " << lineNumber <<
" of input";
151 throw std::invalid_argument(os.str());
154 istr >> the_rowIndex;
161 std::ostringstream os;
162 os <<
"Failed to get row index from line " << lineNumber <<
" of input";
163 throw std::invalid_argument(os.str());
172 std::ostringstream os;
173 os <<
"No more data after row index on line " << lineNumber <<
" of input";
174 throw std::invalid_argument(os.str());
177 istr >> the_colIndex;
184 std::ostringstream os;
185 os <<
"Failed to get column index from line " << lineNumber <<
" of input";
186 throw std::invalid_argument(os.str());
189 rowIndex = the_rowIndex;
190 colIndex = the_colIndex;
232 template<
class Ordinal,
class Real>
234 readRealData (std::istream& istr,
238 const size_t lineNumber,
242 if (! readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant))
248 std::ostringstream os;
249 os <<
"Failed to read pattern data from line " << lineNumber <<
" of input";
250 throw std::invalid_argument(os.str());
259 std::ostringstream os;
260 os <<
"No more data after pattern data on line " << lineNumber <<
" of input";
261 throw std::invalid_argument(os.str());
264 istr >> the_realValue;
271 std::ostringstream os;
272 os <<
"Failed to get real value from line " << lineNumber <<
" of input";
273 throw std::invalid_argument(os.str());
276 realValue = the_realValue;
280 #ifdef HAVE_TEUCHOS_COMPLEX
323 template<
class Ordinal,
class Real>
325 readComplexData (std::istream& istr,
330 const size_t lineNumber,
333 Real the_realPart, the_imagPart;
334 if (! readRealData (istr, rowIndex, colIndex, the_realPart, lineNumber, tolerant))
340 std::ostringstream os;
341 os <<
"Failed to read pattern data and/or real value from line "
342 << lineNumber <<
" of input";
343 throw std::invalid_argument(os.str());
352 std::ostringstream os;
353 os <<
"No more data after real value on line "
354 << lineNumber <<
" of input";
355 throw std::invalid_argument(os.str());
358 istr >> the_imagPart;
365 std::ostringstream os;
366 os <<
"Failed to get imaginary value from line "
367 << lineNumber <<
" of input";
368 throw std::invalid_argument(os.str());
371 realPart = the_realPart;
372 imagPart = the_imagPart;
375 #endif // HAVE_TEUCHOS_COMPLEX
377 template<
class Ordinal>
379 readPatternLine (
const std::string& line,
382 const size_t lineNumber,
386 std::istringstream istr (line);
387 return readPatternData (istr, rowIndex, colIndex, lineNumber, tolerant);
390 template<
class Ordinal,
class Real>
392 readRealLine (
const std::string& line,
396 const size_t lineNumber,
400 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
408 throw std::logic_error(
"Should never get here! checkCommentLine() "
409 "is supposed to catch empty lines.");
416 std::istringstream istr (line.substr (start, size));
417 return readRealData (istr, rowIndex, colIndex, realValue, lineNumber, tolerant);
420 #ifdef HAVE_TEUCHOS_COMPLEX
421 template<
class Ordinal,
class Real>
423 readComplexLine (
const std::string& line,
428 const size_t lineNumber,
432 if (checkCommentLine (line, start, end, lineNumber, tolerant))
440 throw std::logic_error(
"Should never get here! checkCommentLine() "
441 "is supposed to catch empty lines.");
446 std::istringstream istr (line.substr (start, end));
448 Real the_realPart, the_imagPart;
450 readComplexData (istr, rowIndex, colIndex, the_realPart, the_imagPart,
451 lineNumber, tolerant);
454 realPart = the_realPart;
455 imagPart = the_imagPart;
459 #endif // HAVE_TEUCHOS_COMPLEX
461 template<
class Ordinal,
class PatternCallback>
462 std::pair<bool, std::vector<size_t> >
463 readPatternCoordinateData (std::istream& in,
465 const size_t startingLineNumber,
469 size_t lineNumber = startingLineNumber;
470 bool anySucceeded =
false;
471 bool allSucceeded =
true;
472 std::vector<size_t> badLineNumbers;
473 size_t validDataLines = 0;
474 while (getline (in, line)) {
476 if (checkCommentLine (line, start, size, lineNumber, tolerant)) {
479 const std::string theLine = line.substr (start, size);
481 Ordinal rowIndex, colIndex;
482 const bool localSuccess =
483 readPatternLine (theLine, rowIndex, colIndex,
484 lineNumber++, tolerant);
485 anySucceeded = anySucceeded || localSuccess;
486 allSucceeded = allSucceeded && localSuccess;
487 if (! localSuccess) {
488 badLineNumbers.push_back (lineNumber);
492 add (rowIndex, colIndex);
496 if (lineNumber == startingLineNumber) {
500 return std::make_pair (allSucceeded, badLineNumbers);
504 template<
class Ordinal>
506 readCoordinateDimensions (std::istream& in,
509 Ordinal& numNonzeros,
515 Ordinal the_numRows, the_numCols, the_numNonzeros;
523 bool commentLine =
true;
527 if (in.eof () || in.fail ()) {
532 std::ostringstream os;
533 os <<
"Unable to get coordinate dimensions line (at all) "
534 "from (line " << lineNumber <<
") of input stream; the "
535 "input stream claims that it is at \"end-of-file\" or has "
536 "an otherwise \"fail\"ed state.";
537 throw std::invalid_argument(os.str());
541 if (! getline (in, line)) {
546 std::ostringstream os;
547 os <<
"Failed to read coordinate dimensions line (at all) "
548 "from (line " << lineNumber <<
" from input stream. The "
549 "line should contain the coordinate matrix dimensions in "
550 <<
" the form \"<numRows> <numCols> <numNonzeros>\".";
551 throw std::invalid_argument(os.str());
558 size_t start = 0, size = 0;
559 commentLine = checkCommentLine (line, start, size,
560 lineNumber, tolerant);
570 std::istringstream istr (line);
571 if (istr.eof () || istr.fail ()) {
576 std::ostringstream os;
577 os <<
"Unable to read any coordinate dimensions data from line "
578 << lineNumber <<
" of input stream. Offending line:" << endl
580 throw std::invalid_argument(os.str());
589 std::ostringstream os;
590 os <<
"Failed to get number of rows from the coordinate "
591 "dimensions line (line " << lineNumber <<
" of input stream)."
592 " Offending line:" << endl << line;
593 throw std::invalid_argument(os.str());
596 else if (istr.eof ()) {
601 std::ostringstream os;
602 os <<
"No more data after number of rows, in the coordinate "
603 "dimensions line (line " << lineNumber <<
" of input stream)."
604 " Offending line:" << endl << line;
605 throw std::invalid_argument(os.str());
614 std::ostringstream os;
615 os <<
"Failed to get number of columns from the coordinate "
616 "dimensions line (line " << lineNumber <<
" of input stream)."
617 " Offending line:" << endl << line;
618 throw std::invalid_argument(os.str());
621 else if (istr.eof ()) {
626 std::ostringstream os;
627 os <<
"No more data after number of columns, in the coordinate "
628 "dimensions line (line " << lineNumber <<
" of input stream)."
629 " Offending line:" << endl << line;
630 throw std::invalid_argument (os.str ());
633 istr >> the_numNonzeros;
639 std::ostringstream os;
640 os <<
"Failed to get number of nonzeros from the coordinate "
641 "dimensions line (line " << lineNumber <<
" of input stream)."
642 " Offending line:" << endl << line;
643 throw std::invalid_argument (os.str ());
646 numRows = the_numRows;
647 numCols = the_numCols;
648 numNonzeros = the_numNonzeros;
659 #endif // __Teuchos_MatrixMarket_generic_hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers...