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());
      }
This commit is contained in:
Mark Olesen
2018-03-07 11:21:48 +01:00
parent 2768500d57
commit bac943e6fc
191 changed files with 4995 additions and 3151 deletions

View File

@ -474,7 +474,7 @@ int main(int argc, char *argv[])
}
{
PackedBoolList select = HashSetOps::bitset(locations);
bitSet select = HashSetOps::bitset(locations);
auto output = ListOps::createWithValue<label>
(
30,
@ -482,8 +482,8 @@ int main(int argc, char *argv[])
100,
-1 // default value
);
Info<< "with PackedBoolList: " << flatOutput(output)
<< " selector: " << flatOutput(select.used()) << nl;
Info<< "with bitSet: " << flatOutput(output)
<< " selector: " << flatOutput(select.toc()) << nl;
}
{
@ -516,7 +516,7 @@ int main(int argc, char *argv[])
}
{
PackedBoolList select = HashSetOps::bitset(locations);
bitSet select = HashSetOps::bitset(locations);
auto output = ListOps::createWithValue<label>
(
30,
@ -524,7 +524,7 @@ int main(int argc, char *argv[])
100,
-1 // default value
);
Info<< "with PackedBoolList: " << flatOutput(output)
Info<< "with bitSet: " << flatOutput(output)
<< " selector: " << flatOutput(HashSetOps::used(select))
<< nl;
}

View File

