From 7ee93afe9e9125e7d3b2d4da934c6e1ec5a479e4 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 25 Jan 2018 10:16:23 +0100 Subject: [PATCH] ENH: allow early completion in PackedBoolList::used() - can stop producing content when the target number of entries has been reached. - change return type to labelList instead an Xfer container. This allows return-value-optimization and avoids a surrounding allocation. This potentially breaks existing code. - make PackedList and PackedBoolList moveable. Drop xfer wrappers. --- .../test/PackedList4/Test-PackedList4.C | 8 ++--- .../Lists/PackedList/PackedBoolList.C | 22 ++++++------ .../Lists/PackedList/PackedBoolList.H | 36 ++++++++++--------- .../Lists/PackedList/PackedBoolListI.H | 36 ++++++++++--------- .../containers/Lists/PackedList/PackedList.C | 13 +++++-- .../containers/Lists/PackedList/PackedList.H | 16 ++++----- .../containers/Lists/PackedList/PackedListI.H | 15 ++++---- 7 files changed, 78 insertions(+), 68 deletions(-) diff --git a/applications/test/PackedList4/Test-PackedList4.C b/applications/test/PackedList4/Test-PackedList4.C index e50eee04f4..cb94feb1b9 100644 --- a/applications/test/PackedList4/Test-PackedList4.C +++ b/applications/test/PackedList4/Test-PackedList4.C @@ -144,18 +144,18 @@ int main(int argc, char *argv[]) Info<< "\ntest Istream constructor\n"; list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; Info<< "\nassign from labelList\n"; list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; // Not yet: // PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; // list5.printInfo(Info, true); - // Info<< list5 << " indices: " << list5.used()() << nl; + // Info<< list5 << " indices: " << list5.used() << nl; Info<< "\nassign from indices\n"; list4.read @@ -168,7 +168,7 @@ int main(int argc, char *argv[]) list4.printInfo(Info, true); - Info<< list4 << " indices: " << list4.used()() << nl; + Info<< list4 << " indices: " << list4.used() << nl; boolList bools(list4.size()); forAll(list4, i) diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C index db6036d715..7179796b7e 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.C @@ -265,26 +265,28 @@ Foam::label Foam::PackedBoolList::subset(const labelUIndList& indices) } -Foam::Xfer Foam::PackedBoolList::used() const +Foam::labelList Foam::PackedBoolList::used() const { - labelList lst(this->count()); + // Number of used (set) entries + const label cnt = this->count(); - if (lst.size()) + labelList lst(cnt); + + if (cnt) { - label nElem = 0; + // The length of the input list + const label len = this->size(); - forAll(*this, elemI) + for (label i=0, usedi=0; (i < len && usedi < cnt); ++i) { - if (get(elemI)) + if (get(i)) { - lst[nElem++] = elemI; + lst[usedi++] = i; } } - - lst.setSize(nElem); } - return lst.xfer(); + return lst; } diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolList.H index 28209b6ea5..717be74288 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 | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,7 +91,7 @@ public: // Constructors //- Construct null - inline PackedBoolList(); + PackedBoolList() = default; //- Construct from Istream PackedBoolList(Istream& is); @@ -102,17 +102,17 @@ public: //- Construct with given size and value for all elements inline PackedBoolList(const label size, const bool val); - //- Copy constructor + //- Copy construct inline PackedBoolList(const PackedBoolList& lst); - //- Copy constructor + //- Copy construct explicit inline PackedBoolList(const PackedList<1>& lst); - //- Construct by transferring the parameter contents - inline PackedBoolList(const Xfer& lst); + //- Move construct + inline PackedBoolList(PackedBoolList&& lst); - //- Construct by transferring the parameter contents - inline PackedBoolList(const Xfer>& lst); + //- Move construct + inline PackedBoolList(PackedList<1>&& lst); //- Construct with given size and list of labels to set as true. inline PackedBoolList(const label size, const labelUList& indices); @@ -137,7 +137,7 @@ public: // Member Functions - // Access + // Access using PackedList<1>::set; using PackedList<1>::unset; @@ -175,12 +175,11 @@ public: // Return number of elements subsetted. label subset(const labelUIndList& indices); - //- Return indices of the used (true) elements as a list of labels - Xfer used() const; + labelList used() const; - // Edit + // Edit //- Transfer the contents of the argument list into this list //- and annul the argument list. @@ -190,21 +189,24 @@ public: //- and annul the argument list. inline void transfer(PackedList<1>& lst); - //- Transfer contents to the Xfer container - inline Xfer xfer(); - // Member Operators //- Assignment of all entries to the given value. inline void operator=(const bool val); - //- Assignment operator. + //- Copy assignment inline void operator=(const PackedBoolList& lst); - //- Assignment operator. + //- Copy assignment inline void operator=(const PackedList<1>& lst); + //- Move assignment + inline void operator=(PackedBoolList&& lst); + + //- Move assignment + inline void operator=(PackedList<1>&& lst); + //- Assignment operator. void operator=(const UList& lst); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H index db40fab00b..c89acf0bc3 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedBoolListI.H @@ -25,12 +25,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline Foam::PackedBoolList::PackedBoolList() -: - PackedList<1>() -{} - - inline Foam::PackedBoolList::PackedBoolList(const label size) : PackedList<1>(size) @@ -59,18 +53,20 @@ inline Foam::PackedBoolList::PackedBoolList(const PackedList<1>& lst) {} -inline Foam::PackedBoolList::PackedBoolList(const Xfer& lst) +inline Foam::PackedBoolList::PackedBoolList(PackedBoolList&& lst) : PackedList<1>() { - transfer(lst()); + transfer(lst); } -inline Foam::PackedBoolList::PackedBoolList(const Xfer>& lst) +inline Foam::PackedBoolList::PackedBoolList(PackedList<1>&& lst) : - PackedList<1>(lst) -{} + PackedList<1>() +{ + transfer(lst); +} inline Foam::PackedBoolList::PackedBoolList(const UList& lst) @@ -147,12 +143,6 @@ inline void Foam::PackedBoolList::transfer(PackedList<1>& lst) } -inline Foam::Xfer Foam::PackedBoolList::xfer() -{ - return xferMove(*this); -} - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // inline void Foam::PackedBoolList::operator=(const bool val) @@ -173,6 +163,18 @@ inline void Foam::PackedBoolList::operator=(const PackedList<1>& lst) } +inline void Foam::PackedBoolList::operator=(PackedBoolList&& lst) +{ + transfer(lst); +} + + +inline void Foam::PackedBoolList::operator=(PackedList<1>&& lst) +{ + transfer(lst); +} + + inline void Foam::PackedBoolList::operator=(const labelUList& indices) { clear(); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C index f9f87b8c00..63c0908961 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C @@ -141,16 +141,16 @@ void Foam::PackedList::flip() template -Foam::Xfer Foam::PackedList::values() const +Foam::labelList Foam::PackedList::values() const { labelList elems(size_); - forAll(*this, i) + for (label i=0; i < size_; ++i) { elems[i] = get(i); } - return elems.xfer(); + return elems; } @@ -522,6 +522,13 @@ void Foam::PackedList::operator=(const PackedList& lst) } +template +void Foam::PackedList::operator=(PackedList&& lst) +{ + transfer(lst); +} + + template void Foam::PackedList::operator=(const labelUList& lst) { diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H index 2ae542aeaa..8ffd02e704 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.H @@ -235,11 +235,11 @@ public: //- Construct from Istream inline PackedList(Istream& is); - //- Copy constructor + //- Copy construct inline PackedList(const PackedList& lst); - //- Construct by transferring the parameter contents - inline PackedList(const Xfer>& lst); + //- Move construct + inline PackedList(PackedList&& lst); //- Construct from a list of labels explicit inline PackedList(const labelUList& lst); @@ -297,7 +297,7 @@ public: unsigned int count() const; //- Return the values as a list of labels - Xfer values() const; + labelList values() const; //- Print bit patterns, optionally output unused elements // @@ -356,9 +356,6 @@ public: // and annul the argument list. inline void transfer(PackedList& lst); - //- Transfer contents to the Xfer container - inline Xfer> xfer(); - // IO @@ -407,9 +404,12 @@ public: //- Assignment of all entries to the given value. Takes linear time. inline void operator=(const unsigned int val); - //- Assignment operator. + //- Copy assignment. void operator=(const PackedList& lst); + //- Move assignment. + void operator=(PackedList&& lst); + //- Assignment operator. void operator=(const labelUList& lst); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H index 94f91cee4b..b4efd00711 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedListI.H @@ -223,9 +223,13 @@ inline Foam::PackedList::PackedList(const PackedList& lst) template -inline Foam::PackedList::PackedList(const Xfer>& lst) +inline Foam::PackedList::PackedList(PackedList&& lst) +: + PackedListCore(), + StorageList(), + size_(0) { - transfer(lst()); + transfer(lst); } @@ -956,13 +960,6 @@ inline void Foam::PackedList::transfer(PackedList& lst) } -template -inline Foam::Xfer> Foam::PackedList::xfer() -{ - return xferMove(*this); -} - - template inline unsigned int Foam::PackedList::get(const label i) const {