Skip to content
Snippets Groups Projects
Commit c428cfa2 authored by mark's avatar mark
Browse files

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())
    {
        ...
    }
parent 37ef335d
Branches
Tags
1 merge request!121Merge develop into master for v1706 release
...@@ -22,10 +22,9 @@ License ...@@ -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; ...@@ -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[]) ...@@ -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[]) ...@@ -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())
......
...@@ -403,6 +403,10 @@ public: ...@@ -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;
......
...@@ -3,7 +3,7 @@ ...@@ -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() ...@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment