ENH: consistent naming for ZoneMesh lookup method

- findZone(), cfindZone() to return pointer to existing or nullptr if
  not found. This fits with methods such as findObject() etc for other
  classes and can simplify code without checks for '-1' as not found.

- use simpler constructors for empty cell/face/point zones
This commit is contained in:
Mark Olesen
2020-11-20 17:24:55 +01:00
parent be7a3f21be
commit 501cd35351
2 changed files with 76 additions and 103 deletions

View File

@ -487,39 +487,68 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findZoneID
{ {
label zoneId = findIndexImpl(*this, zoneName); label zoneId = findIndexImpl(*this, zoneName);
if (zoneId >= 0) if (zoneId < 0)
{ {
return zoneId;
}
// Zone not found
DebugInFunction DebugInFunction
<< "Zone named " << zoneName << " not found. " << "Zone named " << zoneName << " not found. "
<< "List of available zone names: " << names() << endl; << "List of available zone names: " << names() << endl;
// Used for -dry-run, for example
if (disallowGenericZones != 0) if (disallowGenericZones != 0)
{ {
// Create a new ...
Info<< "Creating dummy zone " << zoneName << endl;
dictionary dict;
dict.set("type", ZoneType::typeName);
dict.set(ZoneType::labelsName, labelList());
// flipMap only really applicable for face zones, but should not get
// in the way for cell- and point-zones...
dict.set("flipMap", boolList());
auto& zm = const_cast<ZoneMesh<ZoneType, MeshType>&>(*this); auto& zm = const_cast<ZoneMesh<ZoneType, MeshType>&>(*this);
zoneId = zm.size(); zoneId = zm.size();
zm.append(new ZoneType(zoneName, dict, zoneId, zm)); Info<< "Creating dummy zone " << zoneName << endl;
zm.append(new ZoneType(zoneName, zoneId, zm));
}
} }
return zoneId; return zoneId;
} }
template<class ZoneType, class MeshType>
const ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::cfindZone
(
const word& zoneName
) const
{
const PtrList<ZoneType>& zones = *this;
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
const ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
// Used for -dry-run, for example
if (disallowGenericZones != 0)
{
auto& zm = const_cast<ZoneMesh<ZoneType, MeshType>&>(*this);
Info<< "Creating dummy zone " << zoneName << endl;
zm.append(new ZoneType(zoneName, zm.size(), zm));
}
return nullptr;
}
template<class ZoneType, class MeshType>
ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::findZone
(
const word& zoneName
)
{
return const_cast<ZoneType*>(this->cfindZone(zoneName));
}
template<class ZoneType, class MeshType> template<class ZoneType, class MeshType>
Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::selection Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::selection
( (
@ -570,50 +599,6 @@ Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::selection
} }
template<class ZoneType, class MeshType>
const ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
(
const word& zoneName
) const
{
const PtrList<ZoneType>& zones = *this;
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
const ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
return nullptr;
}
template<class ZoneType, class MeshType>
ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
(
const word& zoneName
)
{
PtrList<ZoneType>& zones = *this;
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
return nullptr;
}
template<class ZoneType, class MeshType> template<class ZoneType, class MeshType>
void Foam::ZoneMesh<ZoneType, MeshType>::clearAddressing() void Foam::ZoneMesh<ZoneType, MeshType>::clearAddressing()
{ {
@ -801,34 +786,25 @@ ZoneType& Foam::ZoneMesh<ZoneType, MeshType>::operator()
const bool verbose const bool verbose
) )
{ {
PtrList<ZoneType>& zones = *this; ZoneType* ptr = findZone(zoneName);
label zoneId = findZoneID(zoneName); const bool existing = bool(ptr);
if (zoneId < 0) if (!ptr)
{ {
zoneId = zones.size(); ptr = new ZoneType(zoneName, this->size(), *this);
zones.resize(zoneId+1); this->append(ptr);
zones.set(zoneId, new ZoneType(zoneName, zoneId, *this)); }
if (verbose) if (verbose)
{ {
Info<< ZoneType::typeName << " " << zoneName Info<< ZoneType::typeName << ' ' << zoneName
<< " (new at index " << zoneId << ")" << " (" << (existing ? "existing" : "new")
<< " at index " << ptr->index() << ')'
<< endl; << endl;
} }
}
else
{
if (verbose)
{
Info<< ZoneType::typeName << " " << zoneName
<< " (existing at index " << zoneId << ")"
<< endl;
}
}
return zones[zoneId]; return *ptr;
} }

View File

@ -198,17 +198,21 @@ public:
//- Return zone indices for all matches //- Return zone indices for all matches
labelList indices(const wordRes& matcher) const; labelList indices(const wordRes& matcher) const;
//- Zone index for the first match, return -1 if not found
//- Return zone index for the first match, return -1 if not found
label findIndex(const keyType& key) const; label findIndex(const keyType& key) const;
//- Return zone index for the first match, return -1 if not found //- Zone index for the first match, return -1 if not found
label findIndex(const wordRes& matcher) const; label findIndex(const wordRes& matcher) const;
//- Find zone index by name, return -1 if not found
//- Find zone index given a name, return -1 if not found
label findZoneID(const word& zoneName) const; label findZoneID(const word& zoneName) const;
//- Find zone by name and return const pointer, nullptr on error
const ZoneType* cfindZone(const word& zoneName) const;
//- Find zone by name and return pointer, nullptr on error
ZoneType* findZone(const word& zoneName);
//- Return all elements (cells, faces, points) contained in the //- Return all elements (cells, faces, points) contained in the
//- listed zones. //- listed zones.
@ -229,13 +233,6 @@ public:
bitSet selection(const wordRes& matcher) const; bitSet selection(const wordRes& matcher) const;
//- Lookup zone by name and return const pointer, nullptr on error.
const ZoneType* zonePtr(const word& zoneName) const;
//- Lookup zone by name and return pointer, nullptr on error.
ZoneType* zonePtr(const word& zoneName);
//- Clear addressing //- Clear addressing
void clearAddressing(); void clearAddressing();