diff --git a/applications/test/PackedList/Test-PackedList.C b/applications/test/PackedList/Test-PackedList.C index 90ed8ac740..45d5ab00b6 100644 --- a/applications/test/PackedList/Test-PackedList.C +++ b/applications/test/PackedList/Test-PackedList.C @@ -41,7 +41,7 @@ using namespace Foam; template inline void reportInfo() { - unsigned offset = PackedList::packing(); + const unsigned offset = PackedList::packing(); unsigned useSHL = ((1u << (nBits * offset)) - 1); unsigned useSHR = (~0u >> (sizeof(unsigned)*CHAR_BIT - nBits * offset)); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C index 4b01a43dd6..543c5b8558 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C @@ -92,13 +92,15 @@ bool Foam::PackedBoolList::bitorPrepare template Foam::label Foam::PackedBoolList::setIndices(const LabelListType& indices) { - // no better information, just guess something about the size - reserve(indices.size()); + const label len = indices.size(); + + // No better information, just guess something from the size + reserve(len); label cnt = 0; - forAll(indices, elemI) + for (label i = 0; i < len; ++i) { - if (set(indices[elemI])) + if (set(indices[i])) { ++cnt; } @@ -112,9 +114,10 @@ template Foam::label Foam::PackedBoolList::unsetIndices(const LabelListType& indices) { label cnt = 0; - forAll(indices, elemI) + const label len = indices.size(); + for (label i = 0; i < len; ++i) { - if (unset(indices[elemI])) + if (unset(indices[i])) { ++cnt; } @@ -127,29 +130,30 @@ Foam::label Foam::PackedBoolList::unsetIndices(const LabelListType& indices) template Foam::label Foam::PackedBoolList::subsetIndices(const LabelListType& indices) { - // handle trivial case - if (empty() || indices.empty()) + const label len = indices.size(); + + // Handle trivial case + if (empty() || !len) { clear(); return 0; } - // normal case - PackedBoolList anded; - anded.reserve(size()); + PackedBoolList result; + result.reserve(size()); label cnt = 0; - forAll(indices, elemI) + for (label i = 0; i < len; ++i) { - const label& index = indices[elemI]; - if (operator[](index)) + const label index = indices[i]; + if (get(index)) { - anded.set(index); + result.set(index); ++cnt; } } - transfer(anded); + transfer(result); return cnt; } @@ -176,7 +180,7 @@ void Foam::PackedBoolList::set(const PackedList<1>& lst) StorageList& lhs = this->storage(); const StorageList& rhs = lst.storage(); - for (label i=0; i < len; ++i) + for (label i = 0; i < len; ++i) { lhs[i] |= rhs[i]; } @@ -209,7 +213,7 @@ void Foam::PackedBoolList::unset(const PackedList<1>& lst) // overlapping storage size const label len = min(this->packedLength(), lst.packedLength()); - for (label i=0; i < len; ++i) + for (label i = 0; i < len; ++i) { lhs[i] &= ~rhs[i]; } @@ -242,7 +246,7 @@ void Foam::PackedBoolList::subset(const PackedList<1>& lst) const label len = this->packedLength(); - for (label i=0; i < len; ++i) + for (label i = 0; i < len; ++i) { lhs[i] &= rhs[i]; } @@ -288,12 +292,13 @@ Foam::Xfer Foam::PackedBoolList::used() const void Foam::PackedBoolList::operator=(const UList& lst) { - this->setSize(lst.size()); + const label len = lst.size(); + this->setSize(len); - // overwrite with new true/false values - forAll(*this, elemI) + // Overwrite with new true/false values + for (label i = 0; i < len; ++i) { - set(elemI, lst[elemI]); + set(i, lst[i]); } } @@ -301,15 +306,15 @@ void Foam::PackedBoolList::operator=(const UList& lst) Foam::PackedBoolList& Foam::PackedBoolList::operator^=(const PackedList<1>& lst) { - // extend addressable area if needed, return maximum size possible + // Extend addressable area if needed, return maximum size possible label len = 0; const bool needTrim = bitorPrepare(lst, len); - // operate directly with the underlying storage + // Operate directly with the underlying storage StorageList& lhs = this->storage(); const StorageList& rhs = lst.storage(); - for (label i=0; i < len; ++i) + for (label i = 0; i < len; ++i) { lhs[i] ^= rhs[i]; } @@ -334,7 +339,7 @@ Foam::PackedBoolList Foam::operator& PackedBoolList result(lst1); result &= lst2; - // trim to bits actually used + // Trim to bits actually used result.trim(); return result; @@ -350,7 +355,7 @@ Foam::PackedBoolList Foam::operator^ PackedBoolList result(lst1); result ^= lst2; - // trim to bits actually used + // Trim to bits actually used result.trim(); return result; diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H index f98545a9bd..a667e8aecf 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.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 | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -96,7 +96,7 @@ public: //- Construct from Istream PackedBoolList(Istream& is); - //- Construct with given size, initializes list to 0 + //- Construct with given size, initializes list to 0 (false) explicit inline PackedBoolList(const label size); //- Construct with given size and value for all elements @@ -114,6 +114,20 @@ public: //- Construct by transferring the parameter contents inline PackedBoolList(const Xfer>& lst); + //- Construct with given size and list of labels to set as true. + inline PackedBoolList + ( + const label size, + const labelUList& indices + ); + + //- Construct with given size and list of labels to set as true. + inline PackedBoolList + ( + const label size, + const UIndirectList