ENH: additional Map/HashTable constructors and ListOp functions

- construct Map/HashTable from key/value lists.

- invertToMap() : like invert() but returns a Map<label>,
  which is useful for sparse numbering

- inplaceRenumber() : taking a Map<label> for the mapper

ENH: construct/reset CStringList for list of C-strings
This commit is contained in:
Mark Olesen
2024-02-21 09:34:58 +01:00
parent f7cdd3ef63
commit 4f43f0302d
37 changed files with 363 additions and 298 deletions

View File

@ -124,11 +124,11 @@ int main(int argc, char *argv[])
<< "toc: " << flatOutput(table0.toc()) << nl;
HashTable<label, label, Hash<label>> table2
({
{3, 10},
{5, 12},
{7, 16}
});
(
// From key/value pairs
labelList({3, 5, 7}),
labelList({10, 12, 16})
);
Info<< "table2: " << table2 << nl
<< "toc: " << flatOutput(table2.toc()) << nl;

View File

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

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenCFD Ltd.
Copyright (C) 2016-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -81,8 +81,7 @@ int print(char *argv[])
int main(int argc, char *argv[])
{
DynamicList<string> dynlst;
dynlst.reserve(16);
DynamicList<string> dynlst(16);
dynlst.push_back("string1 with content");
dynlst.push_back("string2 other content");
@ -104,6 +103,18 @@ int main(int argc, char *argv[])
Info<< nl;
}
{
CStringList inC({ "string1", "string2", "string3", "end" });
Info<< "null-terminated string list from " << nl;
print(inC.strings());
Info<< "sublist: starting at " << inC.size()/2 << nl;
print(inC.strings(inC.size()/2));
Info<< nl;
}
{
string testInput
(
@ -124,7 +135,7 @@ int main(int argc, char *argv[])
Info<< nl;
}
Info<<"command-line with " << CStringList::count(argv) << " items"<< endl;
Info<< "command-line with " << CStringList::count(argv) << " items" << nl;
print(argc, argv);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020-2023 OpenCFD Ltd.
Copyright (C) 2020-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -98,19 +98,6 @@ bool skipSection(IFstream& inFile)
}
void renumber
(
const Map<label>& mshToFoam,
labelList& labels
)
{
forAll(labels, labelI)
{
labels[labelI] = mshToFoam[labels[labelI]];
}
}
// Find face in pp which uses all vertices in meshF (in mesh point labels)
label findFace(const primitivePatch& pp, const labelList& meshF)
{
@ -587,7 +574,7 @@ void readCellsLegacy
{
lineStr >> triPoints[0] >> triPoints[1] >> triPoints[2];
renumber(mshToFoam, triPoints);
inplaceRenumber(mshToFoam, triPoints);
const auto regFnd = physToPatch.cfind(regPhys);
@ -620,7 +607,7 @@ void readCellsLegacy
>> quadPoints[0] >> quadPoints[1] >> quadPoints[2]
>> quadPoints[3];
renumber(mshToFoam, quadPoints);
inplaceRenumber(mshToFoam, quadPoints);
const auto regFnd = physToPatch.cfind(regPhys);
@ -662,7 +649,7 @@ void readCellsLegacy
>> tetPoints[0] >> tetPoints[1] >> tetPoints[2]
>> tetPoints[3];
renumber(mshToFoam, tetPoints);
inplaceRenumber(mshToFoam, tetPoints);
cells[celli++].reset(tet, tetPoints);
@ -683,7 +670,7 @@ void readCellsLegacy
>> pyrPoints[0] >> pyrPoints[1] >> pyrPoints[2]
>> pyrPoints[3] >> pyrPoints[4];
renumber(mshToFoam, pyrPoints);
inplaceRenumber(mshToFoam, pyrPoints);
cells[celli++].reset(pyr, pyrPoints);
@ -704,7 +691,7 @@ void readCellsLegacy
>> prismPoints[0] >> prismPoints[1] >> prismPoints[2]
>> prismPoints[3] >> prismPoints[4] >> prismPoints[5];
renumber(mshToFoam, prismPoints);
inplaceRenumber(mshToFoam, prismPoints);
cells[celli].reset(prism, prismPoints);
@ -745,7 +732,7 @@ void readCellsLegacy
>> hexPoints[4] >> hexPoints[5]
>> hexPoints[6] >> hexPoints[7];
renumber(mshToFoam, hexPoints);
inplaceRenumber(mshToFoam, hexPoints);
cells[celli].reset(hex, hexPoints);
@ -929,7 +916,7 @@ void readCells
IStringStream lineStr(line);
lineStr >> elemID >> triPoints[0] >> triPoints[1] >> triPoints[2];
renumber(mshToFoam, triPoints);
inplaceRenumber(mshToFoam, triPoints);
const auto regFnd = physToPatch.cfind(regPhys);
@ -967,7 +954,7 @@ void readCells
>> quadPoints[0] >> quadPoints[1] >> quadPoints[2]
>> quadPoints[3];
renumber(mshToFoam, quadPoints);
inplaceRenumber(mshToFoam, quadPoints);
const auto regFnd = physToPatch.cfind(regPhys);
@ -1017,7 +1004,7 @@ void readCells
>> tetPoints[0] >> tetPoints[1] >> tetPoints[2]
>> tetPoints[3];
renumber(mshToFoam, tetPoints);
inplaceRenumber(mshToFoam, tetPoints);
cells[celli++].reset(tet, tetPoints);
}
@ -1044,7 +1031,7 @@ void readCells
>> pyrPoints[0] >> pyrPoints[1] >> pyrPoints[2]
>> pyrPoints[3] >> pyrPoints[4];
renumber(mshToFoam, pyrPoints);
inplaceRenumber(mshToFoam, pyrPoints);
cells[celli++].reset(pyr, pyrPoints);
}
@ -1071,7 +1058,7 @@ void readCells
>> prismPoints[0] >> prismPoints[1] >> prismPoints[2]
>> prismPoints[3] >> prismPoints[4] >> prismPoints[5];
renumber(mshToFoam, prismPoints);
inplaceRenumber(mshToFoam, prismPoints);
cells[celli].reset(prism, prismPoints);
@ -1118,7 +1105,7 @@ void readCells
>> hexPoints[4] >> hexPoints[5]
>> hexPoints[6] >> hexPoints[7];
renumber(mshToFoam, hexPoints);
inplaceRenumber(mshToFoam, hexPoints);
cells[celli].reset(hex, hexPoints);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -219,14 +219,14 @@ void readPoints
<< endl;
}
point pt;
is.getLine(line);
pt[0] = readUnvScalar(line.substr(0, 25));
pt[1] = readUnvScalar(line.substr(25, 25));
pt[2] = readUnvScalar(line.substr(50, 25));
unvPointID.append(pointi);
points.append(pt);
unvPointID.push_back(pointi);
point& p = points.emplace_back();
p.x() = readUnvScalar(line.substr(0, 25));
p.y() = readUnvScalar(line.substr(25, 25));
p.z() = readUnvScalar(line.substr(50, 25));
}
points.shrink();
@ -847,7 +847,7 @@ int main(int argc, char *argv[])
labelList own(boundaryFaces.size(), -1);
labelList nei(boundaryFaces.size(), -1);
Map<label> faceToCell[2];
Pair<Map<label>> faceToCell;
{
// Can use face::symmHasher or use sorted indices instead
@ -996,12 +996,7 @@ int main(int argc, char *argv[])
labelHashSet alreadyOnBoundary;
// Construct map from boundaryFaceIndices
Map<label> boundaryFaceToIndex(boundaryFaceIndices.size());
forAll(boundaryFaceIndices, i)
{
boundaryFaceToIndex.insert(boundaryFaceIndices[i], i);
}
Map<label> boundaryFaceToIndex(invertToMap(boundaryFaceIndices));
forAll(patchFaceVerts, patchi)
{
@ -1216,20 +1211,13 @@ int main(int argc, char *argv[])
{
const label old = oldIndizes[i];
label noveau = -1;
label c1 = -1, c2 = -1;
if (faceToCell[0].found(old))
{
c1 = faceToCell[0][old];
}
if (faceToCell[1].found(old))
{
c2 = faceToCell[1][old];
}
label c1 = faceToCell[0].lookup(old, -1);
label c2 = faceToCell[1].lookup(old, -1);
if (c1 < c2)
{
label tmp = c1;
c1 = c2;
c2 = tmp;
std::swap(c1, c2);
}
if (c2 == -1)
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -197,7 +197,7 @@ int main(int argc, char *argv[])
//
pointField points(nNodes);
Map<label> nodeToPoint(nNodes);
Map<label> nodeToPoint(2*nNodes);
{
labelList pointIndex(nNodes);
@ -439,23 +439,16 @@ int main(int argc, char *argv[])
// Get Foam patchID and update region->patch table.
label patchi = 0;
label patchi = regionToPatch.lookup(region, -1);
const auto patchFind = regionToPatch.cfind(region);
if (patchFind.good())
{
patchi = *patchFind;
}
else
if (patchi < 0)
{
patchi = nPatches;
regionToPatch.insert(region, nPatches++);
Info<< "Mapping tetgen region " << region
<< " to patch "
<< patchi << endl;
regionToPatch.insert(region, nPatches++);
}
boundaryPatch[facei] = patchi;

View File

@ -295,9 +295,7 @@ void addToInterface
else
{
// Create new interface of size 1.
Map<label> zoneToSize;
zoneToSize.insert(zoneID, 1);
regionsToSize.insert(interface, zoneToSize);
regionsToSize(interface).insert(zoneID, 1);
}
}
@ -484,18 +482,10 @@ void getInterfaceSizes
zoneName + "_" + name1 + "_to_" + name0
);
}
interfaceSizes[nInterfaces] = infoIter();
if (regionsToInterface.found(e))
{
regionsToInterface[e].insert(zoneID, nInterfaces);
}
else
{
Map<label> zoneAndInterface;
zoneAndInterface.insert(zoneID, nInterfaces);
regionsToInterface.insert(e, zoneAndInterface);
}
interfaceSizes[nInterfaces] = infoIter();
regionsToInterface(e).insert(zoneID, nInterfaces);
nInterfaces++;
}
}