Commit 6a5ea9a2 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve HashSet construction and assignment

- make construct from UList explicit and provide corresponding
  assignment operator.

- add construct,insert,set,assignment from FixedList.
  This is convenient when dealing with things like edges or triFaces.
parent a2ddf7dd
......@@ -128,6 +128,9 @@ int main(int argc, char *argv[])
1, 11, 42
};
setB = FixedList<label, 4>({1, 2, 3, 4});
setB = {1, 2, 4};
setB = List<label>({1, 2, 4});
Info<< "setB : " << setB << endl;
labelPair pair(12, 15);
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -27,6 +27,52 @@ License
#define HashSet_C
#include "HashSet.H"
#include "FixedList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Key, class Hash>
template<class InputIter>
inline Foam::label Foam::HashSet<Key, Hash>::insertMultiple
(
const InputIter begIter,
const InputIter endIter
)
{
label changed = 0;
for (InputIter iter = begIter; iter != endIter; ++iter)
{
if (insert(*iter))
{
++changed;
}
}
return changed;
}
template<class Key, class Hash>
template<class InputIter>
inline Foam::label Foam::HashSet<Key, Hash>::assignMultiple
(
const InputIter begIter,
const InputIter endIter,
const label sz
)
{
if (!this->capacity())
{
// Could be zero-sized from a previous transfer()?
this->resize(sz);
}
else
{
this->clear();
}
return insertMultiple(begIter, endIter);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -35,9 +81,22 @@ Foam::HashSet<Key, Hash>::HashSet(const UList<Key>& lst)
:
HashTable<nil, Key, Hash>(2*lst.size())
{
forAll(lst, elemI)
for (const auto& k : lst)
{
this->insert(k);
}
}
template<class Key, class Hash>
template<unsigned Size>
Foam::HashSet<Key, Hash>::HashSet(const FixedList<Key, Size>& lst)
:
HashTable<nil, Key, Hash>(2*lst.size())
{
for (const auto& k : lst)
{
this->insert(lst[elemI]);
this->insert(k);
}
}
......@@ -47,7 +106,7 @@ Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> lst)
:
HashTable<nil, Key, Hash>(2*lst.size())
{
for (const Key& k : lst)
for (const auto& k : lst)
{
this->insert(k);
}
......@@ -81,36 +140,47 @@ Foam::HashSet<Key, Hash>::HashSet
template<class Key, class Hash>
Foam::label Foam::HashSet<Key, Hash>::insert(const UList<Key>& lst)
{
label count = 0;
forAll(lst, elemI)
{
if (this->insert(lst[elemI]))
{
++count;
}
}
return insertMultiple(lst.begin(), lst.end());
}
return count;
template<class Key, class Hash>
template<unsigned Size>
Foam::label Foam::HashSet<Key, Hash>::insert(const FixedList<Key, Size>& lst)
{
return insertMultiple(lst.begin(), lst.end());
}
template<class Key, class Hash>
Foam::label Foam::HashSet<Key, Hash>::insert(std::initializer_list<Key> lst)
{
label count = 0;
for (const Key& k : lst)
{
if (this->insert(k))
{
++count;
}
}
return count;
return insertMultiple(lst.begin(), lst.end());
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& lst)
{
assignMultiple(lst.begin(), lst.end(), lst.size());
}
template<class Key, class Hash>
template<unsigned Size>
void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& lst)
{
assignMultiple(lst.begin(), lst.end(), lst.size());
}
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> lst)
{
assignMultiple(lst.begin(), lst.end(), lst.size());
}
template<class Key, class Hash>
inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
{
......
......@@ -61,6 +61,25 @@ class HashSet
:
public HashTable<nil, Key, Hash>
{
// Private Member Functions
//- Insert values, using begin/end iterators.
template<class InputIter>
inline label insertMultiple
(
const InputIter begIter,
const InputIter endIter
);
//- Assign using begin/end iterators.
template<class InputIter>
inline label assignMultiple
(
const InputIter begIter,
const InputIter endIter,
const label sz
);
public:
......@@ -83,7 +102,11 @@ public:
{}
//- Construct from UList of Key
HashSet(const UList<Key>& lst);
explicit HashSet(const UList<Key>& lst);
//- Construct from FixedList of Key
template<unsigned Size>
explicit HashSet(const FixedList<Key, Size>& lst);
//- Construct from an initializer list of Key
HashSet(std::initializer_list<Key> lst);
......@@ -109,7 +132,7 @@ public:
//- Construct from the keys of another HashTable,
// the type of values held is arbitrary.
template<class AnyType, class AnyHash>
HashSet(const HashTable<AnyType, Key, AnyHash>& h);
explicit HashSet(const HashTable<AnyType, Key, AnyHash>& h);
// Member Functions
......@@ -126,6 +149,11 @@ public:
// Return the number of new elements inserted
label insert(const UList<Key>& lst);
//- Insert keys from the list of Key
// Return the number of new elements inserted
template<unsigned Size>
label insert(const FixedList<Key, Size>& lst);
//- Insert keys from a initializer list of Key
// Return the number of new elements inserted
label insert(std::initializer_list<Key> lst);
......@@ -142,6 +170,13 @@ public:
return insert(lst);
}
//- Same as insert (cannot overwrite nil content)
template<unsigned Size>
label set(const FixedList<Key, Size>& lst)
{
return insert(lst);
}
//- Same as insert (cannot overwrite nil content)
label set(std::initializer_list<Key> lst)
{
......@@ -187,6 +222,17 @@ public:
bool operator!=(const HashSet<Key, Hash>& rhs) const;
//- Assignment from a UList of keys
void operator=(const UList<Key>& lst);
//- Assignment from a FixedList of keys
template<unsigned Size>
void operator=(const FixedList<Key, Size>& lst);
//- Assignment from an initializer list of keys
void operator=(std::initializer_list<Key> lst);
//- Combine entries from HashSets
void operator|=(const HashSet<Key, Hash>& rhs);
......
......@@ -1384,6 +1384,23 @@ void Foam::fvMeshSubset::setLargeCellSubset
}
void Foam::fvMeshSubset::setLargeCellSubset
(
const UList<label>& globalCellMap,
const label patchID,
const bool syncPar
)
{
labelList region(baseMesh().nCells(), 0);
for (auto cellId : globalCellMap)
{
region[cellId] = 1;
}
setLargeCellSubset(region, 1, patchID, syncPar);
}
void Foam::fvMeshSubset::setLargeCellSubset
(
const labelHashSet& globalCellMap,
......
......@@ -177,6 +177,14 @@ public:
const bool syncCouples = true
);
//- setLargeCellSubset but only marking certain cells
void setLargeCellSubset
(
const UList<label>& globalCellMap,
const label patchID = -1,
const bool syncPar = true
);
//- setLargeCellSubset but with labelHashSet.
void setLargeCellSubset
(
......
......@@ -90,6 +90,22 @@ void Foam::motionSmootherAlgo::checkFld(const pointScalarField& fld)
}
Foam::labelHashSet Foam::motionSmootherAlgo::getPoints
(
const UList<label>& faceLabels
) const
{
labelHashSet usedPoints(mesh_.nPoints()/100);
for (auto faceId : faceLabels)
{
usedPoints.insert(mesh_.faces()[faceId]);
}
return usedPoints;
}
Foam::labelHashSet Foam::motionSmootherAlgo::getPoints
(
const labelHashSet& faceLabels
......@@ -99,12 +115,7 @@ Foam::labelHashSet Foam::motionSmootherAlgo::getPoints
forAllConstIter(labelHashSet, faceLabels, iter)
{
const face& f = mesh_.faces()[iter.key()];
forAll(f, fp)
{
usedPoints.insert(f[fp]);
}
usedPoints.insert(mesh_.faces()[iter.key()]);
}
return usedPoints;
......
......@@ -209,7 +209,10 @@ class motionSmootherAlgo
static void checkFld(const pointScalarField&);
//- Get points used by given faces
labelHashSet getPoints(const labelHashSet&) const;
labelHashSet getPoints(const UList<label>& faceLabels) const;
//- Get points used by given faces
labelHashSet getPoints(const labelHashSet& faceLabels) const;
//- Calculate per-edge weight
tmp<scalarField> calcEdgeWeights(const pointField&) const;
......
......@@ -42,7 +42,7 @@ defineTypeNameAndDebug(edgeCollapser, 0);
}
Foam::HashSet<Foam::label> Foam::edgeCollapser::checkBadFaces
Foam::labelHashSet Foam::edgeCollapser::checkBadFaces
(
const polyMesh& mesh,
const dictionary& meshQualityDict
......
......@@ -263,7 +263,7 @@ public:
// Check
//- Calls motionSmoother::checkMesh and returns a set of bad faces
static HashSet<label> checkBadFaces
static labelHashSet checkBadFaces
(
const polyMesh& mesh,
const dictionary& meshQualityDict
......
......@@ -178,7 +178,7 @@ bool Foam::functionObjects::ddt2::execute()
{
results_.clear();
wordHashSet candidates = subsetStrings(selectFields_, mesh_.names());
wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
DynamicList<word> missing(selectFields_.size());
DynamicList<word> ignored(selectFields_.size());
......
......@@ -135,7 +135,7 @@ bool Foam::functionObjects::zeroGradient::execute()
{
results_.clear();
wordHashSet candidates = subsetStrings(selectFields_, mesh_.names());
wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
DynamicList<word> missing(selectFields_.size());
DynamicList<word> ignored(selectFields_.size());
......
......@@ -246,7 +246,7 @@ bool Foam::functionObjects::ensightWrite::write()
ensCase().setTime(t.value(), t.timeIndex());
}
wordHashSet candidates = subsetStrings(selectFields_, mesh_.names());
wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
DynamicList<word> missing(selectFields_.size());
DynamicList<word> ignored(selectFields_.size());
......
......@@ -1352,6 +1352,12 @@ public:
const labelHashSet& set
) const;
// Pick up faces of cells of faces in set.
labelList growFaceCellFace
(
const UList<label>& set
) const;
// Pick up faces of cells of faces in set.
labelList growFaceCellFace
(
......
......@@ -790,37 +790,76 @@ Foam::labelList Foam::meshRefinement::collectFaces
}
// Pick up faces of cells of faces in set.
Foam::labelList Foam::meshRefinement::growFaceCellFace
(
const labelHashSet& set
) const
namespace Foam
{
boolList selected(mesh_.nFaces(), false);
forAllConstIter(faceSet, set, iter)
// Pick up faces of cells of faces in set.
// file-scope
static inline void markGrowFaceCellFace
(
const polyMesh& pMesh,
const label faceI,
boolList& selected
)
{
label faceI = iter.key();
label own = mesh_.faceOwner()[faceI];
const label own = pMesh.faceOwner()[faceI];
const cell& ownFaces = mesh_.cells()[own];
const cell& ownFaces = pMesh.cells()[own];
forAll(ownFaces, ownFaceI)
{
selected[ownFaces[ownFaceI]] = true;
}
if (mesh_.isInternalFace(faceI))
if (pMesh.isInternalFace(faceI))
{
label nbr = mesh_.faceNeighbour()[faceI];
const label nbr = pMesh.faceNeighbour()[faceI];
const cell& nbrFaces = mesh_.cells()[nbr];
const cell& nbrFaces = pMesh.cells()[nbr];
forAll(nbrFaces, nbrFaceI)
{
selected[nbrFaces[nbrFaceI]] = true;
}
}
}
}
// Pick up faces of cells of faces in set.
Foam::labelList Foam::meshRefinement::growFaceCellFace
(
const UList<label>& set
) const
{
boolList selected(mesh_.nFaces(), false);
for (auto faceI : set)
{
markGrowFaceCellFace(mesh_, faceI, selected);
}
syncTools::syncFaceList
(
mesh_,
selected,
orEqOp<bool>() // combine operator
);
return findIndices(selected, true);
}
// Pick up faces of cells of faces in set.
Foam::labelList Foam::meshRefinement::growFaceCellFace
(
const labelHashSet& set
) const
{
boolList selected(mesh_.nFaces(), false);
forAllConstIter(labelHashSet, set, iter)
{
const label faceI = iter.key();
markGrowFaceCellFace(mesh_, faceI, selected);
}
syncTools::syncFaceList
(
mesh_,
......
......@@ -30,29 +30,26 @@ License
#include "addToRunTimeSelectionTable.H"
#include "mapDistributePolyMesh.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(cellSet, 0);
addToRunTimeSelectionTable(topoSet, cellSet, word);
addToRunTimeSelectionTable(topoSet, cellSet, size);
addToRunTimeSelectionTable(topoSet, cellSet, set);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
cellSet::cellSet(const IOobject& obj)
Foam::cellSet::cellSet(const IOobject& obj)
:
topoSet(obj, typeName)
{}
cellSet::cellSet
Foam::cellSet::cellSet
(
const polyMesh& mesh,
const word& name,
......@@ -67,7 +64,7 @@ cellSet::cellSet
}
cellSet::cellSet
Foam::cellSet::cellSet
(
const polyMesh& mesh,
const word& name,
......@@ -79,7 +76,7 @@ cellSet::cellSet
{}
cellSet::cellSet
Foam::cellSet::cellSet
(
const polyMesh& mesh,
const word& name,
......@@ -91,7 +88,7 @@ cellSet::cellSet
{}
cellSet::cellSet
Foam::cellSet::cellSet
(
const polyMesh& mesh,
const word& name,
......@@ -103,8 +100,20 @@ cellSet::cellSet
{}
Foam::cellSet::cellSet
(
const polyMesh& mesh,
const word& name,
const UList<label>& set,
writeOption w
)
:
topoSet(mesh, name, set, w)
{}
// Database constructors (for when no mesh available)
cellSet::cellSet
Foam::cellSet::cellSet
(
const Time& runTime,
const word& name,
......@@ -114,32 +123,13 @@ cellSet::cellSet
:
topoSet
(
IOobject
(
name,
runTime.findInstance
(
polyMesh::meshSubDir/"sets", //polyMesh::meshSubDir,
word::null, //"faces"
IOobject::MUST_READ,