ENH: align bitSet and FixedList methods names

- bitSet and PackedList fill() as per boost dynamic_bitset.
  Silently deprecate assign(val), which is potentially confusing
  with other forms of assign().

- FixedList fill() as per std::array.
  Add missing assignment from Foam::zero

- minor code simplication in bitSet and PackedList

STYLE: fix typo in comment, qualify Foam::zero
This commit is contained in:
Mark Olesen
2020-11-30 10:22:14 +01:00
parent 66c8607d5f
commit 986199f897
12 changed files with 156 additions and 123 deletions

View File

@ -123,10 +123,10 @@ bool Foam::PackedList<Width>::uniform() const
} }
else if (nblocks > 1) else if (nblocks > 1)
{ {
// Check all blocks that are completely occupied: (nblocks-1) // Fill value for complete blocks
const unsigned int blockval = const unsigned int blockval = repeated_value(val);
BitOps::repeat_value<block_type,Width>(val);
// Check each complete block (nblocks-1)
for (label blocki = 0; identical && blocki < (nblocks-1); ++blocki) for (label blocki = 0; identical && blocki < (nblocks-1); ++blocki)
{ {
identical = (blocks_[blocki] == blockval); identical = (blocks_[blocki] == blockval);

View File

@ -109,9 +109,6 @@ namespace Foam
template<unsigned Width> class PackedList; template<unsigned Width> class PackedList;
class labelRange; class labelRange;
class Istream;
class Ostream;
template<unsigned Width> template<unsigned Width>
Istream& operator>>(Istream& is, PackedList<Width>& list); Istream& operator>>(Istream& is, PackedList<Width>& list);
@ -136,9 +133,11 @@ public:
// Types and dimension information // Types and dimension information
//- The storage block type for bit elements //- The storage block type for bit elements
// \note Type name compatibility with boost::dynamic_bitset
typedef unsigned int block_type; typedef unsigned int block_type;
//- The number of bits in a single block //- The number of bits in a single block
// \note Type name compatibility with boost::dynamic_bitset
static constexpr unsigned bits_per_block static constexpr unsigned bits_per_block
= (std::numeric_limits<block_type>::digits); = (std::numeric_limits<block_type>::digits);
@ -219,6 +218,9 @@ protected:
// This \a rubbish may have arisen from block-wise operations etc. // This \a rubbish may have arisen from block-wise operations etc.
inline void clear_trailing_bits(); inline void clear_trailing_bits();
//- Copy assignment
inline void copyAssign(const PackedList<Width>& rhs);
public: public:
@ -335,11 +337,8 @@ public:
// Edit // Edit
//- Assign all entries to the given value. Takes linear time. //- Assign all entries to the given value.
inline void assign(const unsigned int val); inline void fill(const unsigned int val);
//- Copy assignment.
inline void assign(const PackedList<Width>& rhs);
//- Trim any trailing zero elements, optionally specifying a //- Trim any trailing zero elements, optionally specifying a
//- a minimum position, below which trimming will not occur. //- a minimum position, below which trimming will not occur.
@ -348,6 +347,7 @@ public:
inline bool trim(label minpos=-1); inline bool trim(label minpos=-1);
//- Clear all bits but do not adjust the addressable size. //- Clear all bits but do not adjust the addressable size.
// \note Method name compatibility with boost::dynamic_bitset
inline void reset(); inline void reset();
//- Alter the size of the underlying storage. //- Alter the size of the underlying storage.
@ -359,9 +359,6 @@ public:
// Optionally specify a value for new elements. // Optionally specify a value for new elements.
inline void resize(const label nElem, const unsigned int val = 0u); inline void resize(const label nElem, const unsigned int val = 0u);
//- Alias for resize()
inline void setSize(const label nElem, const unsigned int val = 0u);
//- Reserve allocation space for at least this size. //- Reserve allocation space for at least this size.
// Never shrinks the allocated size. // Never shrinks the allocated size.
// The list size is adjusted as per DynamicList with // The list size is adjusted as per DynamicList with
@ -382,7 +379,7 @@ public:
inline void swap(PackedList<Width>& rhs); inline void swap(PackedList<Width>& rhs);
//- 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.
inline void transfer(PackedList<Width>& rhs); inline void transfer(PackedList<Width>& rhs);
@ -435,15 +432,15 @@ public:
// Fatal for out-of-range indices // Fatal for out-of-range indices
inline reference operator[](const label i); inline reference operator[](const label i);
//- Assignment of all entries to the given value. Takes linear time.
inline void operator=(const unsigned int val);
//- Copy assignment. //- Copy assignment.
inline void operator=(const PackedList<Width>& lst); inline void operator=(const PackedList<Width>& lst);
//- Move assignment. //- Move assignment.
inline void operator=(PackedList<Width>&& lst); inline void operator=(PackedList<Width>&& lst);
//- Assign all entries to the given value. fill()
inline void operator=(const unsigned int val);
// Access helpers // Access helpers
@ -509,6 +506,23 @@ public:
Istream& is, Istream& is,
PackedList<Width>& list PackedList<Width>& list
); );
// Housekeeping
//- Deprecated(2020-11) use fill()
// \deprecated(2020-11) use fill()
void assign(const unsigned int val) { this->fill(val); }
//- Deprecated(2020-11) use operator=
// \deprecated(2020-11) use operator=
void assign(const PackedList<Width>& rhs) { (*this) = rhs; }
//- Alias for resize()
void setSize(const label len, unsigned int val = 0u)
{
resize(len, val);
}
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -138,24 +138,35 @@ inline bool Foam::PackedList<Width>::trim(label minpos)
} }
template<unsigned Width>
inline void Foam::PackedList<Width>::copyAssign(const PackedList<Width>& rhs)
{
// Self-assignment silently ignored
blocks_ = rhs.blocks_;
size_ = rhs.size_;
}
// * * * * * * * * * * * * * * * Specializations * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Specializations * * * * * * * * * * * * * * //
namespace Foam namespace Foam
{ {
template<> inline unsigned int PackedList<1>::repeated_value(unsigned val)
{
return (val ? ~0u : 0u);
}
template<> inline unsigned int PackedList<1>::readValue(Istream& is) // constexpr noexcept
{ template<> inline unsigned int PackedList<1>::repeated_value(unsigned val)
return readBool(is); {
} return (val ? ~0u : 0u);
}
template<> inline void PackedList<1>::setPair(Istream& is) template<> inline unsigned int PackedList<1>::readValue(Istream& is)
{ {
set(readLabel(is), true); return readBool(is);
} }
template<> inline void PackedList<1>::setPair(Istream& is)
{
set(readLabel(is), true);
}
} // End namespace Foam } // End namespace Foam
@ -446,17 +457,6 @@ inline void Foam::PackedList<Width>::resize
} }
template<unsigned Width>
inline void Foam::PackedList<Width>::setSize
(
const label newSize,
const unsigned int val
)
{
resize(newSize, val);
}
template<unsigned Width> template<unsigned Width>
inline void Foam::PackedList<Width>::setCapacity(const label numElem) inline void Foam::PackedList<Width>::setCapacity(const label numElem)
{ {
@ -680,41 +680,27 @@ inline unsigned int Foam::PackedList<Width>::remove()
template<unsigned Width> template<unsigned Width>
inline void Foam::PackedList<Width>::assign(const unsigned int val) inline void Foam::PackedList<Width>::fill(const unsigned int val)
{ {
if (empty())
{
return; // Trivial case
}
const label nblocks = num_blocks(size()); const label nblocks = num_blocks(size());
// Trivial cases first
if (!nblocks)
{
return;
}
else if (!val)
{
for (label blocki=0; blocki < nblocks; ++blocki)
{
blocks_[blocki] = 0u;
}
return;
}
// Fill value for complete blocks // Fill value for complete blocks
const unsigned int blockval = repeated_value(val); const unsigned int blockval = (val ? repeated_value(val) : 0u);
for (label blocki=0; blocki < nblocks; ++blocki) for (label blocki=0; blocki < nblocks; ++blocki)
{ {
blocks_[blocki] = blockval; blocks_[blocki] = blockval;
} }
clear_trailing_bits(); if (val)
} {
clear_trailing_bits();
}
template<unsigned Width>
inline void Foam::PackedList<Width>::assign(const PackedList<Width>& list)
{
blocks_ = list.blocks_;
size_ = list.size_;
} }
@ -738,17 +724,10 @@ Foam::PackedList<Width>::operator[](const label i)
} }
template<unsigned Width>
inline void Foam::PackedList<Width>::operator=(const unsigned int val)
{
assign(val);
}
template<unsigned Width> template<unsigned Width>
inline void Foam::PackedList<Width>::operator=(const PackedList<Width>& rhs) inline void Foam::PackedList<Width>::operator=(const PackedList<Width>& rhs)
{ {
assign(rhs); copyAssign(rhs);
} }
@ -759,6 +738,13 @@ inline void Foam::PackedList<Width>::operator=(PackedList<Width>&& rhs)
} }
template<unsigned Width>
inline void Foam::PackedList<Width>::operator=(const unsigned int val)
{
fill(val);
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
template<unsigned Width> template<unsigned Width>

View File

@ -329,8 +329,8 @@ void Foam::bitSet::assign(const UList<bool>& bools)
{ {
const label len = bools.size(); const label len = bools.size();
clear();
resize(len); resize(len);
assign(false);
// Could also handle block-wise (in the future?) // Could also handle block-wise (in the future?)

View File

@ -56,7 +56,6 @@ namespace Foam
// Forward Declarations // Forward Declarations
class bitSet; class bitSet;
class labelRange;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class bitSet Declaration Class bitSet Declaration
@ -66,8 +65,6 @@ class bitSet
: :
public PackedList<1> public PackedList<1>
{ {
private:
// Private Member Functions // Private Member Functions
//- Find the first block with a '0' bit //- Find the first block with a '0' bit
@ -218,12 +215,15 @@ public:
// Query // Query
//- True if all bits in this bitset are set or if the set is empty. //- True if all bits in this bitset are set or if the set is empty.
// \note Method name compatibility with boost::dynamic_bitset
inline bool all() const; inline bool all() const;
//- True if any bits in this bitset are set. //- True if any bits in this bitset are set.
// \note Method name compatibility with boost::dynamic_bitset
inline bool any() const; inline bool any() const;
//- True if no bits in this bitset are set. //- True if no bits in this bitset are set.
// \note Method name compatibility with boost::dynamic_bitset
inline bool none() const; inline bool none() const;
//- True if all entries have identical values, and the set is non-empty //- True if all entries have identical values, and the set is non-empty
@ -232,6 +232,7 @@ public:
//- Count number of bits set. //- Count number of bits set.
// \param on can be set to false to count the number of unset bits // \param on can be set to false to count the number of unset bits
// instead. // instead.
// \note Method name compatibility with boost::dynamic_bitset
inline unsigned int count(const bool on=true) const; inline unsigned int count(const bool on=true) const;
//- True if any bits in the other bitset intersect (are the same). //- True if any bits in the other bitset intersect (are the same).
@ -292,7 +293,7 @@ public:
// Assignment // Assignment
//- Assign all entries to the given value. //- Assign all entries to the given value.
inline void assign(const bool val); inline void fill(const bool val);
//- Copy assign all entries from a list of bools. //- Copy assign all entries from a list of bools.
void assign(const UList<bool>& bools); void assign(const UList<bool>& bools);
@ -544,15 +545,15 @@ public:
// Fatal for out-of-range indices // Fatal for out-of-range indices
inline reference operator[](const label i); inline reference operator[](const label i);
//- Assignment of all entries to the given value.
inline bitSet& operator=(const bool val);
//- Copy assignment //- Copy assignment
inline bitSet& operator=(const bitSet& bitset); inline bitSet& operator=(const bitSet& bitset);
//- Move assignment //- Move assignment
inline bitSet& operator=(bitSet&& bitset); inline bitSet& operator=(bitSet&& bitset);
//- Assign all entries to the given value. fill()
inline bitSet& operator=(const bool val);
//- Bitwise-AND all the bits in other with the bits in this bitset. //- Bitwise-AND all the bits in other with the bits in this bitset.
// The operands may have dissimilar sizes without affecting the size // The operands may have dissimilar sizes without affecting the size
// of the set. // of the set.
@ -591,6 +592,13 @@ public:
{ {
return *this; return *this;
} }
// Housekeeping
//- Deprecated(2020-11) use fill()
// \deprecated(2020-11) use fill()
void assign(const unsigned int val) { this->fill(val); }
}; };

View File

@ -90,7 +90,7 @@ inline Foam::bitSet::bitSet(const label n, const bool val)
: :
bitSet(n) bitSet(n)
{ {
if (val) assign(val); if (val) fill(val);
} }
@ -539,7 +539,7 @@ inline void Foam::bitSet::transfer(bitSet& bitset)
} }
inline void Foam::bitSet::assign(const bool val) inline void Foam::bitSet::fill(const bool val)
{ {
if (empty()) if (empty())
{ {
@ -548,21 +548,18 @@ inline void Foam::bitSet::assign(const bool val)
const label nblocks = num_blocks(size()); const label nblocks = num_blocks(size());
// Fill value for complete blocks
const unsigned int blockval = (val ? ~0u : 0u);
for (label blocki=0; blocki < nblocks; ++blocki)
{
blocks_[blocki] = blockval;
}
if (val) if (val)
{ {
for (label blocki=0; blocki < nblocks; ++blocki)
{
blocks_[blocki] = (~0u);
}
clear_trailing_bits(); clear_trailing_bits();
} }
else
{
for (label blocki=0; blocki < nblocks; ++blocki)
{
blocks_[blocki] = (0u);
}
}
} }
@ -691,13 +688,6 @@ inline Foam::bitSet::reference Foam::bitSet::operator[](const label i)
} }
inline Foam::bitSet& Foam::bitSet::operator=(const bool val)
{
PackedList<1>::operator=(val);
return *this;
}
inline Foam::bitSet& Foam::bitSet::operator=(const bitSet& bitset) inline Foam::bitSet& Foam::bitSet::operator=(const bitSet& bitset)
{ {
PackedList<1>::operator=(bitset); PackedList<1>::operator=(bitset);
@ -712,6 +702,13 @@ inline Foam::bitSet& Foam::bitSet::operator=(bitSet&& bitset)
} }
inline Foam::bitSet& Foam::bitSet::operator=(const bool val)
{
fill(val);
return *this;
}
inline Foam::bitSet& Foam::bitSet::operator&=(const bitSet& other) inline Foam::bitSet& Foam::bitSet::operator&=(const bitSet& other)
{ {
return andEq(other); return andEq(other);

View File

@ -90,7 +90,7 @@ template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList inline Foam::DynamicList<T, SizeMin>::DynamicList
( (
const label nElem, const label nElem,
const zero const Foam::zero
) )
: :
List<T>(nElem, Zero), List<T>(nElem, Zero),
@ -762,7 +762,7 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
template<class T, int SizeMin> template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::operator= inline void Foam::DynamicList<T, SizeMin>::operator=
( (
const zero const Foam::zero
) )
{ {
UList<T>::operator=(Zero); UList<T>::operator=(Zero);

View File

@ -275,12 +275,18 @@ public:
// Edit // Edit
//- Dummy resize function, to make FixedList consistent with List //- Dummy function, to make FixedList consistent with List
inline void resize(const label n); inline void resize(const label n);
//- Dummy setSize function, to make FixedList consistent with List //- Dummy function, to make FixedList consistent with List
inline void setSize(const label n); inline void setSize(const label n);
//- Assign all entries to the given value
inline void fill(const T& val);
//- Assign all entries to zero
inline void fill(const Foam::zero);
//- Move element to the first position. //- Move element to the first position.
void moveFirst(const label i); void moveFirst(const label i);
@ -318,9 +324,12 @@ public:
//- Assignment to an initializer list. Takes linear time //- Assignment to an initializer list. Takes linear time
inline void operator=(std::initializer_list<T> list); inline void operator=(std::initializer_list<T> list);
//- Assignment of all entries to the given value //- Assign all entries to the given value. fill()
inline void operator=(const T& val); inline void operator=(const T& val);
//- Assign all entries to zero. fill()
inline void operator=(const Foam::zero);
//- Copy assignment //- Copy assignment
inline void operator=(const FixedList<T, N>& list); inline void operator=(const FixedList<T, N>& list);

View File

@ -45,20 +45,14 @@ inline const Foam::FixedList<T, N>& Foam::FixedList<T, N>::null()
template<class T, unsigned N> template<class T, unsigned N>
inline Foam::FixedList<T, N>::FixedList(const T& val) inline Foam::FixedList<T, N>::FixedList(const T& val)
{ {
for (unsigned i=0; i<N; ++i) this->fill(val);
{
v_[i] = val;
}
} }
template<class T, unsigned N> template<class T, unsigned N>
inline Foam::FixedList<T, N>::FixedList(const Foam::zero) inline Foam::FixedList<T, N>::FixedList(const Foam::zero)
{ {
for (unsigned i=0; i<N; ++i) this->fill(Zero);
{
v_[i] = Zero;
}
} }
@ -342,6 +336,26 @@ inline void Foam::FixedList<T, N>::setSize(const label n)
} }
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::fill(const T& val)
{
for (unsigned i=0; i<N; ++i)
{
v_[i] = val;
}
}
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::fill(const Foam::zero)
{
for (unsigned i=0; i<N; ++i)
{
v_[i] = Zero;
}
}
template<class T, unsigned N> template<class T, unsigned N>
inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& list) inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& list)
{ {
@ -443,12 +457,17 @@ inline void Foam::FixedList<T, N>::operator=(std::initializer_list<T> list)
template<class T, unsigned N> template<class T, unsigned N>
inline void Foam::FixedList<T, N>::operator=(const T& val) inline void Foam::FixedList<T, N>::operator=(const T& val)
{ {
for (unsigned i=0; i<N; ++i) this->fill(val);
{
v_[i] = val;
}
} }
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::operator=(const Foam::zero)
{
this->fill(Zero);
}
template<class T, unsigned N> template<class T, unsigned N>
inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list) inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
{ {

View File

@ -93,7 +93,7 @@ public:
//- Move construct //- Move construct
inline PtrDynList(PtrDynList<T, SizeMin>&& list); inline PtrDynList(PtrDynList<T, SizeMin>&& list);
//- Take ownerskip of pointers in the list, set old pointers to null. //- Take ownership of pointers in the list, set old pointers to null.
inline explicit PtrDynList(UList<T*>& list); inline explicit PtrDynList(UList<T*>& list);

View File

@ -99,7 +99,7 @@ public:
//- Move construct //- Move construct
inline PtrList(PtrList<T>&& list); inline PtrList(PtrList<T>&& list);
//- Take ownerskip of pointers in the list, set old pointers to null. //- Take ownership of pointers in the list, set old pointers to null.
inline explicit PtrList(UList<T*>& list); inline explicit PtrList(UList<T*>& list);
//- Copy construct using 'clone()' method on each element //- Copy construct using 'clone()' method on each element

View File

@ -90,7 +90,7 @@ template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>::DynamicField inline Foam::DynamicField<T, SizeMin>::DynamicField
( (
const label len, const label len,
const zero const Foam::zero
) )
: :
Field<T>(len, Zero), Field<T>(len, Zero),