Commit 8babc148 authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent a3e97c6e
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -89,10 +89,16 @@ Foam::PackedList<Width>::PackedList
template<unsigned Width>
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
const unsigned int val = get(0);
......@@ -162,11 +168,13 @@ Foam::PackedList<Width>::unpack() const
"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>(size(), val);
return List<IntType>(0);
}
else if (uniform())
{
return List<IntType>(size(), static_cast<IntType>(get(0)));
}
List<IntType> output(size());
......
......@@ -281,8 +281,7 @@ public:
//- The number of elements that can be stored with reallocating
inline label capacity() const;
//- True if there are two or more entries and all entries have
//- identical values.
//- True if all entries have identical values, and list is non-empty
bool uniform() const;
......
......@@ -215,9 +215,9 @@ Foam::Ostream& Foam::PackedList<Width>::writeList
// Write list contents depending on data format
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;
}
else if (!shortLen || len <= shortLen)
......
......@@ -211,8 +211,7 @@ public:
//- True if no bits in this bitset are set.
inline bool none() const;
//- True if there are two or more entries and all entries have
//- identical values.
//- True if all entries have identical values, and the set is non-empty
inline bool uniform() const;
//- Count number of bits set.
......
......@@ -449,7 +449,7 @@ inline bool Foam::bitSet::none() const
inline bool Foam::bitSet::uniform() const
{
return (size() > 1 && (test(0) ? all() : none()));
return (size() == 1 || (size() > 1 && (test(0) ? all() : none())));
}
......
......@@ -48,7 +48,7 @@ Foam::Ostream& Foam::bitSet::writeList
// Write list contents depending on data format
if (os.format() == IOstream::ASCII)
{
if (list.uniform())
if (len > 1 && list.uniform())
{
// Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
......
......@@ -90,8 +90,7 @@ protected:
// Protected Member Functions
//- True if there are two or more entries and all entries have
//- identical values.
//- True if all entries have identical values, and list is non-empty
inline bool uniform() const;
//- Write the FixedList with its compound type
......
......@@ -42,22 +42,17 @@ inline const Foam::FixedList<T, N>& Foam::FixedList<T, N>::null()
template<class T, unsigned N>
inline bool Foam::FixedList<T, N>::uniform() const
{
if (N > 1)
{
const T& val = first();
if (empty()) return false; // <- Compile-time disabled anyhow
for (unsigned i=1; i<N; ++i)
for (unsigned i=1; i<N; ++i)
{
if (v_[0] != v_[i])
{
if (val != (*this)[i])
{
return false;
}
return false;
}
return true;
}
return false;
return true;
}
......
......@@ -71,8 +71,7 @@ protected:
// Protected Member Functions
//- True if there are two or more entries and all entries have
// identical values.
//- True if all entries have identical values, and list is non-empty
inline bool uniform() const;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -30,22 +30,22 @@ inline bool Foam::UIndirectList<T>::uniform() const
{
const label len = this->size();
if (len > 1)
if (!len)
{
const T& val = (*this)[0];
return false;
}
const T& val = (*this)[0];
for (label i=1; i<len; ++i)
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
if (val != (*this)[i])
{
return false;
}
return false;
}
return true;
}
return false;
return true;
}
......
......@@ -44,7 +44,7 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList
// Write list contents depending on data format
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.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
......
......@@ -112,8 +112,7 @@ protected:
// Use with care
inline void size(const label n);
//- True if there are two or more entries and all entries have
// identical values.
//- True if all entries have identical values, and list is non-empty
inline bool uniform() const;
//- Write the UList with its compound type
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / 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
This file is part of OpenFOAM.
......@@ -24,7 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "contiguous.H"
#include "pTraits.H"
#include "Swap.H"
......@@ -35,22 +34,22 @@ inline bool Foam::UList<T>::uniform() const
{
const label len = size();
if (len > 1)
if (len == 0)
{
const T& val = first();
return false;
}
const T& val = first();
for (label i=1; i<len; ++i)
for (label i=1; i<len; ++i)
{
if (val != (*this)[i])
{
if (val != (*this)[i])
{
return false;
}
return false;
}
return true;
}
return false;
return true;
}
......
......@@ -81,7 +81,7 @@ Foam::Ostream& Foam::UList<T>::writeList
// Write list contents depending on data format
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.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
......@@ -232,7 +232,7 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list)
}
else
{
// uniform content (delimiter == token::BEGIN_BLOCK)
// Uniform content (delimiter == token::BEGIN_BLOCK)
T element;
is >> element;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / 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
This file is part of OpenFOAM.
......@@ -629,27 +629,12 @@ void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const
{
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)?
bool uniform = (contiguous<Type>() && len);
if (uniform)
if (contiguous<Type>() && List<Type>::uniform())
{
const Type& val = this->operator[](0);
for (label i=1; i<len; ++i)
{
if (val != this->operator[](i))
{
uniform = false;
break;
}
}
}
if (uniform)
{
os << "uniform " << this->operator[](0);
os << "uniform " << this->first();
}
else
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment