From 9bc61e5f416466d5ecdc76849faebfa71a8601c6 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 12 Nov 2018 08:55:45 +0100 Subject: [PATCH] ENH: improve IOobjectList name filtering - support name filtering by class based on or predicates. Eg, objects.sortedNames(namePattern); vs objects.sortedNames(volScalarField::typeName, namePattern); These can also be used directly for untyped name matching. Eg, objects.sortedNames(namePattern); Can also use a predicate: objects.sortedNames(wordRe("vol.*Field"), namePattern); objects.sortedNames ( [](const word& clsName){ return clsName.startsWith("vol"); }, namePattern ); --- .../test/IOobjectList/Test-IOobjectList.C | 65 ++++++ src/OpenFOAM/db/IOobjectList/IOobjectList.C | 37 ++-- src/OpenFOAM/db/IOobjectList/IOobjectList.H | 125 ++++++++++-- .../db/IOobjectList/IOobjectListTemplates.C | 191 +++++++++++++++--- 4 files changed, 347 insertions(+), 71 deletions(-) diff --git a/applications/test/IOobjectList/Test-IOobjectList.C b/applications/test/IOobjectList/Test-IOobjectList.C index ec7ce76299..3e0850c0d7 100644 --- a/applications/test/IOobjectList/Test-IOobjectList.C +++ b/applications/test/IOobjectList/Test-IOobjectList.C @@ -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 +void filterTest(const IOobjectList& objs, const wordRe& re) +{ + Info<< "Filter = " << re << nl; + + const word& typeName = Type::typeName; + + Info<< " <" << typeName <<">(" << re << ") : " + << objs.count(re) << nl + << " (" << typeName << "::typeName, " << re << ") : " + << objs.count(typeName, re) << nl; + + Info<< " <" << typeName << ">(" << re << ") : " + << flatOutput(objs.sortedNames(re)) << nl + // << flatOutput(objs.names(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 + << " (" << re << ") : " + << flatOutput(objs.sortedNames(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 + << " () : " << objs.count() << nl + << " () : " << objs.count() << nl + << " () : " << objs.count() << nl + << nl; + Info<< " () : " + << objs.count() << nl + << " (volScalarField::typeName) : " + << objs.count(volScalarField::typeName) << nl; + Info<< " () : " + << objs.count() << nl + << " (volVectorField::typeName) : " + << objs.count(volVectorField::typeName) << nl; + + + Info<< nl << "Filter on names:" << nl; + filterTest(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) { diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index 9b6824f856..8d914a1b47 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -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(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(clsName), syncPar); } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + Foam::wordList Foam::IOobjectList::sortedNames() const { return HashPtrTable::sortedToc(); } -Foam::wordList Foam::IOobjectList::sortedNames -( - const bool syncPar -) const +Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const { wordList objNames(HashPtrTable::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(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(clsName), syncPar); } diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index b4882c5d91..692e81c890 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -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 + 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 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 + 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 + template + wordList names + ( + const MatchPredicate1& matchClass, + const MatchPredicate2& matchName, + const bool syncPar + ) const; + + + //- The names of objects with headerClassName == Type::typeName + template + 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 + 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 + 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 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 + 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 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 + 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 + template + wordList sortedNames + ( + const MatchPredicate1& matchClass, + const MatchPredicate2& matchName, + const bool syncPar + ) const; + + + //- The sorted names of objects with headerClassName == Type::typeName + template + 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 + 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 + 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 wordList sortedNames ( - const word& clsName, const MatchPredicate& matchName, const bool syncPar ) const; diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C index c08833c124..b7dc5b9b37 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C @@ -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 -Foam::wordList Foam::IOobjectList::names -( - const word& clsName, - const MatchPredicate& matchName -) const -{ - // sort/sync: false, false - return namesImpl(*this, clsName, matchName, false, false); -} - - template Foam::label Foam::IOobjectList::count ( @@ -385,38 +370,186 @@ Foam::label Foam::IOobjectList::count template 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 -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 +Foam::wordList Foam::IOobjectList::names +( + const MatchPredicate1& matchClass, + const MatchPredicate2& matchName +) const +{ + return namesImpl(*this, matchClass, matchName, false); +} + + +template +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 +Foam::wordList Foam::IOobjectList::names() const +{ + return namesTypeImpl(*this, predicates::always(), false); +} + + +template +Foam::wordList Foam::IOobjectList::names(const bool syncPar) const +{ + wordList objNames(namesTypeImpl(*this, predicates::always(), false)); + + checkNames(objNames, syncPar); + return objNames; +} + + +template +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(*this, matchName, false); +} + + +template +Foam::wordList Foam::IOobjectList::names +( + const MatchPredicate& matchName, + const bool syncPar +) const +{ + wordList objNames(namesTypeImpl(*this, matchName, false)); + + checkNames(objNames, syncPar); + return objNames; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template +Foam::wordList Foam::IOobjectList::sortedNames +( + const MatchPredicate& matchClass +) const +{ + return namesImpl(*this, matchClass, predicates::always(), true); } template 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 +Foam::wordList Foam::IOobjectList::sortedNames +( + const MatchPredicate1& matchClass, + const MatchPredicate2& matchName +) const +{ + return namesImpl(*this, matchClass, matchName, true); +} + +template +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 +Foam::wordList Foam::IOobjectList::sortedNames() const +{ + return namesTypeImpl(*this, predicates::always(), true); +} + + +template +Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const +{ + wordList objNames(namesTypeImpl(*this, predicates::always(), true)); + + checkNames(objNames, syncPar); + return objNames; +} + + +template +Foam::wordList Foam::IOobjectList::sortedNames +( + const MatchPredicate& matchName +) const +{ + return namesTypeImpl(*this, matchName, true); +} + + +template +Foam::wordList Foam::IOobjectList::sortedNames +( const MatchPredicate& matchName, const bool syncPar ) const { - // sort: true - return namesImpl(*this, clsName, matchName, true, syncPar); + return namesTypeImpl(*this, matchName, true, syncPar); }