mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add subset() method to PackedBoolList
- support &= for UList<label>, UIndirectList<label> - support set/unset for PackedList<1>
This commit is contained in:
@ -89,42 +89,6 @@ bool Foam::PackedBoolList::bitorPrepare
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
|
||||||
|
|
||||||
Foam::PackedBoolList::PackedBoolList(Istream& is)
|
|
||||||
:
|
|
||||||
PackedList<1>()
|
|
||||||
{
|
|
||||||
is >> *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
|
|
||||||
|
|
||||||
Foam::Xfer<Foam::labelList> Foam::PackedBoolList::used() const
|
|
||||||
{
|
|
||||||
labelList lst(this->count());
|
|
||||||
|
|
||||||
if (lst.size())
|
|
||||||
{
|
|
||||||
label nElem = 0;
|
|
||||||
|
|
||||||
forAll(*this, elemI)
|
|
||||||
{
|
|
||||||
if (get(elemI))
|
|
||||||
{
|
|
||||||
lst[nElem++] = elemI;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lst.setSize(nElem);
|
|
||||||
}
|
|
||||||
|
|
||||||
return lst.xfer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<class LabelListType>
|
template<class LabelListType>
|
||||||
Foam::label Foam::PackedBoolList::setIndices(const LabelListType& indices)
|
Foam::label Foam::PackedBoolList::setIndices(const LabelListType& indices)
|
||||||
{
|
{
|
||||||
@ -160,6 +124,70 @@ Foam::label Foam::PackedBoolList::unsetIndices(const LabelListType& indices)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class LabelListType>
|
||||||
|
Foam::label Foam::PackedBoolList::subsetIndices(const LabelListType& indices)
|
||||||
|
{
|
||||||
|
// handle trivial case
|
||||||
|
if (empty() || indices.empty())
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// normal case
|
||||||
|
PackedBoolList anded;
|
||||||
|
anded.reserve(size());
|
||||||
|
|
||||||
|
label cnt = 0;
|
||||||
|
forAll(indices, elemI)
|
||||||
|
{
|
||||||
|
const label& index = indices[elemI];
|
||||||
|
if (operator[](index))
|
||||||
|
{
|
||||||
|
anded.set(index);
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer(anded);
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::PackedBoolList::PackedBoolList(Istream& is)
|
||||||
|
:
|
||||||
|
PackedList<1>()
|
||||||
|
{
|
||||||
|
is >> *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::PackedBoolList::set(const PackedList<1>& lst)
|
||||||
|
{
|
||||||
|
// extend addressable area if needed, return maximum size possible
|
||||||
|
label len = 0;
|
||||||
|
const bool needTrim = bitorPrepare(lst, len);
|
||||||
|
|
||||||
|
// operate directly with the underlying storage
|
||||||
|
StorageList& lhs = this->storage();
|
||||||
|
const StorageList& rhs = lst.storage();
|
||||||
|
|
||||||
|
for (label i=0; i < len; ++i)
|
||||||
|
{
|
||||||
|
lhs[i] |= rhs[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needTrim)
|
||||||
|
{
|
||||||
|
trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::label Foam::PackedBoolList::set(const UList<label>& indices)
|
Foam::label Foam::PackedBoolList::set(const UList<label>& indices)
|
||||||
{
|
{
|
||||||
return setIndices(indices);
|
return setIndices(indices);
|
||||||
@ -172,6 +200,22 @@ Foam::label Foam::PackedBoolList::set(const UIndirectList<label>& indices)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Foam::PackedBoolList::unset(const PackedList<1>& lst)
|
||||||
|
{
|
||||||
|
// operate directly with the underlying storage
|
||||||
|
StorageList& lhs = this->storage();
|
||||||
|
const StorageList& rhs = lst.storage();
|
||||||
|
|
||||||
|
// overlapping storage size
|
||||||
|
const label len = min(this->packedLength(), lst.packedLength());
|
||||||
|
|
||||||
|
for (label i=0; i < len; ++i)
|
||||||
|
{
|
||||||
|
lhs[i] &= ~rhs[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::label Foam::PackedBoolList::unset(const UList<label>& indices)
|
Foam::label Foam::PackedBoolList::unset(const UList<label>& indices)
|
||||||
{
|
{
|
||||||
return unsetIndices(indices);
|
return unsetIndices(indices);
|
||||||
@ -184,44 +228,7 @@ Foam::label Foam::PackedBoolList::unset(const UIndirectList<label>& indices)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
|
void Foam::PackedBoolList::subset(const PackedList<1>& lst)
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator=(const UList<bool>& lst)
|
|
||||||
{
|
|
||||||
this->setSize(lst.size());
|
|
||||||
|
|
||||||
forAll(*this, elemI)
|
|
||||||
{
|
|
||||||
set(elemI, lst[elemI]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator=(const UList<label>& indices)
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
set(indices);
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator=(const UIndirectList<label>& indices)
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
set(indices);
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator&=(const PackedList<1>& lst)
|
|
||||||
{
|
{
|
||||||
// shrink addressable area if needed
|
// shrink addressable area if needed
|
||||||
if (this->size() > lst.size())
|
if (this->size() > lst.size())
|
||||||
@ -239,6 +246,56 @@ Foam::PackedBoolList::operator&=(const PackedList<1>& lst)
|
|||||||
{
|
{
|
||||||
lhs[i] &= rhs[i];
|
lhs[i] &= rhs[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::label Foam::PackedBoolList::subset(const UList<label>& indices)
|
||||||
|
{
|
||||||
|
return subsetIndices(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::label Foam::PackedBoolList::subset(const UIndirectList<label>& indices)
|
||||||
|
{
|
||||||
|
return subsetIndices(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::Xfer<Foam::labelList> Foam::PackedBoolList::used() const
|
||||||
|
{
|
||||||
|
labelList lst(this->count());
|
||||||
|
|
||||||
|
if (lst.size())
|
||||||
|
{
|
||||||
|
label nElem = 0;
|
||||||
|
|
||||||
|
forAll(*this, elemI)
|
||||||
|
{
|
||||||
|
if (get(elemI))
|
||||||
|
{
|
||||||
|
lst[nElem++] = elemI;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lst.setSize(nElem);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lst.xfer();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator=(const UList<bool>& lst)
|
||||||
|
{
|
||||||
|
this->setSize(lst.size());
|
||||||
|
|
||||||
|
// overwrite with new true/false values
|
||||||
|
forAll(*this, elemI)
|
||||||
|
{
|
||||||
|
set(elemI, lst[elemI]);
|
||||||
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -269,50 +326,6 @@ Foam::PackedBoolList::operator^=(const PackedList<1>& lst)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator|=(const PackedList<1>& lst)
|
|
||||||
{
|
|
||||||
// extend addressable area if needed, return maximum size possible
|
|
||||||
label len = 0;
|
|
||||||
const bool needTrim = bitorPrepare(lst, len);
|
|
||||||
|
|
||||||
// operate directly with the underlying storage
|
|
||||||
StorageList& lhs = this->storage();
|
|
||||||
const StorageList& rhs = lst.storage();
|
|
||||||
|
|
||||||
for (label i=0; i < len; ++i)
|
|
||||||
{
|
|
||||||
lhs[i] |= rhs[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (needTrim)
|
|
||||||
{
|
|
||||||
trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::PackedBoolList&
|
|
||||||
Foam::PackedBoolList::operator-=(const PackedList<1>& lst)
|
|
||||||
{
|
|
||||||
// operate directly with the underlying storage
|
|
||||||
StorageList& lhs = this->storage();
|
|
||||||
const StorageList& rhs = lst.storage();
|
|
||||||
|
|
||||||
// overlapping storage size
|
|
||||||
const label len = min(this->packedLength(), lst.packedLength());
|
|
||||||
|
|
||||||
for (label i=0; i < len; ++i)
|
|
||||||
{
|
|
||||||
lhs[i] &= ~rhs[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
Foam::PackedBoolList Foam::operator&
|
Foam::PackedBoolList Foam::operator&
|
||||||
|
|||||||
@ -75,6 +75,10 @@ class PackedBoolList
|
|||||||
template<class LabelListType>
|
template<class LabelListType>
|
||||||
label unsetIndices(const LabelListType& indices);
|
label unsetIndices(const LabelListType& indices);
|
||||||
|
|
||||||
|
//- Subset with the listed indices. Return number of elements subsetted.
|
||||||
|
template<class LabelListType>
|
||||||
|
label subsetIndices(const LabelListType& indices);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
@ -122,12 +126,12 @@ public:
|
|||||||
|
|
||||||
// Access
|
// Access
|
||||||
|
|
||||||
//- Return indices of the used (true) elements as a list of labels
|
|
||||||
Xfer<labelList> used() const;
|
|
||||||
|
|
||||||
using PackedList<1>::set;
|
using PackedList<1>::set;
|
||||||
using PackedList<1>::unset;
|
using PackedList<1>::unset;
|
||||||
|
|
||||||
|
//- Set specified bits.
|
||||||
|
void set(const PackedList<1>&);
|
||||||
|
|
||||||
//- Set the listed indices. Return number of elements changed.
|
//- Set the listed indices. Return number of elements changed.
|
||||||
// Does auto-vivify for non-existent entries.
|
// Does auto-vivify for non-existent entries.
|
||||||
label set(const UList<label>& indices);
|
label set(const UList<label>& indices);
|
||||||
@ -136,6 +140,9 @@ public:
|
|||||||
// Does auto-vivify for non-existent entries.
|
// Does auto-vivify for non-existent entries.
|
||||||
label set(const UIndirectList<label>& indices);
|
label set(const UIndirectList<label>& indices);
|
||||||
|
|
||||||
|
//- Unset specified bits.
|
||||||
|
void unset(const PackedList<1>&);
|
||||||
|
|
||||||
//- Unset the listed indices. Return number of elements changed.
|
//- Unset the listed indices. Return number of elements changed.
|
||||||
// Never auto-vivify entries.
|
// Never auto-vivify entries.
|
||||||
label unset(const UList<label>& indices);
|
label unset(const UList<label>& indices);
|
||||||
@ -144,6 +151,21 @@ public:
|
|||||||
// Never auto-vivify entries.
|
// Never auto-vivify entries.
|
||||||
label unset(const UIndirectList<label>& indices);
|
label unset(const UIndirectList<label>& indices);
|
||||||
|
|
||||||
|
//- Subset with the specified list.
|
||||||
|
void subset(const PackedList<1>&);
|
||||||
|
|
||||||
|
//- Subset with the listed indices.
|
||||||
|
// Return number of elements subsetted.
|
||||||
|
label subset(const UList<label>& indices);
|
||||||
|
|
||||||
|
//- Subset with the listed indices.
|
||||||
|
// Return number of elements subsetted.
|
||||||
|
label subset(const UIndirectList<label>& indices);
|
||||||
|
|
||||||
|
|
||||||
|
//- Return indices of the used (true) elements as a list of labels
|
||||||
|
Xfer<labelList> used() const;
|
||||||
|
|
||||||
|
|
||||||
// Edit
|
// Edit
|
||||||
|
|
||||||
@ -175,24 +197,32 @@ public:
|
|||||||
|
|
||||||
//- Assignment operator,
|
//- Assignment operator,
|
||||||
// using the labels as indices to indicate which bits are set
|
// using the labels as indices to indicate which bits are set
|
||||||
PackedBoolList& operator=(const UList<label>& indices);
|
inline PackedBoolList& operator=(const UList<label>& indices);
|
||||||
|
|
||||||
//- Assignment operator,
|
//- Assignment operator,
|
||||||
// using the labels as indices to indicate which bits are set
|
// using the labels as indices to indicate which bits are set
|
||||||
PackedBoolList& operator=(const UIndirectList<label>& indices);
|
inline PackedBoolList& operator=(const UIndirectList<label>&);
|
||||||
|
|
||||||
//- Complement operator
|
//- Complement operator
|
||||||
inline PackedBoolList operator~() const;
|
inline PackedBoolList operator~() const;
|
||||||
|
|
||||||
//- And operator (lists may be dissimilar sizes)
|
//- And operator (lists may be dissimilar sizes)
|
||||||
PackedBoolList& operator&=(const PackedList<1>&);
|
inline PackedBoolList& operator&=(const PackedList<1>&);
|
||||||
|
|
||||||
|
//- And operator (lists may be dissimilar sizes)
|
||||||
|
// using the labels as indices to indicate which bits are set
|
||||||
|
inline PackedBoolList& operator&=(const UList<label>& indices);
|
||||||
|
|
||||||
|
//- And operator (lists may be dissimilar sizes)
|
||||||
|
// using the labels as indices to indicate which bits are set
|
||||||
|
inline PackedBoolList& operator&=(const UIndirectList<label>&);
|
||||||
|
|
||||||
//- Xor operator (lists may be dissimilar sizes)
|
//- Xor operator (lists may be dissimilar sizes)
|
||||||
// Retains unique entries
|
// Retains unique entries
|
||||||
PackedBoolList& operator^=(const PackedList<1>&);
|
PackedBoolList& operator^=(const PackedList<1>&);
|
||||||
|
|
||||||
//- Or operator (lists may be dissimilar sizes)
|
//- Or operator (lists may be dissimilar sizes)
|
||||||
PackedBoolList& operator|=(const PackedList<1>&);
|
inline PackedBoolList& operator|=(const PackedList<1>&);
|
||||||
|
|
||||||
//- Or operator (lists may be dissimilar sizes),
|
//- Or operator (lists may be dissimilar sizes),
|
||||||
// using the labels as indices to indicate which bits are set
|
// using the labels as indices to indicate which bits are set
|
||||||
@ -213,7 +243,7 @@ public:
|
|||||||
inline PackedBoolList& operator+=(const UIndirectList<label>&);
|
inline PackedBoolList& operator+=(const UIndirectList<label>&);
|
||||||
|
|
||||||
//- Remove entries from this list - unset the specified bits
|
//- Remove entries from this list - unset the specified bits
|
||||||
PackedBoolList& operator-=(const PackedList<1>&);
|
inline PackedBoolList& operator-=(const PackedList<1>&);
|
||||||
|
|
||||||
//- Remove entries from this list - unset the specified bits
|
//- Remove entries from this list - unset the specified bits
|
||||||
inline PackedBoolList& operator-=(const UList<label>& indices);
|
inline PackedBoolList& operator-=(const UList<label>& indices);
|
||||||
|
|||||||
@ -151,6 +151,26 @@ Foam::PackedBoolList::operator=(const PackedList<1>& lst)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator=(const UList<label>& indices)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
set(indices);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator=(const UIndirectList<label>& indices)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
set(indices);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline Foam::PackedBoolList
|
inline Foam::PackedBoolList
|
||||||
Foam::PackedBoolList::operator~() const
|
Foam::PackedBoolList::operator~() const
|
||||||
{
|
{
|
||||||
@ -161,6 +181,38 @@ Foam::PackedBoolList::operator~() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator&=(const PackedList<1>& lst)
|
||||||
|
{
|
||||||
|
subset(lst);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator&=(const UList<label>& indices)
|
||||||
|
{
|
||||||
|
subset(indices);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator&=(const UIndirectList<label>& indices)
|
||||||
|
{
|
||||||
|
subset(indices);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator|=(const PackedList<1>& lst)
|
||||||
|
{
|
||||||
|
set(lst);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline Foam::PackedBoolList&
|
inline Foam::PackedBoolList&
|
||||||
Foam::PackedBoolList::operator|=(const UList<label>& indices)
|
Foam::PackedBoolList::operator|=(const UList<label>& indices)
|
||||||
{
|
{
|
||||||
@ -180,21 +232,29 @@ Foam::PackedBoolList::operator|=(const UIndirectList<label>& indices)
|
|||||||
inline Foam::PackedBoolList&
|
inline Foam::PackedBoolList&
|
||||||
Foam::PackedBoolList::operator+=(const PackedList<1>& lst)
|
Foam::PackedBoolList::operator+=(const PackedList<1>& lst)
|
||||||
{
|
{
|
||||||
return this->operator|=(lst);
|
return operator|=(lst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline Foam::PackedBoolList&
|
inline Foam::PackedBoolList&
|
||||||
Foam::PackedBoolList::operator+=(const UList<label>& indices)
|
Foam::PackedBoolList::operator+=(const UList<label>& indices)
|
||||||
{
|
{
|
||||||
return this->operator|=(indices);
|
return operator|=(indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline Foam::PackedBoolList&
|
inline Foam::PackedBoolList&
|
||||||
Foam::PackedBoolList::operator+=(const UIndirectList<label>& indices)
|
Foam::PackedBoolList::operator+=(const UIndirectList<label>& indices)
|
||||||
{
|
{
|
||||||
return this->operator|=(indices);
|
return operator|=(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Foam::PackedBoolList&
|
||||||
|
Foam::PackedBoolList::operator-=(const PackedList<1>& lst)
|
||||||
|
{
|
||||||
|
unset(lst);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user