ENH: add BitOps::set(), unset() functions for boolList, labelHashSet

This commit is contained in:
Mark Olesen
2020-11-24 11:22:28 +01:00
parent 0de32a6e6f
commit be7a3f21be
6 changed files with 286 additions and 5 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,8 +28,96 @@ License
#include "BitOps.H"
#include "bitSet.H"
#include "HashSet.H"
#include "List.H"
#include "labelRange.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * BitOps * * * * * * * * * * * * * * * * //
// See bitSet::set(labelRange) for original implementation
void Foam::BitOps::set(List<bool>& bools, const labelRange& range)
{
labelRange slice(range);
slice.adjust(); // No negative start, size adjusted accordingly
// Range is invalid (zero-sized or entirely negative) - noop
if (slice.empty())
{
return;
}
// Range finishes at or beyond the right side.
// - zero fill any gaps that we might create.
// - flood-fill the rest, which now corresponds to the full range.
//
// NB: use labelRange after() for the exclusive end-value, which
// corresponds to our new set size.
if (slice.after() >= bools.size())
{
label i = bools.size();
bools.resize(slice.after(), true);
// Backfill with false
while (i < slice.start())
{
bools.unset(i);
++i;
}
return;
}
for (label i = slice.first(); i <= slice.last(); ++i)
{
bools.set(i);
}
}
// See bitSet::set(labelRange) for original implementation
void Foam::BitOps::set(labelHashSet& hashset, const labelRange& range)
{
labelRange slice(range);
slice.adjust(); // No negative start, size adjusted accordingly
for (label i = slice.first(); i <= slice.last(); ++i)
{
hashset.set(i);
}
}
void Foam::BitOps::set(bitSet& bitset, const labelRange& range)
{
bitset.set(range);
}
// See bitSet::unset(labelRange) for original implementation
void Foam::BitOps::unset(List<bool>& bools, const labelRange& range)
{
for (label i = range.first(); i <= range.last(); ++i)
{
bools.unset(i);
}
}
void Foam::BitOps::unset(labelHashSet& hashset, const labelRange& range)
{
for (label i = range.first(); i <= range.last(); ++i)
{
hashset.unset(i);
}
}
void Foam::BitOps::unset(bitSet& bitset, const labelRange& range)
{
bitset.unset(range);
}
// * * * * * * * * * * * * * * * * BitSetOps * * * * * * * * * * * * * * * * //
Foam::bitSet Foam::BitSetOps::create
(

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,6 +37,8 @@ Namespace
Description
Factory and other methods for bitSet.
Adaptor methods for other containers that are somewhat similar to
bitSet (eg, boolList, labelHashSet).
\*---------------------------------------------------------------------------*/
@ -56,8 +58,9 @@ Description
namespace Foam
{
// Forward declarations
// Forward Declarations
class bitSet;
template<class T> class List;
/*---------------------------------------------------------------------------*\
Namespace BitOps Declaration
@ -98,6 +101,30 @@ inline bool none(const UList<bool>& bools)
}
//- Set the specified range 'on' in a boolList.
// For compatibility with bitSet::set(labelRange)
void set(List<bool>& bools, const labelRange& range);
//- Set the specified range in a labelHashSet.
// For compatibility with bitSet::set(labelRange)
void set(labelHashSet& hashset, const labelRange& range);
//- Forward to bitSet::set(labelRange)
void set(bitSet& bitset, const labelRange& range);
//- Unset the specified range 'on' in a boolList.
// For compatibility with bitSet::unset(labelRange)
void unset(List<bool>& bools, const labelRange& range);
//- Unset the specified range in a labelHashSet.
// For compatibility with bitSet::unset(labelRange)
void unset(labelHashSet& hashset, const labelRange& range);
//- Forward to bitSet::unset(labelRange)
void unset(bitSet& bitset, const labelRange& range);
//- Count arbitrary number of bits (of an integral type)
template<class UIntType>
inline unsigned int bit_count(UIntType x)

View File

@ -308,7 +308,7 @@ public:
// (auto-vivifies).
inline void set(const bitSet& bitset);
//- Set the specified range of bits specified
//- Set the specified range of bits
// The current set size may grow to accommodate any new bits
// (auto-vivifies).
// \note this operation is generally more efficient than calling