@ -145,7 +145,7 @@ int main(int argc, char *argv[])
Info<<"Test reorder - oldToNew:" << nl
<< flatOutput(oldToNew) << nl << nl;
PackedBoolList bitset
bitSet bitset
(
ListOps::createWithValue<bool>
(

View File

@ -32,7 +32,7 @@ Description
#include "IOobject.H"
#include "IOstreams.H"
#include "IFstream.H"
#include "PackedBoolList.H"
#include "bitSet.H"
#include <climits>
@ -131,7 +131,7 @@ int main(int argc, char *argv[])
IFstream ifs(srcFile);
List<label> rawLst(ifs);
PackedBoolList packLst(rawLst);
bitSet packLst(rawLst);
Info<< "size: " << packLst.size() << nl;

View File

@ -28,61 +28,100 @@ Description
\*---------------------------------------------------------------------------*/
#include "uLabel.H"
#include "labelRange.H"
#include "bitSet.H"
#include "FlatOutput.H"
#include "IOstreams.H"
#include "PackedBoolList.H"
using namespace Foam;
template<unsigned Width>
inline Ostream& report
(
const PackedList<Width>& list,
bool showBits = false,
bool debugOutput = false
)
{
Info<< list.info();
if (showBits)
{
list.printBits(Info, debugOutput) << nl;
}
return Info;
}
inline Ostream& report
(
const bitSet& bitset,
bool showBits = false,
bool debugOutput = false
)
{
Info<< bitset.info();
Info<< "all:" << bitset.all()
<< " any:" << bitset.any()
<< " none:" << bitset.none() << nl;
if (showBits)
{
bitset.printBits(Info, debugOutput) << nl;
}
return Info;
}
// BitOps::printBits((Info<< list1.info()), true) << nl;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "PackedList max_bits() = " << PackedList<>::max_bits() << nl;
Info<< "\ntest allocation with value\n";
PackedList<3> list1(5,1);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign uniform value\n";
list1 = 3;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign uniform value (with overflow)\n";
list1 = -1;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest zero\n";
list1 = 0;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest set() with default argument (max_value)\n";
list1.set(1);
list1.set(3);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest unset() with in-range and out-of-range\n";
list1.unset(3);
list1.unset(100000);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign between references\n";
list1[2] = 3;
list1[4] = list1[2];
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign between references, with chaining\n";
list1[0] = 1;
list1[4] = 1;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign between references, with chaining and auto-vivify\n";
list1[1] = 2;
list1[8] = 2;
list1[10] = 2;
list1[14] = 2;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest operator== between references\n";
if (list1[1] == list1[8])
@ -106,7 +145,9 @@ int main(int argc, char *argv[])
{
const PackedList<3>& constLst = list1;
Info<< "\ntest operator[] const with out-of-range index\n";
constLst.printInfo(Info, true);
report(constLst);
if (constLst[20])
{
Info<< "[20] is true (unexpected)\n";
@ -116,7 +157,7 @@ int main(int argc, char *argv[])
Info<< "[20] is false (expected) list size should be unchanged "
<< "(const)\n";
}
constLst.printInfo(Info, true);
report(constLst);
Info<< "\ntest operator[] non-const with out-of-range index\n";
if (list1[20])
@ -128,7 +169,8 @@ int main(int argc, char *argv[])
Info<< "[20] is false (expected) but list was resized?? "
<< "(non-const)\n";
}
list1.printInfo(Info, true);
report(list1);
}
@ -137,94 +179,94 @@ int main(int argc, char *argv[])
{
Info<< "[20] is false, as expected\n";
}
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest resize with value (without reallocation)\n";
list1.resize(8, list1.max_value());
list1.printInfo(Info, true);
list1.resize(8, list1.max_value);
report(list1);
Info<< "\ntest flip() function\n";
list1.flip();
list1.printInfo(Info, true);
report(list1);
Info<< "\nre-flip()\n";
list1.flip();
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest set() function\n";
list1.set(1, 5);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign bool\n";
list1 = false;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest assign bool\n";
list1 = true;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest resize without value (with reallocation)\n";
list1.resize(12);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest resize with value (with reallocation)\n";
list1.resize(25, list1.max_value());
list1.printInfo(Info, true);
list1.resize(25, list1.max_value);
report(list1);
Info<< "\ntest resize smaller (should not touch allocation)\n";
list1.resize(8);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest append() operation\n";
list1.append(2);
list1.append(3);
list1.append(4);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest reserve() operation\n";
list1.reserve(32);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest shrink() operation\n";
list1.shrink();
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest setCapacity() operation\n";
list1.setCapacity(15);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest setCapacity() operation\n";
list1.setCapacity(100);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest operator[] assignment\n";
list1[16] = 5;
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest operator[] assignment with auto-vivify\n";
list1[36] = list1.max_value();
list1.printInfo(Info, true);
list1[36] = list1.max_value;
report(list1);
Info<< "\ntest setCapacity smaller\n";
list1.setCapacity(24);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest resize much smaller\n";
list1.resize(150);
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest trim\n";
list1.trim();
list1.printInfo(Info, true);
report(list1);
// add in some misc values
// Add in some misc values
list1[31] = 1;
list1[32] = 2;
list1[33] = 3;
Info<< "\ntest get() method\n";
Info<< "get(10):" << list1.get(10) << " and list[10]:" << list1[10] << "\n";
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest operator[] auto-vivify\n";
Info<< "size:" << list1.size() << "\n";
@ -234,33 +276,34 @@ int main(int argc, char *argv[])
Info<< "list[45]:" << val << "\n";
Info<< "size after read:" << list1.size() << "\n";
list1[45] = list1.max_value();
list1[45] = list1.max_value;
Info<< "size after write:" << list1.size() << "\n";
Info<< "list[45]:" << list1[45] << "\n";
list1[49] = list1[100];
list1.printInfo(Info, true);
report(list1);
Info<< "\ntest copy constructor + append\n";
PackedList<3> list2(list1);
list2.append(4);
Info<< "source list:\n";
list1.printInfo(Info, true);
report(list1);
Info<< "destination list:\n";
list2.printInfo(Info, true);
report(list2);
Info<< "\ntest pattern that fills all bits\n";
PackedList<4> list3(8, 8);
label pos = list3.size() - 1;
list3[pos--] = list3.max_value();
list3[pos--] = list3.max_value;
list3[pos--] = 0;
list3[pos--] = list3.max_value();
list3.printInfo(Info, true);
list3[pos--] = list3.max_value;
report(list3);
Info<< "removed final value: " << list3.remove() << endl;
list3.printInfo(Info, true);
report(list3);
Info<<"list: " << list3 << endl;
@ -289,18 +332,117 @@ int main(int argc, char *argv[])
}
PackedBoolList listb(list4);
bitSet listb(list4);
Info<< "copied from bool list " << endl;
listb.printInfo(Info, true);
// report(listb);
{
labelList indices = listb.used();
labelList indices = listb.toc();
Info<< "indices: " << indices << endl;
}
Info<< nl
<< "resizing: " << nl;
{
PackedList<1> list1(81, 1);
PackedList<3> list3(27, 5); // ie, 101
Info<< "initial" << nl; report(list1, true);
Info<< "initial" << nl; report(list3, true);
list1.resize(118, 1);
list3.resize(37, 3);
Info<< "extend with val" << nl; report(list1, true);
Info<< "extend with val" << nl; report(list3, true);
list1.resize(90, 0);
list3.resize(30, 4);
Info<< "contract with val" << nl; report(list1, true);
Info<< "contract with val" << nl; report(list3, true);
}
{
bitSet bits(45);
Info<< "bits" << nl; report(bits, true);
bits = true;
Info<< "bits" << nl; report(bits, true);
bits.unset(35);
Info<< "bits" << nl; report(bits, true);
bits.resize(39);
Info<< "bits" << nl; report(bits, true);
Info<< "values:" << flatOutput(bits.values()) << nl;
Info<< "used:" << flatOutput(bits.toc()) << nl;
bits.unset(labelRange(-15, 8));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(-15, 100));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(-15, 100));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(150, 15));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(0, 5));
Info<< "bits" << nl; report(bits, true);
bits.reset();
bits.resize(50);
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(4, 8));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(30, 35));
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(80, 12));
Info<< "bits" << nl; report(bits, true);
bits.unset(labelRange(35, 16));
Info<< "bits" << nl; report(bits, true);
bits.unset(labelRange(0, 50));
bits.resize(100000);
bits.set(labelRange(30, 6));
bits[33] = false;
Info<<"used: " << flatOutput(bits.toc()) << endl;
Info<<"first: " << bits.find_first() << endl;
Info<<"next: " << bits.find_next(29) << endl;
Info<<"next: " << bits.find_next(30) << endl;
Info<<"next: " << bits.find_next(31) << endl;
Info<<"next: " << bits.find_next(31) << endl;
bits.set(labelRange(80, 10));
bits.resize(100);
Info<< "bits" << nl; report(bits, true);
bits.set(labelRange(125, 10));
Info<<"next: " << bits.find_next(64) << endl;
Info<<"used: " << flatOutput(bits.toc()) << endl;
for (const auto pos : bits)
{
Info<<"have: " << pos << nl;
}
}
Info<< "\n\nDone.\n";
return 0;

