Commit 5d1fb235 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: code reduction in PackedList, PackedBoolList (issue #751)

- eliminate iterators from PackedList since they were unused, had
  lower performance than direct access and added unneeded complexity.

- eliminate auto-vivify for the PackedList '[] operator.
  The set() method provides any required auto-vivification and
  removing this ability from the '[]' operator allows for a lower
  when accessing the values. Replaced the previous cascade of iterators
  with simpler reference class.

PackedBoolList:

- (temporarily) eliminate logic and addition operators since
  these contained partially unclear semantics.

- 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.

- more consistent use of PackedBoolList test(), set(), unset() methods
  for fewer operation and clearer code. Eg,

      if (list.test(index)) ...    |  if (list[index]) ...
      if (!list.test(index)) ...   |  if (list[index] == 0u) ...
      list.set(index);             |  list[index] = 1u;
      list.unset(index);           |  list[index] = 0u;

- deleted the operator=(const labelUList&) and replaced with a setMany()
  method for more clarity about the intended operation and to avoid any
  potential inadvertent behaviour.
parent 23b6ea4b
......@@ -128,14 +128,14 @@ int main(int argc, char *argv[])
if (protectedCell.empty())
{
protectedCell.setSize(mesh.nCells());
protectedCell = 0;
protectedCell = false;
}
forAll(betav, celli)
{
if (betav[celli] < 0.99)
{
protectedCell[celli] = 1;
protectedCell.set(celli);
}
}
......
......@@ -41,7 +41,7 @@ labelList receptorNeigCell(mesh.nInternalFaces(), -1);
&& neiType == cellCellStencil::CALCULATED
)
{
isOwnerInterpolatedFace[faceI] = true;
isOwnerInterpolatedFace.set(faceI);
const vector& fc = mesh.faceCentres()[faceI];
......@@ -106,7 +106,7 @@ labelList receptorNeigCell(mesh.nInternalFaces(), -1);
&& neiType == cellCellStencil::INTERPOLATED
)
{
isNeiInterpolatedFace[faceI] = true;
isNeiInterpolatedFace.set(faceI);
const vector& fc = mesh.faceCentres()[faceI];
for (label zoneI = 0; zoneI < nZones; zoneI++)
......
......@@ -103,30 +103,6 @@ int main(int argc, char *argv[])
Info<< "[0] == [1] (unexpected)\n";
}
Info<< "\ntest operator== with iterator\n";
{
PackedList<3>::iterator iter = list1[1];
if (iter != list1[8])
{
Info<< "iter != [8] (expected)\n";
}
else
{
Info<< "iter == [8] (unexpected)\n";
}
if (*iter != list1[8])
{
Info<< "*iter != [8] (unexpected)\n";
}
else
{
Info<< "*iter == [8] (expected)\n";
}
}
{
const PackedList<3>& constLst = list1;
Info<< "\ntest operator[] const with out-of-range index\n";
......@@ -246,56 +222,10 @@ int main(int argc, char *argv[])
list1[32] = 2;
list1[33] = 3;
Info<< "\ntest iterator\n";
PackedList<3>::iterator iter = list1.begin();
Info<< "begin():";
iter.printInfo(Info) << "\n";
Info<< "iterator:" << *iter << "\n";
*iter = 5;
iter.printInfo(Info);
list1.printInfo(Info, true);
iter = list1[31];
Info<< "iterator:" << *iter << "\n";
iter.printInfo(Info);
Info<< "\ntest get() method\n";
Info<< "get(10):" << list1.get(10) << " and list[10]:" << list1[10] << "\n";
list1.printInfo(Info, true);
Info<< "\ntest iterator indexing\n";
Info<< "cend() ";
list1.cend().printInfo(Info) << "\n";
{
Info<< "\ntest assignment of iterator\n";
list1.printInfo(Info, true);
Info<< "cend()\n";
list1.end().printInfo(Info);
PackedList<3>::iterator cit = list1[100];
Info<< "out-of-range: ";
cit.printInfo(Info);
cit = list1[15];
Info<< "in-range: ";
cit.printInfo(Info);
Info<< "out-of-range: ";
cit = list1[1000];
cit.printInfo(Info);
}
for
(
PackedList<3>::iterator cit = list1[30];
cit != list1.end();
++cit
)
{
cit.printInfo(Info);
}
Info<< "\ntest operator[] auto-vivify\n";
Info<< "size:" << list1.size() << "\n";
......
......@@ -225,36 +225,6 @@ int main(int argc, char *argv[])
<< " sum " << sum << nl;
// Read via iterator
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAllIters(packed, it)
{
sum += it;
}
}
std::cout
<< "Reading packed using iterator:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read via iterator
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAllConstIters(packed, cit)
{
sum += *cit;
}
}
std::cout
<< "Reading packed using const_iterator():" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read empty hash
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
......@@ -367,18 +337,6 @@ int main(int argc, char *argv[])
Info<< "Writing packed using set:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
forAllIters(packed, it)
{
*it = 1;
}
}
Info<< "Writing packed using iterator:" << timer.cpuTimeIncrement()
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
{
......
......@@ -53,9 +53,10 @@ int main(int argc, char *argv[])
Info<< "\nalternating bit pattern\n";
list1.printInfo(Info, true);
PackedBoolList list2 = ~list1;
PackedBoolList list2(list1);
list2.flip();
Info<< "\ncomplementary bit pattern\n";
Info<< "\nflipped bit pattern\n";
list2.printBits(Info);
// set every other on
......@@ -80,62 +81,6 @@ int main(int argc, char *argv[])
labelList list2Labels = list2.used();
Info<< "\noperator|\n";
list1.printBits(Info);
list2.printBits(Info);
Info<< "==\n";
(list1 | list2).printBits(Info);
Info<< "\noperator& : does trim\n";
(list1 & list2).printBits(Info);
Info<< "\noperator^\n";
(list1 ^ list2).printBits(Info);
Info<< "\noperator|=\n";
{
PackedBoolList list3 = list1;
(list3 |= list2).printBits(Info);
}
Info<< "\noperator|= with labelUList\n";
{
PackedBoolList list3 = list1;
(list3 |= list2Labels).printBits(Info);
}
Info<< "\noperator&=\n";
{
PackedBoolList list3 = list1;
(list3 &= list2).printBits(Info);
}
Info<< "\noperator+=\n";
{
PackedBoolList list3 = list1;
(list3 += list2).printBits(Info);
}
Info<< "\noperator+= with labelUList\n";
{
PackedBoolList list3 = list1;
(list3 += list2Labels).printBits(Info);
}
Info<< "\noperator-=\n";
{
PackedBoolList list3 = list1;
(list3 -= list2).printBits(Info);
}
Info<< "\noperator-= with labelUList\n";
{
PackedBoolList list3 = list1;
(list3 -= list2Labels).printBits(Info);
}
PackedBoolList list4
(
ITstream
......@@ -151,7 +96,8 @@ int main(int argc, char *argv[])
Info<< list4 << " indices: " << list4.used() << nl;
Info<< "\nassign from labelList\n";
list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
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;
......
......@@ -84,7 +84,7 @@ void modifyOrAddFace
PackedBoolList& modifiedFace
)
{
if (!modifiedFace[facei])
if (modifiedFace.set(facei))
{
// First usage of face. Modify.
meshMod.setAction
......@@ -102,7 +102,6 @@ void modifyOrAddFace
zoneFlip // face flip in zone
)
);
modifiedFace[facei] = 1;
}
else
{
......@@ -342,7 +341,7 @@ void subsetTopoSets
PackedBoolList isSet(set.maxSize(mesh));
forAllConstIter(labelHashSet, set, iter)
{
isSet[iter.key()] = true;
isSet.set(iter.key());
}
label nSet = 0;
forAll(map, i)
......@@ -375,7 +374,7 @@ void createCoupledBaffles
fvMesh& mesh,
const labelList& coupledWantedPatch,
polyTopoChange& meshMod,
PackedBoolList& modifiedFace
PackedBoolList& modifiedFace
)
{
const faceZoneMesh& faceZones = mesh.faceZones();
......@@ -443,7 +442,7 @@ void createCyclicCoupledBaffles
const labelList& cyclicMasterPatch,
const labelList& cyclicSlavePatch,
polyTopoChange& meshMod,
PackedBoolList& modifiedFace
PackedBoolList& modifiedFace
)
{
const faceZoneMesh& faceZones = mesh.faceZones();
......
......@@ -588,7 +588,7 @@ int main(int argc, char *argv[])
label edgeI = iter.key();
const edge& e = edges[edgeI];
collapseEdge[edgeI] = true;
collapseEdge.set(edgeI);
collapsePointToLocation.set(e[1], points[e[0]]);
newPoints[e[0]] = iter();
......
......@@ -907,7 +907,7 @@ int main(int argc, char *argv[])
Info<< "Merging edge " << e << " since length " << d
<< " << " << mergeDim << nl;
collapseEdge[edgeI] = true;
collapseEdge.set(edgeI);
collapsePointToLocation.set(e[1], points[e[0]]);
}
}
......
......@@ -2190,12 +2190,12 @@ int main(int argc, char *argv[])
// and generate space overlapping columns of cells.
if (eFaces.size() != 2)
{
nonManifoldEdge[edgeI] = 1;
nonManifoldEdge.set(edgeI);
}
}
else
{
nonManifoldEdge[edgeI] = 1;
nonManifoldEdge.set(edgeI);
}
}
else if (eFaces.size() == 2)
......@@ -2222,7 +2222,7 @@ int main(int argc, char *argv[])
ePatches[1] = zoneZonePatch_min[index];
}
nonManifoldEdge[edgeI] = 1;
nonManifoldEdge.set(edgeI);
}
}
else if (sidePatchID[edgeI] != -1)
......@@ -2260,7 +2260,7 @@ int main(int argc, char *argv[])
ePatches[i] = zoneSidePatch[zoneID[eFaces[i]]];
}
}
nonManifoldEdge[edgeI] = 1;
nonManifoldEdge.set(edgeI);
}
}
......
......@@ -270,7 +270,7 @@ int main(int argc, char *argv[])
Info<< "Merging edge " << e << " since length " << d
<< " << " << mergeDim << nl;
collapseEdge[edgeI] = true;
collapseEdge.set(edgeI);
collapsePointToLocation.set(e[1], points[e[0]]);
}
}
......
......@@ -341,7 +341,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
if (!keep)
{
keepVertex[vI] = false;
keepVertex.unset(vI);
}
}
......@@ -516,7 +516,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
if (!keep)
{
keepVertex[vI] = false;
keepVertex.unset(vI);
}
}
......
......@@ -1081,11 +1081,7 @@ void Foam::conformalVoronoiMesh::move()
Zero
);
PackedBoolList pointToBeRetained
(
number_of_vertices(),
true
);
PackedBoolList pointToBeRetained(number_of_vertices(), true);
DynamicList<Point> pointsToInsert(number_of_vertices());
......@@ -1170,8 +1166,8 @@ void Foam::conformalVoronoiMesh::move()
if
(
pointToBeRetained[vA->index()] == true
&& pointToBeRetained[vB->index()] == true
pointToBeRetained.test(vA->index())
&& pointToBeRetained.test(vB->index())
)
{
const Foam::point pt(0.5*(dVA + dVB));
......@@ -1185,12 +1181,12 @@ void Foam::conformalVoronoiMesh::move()
if (vA->internalPoint() && !vA->referred() && !vA->fixed())
{
pointToBeRetained[vA->index()] = false;
pointToBeRetained.unset(vA->index());
}
if (vB->internalPoint() && !vB->referred() && !vB->fixed())
{
pointToBeRetained[vB->index()] = false;
pointToBeRetained.unset(vB->index());
}
// Do not consider this Delaunay edge any further
......@@ -1368,8 +1364,8 @@ void Foam::conformalVoronoiMesh::move()
// removed.
if
(
pointToBeRetained[vA->index()] == true
&& pointToBeRetained[vB->index()] == true
pointToBeRetained.test(vA->index())
&& pointToBeRetained.test(vB->index())
)
{
const Foam::point pt(0.5*(dVA + dVB));
......@@ -1388,7 +1384,7 @@ void Foam::conformalVoronoiMesh::move()
&& !vA->fixed()
)
{
pointToBeRetained[vA->index()] = false;
pointToBeRetained.unset(vA->index());
}
if
......@@ -1398,7 +1394,7 @@ void Foam::conformalVoronoiMesh::move()
&& !vB->fixed()
)
{
pointToBeRetained[vB->index()] = false;
pointToBeRetained.unset(vB->index());
}
}
else
......@@ -1454,7 +1450,7 @@ void Foam::conformalVoronoiMesh::move()
{
if (vit->internalPoint() && !vit->referred() && !vit->fixed())
{
if (pointToBeRetained[vit->index()] == true)
if (pointToBeRetained.test(vit->index()))
{
limitDisplacement
(
......@@ -1484,7 +1480,7 @@ void Foam::conformalVoronoiMesh::move()
{
if (vit->internalPoint() && !vit->referred() && !vit->fixed())
{
if (pointToBeRetained[vit->index()] == true)
if (pointToBeRetained.test(vit->index()))
{
// Convert vit->point() to FOAM vector (double) to do addition,
// avoids memory increase because a record of the constructions
......@@ -1540,7 +1536,7 @@ void Foam::conformalVoronoiMesh::move()
{
if (vit->internalPoint() && !vit->referred())
{
if (pointToBeRetained[vit->index()] == true)
if (pointToBeRetained.test(vit->index()))
{
meshTools::writeOBJ(str, topoint(vit->point()));
......
......@@ -1116,10 +1116,7 @@ Foam::labelHashSet Foam::conformalVoronoiMesh::checkPolyMeshQuality
{
const face f = pMesh.faces()[iter.key()];
forAll(f, fPtI)
{
ptToBeLimited[f[fPtI]] = true;
}
ptToBeLimited.setMany(f);
}
// // Limit connected cells
......@@ -1153,10 +1150,7 @@ Foam::labelHashSet Foam::conformalVoronoiMesh::checkPolyMeshQuality
// const labelList& cP = cellPts[celli];
// forAll(cP, cPI)
// {
// ptToBeLimited[cP[cPI]] = true;
// }
// ptToBeLimited.setMany(cP);
// }
......@@ -1176,7 +1170,7 @@ Foam::labelHashSet Foam::conformalVoronoiMesh::checkPolyMeshQuality
if (cI >= 0)
{
if (ptToBeLimited[cI] == true)
if (ptToBeLimited.test(cI))
{
cit->filterCount()++;
}
......@@ -2557,10 +2551,7 @@ void Foam::conformalVoronoiMesh::removeUnusedPoints
{
const face& f = faces[fI];
forAll(f, fPtI)
{
ptUsed[f[fPtI]] = true;
}
ptUsed.setMany(f);
}
label pointi = 0;
......@@ -2572,7 +2563,7 @@ void Foam::conformalVoronoiMesh::removeUnusedPoints
forAll(ptUsed, ptUI)
{
if (ptUsed[ptUI] == true)
if (ptUsed.test(ptUI))
{
oldToNew[ptUI] = pointi++;
}
......@@ -2610,15 +2601,8 @@ Foam::labelList Foam::conformalVoronoiMesh::removeUnusedCells
// Scan all faces to find all of the cells that are used
forAll(owner, oI)
{
cellUsed[owner[oI]] = true;
}
forAll(neighbour, nI)
{
cellUsed[neighbour[nI]] = true;
}
cellUsed.setMany(owner);
cellUsed.setMany(neighbour);
label celli = 0;
......@@ -2629,7 +2613,7 @@ Foam::labelList Foam::conformalVoronoiMesh::removeUnusedCells
forAll(cellUsed, cellUI)
{
if (cellUsed[cellUI] == true)
if (cellUsed.test(cellUI))
{
oldToNew[cellUI] = celli++;
}
......@@ -2645,7 +2629,7 @@ Foam::labelList Foam::conformalVoronoiMesh::removeUnusedCells
forAll(cellUsed, cUI)
{
if (cellUsed[cUI] == false)
if (!cellUsed.test(cUI))