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
This commit is contained in:
Mark Olesen
2020-12-04 15:53:47 +01:00
parent 08efeb1a03
commit a939042e1b
6 changed files with 228 additions and 2 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.
@ -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)

View File

@ -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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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
// ************************************************************************* //

View File

@ -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 <http://www.gnu.org/licenses/>.
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
// ************************************************************************* //

View File

@ -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;

View File

@ -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<bitSet>();
}
inline bool Foam::bitSet::all() const
{
if (empty()) return true; // SIC. boost convention
return -1 == first_not_block();
}