/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "Matrix.H" #include "Istream.H" #include "Ostream.H" #include "token.H" #include "contiguous.H" // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // template Foam::Matrix::Matrix(Istream& is) : nRows_(0), nCols_(0), v_(NULL) { operator>>(is, *this); } template Foam::Istream& Foam::operator>>(Istream& is, Matrix& M) { // Anull matrix M.clear(); is.fatalCheck("operator>>(Istream&, Matrix&)"); token firstToken(is); is.fatalCheck ( "operator>>(Istream&, Matrix&) : reading first token" ); if (firstToken.isLabel()) { M.nRows_ = firstToken.labelToken(); M.nCols_ = readLabel(is); label nm = M.nRows_*M.nCols_; // Read list contents depending on data format if (is.format() == IOstream::ASCII || !contiguous()) { // Read beginning of contents char listDelimiter = is.readBeginList("Matrix"); if (nm) { M.allocate(); Type* v = M.v_[0]; if (listDelimiter == token::BEGIN_LIST) { label k = 0; // loop over rows for (label i=0; i> v[k++]; is.fatalCheck ( "operator>>(Istream&, Matrix&) : " "reading entry" ); } is.readEndList("MatrixRow"); } } else { Type element; is >> element; is.fatalCheck ( "operator>>(Istream&, Matrix&) : " "reading the single entry" ); for (label i=0; i(v), nm*sizeof(Type)); is.fatalCheck ( "operator>>(Istream&, Matrix&) : " "reading the binary block" ); } } } else { FatalIOErrorInFunction(is) << "incorrect first token, expected , found " << firstToken.info() << exit(FatalIOError); } return is; } template Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix& M) { label nm = M.nRows_*M.nCols_; os << M.n() << token::SPACE << M.m(); // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { if (nm) { bool uniform = false; const Type* v = M.v_[0]; if (nm > 1 && contiguous()) { uniform = true; for (label i=0; i< nm; i++) { if (v[i] != v[0]) { uniform = false; break; } } } if (uniform) { // Write size of list and start contents delimiter os << token::BEGIN_BLOCK; // Write list contents os << v[0]; // Write end of contents delimiter os << token::END_BLOCK; } else if (nm < 10 && contiguous()) { // Write size of list and start contents delimiter os << token::BEGIN_LIST; label k = 0; // loop over rows for (label i=0; i< M.n(); i++) { os << token::BEGIN_LIST; // Write row for (label j=0; j< M.m(); j++) { if (j > 0) os << token::SPACE; os << v[k++]; } os << token::END_LIST; } // Write end of contents delimiter os << token::END_LIST; } else { // Write size of list and start contents delimiter os << nl << token::BEGIN_LIST; label k = 0; // loop over rows for (label i=0; i< M.n(); i++) { os << nl << token::BEGIN_LIST; // Write row for (label j=0; j< M.m(); j++) { os << nl << v[k++]; } os << nl << token::END_LIST; } // Write end of contents delimiter os << nl << token::END_LIST << nl; } } else { os << token::BEGIN_LIST << token::END_LIST << nl; } } else { if (nm) { os.write(reinterpret_cast(M.v_[0]), nm*sizeof(Type)); } } // Check state of IOstream os.check("Ostream& operator<<(Ostream&, const Matrix&)"); return os; } // ************************************************************************* //