From a939042e1ba1fbfb4fa41ca6716e857e693a9033 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 4 Dec 2020 15:53:47 +0100 Subject: [PATCH] ENH: add bitSet::null() and clarify some documentation - the NullObject singleton can also be cast to a bitSet (sufficient size and bit-pattern). Useful for places that need to hold a reference on construction --- applications/test/bitSet2/Test-bitSet2.C | 19 +++- applications/test/boolList/Test-boolList.C | 19 ++++ applications/test/boolList/bitSetOrBoolList.H | 102 ++++++++++++++++++ applications/test/boolList/disabledBoolList.H | 73 +++++++++++++ src/OpenFOAM/containers/Bits/bitSet/bitSet.H | 9 +- src/OpenFOAM/containers/Bits/bitSet/bitSetI.H | 8 ++ 6 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 applications/test/boolList/bitSetOrBoolList.H create mode 100644 applications/test/boolList/disabledBoolList.H diff --git a/applications/test/bitSet2/Test-bitSet2.C b/applications/test/bitSet2/Test-bitSet2.C index 5339e9dc6f..caa3800fb7 100644 --- a/applications/test/bitSet2/Test-bitSet2.C +++ b/applications/test/bitSet2/Test-bitSet2.C @@ -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. @@ -164,6 +164,23 @@ inline bool compare int main(int argc, char *argv[]) { + Info<< "bitSet::null()" << nl + << "sizeof : " << sizeof(bitSet::null()) << " bytes" << nl; + + info(bitSet::null()); + + { + bitSet emptySet; + info(emptySet); + extent(emptySet); + + emptySet.resize(10); + info(emptySet); + extent(emptySet); + } + + Info<< nl << "Tests" << nl; + bitSet list1(22); // Set every third one on forAll(list1, i) diff --git a/applications/test/boolList/Test-boolList.C b/applications/test/boolList/Test-boolList.C index 5e0a4772f5..dd4f79a38f 100644 --- a/applications/test/boolList/Test-boolList.C +++ b/applications/test/boolList/Test-boolList.C @@ -36,6 +36,7 @@ Description #include "bitSet.H" #include "BitOps.H" #include "FlatOutput.H" +#include "bitSetOrBoolList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -153,6 +154,24 @@ int main(int argc, char *argv[]) Info<< "\nafter set [13,5]\n"; compare(list1, "1..1..1..1..111111...."); + { + boolList list2(5, true); + list2.unset(2); + + Info<< "Test wrapper idea" << nl; + + bitSetOrBoolList wrapper(list2); + + if (wrapper.test(1)) + { + Info<< "1 is on" << nl; + } + if (!wrapper.test(2)) + { + Info<< "2 is off" << nl; + } + } + Info<< "\nDone" << nl << endl; return 0; } diff --git a/applications/test/boolList/bitSetOrBoolList.H b/applications/test/boolList/bitSetOrBoolList.H new file mode 100644 index 0000000000..56e3b9d1f5 --- /dev/null +++ b/applications/test/boolList/bitSetOrBoolList.H @@ -0,0 +1,102 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 . + +Class + Foam::bitSetOrBoolList + +Description + Simple wrapper for handling test() on bitSet or boolList + without a templating layer or lambda expresssion. + +\*---------------------------------------------------------------------------*/ + +#ifndef bitSetOrBoolList_H +#define bitSetOrBoolList_H + +#include "bitSet.H" +#include "boolList.H" + +/*---------------------------------------------------------------------------*\ + Class bitSetOrBoolList Declaration +\*---------------------------------------------------------------------------*/ + +namespace Foam +{ + +class bitSetOrBoolList +{ + const bitSet& bits_; + const boolList& bools_; + +public: + + // Constructors + + //- Construct with a bitSet reference + explicit bitSetOrBoolList(const bitSet& select) + : + bits_(select), + bools_(boolList::null()) + {} + + //- Construct with a boolList reference + explicit bitSetOrBoolList(const boolList& select) + : + bits_(bitSet::null()), + bools_(select) + {} + + + // Member Functions + + //- Is empty + bool empty() const + { + return bits_.empty() && bools_.empty(); + } + + //- Size + label size() const + { + return bits_.size() + bools_.size(); + } + + //- Test function + bool test(const label i) const + { + return bits_.test(i) || bools_.test(i); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/applications/test/boolList/disabledBoolList.H b/applications/test/boolList/disabledBoolList.H new file mode 100644 index 0000000000..7adc481e35 --- /dev/null +++ b/applications/test/boolList/disabledBoolList.H @@ -0,0 +1,73 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 . + +Class + Foam::disabledBoolList + +Description + A trivial structure to use as a boolList replacement when testing + compilation without using the [] accessors. + + Example, + \code + const disableBoolList blockedFace; + ... + + if (blockedFace.test(facei)) ... // Good + if (blockedFace[facei]) ... // Compile error + \endcode + +\*---------------------------------------------------------------------------*/ + +#ifndef disabledBoolList_H +#define disabledBoolList_H + +/*---------------------------------------------------------------------------*\ + Class disabledBoolList Declaration +\*---------------------------------------------------------------------------*/ + +namespace Foam +{ + +struct disabledBoolList +{ + bool empty() const { return true; } + + int size() const { return 0; } + + bool test(int) const { return true; } + + void set(bool) {} +}; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index 119d211298..ee82696182 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -128,6 +128,11 @@ public: class const_iterator; typedef unsigned int const_reference; + // Static Member Functions + + //- Return a null bitSet reference + inline static const bitSet& null(); + //- Declare type-name (with debug switch) ClassName("bitSet"); @@ -214,7 +219,9 @@ public: // 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 \b empty. + // Returning true for an empty set may not seem intuitive, but + // conforms to how boost has defined things. // \note Method name compatibility with boost::dynamic_bitset inline bool all() const; diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H index acad8c5ec9..1ad6e80a80 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H @@ -451,8 +451,16 @@ inline Foam::label Foam::bitSet::find_next(label pos) const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +inline const Foam::bitSet& Foam::bitSet::null() +{ + return NullObjectRef(); +} + + inline bool Foam::bitSet::all() const { + if (empty()) return true; // SIC. boost convention + return -1 == first_not_block(); }