View File

@ -29,7 +29,7 @@ Description
#include "argList.H"
#include "boolList.H"
#include "PackedBoolList.H"
#include "bitSet.H"
#include "HashSet.H"
#include "cpuTime.H"
#include <vector>
@ -63,7 +63,7 @@ int main(int argc, char *argv[])
unsigned long sum = 0;
PackedBoolList packed(n, 1);
bitSet packed(n, true);
boolList unpacked(n, true);
#ifdef TEST_STD_BOOLLIST

View File

@ -1,3 +0,0 @@
Test-PackedList3.C
EXE = $(FOAM_USER_APPBIN)/Test-PackedList3

View File

@ -1,3 +0,0 @@
Test-PackedList4.C
EXE = $(FOAM_USER_APPBIN)/Test-PackedList4

View File

@ -1,168 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 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
Description
\*---------------------------------------------------------------------------*/
#include "uLabel.H"
#include "boolList.H"
#include "DynamicList.H"
#include "IOstreams.H"
#include "PackedBoolList.H"
#include "ITstream.H"
#include "StringStream.H"
#include "FlatOutput.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
PackedBoolList list1(20);
// set every third one on
forAll(list1, i)
{
list1[i] = !(i % 3);
}
Info<< "\nalternating bit pattern\n";
list1.printInfo(Info, true);
PackedBoolList list2(list1);
list2.flip();
Info<< "\nflipped bit pattern\n";
list2.printBits(Info);
// set every other on
forAll(list2, i)
{
list2[i] = !(i % 2);
}
Info<< "\nalternating bit pattern\n";
list2.printBits(Info);
list2.resize(28, false);
list2.resize(34, true);
list2.resize(40, false);
for (label i=0; i < 4; ++i)
{
list2[i] = true;
}
Info<< "\nresized with false, 6 true + 6 false, bottom 4 bits true\n";
list2.printInfo(Info, true);
labelList list2Labels = list2.used();
PackedBoolList list4
(
ITstream
(
"input",
"(1 n 1 n 1 n 1 1 off 0 0 f f 0 y yes y true y false on t)"
)()
);
Info<< "\ntest Istream constructor\n";
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used() << nl;
Info<< "\nassign from labelList\n";
list4.clear();
list4.setMany(labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21});
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used() << nl;
// Not yet:
// PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
// list5.printInfo(Info, true);
// Info<< list5 << " indices: " << list5.used() << nl;
Info<< "\nassign from indices\n";
list4.read
(
IStringStream
(
"{0 1 2 3 12 13 14 19 20 21}"
)()
);
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used() << nl;
boolList bools(list4.size());
forAll(list4, i)
{
bools[i] = list4[i];
}
Info<< "boolList: " << bools << nl;
// check roundabout assignments
PackedList<2> pl2
(
IStringStream
(
"{(0 3)(1 3)(2 3)(3 3)(12 3)(13 3)(14 3)(19 3)(20 3)(21 3)}"
)()
);
Info<< "roundabout assignment: " << pl2 << nl;
list4.clear();
forAll(pl2, i)
{
list4[i] = pl2[i];
}
list4.writeList(Info, -1) << nl; // indexed output
list4.writeEntry("PackedBoolList", Info);
// Construct from labelUList, labelUIndList
{
DynamicList<label> indices({10, 50, 300});
Info<< "set: " << flatOutput(indices) << endl;
PackedBoolList bools1(indices);
Info<< "used: " << bools1.size() << " "
<< flatOutput(bools1.used()) << endl;
}
return 0;
}
// ************************************************************************* //

View File

@ -218,7 +218,7 @@ int main(int argc, char *argv[])
labelList patchEdges;
labelList coupledEdges;
PackedBoolList sameEdgeOrientation;
bitSet sameEdgeOrientation;
PatchTools::matchEdges
(
pp,

View File

@ -0,0 +1,3 @@
Test-bitSet1.C
EXE = $(FOAM_USER_APPBIN)/Test-bitSet1

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -22,50 +22,60 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-bitSet1
Description
Test bitSet functionality
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "boolList.H"
#include "bitSet.H"
#include "HashSet.H"
#include "cpuTime.H"
#include "PackedBoolList.H"
#include <vector>
#include <unordered_set>
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
inline Ostream& report
(
const bitSet& bitset,
bool showBits = false,
bool debugOutput = false
)
{
Info<< "size=" << bitset.size() << "/" << bitset.capacity()
<< " count=" << bitset.count()
<< " all:" << bitset.all()
<< " any:" << bitset.any()
<< " none:" << bitset.none() << nl;
Info<< "values: " << flatOutput(bitset) << nl;
if (showBits)
{
bitset.printBits(Info, debugOutput) << nl;
}
return Info;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
const label nLoop = 5;
const label n = 100000000;
// const label nReport = 1000000;
bitSet set1(100);
cpuTime timer;
Info<<"bitSet(label): "; report(set1, true);
// test inserts
PackedBoolList packed;
for (label iloop = 0; iloop < nLoop; ++iloop)
{
for (label i = 0; i < n; ++i)
{
// if ((i % nReport) == 0 && i)
// {
// Info<< "." << flush;
// }
packed[i] = 1;
// Make compiler do something else too
packed[i/2] = 0;
}
}
Info<< nl
<< "insert test: " << nLoop << "*" << n << " elements in "
<< timer.cpuTimeIncrement() << " s\n\n";
bitSet set2(100, { -1, 10, 25, 45});
Info<<"bitSet(label, labels): "; report(set2, true);
Info << "\nEnd\n" << endl;
Info<< "End\n" << endl;
return 0;
}

View File

@ -0,0 +1,3 @@
Test-bitSet2.C
EXE = $(FOAM_USER_APPBIN)/Test-bitSet2

View File

@ -0,0 +1,311 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
Application
Test-bitSet2
Description
Test bitSet functionality
\*---------------------------------------------------------------------------*/
#include "uLabel.H"
#include "boolList.H"
#include "DynamicList.H"
#include "IOstreams.H"
#include "ITstream.H"
#include "StringStream.H"
#include "bitSet.H"
#include "FlatOutput.H"
using namespace Foam;
inline Ostream& report
(
const bitSet& bitset,
bool showBits = false,
bool debugOutput = false
)
{
Info<< "size=" << bitset.size() << "/" << bitset.capacity()
<< " count=" << bitset.count()
<< " all:" << bitset.all()
<< " any:" << bitset.any()
<< " none:" << bitset.none() << nl;
Info<< "values: " << flatOutput(bitset) << nl;
if (showBits)
{
bitset.printBits(Info, debugOutput) << nl;
}
return Info;
}
template<class UIntType>
std::string toString(UIntType value, char off='.', char on='1')
{
std::string str(std::numeric_limits<UIntType>::digits, off);
unsigned n = 0;
// Starting from most significant bit - makes for easy reading.
for
(
unsigned test = (1u << (std::numeric_limits<UIntType>::digits-1));
test;
test >>= 1u
)
{
str[n++] = ((value & test) ? on : off);
}
return str;
}
inline bool compare
(
const bitSet& bitset,
const std::string& expected
)
{
const List<unsigned int>& store = bitset.storage();
std::string has;
for (label blocki=0; blocki < bitset.nBlocks(); ++blocki)
{
has += toString(store[blocki]);
}
if (has == expected)
{
Info<< "pass: " << has << nl;
return true;
}
Info<< "fail: " << has << nl;
Info<< "expect: " << expected << nl;
return false;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
bitSet list1(22);
// Set every third one on
forAll(list1, i)
{
list1[i] = !(i % 3);
}
Info<< "\nalternating bit pattern\n";
compare(list1, "..........1..1..1..1..1..1..1..1");
list1.unset(labelRange(13, 20)); // In range
Info<< "\nafter clear [13,..]\n";
compare(list1, "...................1..1..1..1..1");
report(list1, true);
list1.unset(labelRange(40, 20)); // out of range
Info<< "\nafter clear [40,..]\n";
compare(list1, "...................1..1..1..1..1");
report(list1, true);
Info<< "first: " << list1.find_first()
<< " last: " << list1.find_last() << endl;
Info<< "iterate through:";
for (const label idx : list1)
{
Info<<" " << idx;
}
Info<< nl;
Info<< "\nalternating bit pattern\n";
report(list1, true);
bitSet list2 = ~list1;
Info<< "\nflipped bit pattern\n";
report(list2, true);
// set every other on
forAll(list2, i)
{
list2[i] = !(i % 2);
}
Info<< "\nstarting pattern\n";
report(list2, true);
list2.resize(28, false);
list2.resize(34, true);
list2.resize(40, false);
for (label i=0; i < 4; ++i)
{
list2[i] = true;
}
Info<< "\nresized with false, [28,34) true + 6 false, bottom 4 bits true\n";
compare
(
list2,
"1111.......1.1.1.1.1.1.1.1.11111"
"..............................11"
);
report(list2, true);
labelList list2Labels = list2.toc();
Info<< "\noperator|\n";
list1.printBits(Info);
list2.printBits(Info);
Info<< "==\n";
(list1 | list2).printBits(Info);
Info<< "\noperator& : does trim\n";
report((list1 & list2), true);
Info<< "\noperator^\n";
report((list1 ^ list2), true);
Info<< "\noperator|=\n";
{
bitSet list3 = list1;
report((list3 |= list2), true);
}
Info<< "\noperator&=\n";
{
bitSet list3 = list1;
report((list3 &= list2), true);
}
Info<< "\noperator^=\n";
{
bitSet list3 = list1;
report((list3 ^= list2), true);
}
Info<< "\noperator-=\n";
{
bitSet list3 = list1;
report((list3 -= list2), true);
}
bitSet list4
(
ITstream
(
"input",
"(1 n 1 n 1 n 1 1 off 0 0 f f 0 y yes y true y false on t)"
)()
);
Info<< "\ntest Istream constructor\n";
report(list4, true);
Info<< "\nclear/assign from labelList\n";
list4.clear();
list4.setMany(labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21});
report(list4, true);
// Not yet:
// bitSet list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
// list5.printInfo(Info, true);
// Info<< list5 << " indices: " << list5.toc() << nl;
Info<< "\nassign from indices\n";
list4.read
(
IStringStream
(
"{0 1 2 3 12 13 14 19 20 21}"
)()
);
report(list4, true);
compare(list4, "..........111....111........1111");
list4.set(labelRange(28, 6)); // extends size
Info<<"extended\n";
compare
(
list4,
"1111......111....111........1111"
"..............................11"
);
list4.set(labelRange(40, 6)); // extends size
Info<<"extended\n";
compare
(
list4,
"1111......111....111........1111"
"..................111111......11"
);
list4.unset(labelRange(14, 19));
Info<<"cleared [14,33)\n";
compare
(
list4,
"..................11........1111"
"..................111111......1."
);
// Construct from labelUList, labelUIndList
{
DynamicList<label> indices({10, 50, 300});
Info<< "set: " << flatOutput(indices) << endl;
bitSet bools1(indices);
Info<< "used: " << bools1.size() << " "
<< flatOutput(bools1.toc()) << endl;
}
return 0;
}
// ************************************************************************* //

