ENH: provide HashTable::iterator::found() method

- This can be used as a convenient alternative to comparing against end().
  Eg,

    dictionaryConstructorTable::iterator cstrIter =
        dictionaryConstructorTablePtr_->find(methodType);

    if (cstrIter.found())
    {
        ...
    }
  vs.
    if (cstrIter != dictionaryConstructorTablePtr_->end())
    {
        ...
    }
This commit is contained in:
Mark Olesen
2017-04-11 09:55:54 +02:00
parent b56227ee2b
commit f2e3c1c422
3 changed files with 17 additions and 9 deletions

View File

@ -22,10 +22,9 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application Application
testMapIterators Test-Map
Description Description
For each time calculate the magnitude of velocity.
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -39,9 +38,7 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Map<bool> banana; Map<bool> banana{{5, true}};
banana.insert(5, true);
// Taking a const iterator from find does not work! // Taking a const iterator from find does not work!
// Also, fails later on op== // Also, fails later on op==
@ -50,7 +47,7 @@ int main(int argc, char *argv[])
// This works but now I can change the value. // This works but now I can change the value.
//Map<bool>::iterator bananaIter = banana.find(5); //Map<bool>::iterator bananaIter = banana.find(5);
if (bananaIter == banana.end()) if (!bananaIter.found()) // same as (bananaIter == banana.end())
{ {
Info<< "not found" << endl; Info<< "not found" << endl;
} }
@ -62,8 +59,7 @@ int main(int argc, char *argv[])
// Same with STL // Same with STL
Info<< "Same with STL" << endl; Info<< "Same with STL" << endl;
std::map<label, bool> STLbanana; std::map<label, bool> STLbanana{{5, true}};
STLbanana[5] = true;
std::map<label, bool>::const_iterator STLbananaIter = STLbanana.find(5); std::map<label, bool>::const_iterator STLbananaIter = STLbanana.find(5);
if (STLbananaIter == STLbanana.end()) if (STLbananaIter == STLbanana.end())

View File

@ -403,6 +403,10 @@ public:
// Access // Access
//- True if iterator points to a hashedEntry.
// This can be used instead of a comparison to end()
inline bool found() const;
//- Return the Key corresponding to the iterator //- Return the Key corresponding to the iterator
inline const Key& key() const; inline const Key& key() const;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -257,6 +257,14 @@ Foam::HashTable<T, Key, Hash>::iteratorBase::increment()
} }
template<class T, class Key, class Hash>
inline bool
Foam::HashTable<T, Key, Hash>::iteratorBase::found() const
{
return entryPtr_;
}
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
inline inline
const Key& Foam::HashTable<T, Key, Hash>::iteratorBase::key() const const Key& Foam::HashTable<T, Key, Hash>::iteratorBase::key() const