STYLE: minor simplification of check for uniform contents

This commit is contained in:
Mark Olesen
2016-07-01 09:09:50 +02:00
parent cae7ce37f5
commit ce121c92dc
8 changed files with 56 additions and 48 deletions

View File

@ -42,13 +42,13 @@ Foam::Istream& Foam::operator>>(Istream& is, CompactListList<T, Container>& lst)
{ {
is >> lst.offsets_ >> lst.m_; is >> lst.offsets_ >> lst.m_;
// Note: empty list gets output as two empty lists // Note: empty list gets output as two empty lists
if (lst.offsets_.size() == 0) if (lst.offsets_.size())
{ {
lst.size_ = 0; lst.size_ = lst.offsets_.size()-1;
} }
else else
{ {
lst.size_ = lst.offsets_.size()-1; lst.size_ = 0;
} }
return is; return is;
} }

View File

@ -118,6 +118,8 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
} }
else else
{ {
// contents are binary and contiguous
is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T)); is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
is.fatalCheck is.fatalCheck
@ -167,12 +169,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII || !contiguous<T>()) if (os.format() == IOstream::ASCII || !contiguous<T>())
{ {
bool uniform = false; // Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (Size > 1 && contiguous<T>());
if (Size > 1 && contiguous<T>()) if (uniform)
{ {
uniform = true;
forAll(L, i) forAll(L, i)
{ {
if (L[i] != L[0]) if (L[i] != L[0])
@ -194,7 +194,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
// Write end delimiter // Write end delimiter
os << token::END_BLOCK; os << token::END_BLOCK;
} }
else if (Size <= 1 ||(Size < 11 && contiguous<T>())) else if (Size <= 1 || (Size < 11 && contiguous<T>()))
{ {
// Write start delimiter // Write start delimiter
os << token::BEGIN_LIST; os << token::BEGIN_LIST;
@ -202,7 +202,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
// Write contents // Write contents
forAll(L, i) forAll(L, i)
{ {
if (i > 0) os << token::SPACE; if (i) os << token::SPACE;
os << L[i]; os << L[i];
} }
@ -226,6 +226,9 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
} }
else else
{ {
// Contents are binary and contiguous
// write(...) includes surrounding start/end delimiters
os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T)); os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
} }

View File

@ -92,6 +92,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
} }
else else
{ {
// uniform content (delimiter == token::BEGIN_BLOCK)
T element; T element;
is >> element; is >> element;
@ -113,6 +115,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
} }
else else
{ {
// contents are binary and contiguous
if (s) if (s)
{ {
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T)); is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));

View File

