Files
openfoam/src/OpenFOAM/containers/HashTables/HashOps/HashOps.H
Mark Olesen bac943e6fc ENH: new bitSet class and improved PackedList class (closes #751)
- The bitSet class replaces the old PackedBoolList class.
  The redesign provides better block-wise access and reduced method
  calls. This helps both in cases where the bitSet may be relatively
  sparse, and in cases where advantage of contiguous operations can be
  made. This makes it easier to work with a bitSet as top-level object.

  In addition to the previously available count() method to determine
  if a bitSet is being used, now have simpler queries:

    - all()  - true if all bits in the addressable range are empty
    - any()  - true if any bits are set at all.
    - none() - true if no bits are set.

  These are faster than count() and allow early termination.

  The new test() method tests the value of a single bit position and
  returns a bool without any ambiguity caused by the return type
  (like the get() method), nor the const/non-const access (like
  operator[] has). The name corresponds to what std::bitset uses.

  The new find_first(), find_last(), find_next() methods provide a faster
  means of searching for bits that are set.

  This can be especially useful when using a bitSet to control an
  conditional:

  OLD (with macro):

      forAll(selected, celli)
      {
          if (selected[celli])
          {
              sumVol += mesh_.cellVolumes()[celli];
          }
      }

  NEW (with const_iterator):

      for (const label celli : selected)
      {
          sumVol += mesh_.cellVolumes()[celli];
      }

      or manually

      for
      (
          label celli = selected.find_first();
          celli != -1;
          celli = selected.find_next()
      )
      {
          sumVol += mesh_.cellVolumes()[celli];
      }

- When marking up contiguous parts of a bitset, an interval can be
  represented more efficiently as a labelRange of start/size.
  For example,

  OLD:

      if (isA<processorPolyPatch>(pp))
      {
          forAll(pp, i)
          {
              ignoreFaces.set(i);
          }
      }

  NEW:

      if (isA<processorPolyPatch>(pp))
      {
          ignoreFaces.set(pp.range());
      }
2018-03-07 11:21:48 +01:00

156 lines
4.3 KiB
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/>.
Namspace
Foam::HashSetOps
Description
Various operations for HashSet.
Namspace
Foam::HashTableOps
Description
Various operations for HashTable.
SourceFiles
HashOps.H
\*---------------------------------------------------------------------------*/
#ifndef HashOps_H
#define HashOps_H
#include "HashSet.H"
#include "List.H"
namespace Foam
{
// Forward declarations
class bitSet;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
/*---------------------------------------------------------------------------*\
Namespace HashSetOps Declaration
\*---------------------------------------------------------------------------*/
namespace HashSetOps
{
//- Combine HashSet operation. Equivalent to 'a |= b'
template<class Key=word, class Hash=string::hash>
struct plusEqOp
{
typedef HashSet<Key, Hash> value_type;
void operator()(value_type& a, const value_type& b) const
{
a |= b;
}
};
//- Convert a bitset to a labelHashSet of the indices used.
//
// \param select the bitset for which an \a on entry corresponds
// to an index in the output labelHashSet
//
// \return a labelHashSet of the selected indices
//
// This is equivalent of the following code, but more efficiently implemented.
// \code
// bitSet select = ...;
// return labelHashSet(select.toc());
// \endcode
labelHashSet used(const bitSet& select);
//- Convert a list of bools to a labelHashSet of the indices used.
//
// \param select the boolList for which a \a true entry corresponds
// to an index in the output labelHashSet
//
// \return a labelHashSet of the selected indices
labelHashSet used(const UList<bool>& select);
//- Transform the \a on locations to 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.
bitSet bitset(const labelHashSet& locations);
//- Transform the \a on locations to a boolList, with \a true for each
//- non-negative location and \a false for all others.
//
// \param locations the list of positions corresponding to an \a on bit.
//
// \return a boolList
//
// \note The operation necessarily discards any negative values since these
// are invalid positions in a boolList.
List<bool> bools(const labelHashSet& locations);
} // End namespace HashSetOps
/*---------------------------------------------------------------------------*\
Namespace HashTableOps Declaration
\*---------------------------------------------------------------------------*/
namespace HashTableOps
{
//- Combine HashTable operation. Equivalent to 'a += b'
template<class T, class Key=word, class Hash=string::hash>
struct plusEqOp
{
typedef HashTable<T, Key, Hash> value_type;
void operator()(value_type& a, const value_type& b) const
{
a += b;
}
};
} // End namespace HashTableOps
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //