ENH: make List uniform() method consistent with Field

- this protected method was previously used directly for the list
  output and had the check for 2 or more elements in it. Now simply
  test the List content and handle the output preference separately.
This commit is contained in:
Mark Olesen
2019-01-11 12:49:08 +01:00
parent 6c419174e1
commit 34386291cb
15 changed files with 66 additions and 84 deletions

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) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -89,10 +89,16 @@ Foam::PackedList<Width>::PackedList
template<unsigned Width> template<unsigned Width>
bool Foam::PackedList<Width>::uniform() const bool Foam::PackedList<Width>::uniform() const
{ {
if (size() < 2) // Trivial cases
if (empty())
{ {
return false; // Trivial case return false;
} }
else if (size() == 1)
{
return true;
}
// The value of the first element for testing // The value of the first element for testing
const unsigned int val = get(0); const unsigned int val = get(0);
@ -162,11 +168,13 @@ Foam::PackedList<Width>::unpack() const
"Width of IntType is too small to hold result" "Width of IntType is too small to hold result"
); );
if (size() < 2 || uniform()) if (empty())
{ {
const IntType val = (size() ? get(0) : 0); return List<IntType>(0);
}
return List<IntType>(size(), val); else if (uniform())
{
return List<IntType>(size(), static_cast<IntType>(get(0)));
} }
List<IntType> output(size()); List<IntType> output(size());

View File

@ -281,8 +281,7 @@ public:
//- The number of elements that can be stored with reallocating //- The number of elements that can be stored with reallocating
inline label capacity() const; inline label capacity() const;
//- True if there are two or more entries and all entries have //- True if all entries have identical values, and list is non-empty
//- identical values.
bool uniform() const; bool uniform() const;

View File

@ -215,9 +215,9 @@ Foam::Ostream& Foam::PackedList<Width>::writeList
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII) if (os.format() == IOstream::ASCII)
{ {
if (list.uniform()) if (len > 1 && list.uniform())
{ {
// Two or more entries, and all entries have identical values. // Two or more entries, and all have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
} }
else if (!shortLen || len <= shortLen) else if (!shortLen || len <= shortLen)

View File

@ -211,8 +211,7 @@ public:
//- True if no bits in this bitset are set. //- True if no bits in this bitset are set.
inline bool none() const; inline bool none() const;
//- True if there are two or more entries and all entries have //- True if all entries have identical values, and the set is non-empty
//- identical values.
inline bool uniform() const; inline bool uniform() const;
//- Count number of bits set. //- Count number of bits set.

View File

@ -449,7 +449,7 @@ inline bool Foam::bitSet::none() const
inline bool Foam::bitSet::uniform() const inline bool Foam::bitSet::uniform() const
{ {
return (size() > 1 && (test(0) ? all() : none())); return (size() == 1 || (size() > 1 && (test(0) ? all() : none())));
} }

View File

@ -48,7 +48,7 @@ Foam::Ostream& Foam::bitSet::writeList
// Write list contents depending on data format // Write list contents depending on data format
if (os.format() == IOstream::ASCII) if (os.format() == IOstream::ASCII)
{ {
if (list.uniform()) if (len > 1 && list.uniform())
{ {
// Two or more entries, and all entries have identical values. // Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;

View File

@ -90,8 +90,7 @@ protected:
// Protected Member Functions // Protected Member Functions
//- True if there are two or more entries and all entries have //- True if all entries have identical values, and list is non-empty
//- identical values.
inline bool uniform() const; inline bool uniform() const;
//- Write the FixedList with its compound type //- Write the FixedList with its compound type

View File

@ -42,22 +42,17 @@ inline const Foam::FixedList<T, N>& Foam::FixedList<T, N>::null()
template<class T, unsigned N> template<class T, unsigned N>
inline bool Foam::FixedList<T, N>::uniform() const inline bool Foam::FixedList<T, N>::uniform() const
{ {
if (N > 1) if (empty()) return false; // <- Compile-time disabled anyhow
for (unsigned i=1; i<N; ++i)
{ {
const T& val = first(); if (v_[0] != v_[i])
for (unsigned i=1; i<N; ++i)
{ {
if (val != (*this)[i]) return false;
{
return false;
}
} }
return true;
} }
return false; return true;
} }

View File

@ -71,8 +71,7 @@ protected:
// Protected Member Functions // Protected Member Functions
//- True if there are two or more entries and all entries have //- True if all entries have identical values, and list is non-empty
// identical values.
inline bool uniform() const; inline bool uniform() const;

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-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -30,22 +30,22 @@ inline bool Foam::UIndirectList<T>::uniform() const
{ {
const label len = this->size(); const label len = this->size();
if (len > 1) if (!len)
{ {
const T& val = (*this)[0]; return false;
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
return false;
}
}
return true;
} }
return false; const T& val = (*this)[0];
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
return false;
}
}
return true;
} }

View File

@ -44,7 +44,7 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList
// 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>())
{ {
if (contiguous<T>() && list.uniform()) if (len > 1 && contiguous<T>() && list.uniform())
{ {
// Two or more entries, and all entries have identical values. // Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;

View File

@ -112,8 +112,7 @@ protected:
// Use with care // Use with care
inline void size(const label n); inline void size(const label n);
//- True if there are two or more entries and all entries have //- True if all entries have identical values, and list is non-empty
// identical values.
inline bool uniform() const; inline bool uniform() const;
//- Write the UList with its compound type //- Write the UList with its compound type

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 | Copyright (C) 2015-2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,7 +24,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "error.H" #include "error.H"
#include "contiguous.H"
#include "pTraits.H" #include "pTraits.H"
#include "Swap.H" #include "Swap.H"
@ -35,22 +34,22 @@ inline bool Foam::UList<T>::uniform() const
{ {
const label len = size(); const label len = size();
if (len > 1) if (len == 0)
{ {
const T& val = first(); return false;
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
return false;
}
}
return true;
} }
return false; const T& val = first();
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
return false;
}
}
return true;
} }

View File

@ -81,7 +81,7 @@ Foam::Ostream& Foam::UList<T>::writeList
// 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>())
{ {
if (contiguous<T>() && list.uniform()) if (len > 1 && contiguous<T>() && list.uniform())
{ {
// Two or more entries, and all entries have identical values. // Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
@ -232,7 +232,7 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list)
} }
else else
{ {
// uniform content (delimiter == token::BEGIN_BLOCK) // Uniform content (delimiter == token::BEGIN_BLOCK)
T element; T element;
is >> element; is >> element;

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 | Copyright (C) 2015-2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -629,27 +629,12 @@ void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const
{ {
os.writeKeyword(keyword); os.writeKeyword(keyword);
const label len = this->size(); // The contents are 'uniform' if the list is non-empty
// and all entries have identical values.
// Can the contents be considered 'uniform' (ie, identical)? if (contiguous<Type>() && List<Type>::uniform())
bool uniform = (contiguous<Type>() && len);
if (uniform)
{ {
const Type& val = this->operator[](0); os << "uniform " << this->first();
for (label i=1; i<len; ++i)
{
if (val != this->operator[](i))
{
uniform = false;
break;
}
}
}
if (uniform)
{
os << "uniform " << this->operator[](0);
} }
else else
{ {