@ -407,12 +407,10 @@ Foam::Ostream& Foam::PackedList<nBits>::write
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII) if (os.format() == IOstream::ASCII)
{ {
bool uniform = false; // Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (sz > 1 && !indexedOutput);
if (sz > 1 && !indexedOutput) if (uniform)
{ {
uniform = true;
forAll(lst, i) forAll(lst, i)
{ {
if (lst[i] != lst[0]) if (lst[i] != lst[0])
@ -475,9 +473,12 @@ Foam::Ostream& Foam::PackedList<nBits>::write
} }
else else
{ {
// Contents are binary and contiguous
os << nl << sz << nl; os << nl << sz << nl;
if (sz) if (sz)
{ {
// write(...) includes surrounding start/end delimiters
os.write os.write
( (
reinterpret_cast<const char*>(lst.storage().cdata()), reinterpret_cast<const char*>(lst.storage().cdata()),

View File

@ -3,7 +3,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-2014 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -40,12 +40,10 @@ Foam::Ostream& Foam::operator<<
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII || !contiguous<T>()) if (os.format() == IOstream::ASCII || !contiguous<T>())
{ {
bool uniform = false; // Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (L.size() > 1 && contiguous<T>());
if (L.size() > 1 && contiguous<T>()) if (uniform)
{ {
uniform = true;
forAll(L, i) forAll(L, i)
{ {
if (L[i] != L[0]) if (L[i] != L[0])
@ -99,14 +97,15 @@ Foam::Ostream& Foam::operator<<
} }
else else
{ {
// this is annoying, and wasteful, but there's currently no alternative // Contents are binary and contiguous
os << nl << L.size() << nl; os << nl << L.size() << nl;
if (L.size()) if (L.size())
{ {
// This is annoying, and wasteful, but currently no alternative
List<T> lst = L(); List<T> lst = L();
// write(...) includes surrounding start/end delimiters
os.write os.write
( (
reinterpret_cast<const char*>(lst.cdata()), reinterpret_cast<const char*>(lst.cdata()),

View File

@ -3,7 +3,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-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -65,12 +65,10 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII || !contiguous<T>()) if (os.format() == IOstream::ASCII || !contiguous<T>())
{ {
bool uniform = false; // Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (L.size() > 1 && contiguous<T>());
if (L.size() > 1 && contiguous<T>()) if (uniform)
{ {
uniform = true;
forAll(L, i) forAll(L, i)
{ {
if (L[i] != L[0]) if (L[i] != L[0])
@ -100,7 +98,7 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
// Write contents // Write contents
forAll(L, i) forAll(L, i)
{ {
if (i > 0) os << token::SPACE; if (i) os << token::SPACE;
os << L[i]; os << L[i];
} }
@ -124,10 +122,13 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
} }
else else
{ {
// Contents are binary and contiguous
os << nl << L.size() << nl; os << nl << L.size() << nl;
if (L.size()) if (L.size())
{ {
os.write(reinterpret_cast<const char*>(L.v_), L.byteSize()); // write(...) includes surrounding start/end delimiters
os.write(reinterpret_cast<const char*>(L.cdata()), L.byteSize());
} }
} }
@ -208,6 +209,8 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L)
} }
else else
{ {
// uniform content (delimiter == token::BEGIN_BLOCK)
T element; T element;
is >> element; is >> element;
@ -229,6 +232,8 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L)
} }
else else
{ {
// contents are binary and contiguous
if (s) if (s)
{ {
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T)); is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));

View File

@ -727,12 +727,10 @@ void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const
{ {
os.writeKeyword(keyword); os.writeKeyword(keyword);
bool uniform = false; // Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (this->size() && contiguous<Type>());
if (this->size() && contiguous<Type>()) if (uniform)
{ {
uniform = true;
forAll(*this, i) forAll(*this, i)
{ {
if (this->operator[](i) != this->operator[](0)) if (this->operator[](i) != this->operator[](0))

View File

@ -160,15 +160,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
{ {
if (mn) if (mn)
{ {
bool uniform = false;
const Type* v = M.v_; const Type* v = M.v_;
if (mn > 1 && contiguous<Type>()) // can the contents be considered 'uniform' (ie, identical)
bool uniform = (mn > 1 && contiguous<Type>());
if (uniform)
{ {
uniform = true; for (label i=0; i<mn; ++i)
for (label i=0; i<mn; i++)
{ {
if (v[i] != v[0]) if (v[i] != v[0])
{ {
@ -180,18 +178,18 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
if (uniform) if (uniform)
{ {
// Write size of list and start contents delimiter // Write start delimiter
os << token::BEGIN_BLOCK; os << token::BEGIN_BLOCK;
// Write list contents // Write contents
os << v[0]; os << v[0];
// Write end of contents delimiter // Write end delimiter
os << token::END_BLOCK; os << token::END_BLOCK;
} }
else if (mn < 10 && contiguous<Type>()) else if (mn < 10 && contiguous<Type>())
{ {
// Write size of list and start contents delimiter // Write start contents delimiter
os << token::BEGIN_LIST; os << token::BEGIN_LIST;
label k = 0; label k = 0;
@ -204,7 +202,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
// Write row // Write row
for (label j=0; j< M.n(); j++) for (label j=0; j< M.n(); j++)
{ {
if (j > 0) os << token::SPACE; if (j) os << token::SPACE;
os << v[k++]; os << v[k++];
} }
@ -216,7 +214,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
} }
else else
{ {
// Write size of list and start contents delimiter // Write start contents delimiter
os << nl << token::BEGIN_LIST; os << nl << token::BEGIN_LIST;
label k = 0; label k = 0;