Matrix: Improved readability of the code

This commit is contained in:
Henry Weller
2016-03-06 19:04:53 +00:00
parent 0f5059927c
commit e18ac8ff50
4 changed files with 47 additions and 47 deletions

View File

@ -30,14 +30,14 @@ License
template<class Form, class Type> template<class Form, class Type>
void Foam::Matrix<Form, Type>::allocate() void Foam::Matrix<Form, Type>::allocate()
{ {
if (n_ && m_) if (nRows_ && nCols_)
{ {
v_ = new Type*[n_]; v_ = new Type*[nRows_];
v_[0] = new Type[n_*m_]; v_[0] = new Type[nRows_*nCols_];
for (label i=1; i<n_; i++) for (label i=1; i<nRows_; i++)
{ {
v_[i] = v_[i-1] + m_; v_[i] = v_[i-1] + nCols_;
} }
} }
} }
@ -61,14 +61,14 @@ Foam::Matrix<Form, Type>::~Matrix()
template<class Form, class Type> template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const label n, const label m) Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
: :
n_(n), nRows_(n),
m_(m), nCols_(m),
v_(NULL) v_(NULL)
{ {
if (n_ < 0 || m_ < 0) if (nRows_ < 0 || nCols_ < 0)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "bad n, m " << n_ << ", " << m_ << "bad n, m " << nRows_ << ", " << nCols_
<< abort(FatalError); << abort(FatalError);
} }
@ -79,14 +79,14 @@ Foam::Matrix<Form, Type>::Matrix(const label n, const label m)
template<class Form, class Type> template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a) Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
: :
n_(n), nRows_(n),
m_(m), nCols_(m),
v_(NULL) v_(NULL)
{ {
if (n_ < 0 || m_ < 0) if (nRows_ < 0 || nCols_ < 0)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "bad n, m " << n_ << ", " << m_ << "bad n, m " << nRows_ << ", " << nCols_
<< abort(FatalError); << abort(FatalError);
} }
@ -96,7 +96,7 @@ Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
{ {
Type* v = v_[0]; Type* v = v_[0];
label nm = n_*m_; label nm = nRows_*nCols_;
for (label i=0; i<nm; i++) for (label i=0; i<nm; i++)
{ {
@ -109,8 +109,8 @@ Foam::Matrix<Form, Type>::Matrix(const label n, const label m, const Type& a)
template<class Form, class Type> template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a) Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
: :
n_(a.n_), nRows_(a.nRows_),
m_(a.m_), nCols_(a.nCols_),
v_(NULL) v_(NULL)
{ {
if (a.v_) if (a.v_)
@ -119,7 +119,7 @@ Foam::Matrix<Form, Type>::Matrix(const Matrix<Form, Type>& a)
Type* v = v_[0]; Type* v = v_[0];
const Type* av = a.v_[0]; const Type* av = a.v_[0];
label nm = n_*m_; label nm = nRows_*nCols_;
for (label i=0; i<nm; i++) for (label i=0; i<nm; i++)
{ {
v[i] = av[i]; v[i] = av[i];
@ -136,8 +136,8 @@ void Foam::Matrix<Form, Type>::clear()
delete[] (v_[0]); delete[] (v_[0]);
delete[] v_; delete[] v_;
} }
n_ = 0; nRows_ = 0;
m_ = 0; nCols_ = 0;
v_ = NULL; v_ = NULL;
} }
@ -147,11 +147,11 @@ void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& a)
{ {
clear(); clear();
n_ = a.n_; nRows_ = a.nRows_;
a.n_ = 0; a.nRows_ = 0;
m_ = a.m_; nCols_ = a.nCols_;
a.m_ = 0; a.nCols_ = 0;
v_ = a.v_; v_ = a.v_;
a.v_ = NULL; a.v_ = NULL;
@ -185,7 +185,7 @@ void Foam::Matrix<Form, Type>::operator=(const Type& t)
{ {
Type* v = v_[0]; Type* v = v_[0];
label nm = n_*m_; label nm = nRows_*nCols_;
for (label i=0; i<nm; i++) for (label i=0; i<nm; i++)
{ {
v[i] = t; v[i] = t;
@ -204,11 +204,11 @@ void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
<< abort(FatalError); << abort(FatalError);
} }
if (n_ != a.n_ || m_ != a.m_) if (nRows_ != a.nRows_ || nCols_ != a.nCols_)
{ {
clear(); clear();
n_ = a.n_; nRows_ = a.nRows_;
m_ = a.m_; nCols_ = a.nCols_;
allocate(); allocate();
} }
@ -217,7 +217,7 @@ void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& a)
Type* v = v_[0]; Type* v = v_[0];
const Type* av = a.v_[0]; const Type* av = a.v_[0];
label nm = n_*m_; label nm = nRows_*nCols_;
for (label i=0; i<nm; i++) for (label i=0; i<nm; i++)
{ {
v[i] = av[i]; v[i] = av[i];

View File

@ -76,7 +76,7 @@ class Matrix
// Private data // Private data
//- Number of rows and columns in Matrix. //- Number of rows and columns in Matrix.
label n_, m_; label nRows_, nCols_;
//- Row pointers //- Row pointers
Type** __restrict__ v_; Type** __restrict__ v_;

View File

@ -28,8 +28,8 @@ License
template<class Form, class Type> template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix() inline Foam::Matrix<Form, Type>::Matrix()
: :
n_(0), nRows_(0),
m_(0), nCols_(0),
v_(NULL) v_(NULL)
{} {}
@ -55,37 +55,37 @@ inline const Foam::Matrix<Form, Type>& Foam::Matrix<Form, Type>::null()
template<class Form, class Type> template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::n() const inline Foam::label Foam::Matrix<Form, Type>::n() const
{ {
return n_; return nRows_;
} }
template<class Form, class Type> template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::m() const inline Foam::label Foam::Matrix<Form, Type>::m() const
{ {
return m_; return nCols_;
} }
template<class Form, class Type> template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::size() const inline Foam::label Foam::Matrix<Form, Type>::size() const
{ {
return n_*m_; return nRows_*nCols_;
} }
template<class Form, class Type> template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checki(const label i) const inline void Foam::Matrix<Form, Type>::checki(const label i) const
{ {
if (!n_) if (!nRows_)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "attempt to access element from zero sized row" << "attempt to access element from zero sized row"
<< abort(FatalError); << abort(FatalError);
} }
else if (i<0 || i>=n_) else if (i<0 || i>=nRows_)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "index " << i << " out of range 0 ... " << n_-1 << "index " << i << " out of range 0 ... " << nRows_-1
<< abort(FatalError); << abort(FatalError);
} }
} }
@ -94,16 +94,16 @@ inline void Foam::Matrix<Form, Type>::checki(const label i) const
template<class Form, class Type> template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checkj(const label j) const inline void Foam::Matrix<Form, Type>::checkj(const label j) const
{ {
if (!m_) if (!nCols_)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "attempt to access element from zero sized column" << "attempt to access element from zero sized column"
<< abort(FatalError); << abort(FatalError);
} }
else if (j<0 || j>=m_) else if (j<0 || j>=nCols_)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "index " << j << " out of range 0 ... " << m_-1 << "index " << j << " out of range 0 ... " << nCols_-1
<< abort(FatalError); << abort(FatalError);
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -34,8 +34,8 @@ License
template<class Form, class Type> template<class Form, class Type>
Foam::Matrix<Form, Type>::Matrix(Istream& is) Foam::Matrix<Form, Type>::Matrix(Istream& is)
: :
n_(0), nRows_(0),
m_(0), nCols_(0),
v_(NULL) v_(NULL)
{ {
operator>>(is, *this); operator>>(is, *this);
@ -59,10 +59,10 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Form, Type>& M)
if (firstToken.isLabel()) if (firstToken.isLabel())
{ {
M.n_ = firstToken.labelToken(); M.nRows_ = firstToken.labelToken();
M.m_ = readLabel(is); M.nCols_ = readLabel(is);
label nm = M.n_*M.m_; label nm = M.nRows_*M.nCols_;
// Read list contents depending on data format // Read list contents depending on data format
if (is.format() == IOstream::ASCII || !contiguous<Type>()) if (is.format() == IOstream::ASCII || !contiguous<Type>())
@ -151,7 +151,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Matrix<Form, Type>& M)
template<class Form, class Type> template<class Form, class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M) Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
{ {
label nm = M.n_*M.m_; label nm = M.nRows_*M.nCols_;
os << M.n() << token::SPACE << M.m(); os << M.n() << token::SPACE << M.m();