From 34386291cb0f7950b1525240992fbc56af23be3e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 11 Jan 2019 12:49:08 +0100 Subject: [PATCH] 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. --- .../containers/Bits/PackedList/PackedList.C | 22 +++++++++----- .../containers/Bits/PackedList/PackedList.H | 3 +- .../containers/Bits/PackedList/PackedListIO.C | 4 +-- src/OpenFOAM/containers/Bits/bitSet/bitSet.H | 3 +- src/OpenFOAM/containers/Bits/bitSet/bitSetI.H | 2 +- .../containers/Bits/bitSet/bitSetIO.C | 2 +- .../containers/Lists/FixedList/FixedList.H | 3 +- .../containers/Lists/FixedList/FixedListI.H | 17 ++++------- .../Lists/UIndirectList/UIndirectList.H | 3 +- .../Lists/UIndirectList/UIndirectListI.H | 28 +++++++++--------- .../Lists/UIndirectList/UIndirectListIO.C | 2 +- src/OpenFOAM/containers/Lists/UList/UList.H | 3 +- src/OpenFOAM/containers/Lists/UList/UListI.H | 29 +++++++++---------- src/OpenFOAM/containers/Lists/UList/UListIO.C | 4 +-- src/OpenFOAM/fields/Fields/Field/Field.C | 25 ++++------------ 15 files changed, 66 insertions(+), 84 deletions(-) diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.C b/src/OpenFOAM/containers/Bits/PackedList/PackedList.C index bb5a1da0fd..f1ecf21738 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.C @@ -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::PackedList template bool Foam::PackedList::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::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(size(), val); + return List(0); + } + else if (uniform()) + { + return List(size(), static_cast(get(0))); } List output(size()); diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index a77213b9e8..26c5cb3b81 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -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; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index a6196f334a..bd985d22a6 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -215,9 +215,9 @@ Foam::Ostream& Foam::PackedList::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) diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index 69d78444ee..2c5c283136 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -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. diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H index f2e648ca3f..18e99a2d83 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H @@ -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()))); } diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C index 472700dbf3..dd76d830f2 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C @@ -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; diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 497e03c13a..28fb68c07d 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -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 diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 1dee7c395d..f324e79c1d 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -42,22 +42,17 @@ inline const Foam::FixedList& Foam::FixedList::null() template inline bool Foam::FixedList::uniform() const { - if (N > 1) + if (empty()) return false; // <- Compile-time disabled anyhow + + for (unsigned i=1; i::uniform() const { const label len = this->size(); - if (len > 1) + if (!len) { - const T& val = (*this)[0]; - - for (label i=1; i::writeList // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { - if (contiguous() && list.uniform()) + if (len > 1 && contiguous() && list.uniform()) { // Two or more entries, and all entries have identical values. os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 40bf4be289..aff773ac23 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -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 diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index 0ee40aeee9..abe26ca776 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -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::uniform() const { const label len = size(); - if (len > 1) + if (len == 0) { - const T& val = first(); - - for (label i=1; i::writeList // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { - if (contiguous() && list.uniform()) + if (len > 1 && contiguous() && 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& list) } else { - // uniform content (delimiter == token::BEGIN_BLOCK) + // Uniform content (delimiter == token::BEGIN_BLOCK) T element; is >> element; diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index 61bc7f69be..59440bc5c6 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -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::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() && len); - if (uniform) + if (contiguous() && List::uniform()) { - const Type& val = this->operator[](0); - - for (label i=1; ioperator[](i)) - { - uniform = false; - break; - } - } - } - - if (uniform) - { - os << "uniform " << this->operator[](0); + os << "uniform " << this->first(); } else {