Commit 7ee93afe authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent cf728cef
...@@ -144,18 +144,18 @@ int main(int argc, char *argv[]) ...@@ -144,18 +144,18 @@ int main(int argc, char *argv[])
Info<< "\ntest Istream constructor\n"; Info<< "\ntest Istream constructor\n";
list4.printInfo(Info, true); list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() << nl; Info<< list4 << " indices: " << list4.used() << nl;
Info<< "\nassign from labelList\n"; Info<< "\nassign from labelList\n";
list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
list4.printInfo(Info, true); list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() << nl; Info<< list4 << " indices: " << list4.used() << nl;
// Not yet: // Not yet:
// PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21}; // PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
// list5.printInfo(Info, true); // list5.printInfo(Info, true);
// Info<< list5 << " indices: " << list5.used()() << nl; // Info<< list5 << " indices: " << list5.used() << nl;
Info<< "\nassign from indices\n"; Info<< "\nassign from indices\n";
list4.read list4.read
...@@ -168,7 +168,7 @@ int main(int argc, char *argv[]) ...@@ -168,7 +168,7 @@ int main(int argc, char *argv[])
list4.printInfo(Info, true); list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() << nl; Info<< list4 << " indices: " << list4.used() << nl;
boolList bools(list4.size()); boolList bools(list4.size());
forAll(list4, i) forAll(list4, i)
......
...@@ -265,26 +265,28 @@ Foam::label Foam::PackedBoolList::subset(const labelUIndList& indices) ...@@ -265,26 +265,28 @@ Foam::label Foam::PackedBoolList::subset(const labelUIndList& indices)
} }
Foam::Xfer<Foam::labelList> 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;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -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) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -91,7 +91,7 @@ public: ...@@ -91,7 +91,7 @@ public:
// Constructors // Constructors
//- Construct null //- Construct null
inline PackedBoolList(); PackedBoolList() = default;
//- Construct from Istream //- Construct from Istream
PackedBoolList(Istream& is); PackedBoolList(Istream& is);
...@@ -102,17 +102,17 @@ public: ...@@ -102,17 +102,17 @@ public:
//- Construct with given size and value for all elements //- Construct with given size and value for all elements
inline PackedBoolList(const label size, const bool val); inline PackedBoolList(const label size, const bool val);
//- Copy constructor //- Copy construct
inline PackedBoolList(const PackedBoolList& lst); inline PackedBoolList(const PackedBoolList& lst);
//- Copy constructor //- Copy construct
explicit inline PackedBoolList(const PackedList<1>& lst); explicit inline PackedBoolList(const PackedList<1>& lst);
//- Construct by transferring the parameter contents //- Move construct
inline PackedBoolList(const Xfer<PackedBoolList>& lst); inline PackedBoolList(PackedBoolList&& lst);
//- Construct by transferring the parameter contents //- Move construct
inline PackedBoolList(const Xfer<PackedList<1>>& lst); inline PackedBoolList(PackedList<1>&& lst);
//- Construct with given size and list of labels to set as true. //- Construct with given size and list of labels to set as true.
inline PackedBoolList(const label size, const labelUList& indices); inline PackedBoolList(const label size, const labelUList& indices);
...@@ -137,7 +137,7 @@ public: ...@@ -137,7 +137,7 @@ public:
// Member Functions // Member Functions
// Access // Access
using PackedList<1>::set; using PackedList<1>::set;
using PackedList<1>::unset; using PackedList<1>::unset;
...@@ -175,12 +175,11 @@ public: ...@@ -175,12 +175,11 @@ public:
// Return number of elements subsetted. // Return number of elements subsetted.
label subset(const labelUIndList& indices); label subset(const labelUIndList& indices);
//- Return indices of the used (true) elements as a list of labels //- Return indices of the used (true) elements as a list of labels
Xfer<labelList> used() const; labelList used() const;
// Edit // Edit
//- Transfer the contents of the argument list into this list //- Transfer the contents of the argument list into this list
//- and annul the argument list. //- and annul the argument list.
...@@ -190,21 +189,24 @@ public: ...@@ -190,21 +189,24 @@ public:
//- and annul the argument list. //- and annul the argument list.
inline void transfer(PackedList<1>& lst); inline void transfer(PackedList<1>& lst);
//- Transfer contents to the Xfer container
inline Xfer<PackedBoolList> xfer();
// Member Operators // Member Operators
//- Assignment of all entries to the given value. //- Assignment of all entries to the given value.
inline void operator=(const bool val); inline void operator=(const bool val);
//- Assignment operator. //- Copy assignment
inline void operator=(const PackedBoolList& lst); inline void operator=(const PackedBoolList& lst);
//- Assignment operator. //- Copy assignment
inline void operator=(const PackedList<1>& lst); inline void operator=(const PackedList<1>& lst);
//- Move assignment
inline void operator=(PackedBoolList&& lst);
//- Move assignment
inline void operator=(PackedList<1>&& lst);
//- Assignment operator. //- Assignment operator.
void operator=(const UList<bool>& lst); void operator=(const UList<bool>& lst);
......
...@@ -25,12 +25,6 @@ License ...@@ -25,12 +25,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::PackedBoolList::PackedBoolList()
:
PackedList<1>()
{}
inline Foam::PackedBoolList::PackedBoolList(const label size) inline Foam::PackedBoolList::PackedBoolList(const label size)
: :
PackedList<1>(size) PackedList<1>(size)
...@@ -59,18 +53,20 @@ inline Foam::PackedBoolList::PackedBoolList(const PackedList<1>& lst) ...@@ -59,18 +53,20 @@ inline Foam::PackedBoolList::PackedBoolList(const PackedList<1>& lst)
{} {}
inline Foam::PackedBoolList::PackedBoolList(const Xfer<PackedBoolList>& lst) inline Foam::PackedBoolList::PackedBoolList(PackedBoolList&& lst)
: :
PackedList<1>() PackedList<1>()
{ {
transfer(lst()); transfer(lst);
} }
inline Foam::PackedBoolList::PackedBoolList(const Xfer<PackedList<1>>& lst) inline Foam::PackedBoolList::PackedBoolList(PackedList<1>&& lst)
: :
PackedList<1>(lst) PackedList<1>()
{} {
transfer(lst);
}
inline Foam::PackedBoolList::PackedBoolList(const UList<bool>& lst) inline Foam::PackedBoolList::PackedBoolList(const UList<bool>& lst)
...@@ -147,12 +143,6 @@ inline void Foam::PackedBoolList::transfer(PackedList<1>& lst) ...@@ -147,12 +143,6 @@ inline void Foam::PackedBoolList::transfer(PackedList<1>& lst)
} }
inline Foam::Xfer<Foam::PackedBoolList> Foam::PackedBoolList::xfer()
{
return xferMove(*this);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline void Foam::PackedBoolList::operator=(const bool val) inline void Foam::PackedBoolList::operator=(const bool val)
...@@ -173,6 +163,18 @@ inline void Foam::PackedBoolList::operator=(const PackedList<1>& lst) ...@@ -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) inline void Foam::PackedBoolList::operator=(const labelUList& indices)
{ {
clear(); clear();
......
...@@ -141,16 +141,16 @@ void Foam::PackedList<nBits>::flip() ...@@ -141,16 +141,16 @@ void Foam::PackedList<nBits>::flip()
template<unsigned nBits> template<unsigned nBits>
Foam::Xfer<Foam::labelList> Foam::PackedList<nBits>::values() const Foam::labelList Foam::PackedList<nBits>::values() const
{ {
labelList elems(size_); labelList elems(size_);
forAll(*this, i) for (label i=0; i < size_; ++i)
{ {
elems[i] = get(i); elems[i] = get(i);
} }
return elems.xfer(); return elems;
} }
...@@ -522,6 +522,13 @@ void Foam::PackedList<nBits>::operator=(const PackedList<nBits>& lst) ...@@ -522,6 +522,13 @@ void Foam::PackedList<nBits>::operator=(const PackedList<nBits>& lst)
} }
template<unsigned nBits>
void Foam::PackedList<nBits>::operator=(PackedList<nBits>&& lst)
{
transfer(lst);
}
template<unsigned nBits> template<unsigned nBits>
void Foam::PackedList<nBits>::operator=(const labelUList& lst) void Foam::PackedList<nBits>::operator=(const labelUList& lst)
{ {
......
...@@ -235,11 +235,11 @@ public: ...@@ -235,11 +235,11 @@ public:
//- Construct from Istream //- Construct from Istream
inline PackedList(Istream& is); inline PackedList(Istream& is);
//- Copy constructor //- Copy construct
inline PackedList(const PackedList<nBits>& lst); inline PackedList(const PackedList<nBits>& lst);
//- Construct by transferring the parameter contents //- Move construct
inline PackedList(const Xfer<PackedList<nBits>>& lst); inline PackedList(PackedList<nBits>&& lst);
//- Construct from a list of labels //- Construct from a list of labels
explicit inline PackedList(const labelUList& lst); explicit inline PackedList(const labelUList& lst);
...@@ -297,7 +297,7 @@ public: ...@@ -297,7 +297,7 @@ public:
unsigned int count() const; unsigned int count() const;
//- Return the values as a list of labels //- Return the values as a list of labels
Xfer<labelList> values() const; labelList values() const;
//- Print bit patterns, optionally output unused elements //- Print bit patterns, optionally output unused elements
// //
...@@ -356,9 +356,6 @@ public: ...@@ -356,9 +356,6 @@ public:
// and annul the argument list. // and annul the argument list.
inline void transfer(PackedList<nBits>& lst); inline void transfer(PackedList<nBits>& lst);
//- Transfer contents to the Xfer container
inline Xfer<PackedList<nBits>> xfer();
// IO // IO
...@@ -407,9 +404,12 @@ public: ...@@ -407,9 +404,12 @@ public:
//- Assignment of all entries to the given value. Takes linear time. //- Assignment of all entries to the given value. Takes linear time.
inline void operator=(const unsigned int val); inline void operator=(const unsigned int val);
//- Assignment operator. //- Copy assignment.
void operator=(const PackedList<nBits>& lst); void operator=(const PackedList<nBits>& lst);
//- Move assignment.
void operator=(PackedList<nBits>&& lst);
//- Assignment operator. //- Assignment operator.
void operator=(const labelUList& lst); void operator=(const labelUList& lst);
......
...@@ -223,9 +223,13 @@ inline Foam::PackedList<nBits>::PackedList(const PackedList<nBits>& lst) ...@@ -223,9 +223,13 @@ inline Foam::PackedList<nBits>::PackedList(const PackedList<nBits>& lst)
template<unsigned nBits> template<unsigned nBits>
inline Foam::PackedList<nBits>::PackedList(const Xfer<PackedList<nBits>>& lst) inline Foam::PackedList<nBits>::PackedList(PackedList<nBits>&& lst)
:
PackedListCore(),
StorageList(),
size_(0)
{ {
transfer(lst()); transfer(lst);
} }
...@@ -956,13 +960,6 @@ inline void Foam::PackedList<nBits>::transfer(PackedList<nBits>& lst) ...@@ -956,13 +960,6 @@ inline void Foam::PackedList<nBits>::transfer(PackedList<nBits>& lst)
} }
template<unsigned nBits>
inline Foam::Xfer<Foam::PackedList<nBits>> Foam::PackedList<nBits>::xfer()
{
return xferMove(*this);
}
template<unsigned nBits> template<unsigned nBits>
inline unsigned int Foam::PackedList<nBits>::get(const label i) const inline unsigned int Foam::PackedList<nBits>::get(const label i) const
{ {
......
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