View File

@ -27,6 +27,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "bool.H"
#include "BitOps.H"
#include "IOstreams.H"
#include "stdFoam.H"
@ -177,6 +178,15 @@ int main(int argc, char *argv[])
printOffset<double>();
Info<<nl << "Test repeat_value" << nl << nl;
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned, 3>(1u))
<< nl;
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned>(1u))
<< nl;
Info << "---\nEnd\n" << endl;
return 0;

View File

@ -0,0 +1,3 @@
Test-limits.C
EXE = $(FOAM_USER_APPBIN)/Test-limits

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -22,44 +22,24 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Test the sizeof for basic types. Can be compiled and run without
any OpenFOAM libraries.
Test the sizeof for basic types.
Can be compiled and run without any OpenFOAM libraries.
g++ -std=c++11 -oTest-machine-sizes Test-machine-sizes.cpp
\*---------------------------------------------------------------------------*/
#include <cstdint>
#include <climits>
#include <cstdlib>
#include <limits>
#include <iostream>
// Can also compile without OpenFOAM
#ifdef WM_LABEL_SIZE
#include "IOstreams.H"
#endif
template<class T>
void print(const char* msg)
{
std::cout<< msg << ' ' << sizeof(T) << '\n';
}
#ifdef WM_LABEL_SIZE
template<class T>
void printMax(const char* msg)
{
std::cout<< msg << ' ' << sizeof(T)
<< " max "
<< std::numeric_limits<T>::max() << '\n';
Foam::Info<< msg << ' ' << sizeof(T)
<< " max "
<< long(std::numeric_limits<T>::max()) << '\n';
}
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -67,42 +47,16 @@ int main(int argc, char *argv[])
{
std::cout<<"machine sizes\n---\n\n";
print<mode_t>("mode_t");
print<short>("short");
print<int>("int");
print<long>("long");
print<long long>("long long");
print<unsigned short>("ushort");
print<unsigned int>("uint");
print<unsigned long>("ulong");
print<unsigned long long>("ulong-long");
print<int16_t>("int16");
print<int32_t>("int32");
print<int64_t>("int64");
print<uint16_t>("uint16");
print<uint32_t>("uint32");
print<uint64_t>("uint64");
print<float>("float");
print<double>("double");
print<long double>("long double");
print<std::string>("std::string");
print<std::string::size_type>("std::string::size_type");
#ifdef WM_LABEL_SIZE
std::cout<<"\nmax values\n---\n\n";
printMax<mode_t>("mode_t");
Foam::Info<< "mode_t 0777: " << mode_t(0777) << '\n';
printMax<short>("short");
printMax<int>("int");
printMax<long>("long");
printMax<unsigned short>("ushort");
printMax<unsigned int>("uint");
printMax<unsigned long>("ulong");
printMax<float>("float");
printMax<double>("double");
#endif
std::cout << "\n---\nEnd\n\n";
return 0;

View File

@ -48,10 +48,6 @@ Usage
- \par -triSurface
Use triSurface library for input/output
- \par -keyed
Use keyedSurface for input/output
Note
The filename extensions are used to determine the file format type.
@ -64,7 +60,6 @@ Note
#include "surfMesh.H"
#include "surfFields.H"
#include "surfPointFields.H"
#include "PackedBoolList.H"
#include "MeshedSurfaces.H"
#include "ModifiableMeshedSurface.H"

View File

@ -406,13 +406,13 @@ void testPointSync(const polyMesh& mesh, Random& rndGen)
{
labelList nMasters(mesh.nPoints(), 0);
PackedBoolList isMasterPoint(syncTools::getMasterPoints(mesh));
bitSet isMasterPoint(syncTools::getMasterPoints(mesh));
forAll(isMasterPoint, pointi)
{
if (isMasterPoint[pointi])
if (isMasterPoint.test(pointi))
{
nMasters[pointi] = 1;
nMasters.set(pointi);
}
}
@ -482,13 +482,13 @@ void testEdgeSync(const polyMesh& mesh, Random& rndGen)
{
labelList nMasters(edges.size(), 0);
PackedBoolList isMasterEdge(syncTools::getMasterEdges(mesh));
bitSet isMasterEdge(syncTools::getMasterEdges(mesh));
forAll(isMasterEdge, edgeI)
{
if (isMasterEdge[edgeI])
if (isMasterEdge.test(edgeI))
{
nMasters[edgeI] = 1;
nMasters.set(edgeI);
}
}
@ -551,13 +551,13 @@ void testFaceSync(const polyMesh& mesh, Random& rndGen)
{
labelList nMasters(mesh.nFaces(), 0);
PackedBoolList isMasterFace(syncTools::getMasterFaces(mesh));
Bitset isMasterFace(syncTools::getMasterFaces(mesh));
forAll(isMasterFace, facei)
{
if (isMasterFace[facei])
if (isMasterFace.test(facei))
{
nMasters[facei] = 1;
nMasters.set(facei);
}
}