From f2e3c1c422850e9d89b84d2298edaa446de69fb6 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 11 Apr 2017 09:55:54 +0200 Subject: [PATCH] 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()) { ... } --- applications/test/Map/Test-Map.C | 12 ++++-------- .../containers/HashTables/HashTable/HashTable.H | 4 ++++ .../containers/HashTables/HashTable/HashTableI.H | 10 +++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/applications/test/Map/Test-Map.C b/applications/test/Map/Test-Map.C index ea46cfcb12..5ced3b569d 100644 --- a/applications/test/Map/Test-Map.C +++ b/applications/test/Map/Test-Map.C @@ -22,10 +22,9 @@ License along with OpenFOAM. If not, see . Application - testMapIterators + Test-Map Description - For each time calculate the magnitude of velocity. \*---------------------------------------------------------------------------*/ @@ -39,9 +38,7 @@ using namespace Foam; int main(int argc, char *argv[]) { - Map banana; - - banana.insert(5, true); + Map banana{{5, true}}; // Taking a const iterator from find does not work! // Also, fails later on op== @@ -50,7 +47,7 @@ int main(int argc, char *argv[]) // This works but now I can change the value. //Map::iterator bananaIter = banana.find(5); - if (bananaIter == banana.end()) + if (!bananaIter.found()) // same as (bananaIter == banana.end()) { Info<< "not found" << endl; } @@ -62,8 +59,7 @@ int main(int argc, char *argv[]) // Same with STL Info<< "Same with STL" << endl; - std::map STLbanana; - STLbanana[5] = true; + std::map STLbanana{{5, true}}; std::map::const_iterator STLbananaIter = STLbanana.find(5); if (STLbananaIter == STLbanana.end()) diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index d12c8929c1..b2c048879b 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -403,6 +403,10 @@ public: // 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 inline const Key& key() const; diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H index 2303497585..6246312021 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -257,6 +257,14 @@ Foam::HashTable::iteratorBase::increment() } +template +inline bool +Foam::HashTable::iteratorBase::found() const +{ + return entryPtr_; +} + + template inline const Key& Foam::HashTable::iteratorBase::key() const