openfoam/applications/test/ListOps/Test-ListOps.C
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

183 lines
5.6 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation
\\/ 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/>.
Application
Test-ListOps
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "List.H"
#include "SubList.H"
#include "ListOps.H"
#include "face.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Test Rotations:" << nl << endl;
List<label> forwardRotate(identity(5));
face testFace(identity(4));
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate forward by " << i << " : "
<< rotateList(forwardRotate, i) << endl;
}
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate backward by " << i << " : "
<< rotateList(forwardRotate, -i) << endl;
}
Info<< nl << "Face : " << testFace << endl;
Info<< "Rotate by 2 : " << rotateList(testFace, 2) << endl;
inplaceRotateList<List, label>(testFace, -6);
Info<< "Rotate inplace by -6 : " << testFace << nl << endl;
Info<< "Test inplace rotate : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, 2);
Info<< "Rotate to the right by 2 : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, -2);
Info<< "Rotate to the left by 2 : " << forwardRotate << endl;
List<label> subRotate(identity(10));
SubList<label> subL(subRotate, 5, 3);
Info<< "Test inplace rotate on sublist : " << subRotate << endl;
inplaceRotateList(subL, 3);
Info<< "Rotate to the right by 3 : " << subRotate << endl;
inplaceRotateList(subL, -8);
Info<< "Rotate to the left by 3 : " << subRotate << endl;
Info<< nl << nl << "Test Reversing:" << nl << endl;
Info<< "List : " << identity(5) << endl;
Info<< "Reverse : " << reverseList(identity(5)) << endl;
Info<< "List : " << identity(6) << endl;
Info<< "Reverse : " << reverseList(identity(6)) << nl << endl;
List<label> test1(identity(5));
Info<< "List : " << test1 << endl;
inplaceReverseList(test1);
Info<< "Inplace Reverse : " << test1 << nl << endl;
List<label> test2(identity(6));
Info<< "List : " << test2 << endl;
inplaceReverseList(test2);
Info<< "Inplace Reverse : " << test2 << nl << endl;
face test3(identity(6));
Info<< "Face : " << test3 << endl;
inplaceReverseList(test3);
Info<< "Inplace Reverse : " << test3 << nl << endl;
FixedList<label, 6> test4(identity(6));
Info<< "FixedList : " << test4 << endl;
inplaceReverseList(test4);
Info<< "Inplace Reverse : " << test4 << nl << endl;
List<label> test5(identity(9));
SubList<label> test5SubList(test5, 4, 3);
Info<< "List : " << test5 << endl;
inplaceReverseList(test5SubList);
Info<< "Reverse Sublist between 3 and 6 : " << test5 << nl << endl;
Info<< nl << "Test lambda predicates:" << nl << endl;
List<label> test6(identity(11));
// shift range for general testing
std::for_each(test6.begin(), test6.end(), [](label& x){ x -= 4; });
Info<< "Subset of non-zero, even values: "
<< subsetList
(
test6,
[](const label& x){ return x && !(x % 2); }
) << nl
<< endl;
test6.append(identity(13));
// Randomize the list
std::random_shuffle(test6.begin(), test6.end());
Info<< "Randomized: " << flatOutput(test6) << endl;
inplaceUniqueSort(test6);
Info<< "Unique : " << flatOutput(test6) << endl;
// List reorder
labelList oldToNew(identity(40));
std::random_shuffle(oldToNew.begin(), oldToNew.end());
// Force a few -1:
oldToNew[4] = oldToNew[8] = -1;
Info<<"Test reorder - oldToNew:" << nl
<< flatOutput(oldToNew) << nl << nl;
bitSet bitset
(
ListOps::createWithValue<bool>
(
25,
labelList({8, 12, 15, 22, 4}),
true
)
);
Info<<"bitset input: " << flatOutput(bitset) << nl;
inplaceReorder(oldToNew, bitset);
Info<<" reorder: " << flatOutput(bitset) << nl << nl;
PackedList<2> packed
(
ListOps::createWithValue<label>
(
25,
labelList({8, 12, 15, 22, 4}),
2
)
);
Info<<"packed input: " << flatOutput(packed) << nl;
inplaceReorder(oldToNew, packed);
Info<<" reorder: " << flatOutput(packed) << nl << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //