Commit 7446d30f authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: additional labelHashSet -> bitSet conversion

- BitSetOps::create(len, locations, on) that generates a bitSet with
  the specified length. The optional 'on' bool can be used to flip the
  logic.
parent 3218b3de
......@@ -151,6 +151,7 @@ primitives/Barycentric2D/barycentric2D/barycentric2D.C
containers/Bits/bitSet/bitSet.C
containers/Bits/bitSet/bitSetIO.C
containers/Bits/BitOps/BitOps.C
containers/Bits/PackedList/PackedListCore.C
containers/HashTables/HashOps/HashOps.C
containers/HashTables/HashTable/HashTableCore.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "BitOps.H"
#include "bitSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::bitSet Foam::BitSetOps::create
(
const label n,
const labelHashSet& locations,
const bool on
)
{
bitSet output(n, !on);
for (const label idx : locations)
{
// Restrict the input size
if (idx < n)
{
output.set(idx, on);
}
}
return output;
}
Foam::bitSet Foam::BitSetOps::create
(
const label n,
const labelUList& locations,
const bool on
)
{
bitSet output(n, !on);
for (const label idx : locations)
{
// Restrict the input size
if (idx < n)
{
output.set(idx, on);
}
}
return output;
}
Foam::bitSet Foam::BitSetOps::create
(
const label n,
const label select,
const labelUList& values,
const bool on
)
{
bitSet output(n, !on);
// Restrict the input size
const label len = std::min(n, values.size());
for (label idx = 0; idx < len; ++idx)
{
if (select == values[idx])
{
output.set(idx, on);
}
}
return output;
}
// ************************************************************************* //
......@@ -30,6 +30,12 @@ Description
The population count uses the Hamming weight
(http://en.wikipedia.org/wiki/Hamming_weight).
Namespace
Foam::BitSetOps
Description
Factory and other methods for bitSet.
\*---------------------------------------------------------------------------*/
#ifndef BitOps_H
......@@ -37,6 +43,7 @@ Description
#include "label.H"
#include "UList.H"
#include "HashSet.H"
#include "Ostream.H"
#include <algorithm>
#include <limits>
......@@ -47,6 +54,9 @@ Description
namespace Foam
{
// Forward declarations
class bitSet;
/*---------------------------------------------------------------------------*\
Namespace BitOps Declaration
\*---------------------------------------------------------------------------*/
......@@ -202,12 +212,78 @@ struct bitInfo
operator UIntType& () { return value; }
};
} // End namespace BitOps
/*---------------------------------------------------------------------------*\
Namespace BitSetOps Declaration
\*---------------------------------------------------------------------------*/
namespace BitSetOps
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Create a bitSet with length n with the specifed \a on locations.
// The resulting bitSet is guaranteed to have exactly the specified length,
// any values or positions larger than n-1 are silently ignored.
//
// \param n the size of the output bitSet
// \param locations the list of positions corresponding to an \a on bit.
// \param on the value for on. Set as false to invert the logic.
//
// \return a bitset
bitSet create
(
const label n,
const labelHashSet& locations,
const bool on = true
);
//- Create a bitSet with length n with the specifed \a on locations.
// The resulting bitSet is guaranteed to have exactly the specified length,
// any values or positions larger than n-1 are silently ignored.
//
// \param n the size of the output bitSet
// \param locations the list of positions corresponding to an \a on bit.
// \param on the value for on. Set as false to invert the logic.
//
// \return a bitset
bitSet create
(
const label n,
const labelUList& locations,
const bool on = true
);
//- Create a bitSet with length n with the specifed \a on locations
//- when the list values are equal to the select value.
//
// The resulting bitSet is guaranteed to have exactly the specified length,
// any values or positions larger than n-1 are silently ignored.
//
// \param n the size of the output bitSet
// \param select the value to select as 'on'
// \param values the values to scan for 'select'
// \param on the value for on. Set as false to invert the logic.
//
// \return a bitset
bitSet create
(
const label n,
const label select,
const labelUList& values,
const bool on = true
);
} // End namespace BitSetOps
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Print 0/1 bits of an (unsigned) integral type via an adapter
template<class UIntType>
inline Ostream& operator<<(Ostream& os, const BitOps::bitInfo<UIntType>& info)
......
......@@ -48,7 +48,6 @@ namespace Foam
{
// Forward declarations
class bitSet;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -98,13 +97,13 @@ labelHashSet used(const UList<bool>& select);
//- Transform the \a on locations to a bitSet.
// Ignored any negative values (invalid positions in a bitset).
//
// \param locations the list of positions corresponding to an \a on bit.
//
// \return a bitset
//
// \note The operation necessarily discards any negative values since these
// are invalid positions in a bitset.
// \see BitSetOps::create for other possiblities
bitSet bitset(const labelHashSet& locations);
......
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