From 7446d30fbde92f585d097c37cfdc098af64faf79 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 25 Jul 2018 17:50:38 +0200 Subject: [PATCH] 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. --- src/OpenFOAM/Make/files | 1 + src/OpenFOAM/containers/Bits/BitOps/BitOps.C | 101 ++++++++++++++++++ src/OpenFOAM/containers/Bits/BitOps/BitOps.H | 78 +++++++++++++- .../containers/HashTables/HashOps/HashOps.H | 5 +- 4 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/OpenFOAM/containers/Bits/BitOps/BitOps.C diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 11f4bb9bfd..25ce355abb 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -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 diff --git a/src/OpenFOAM/containers/Bits/BitOps/BitOps.C b/src/OpenFOAM/containers/Bits/BitOps/BitOps.C new file mode 100644 index 0000000000..0fa3b4fbce --- /dev/null +++ b/src/OpenFOAM/containers/Bits/BitOps/BitOps.C @@ -0,0 +1,101 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +\*---------------------------------------------------------------------------*/ + +#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; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Bits/BitOps/BitOps.H b/src/OpenFOAM/containers/Bits/BitOps/BitOps.H index 706578b708..dc926a9632 100644 --- a/src/OpenFOAM/containers/Bits/BitOps/BitOps.H +++ b/src/OpenFOAM/containers/Bits/BitOps/BitOps.H @@ -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 #include @@ -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 inline Ostream& operator<<(Ostream& os, const BitOps::bitInfo& info) diff --git a/src/OpenFOAM/containers/HashTables/HashOps/HashOps.H b/src/OpenFOAM/containers/HashTables/HashOps/HashOps.H index 8a6ae13c26..97f308f7ce 100644 --- a/src/OpenFOAM/containers/HashTables/HashOps/HashOps.H +++ b/src/OpenFOAM/containers/HashTables/HashOps/HashOps.H @@ -48,7 +48,6 @@ namespace Foam { // Forward declarations - class bitSet; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -98,13 +97,13 @@ labelHashSet used(const UList& 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);