ENH: improve IOobjectList name filtering

- support name filtering by class based on <Type> or predicates.
  Eg,

      objects.sortedNames<volScalarField>(namePattern);
  vs  objects.sortedNames(volScalarField::typeName, namePattern);

  These can also be used directly for untyped name matching.
  Eg,
      objects.sortedNames<void>(namePattern);

  Can also use a predicate:

      objects.sortedNames(wordRe("vol.*Field"), namePattern);
      objects.sortedNames
      (
          [](const word& clsName){ return clsName.startsWith("vol"); },
          namePattern
      );
This commit is contained in:
Mark Olesen
2018-11-12 08:55:45 +01:00
parent 256042158f
commit 9bc61e5f41
4 changed files with 347 additions and 71 deletions

View File

@ -32,6 +32,8 @@ Description
#include "timeSelector.H"
#include "IOobjectList.H"
#include "hashedWordList.H"
#include "labelIOList.H"
#include "scalarIOList.H"
using namespace Foam;
@ -80,6 +82,67 @@ void reportDetail(const IOobjectList& objects)
}
template<class Type>
void filterTest(const IOobjectList& objs, const wordRe& re)
{
Info<< "Filter = " << re << nl;
const word& typeName = Type::typeName;
Info<< " <" << typeName <<">(" << re << ") : "
<< objs.count<Type>(re) << nl
<< " (" << typeName << "::typeName, " << re << ") : "
<< objs.count(typeName, re) << nl;
Info<< " <" << typeName << ">(" << re << ") : "
<< flatOutput(objs.sortedNames<Type>(re)) << nl
// << flatOutput(objs.names<Type>(re)) << nl
<< " (" << typeName << "::typeName, " << re << ") : "
<< flatOutput(objs.sortedNames(typeName, re)) << nl
//<< flatOutput(objs.names(typeName, re)) << nl
;
wordRe reClass("vol.*Field", wordRe::REGEX);
wordRe re2(re, wordRe::REGEX_ICASE);
Info<< "General" << nl
<< " <void>(" << re << ") : "
<< flatOutput(objs.sortedNames<void>(re)) << nl
<< " (" << reClass << ", " << re2 <<" ignore-case) : "
<< flatOutput(objs.sortedNames(reClass, re2)) << nl
;
Info<< nl;
}
void registryTests(const IOobjectList& objs)
{
Info<< nl << "IOobjectList " << flatOutput(objs.sortedNames()) << nl;
Info<< "count" << nl
<< " <void>() : " << objs.count<void>() << nl
<< " <labelList>() : " << objs.count<labelIOList>() << nl
<< " <scalarList>() : " << objs.count<scalarIOList>() << nl
<< nl;
Info<< " <volScalarField>() : "
<< objs.count<volScalarField>() << nl
<< " (volScalarField::typeName) : "
<< objs.count(volScalarField::typeName) << nl;
Info<< " <volVectorField>() : "
<< objs.count<volVectorField>() << nl
<< " (volVectorField::typeName) : "
<< objs.count(volVectorField::typeName) << nl;
Info<< nl << "Filter on names:" << nl;
filterTest<volScalarField>(objs, wordRe("[p-z].*", wordRe::DETECT));
Info<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -165,6 +228,8 @@ int main(int argc, char *argv[])
Info<<"remove: " << flatOutput(subsetTypes) << nl;
Info<<"Pruned: " << classes << nl;
registryTests(objects);
// On last time
if (timeI == timeDirs.size()-1)
{

View File

@ -296,37 +296,33 @@ Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName
) const
Foam::wordList Foam::IOobjectList::names(const char* clsName) const
{
// sort/sync: false, false
return namesImpl(*this, clsName, predicates::always(), false, false);
// No nullptr check - only called with string literals
return names(static_cast<word>(clsName));
}
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const char* clsName,
const bool syncPar
) const
{
// sort: false
return namesImpl(*this, clsName, predicates::always(), false, syncPar);
// No nullptr check - only called with string literals
return names(static_cast<word>(clsName), syncPar);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::wordList Foam::IOobjectList::sortedNames() const
{
return HashPtrTable<IOobject>::sortedToc();
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const bool syncPar
) const
Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
{
wordList objNames(HashPtrTable<IOobject>::sortedToc());
@ -335,24 +331,21 @@ Foam::wordList Foam::IOobjectList::sortedNames
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName
) const
Foam::wordList Foam::IOobjectList::sortedNames(const char* clsName) const
{
// sort/sync: true, false
return namesImpl(*this, clsName, predicates::always(), true, false);
// No nullptr check - only called with string literals
return sortedNames(static_cast<word>(clsName));
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const char* clsName,
const bool syncPar
) const
{
// sort: true
return namesImpl(*this, clsName, predicates::always(), true, syncPar);
// No nullptr check - only called with string literals
return names(static_cast<word>(clsName), syncPar);
}

View File

@ -101,8 +101,7 @@ class IOobjectList
const IOobjectList& list,
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort,
const bool syncPar
const bool doSort
);
//- Templated implementation for names(), sortedNames()
@ -374,31 +373,74 @@ public:
// 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 headerClassName
wordList names(const char* clsName) const;
//- 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;
wordList names(const char* clsName, const bool syncPar) const;
//- The names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate>
wordList names(const MatchPredicate& matchClass) const;
//- 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.
template<class MatchPredicate>
wordList names
(
const word& clsName,
const MatchPredicate& matchName
const MatchPredicate& matchClass,
const bool syncPar
) const;
//- The names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate1, class MatchPredicate2>
wordList names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const;
//- 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>
template<class MatchPredicate1, class MatchPredicate2>
wordList names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool syncPar
) const;
//- The names of objects with headerClassName == Type::typeName
template<class Type>
wordList names() const;
//- The names of objects with headerClassName == Type::typeName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
template<class Type>
wordList names(bool syncPar) const;
//- The names of objects with headerClassName == Type::typeName
//- 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 Type, class MatchPredicate>
wordList names(const MatchPredicate& matchName) const;
//- The names of objects with headerClassName == Type::typeName
//- 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 Type, class MatchPredicate>
wordList names
(
const word& clsName,
const MatchPredicate& matchName,
const bool syncPar
) const;
@ -409,36 +451,79 @@ public:
//- The sorted names of the IOobjects
wordList sortedNames() const;
//- The sorted names of the IOobjects
// With syncPar = true, a FatalError is
// triggered if the names are not consistent on all processors.
//- The sorted names of the IOobjects.
// With syncPar = true, sorts the names and triggers FatalError
// 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;
wordList sortedNames(const char* clsName) const;
//- 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;
wordList sortedNames(const char* 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 MatchPredicate& matchClass) const;
//- 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.
template<class MatchPredicate>
wordList sortedNames
(
const word& clsName,
const MatchPredicate& matchName
const MatchPredicate& matchClass,
const bool syncPar
) const;
//- The sorted names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate1, class MatchPredicate2>
wordList sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const;
//- 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>
template<class MatchPredicate1, class MatchPredicate2>
wordList sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool syncPar
) const;
//- The sorted names of objects with headerClassName == Type::typeName
template<class Type>
wordList sortedNames() const;
//- The sorted names of objects with headerClassName == Type::typeName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
template<class Type>
wordList sortedNames(bool syncPar) const;
//- The sorted names of objects with headerClassName == Type::typeName
//- 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 Type, class MatchPredicate>
wordList sortedNames(const MatchPredicate& matchName) const;
//- The sorted names of objects with headerClassName == Type::typeName
//- 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 Type, class MatchPredicate>
wordList sortedNames
(
const word& clsName,
const MatchPredicate& matchName,
const bool syncPar
) const;

View File

@ -111,8 +111,7 @@ Foam::wordList Foam::IOobjectList::namesImpl
const IOobjectList& list,
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort,
const bool syncPar
const bool doSort
)
{
wordList objNames(list.size());
@ -130,15 +129,13 @@ Foam::wordList Foam::IOobjectList::namesImpl
}
}
objNames.setSize(count);
objNames.resize(count);
if (doSort)
{
Foam::sort(objNames);
}
checkNames(objNames, syncPar);
return objNames;
}
@ -330,18 +327,6 @@ Foam::IOobjectList::classes
}
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::label Foam::IOobjectList::count
(
@ -385,38 +370,186 @@ Foam::label Foam::IOobjectList::count
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const MatchPredicate& matchName,
const bool syncPar
const MatchPredicate& matchClass
) const
{
// sort: false
return namesImpl(*this, clsName, matchName, false, syncPar);
return namesImpl(*this, matchClass, predicates::always(), false);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
Foam::wordList Foam::IOobjectList::names
(
const MatchPredicate& matchClass,
const bool syncPar
) const
{
wordList objNames
(
namesImpl(*this, matchClass, predicates::always(), false)
);
checkNames(objNames, syncPar);
return objNames;
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const
{
return namesImpl(*this, matchClass, matchName, false);
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, matchClass, matchName, false));
checkNames(objNames, syncPar);
return objNames;
}
template<class Type>
Foam::wordList Foam::IOobjectList::names() const
{
return namesTypeImpl<Type>(*this, predicates::always(), false);
}
template<class Type>
Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
{
wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
checkNames(objNames, syncPar);
return objNames;
}
template<class Type, class MatchPredicate>
Foam::wordList Foam::IOobjectList::names
(
const word& clsName,
const MatchPredicate& matchName
) const
{
// sort/sync: true, false
return namesImpl(*this, clsName, matchName, true, false);
return namesTypeImpl<Type>(*this, matchName, false);
}
template<class Type, class MatchPredicate>
Foam::wordList Foam::IOobjectList::names
(
const MatchPredicate& matchName,
const bool syncPar
) const
{
wordList objNames(namesTypeImpl<Type>(*this, matchName, false));
checkNames(objNames, syncPar);
return objNames;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const MatchPredicate& matchClass
) const
{
return namesImpl(*this, matchClass, predicates::always(), true);
}
template<class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& clsName,
const MatchPredicate& matchClass,
const bool syncPar
) const
{
wordList objNames
(
namesImpl(*this, matchClass, predicates::always(), true)
);
checkNames(objNames, syncPar);
return objNames;
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const
{
return namesImpl(*this, matchClass, matchName, true);
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, matchClass, matchName, true));
checkNames(objNames, syncPar);
return objNames;
}
template<class Type>
Foam::wordList Foam::IOobjectList::sortedNames() const
{
return namesTypeImpl<Type>(*this, predicates::always(), true);
}
template<class Type>
Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
{
wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
checkNames(objNames, syncPar);
return objNames;
}
template<class Type, class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const MatchPredicate& matchName
) const
{
return namesTypeImpl<Type>(*this, matchName, true);
}
template<class Type, class MatchPredicate>
Foam::wordList Foam::IOobjectList::sortedNames
(
const MatchPredicate& matchName,
const bool syncPar
) const
{
// sort: true
return namesImpl(*this, clsName, matchName, true, syncPar);
return namesTypeImpl<Type>(*this, matchName, true, syncPar);
}