ENH: simplify/extend IOobjectList code with templated predicates

- replace explicit use of word, wordRe, wordRes, wordHashSet as filters
  with a MatchPredicate, since they all satisfy the requirements for
  use a predicate. This change reduces code duplication, allows other
  matcher types (eg, keyType) as well as lambda functions.

- add special treatment for a 'const char*' parameter
  for lookupClass() and the now-deprecated single item lookup() method
  to promote these parameters to 'word'.
This commit is contained in:
Mark Olesen
2018-11-10 12:12:06 +01:00
parent faaa93fdb5
commit 8562e5278f
3 changed files with 221 additions and 254 deletions

View File

@ -58,6 +58,20 @@ bool Foam::IOobjectList::checkNames(wordList& masterNames, const bool syncPar)
}
void Foam::IOobjectList::syncNames(wordList& objNames)
{
if (Pstream::parRun())
{
// Synchronize names
Pstream::combineGather(objNames, ListOps::uniqueEqOp<word>());
Pstream::combineScatter(objNames);
}
// Sort for consistent order on all processors
Foam::sort(objNames);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IOobjectList::IOobjectList()
@ -246,30 +260,10 @@ Foam::IOobject* Foam::IOobjectList::findObject(const word& objName) const
}
Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matchName) const
Foam::IOobjectList Foam::IOobjectList::lookupClass(const char* clsName) const
{
return lookupImpl(*this, matchName);
}
Foam::IOobjectList Foam::IOobjectList::lookup(const wordRes& matchName) const
{
return lookupImpl(*this, matchName);
}
Foam::IOobjectList Foam::IOobjectList::lookup
(
const wordHashSet& matchName
) const
{
return lookupImpl(*this, matchName);
}
Foam::IOobjectList Foam::IOobjectList::lookupClass(const word& clsName) const
{
return lookupClassImpl(*this, clsName, predicates::always());
// No nullptr check - only called with string literals
return lookupClass(static_cast<word>(clsName));
}
@ -279,39 +273,12 @@ Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classes() const
}
Foam::HashTable<Foam::wordHashSet>
Foam::IOobjectList::classes(const wordRe& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::HashTable<Foam::wordHashSet>
Foam::IOobjectList::classes(const wordRes& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::HashTable<Foam::wordHashSet>
Foam::IOobjectList::classes(const wordHashSet& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::wordList Foam::IOobjectList::names() const
{
return HashPtrTable<IOobject>::toc();
}
Foam::wordList Foam::IOobjectList::sortedNames() const
{
return HashPtrTable<IOobject>::sortedToc();
}
Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
{
wordList objNames(HashPtrTable<IOobject>::toc());
@ -326,7 +293,37 @@ Foam::wordList Foam::IOobjectList::names
const word& clsName
) const
{
return namesImpl(*this, clsName, predicates::always(), false);
// sort/sync: false, false
return namesImpl(*this, clsName, predicates::always(), false, false);
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const bool syncPar
) const
{
// sort: false
return namesImpl(*this, clsName, predicates::always(), false, syncPar);
}
Foam::wordList Foam::IOobjectList::sortedNames() const
{
return HashPtrTable<IOobject>::sortedToc();
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const bool syncPar
) const
{
wordList objNames(HashPtrTable<IOobject>::sortedToc());
checkNames(objNames, syncPar);
return objNames;
}
@ -335,122 +332,19 @@ Foam::wordList Foam::IOobjectList::sortedNames
const word& clsName
) const
{
return namesImpl(*this, clsName, predicates::always(), true);
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, clsName, predicates::always(), false));
checkNames(objNames, syncPar);
return objNames;
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordRe& matchName
) const
{
return namesImpl(*this, clsName, matchName, false);
// sort/sync: true, false
return namesImpl(*this, clsName, predicates::always(), true, false);
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const wordRe& matchName
) const
{
return namesImpl(*this, clsName, matchName, true);
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordRe& matchName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar);
return objNames;
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordRes& matchName
) const
{
return namesImpl(*this, clsName, matchName, false);
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const wordRes& matchName
) const
{
return namesImpl(*this, clsName, matchName, true);
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordRes& matchName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar);
return objNames;
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordHashSet& matchName
) const
{
return namesImpl(*this, clsName, matchName, false);
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const wordHashSet& matchName
) const
{
return namesImpl(*this, clsName, matchName, true);
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const wordHashSet& matchName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar);
return objNames;
// sort: true
return namesImpl(*this, clsName, predicates::always(), true, syncPar);
}

View File

@ -65,6 +65,9 @@ class IOobjectList
// since this is required for consistent ordering across processors.
static bool checkNames(wordList& masterNames, const bool syncPar);
//- Combine names from all processors and sort
static void syncNames(wordList& objNames);
//- Templated implementation for classes()
template<class MatchPredicate>
static HashTable<wordHashSet> classesImpl
@ -74,13 +77,14 @@ class IOobjectList
);
//- Templated implementation for names(), sortedNames()
template<class MatchPredicate>
template<class MatchPredicate1, class MatchPredicate2>
static wordList namesImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName,
const bool doSort
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort,
const bool syncPar
);
//- Templated implementation for lookup()
@ -92,12 +96,12 @@ class IOobjectList
);
//- Templated implementation for lookupClass()
template<class MatchPredicate>
template<class MatchPredicate1, class MatchPredicate2>
static IOobjectList lookupClassImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
);
@ -168,26 +172,25 @@ public:
bool remove(const IOobject& io);
// Lookup single
// Lookup single item
//- Lookup an object by name
//- Locate an object by name
// \return IOobject ptr if found else nullptr
IOobject* findObject(const word& objName) const;
// Lookup multiple
// Lookup multiple items
//- The list of all IOobjects with matching names
IOobjectList lookup(const wordRe& matchName) const;
//- The list of all IOobjects that have a matching object name.
template<class MatchPredicate>
IOobjectList lookup(const MatchPredicate& matchName) const;
//- The list of all IOobjects with matching names
IOobjectList lookup(const wordRes& matchName) const;
//- The list of all IOobjects with the given headerClassName
IOobjectList lookupClass(const char* clsName) const;
//- The list of all IOobjects with matching names
IOobjectList lookup(const wordHashSet& matchName) const;
//- The list of all IOobjects with the given class name
IOobjectList lookupClass(const word& clsName) const;
//- The list of all IOobjects with matching headerClassName
template<class MatchPredicate>
IOobjectList lookupClass(const MatchPredicate& matchClass) const;
// Summary of classes
@ -265,16 +268,9 @@ public:
HashTable<wordHashSet> classes() const;
//- A summary hash of classes used and their associated object names,
//- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRe& matchName) const;
//- A summary hash of classes used and their associated object names,
//- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRes& matchName) const;
//- A summary hash of classes used and their associated object names,
//- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordHashSet& matchName) const;
//- restricted to objects that have a matching object name.
template<class MatchPredicate>
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
// Summary of names
@ -282,62 +278,37 @@ public:
//- The names of the IOobjects
wordList names() const;
//- The names of the IOobjects.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
wordList names(const bool syncPar) const;
//- The names of IOobjects with the given class
wordList names(const word& clsName) const;
//- The names of IOobjects with the given class that also
//- have a name satisfying the input matcher
wordList names(const word& clsName, const wordRe& matchName) const;
//- The names of IOobjects with the given class that also
//- have a name satisfying the input matcher
wordList names(const word& clsName, const wordRes& matchName) const;
//- The names of IOobjects with the given class that also
//- have a name satisfying the input matcher
wordList names(const word& clsName, const wordHashSet& matchName) const;
//- A sorted list of names of the IOobjects.
// With syncPar = true, triggers FatalError if the names are
// not consistent on all processors.
wordList names(const bool syncPar) const;
//- A sorted list of names of the IOobjects
// With syncPar = true, triggers FatalError if the names are
// not consistent on all processors.
//- The names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
wordList names(const word& clsName, const bool syncPar) const;
//- The sorted names of IOobjects with the given class name that also
//- have a name satisfying the input matcher
// With syncPar = true, triggers FatalError if the names are
// not consistent on all processors.
//- The names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate>
wordList names
(
const word& clsName,
const wordRe& matchName,
const bool syncPar
const MatchPredicate& matchName
) const;
//- The sorted names of IOobjects with the given class name that also
//- have a name satisfying the input matcher
// With syncPar = true, triggers FatalError if the names are
// not consistent on all processors.
//- The names of the IOobjects with the given headerClassName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
template<class MatchPredicate>
wordList names
(
const word& clsName,
const wordRes& matchName,
const bool syncPar
) const;
//- The sorted names of IOobjects with the given class that also
//- have a name satisfying the input matcher
// With syncPar = true, triggers FatalError if the names are
// not consistent on all processors.
wordList names
(
const word& cls,
const wordHashSet& matchName,
const MatchPredicate& matchName,
const bool syncPar
) const;
@ -347,31 +318,38 @@ public:
//- The sorted names of the IOobjects
wordList sortedNames() const;
//- The sorted names of IOobjects with the given class
//- The sorted names of the IOobjects
// With syncPar = true, a FatalError is
// triggered if the names are not consistent on all processors.
wordList sortedNames(const bool syncPar) const;
//- The sorted names of IOobjects with the given headerClassName
wordList sortedNames(const word& clsName) const;
//- The sorted names of IOobjects with the given class that also
//- have a name satisfying the input matcher
//- The sorted names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
wordList sortedNames(const word& clsName, const bool syncPar) const;
//- The sorted names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate>
wordList sortedNames
(
const word& clsName,
const wordRe& matchName
const MatchPredicate& matchName
) const;
//- The sorted names of IOobjects with the given class that also
//- have a name satisfying the input matcher
//- The sorted names of the IOobjects with the given headerClassName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
template<class MatchPredicate>
wordList sortedNames
(
const word& clsName,
const wordRes& matchName
) const;
//- The sorted names of IOobjects with the given class that also
//- have a name satisfying the input matcher
wordList sortedNames
(
const word& clsName,
const wordHashSet& matchName
const MatchPredicate& matchName,
const bool syncPar
) const;
@ -386,8 +364,16 @@ public:
// Housekeeping
//- Deprecated(2018-11) Lookup an object by name
// \return IOobject ptr if found else nullptr
//- Deprecated(2018-11) Locate an object by name (c-string).
//- Disambiguated from multiple-lookup version by calling parameter.
// \deprecated(2018-11) use findObject() for non-ambiguous resolution
IOobject* lookup(const char* objName) const
{
return findObject(objName);
}
//- Deprecated(2018-11) Locate an object by name (const word&).
//- Disambiguated from multiple-lookup version by calling parameter.
// \deprecated(2018-11) use findObject() for non-ambiguous resolution
IOobject* lookup(const word& objName) const
{

View File

@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "IOobjectList.H"
#include "predicates.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -55,13 +56,14 @@ Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
// Templated implementation for names(), sortedNames()
template<class MatchPredicate>
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::namesImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName,
const bool doSort
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort,
const bool syncPar
)
{
wordList objNames(list.size());
@ -86,6 +88,8 @@ Foam::wordList Foam::IOobjectList::namesImpl
Foam::sort(objNames);
}
checkNames(objNames, syncPar);
return objNames;
}
@ -121,12 +125,12 @@ Foam::IOobjectList Foam::IOobjectList::lookupImpl
// Templated implementation for lookupClass()
template<class MatchPredicate>
template<class MatchPredicate1, class MatchPredicate2>
Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
)
{
IOobjectList results(list.size());
@ -151,4 +155,87 @@ Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class MatchPredicate>
Foam::IOobjectList Foam::IOobjectList::lookup
(
const MatchPredicate& matchName
) const
{
return lookupImpl(*this, matchName);
}
template<class MatchPredicate>
Foam::IOobjectList Foam::IOobjectList::lookupClass
(
const MatchPredicate& matchClass
) const
{
return lookupClassImpl(*this, matchClass, predicates::always());
}
template<class MatchPredicate>
Foam::HashTable<Foam::wordHashSet>
Foam::IOobjectList::classes
(
const MatchPredicate& matchName
) const
{
return classesImpl(*this, matchName);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const MatchPredicate& matchName
) const
{
// sort/sync: false, false
return namesImpl(*this, clsName, matchName, false, false);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const MatchPredicate& matchName,
const bool syncPar
) const
{
// sort: false
return namesImpl(*this, clsName, matchName, false, syncPar);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const MatchPredicate& matchName
) const
{
// sort/sync: true, false
return namesImpl(*this, clsName, matchName, true, false);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const MatchPredicate& matchName,
const bool syncPar
) const
{
// sort: true
return namesImpl(*this, clsName, matchName, true, syncPar);
}
// ************************************************************************* //