From b83007594aa42730885c4e564f2debb0079b1010 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Fri, 26 May 2017 12:42:30 +0200 Subject: [PATCH] ENH: HashTable cfind() method returning a const_iterator - This follows the same idea as cbegin/cend and is helpful when using C++11 auto to ensure we have unambiguous const-safe access. Previously: ==== typename someLongClass::const_iterator iter = someTable.find(key); ... later on: *iter = value; // Oops, but caught by compiler. We can save some typing with auto, but it is uncertain what we get: ==== auto iter = someTable.find(key); // iterator or const_iterator? // depends on someTable having const or non-const access. ... later on: *iter = value; // Oops, but not caught by compiler. Using cfind instead, auto will deduce const_iterator as the type: ==== auto iter = someTable.cfind(key); // definitely const_iterator ... later on: *iter = value; // Oops, but caught by compiler. --- .../containers/HashTables/HashTable/HashTable.C | 15 +++++++++++++-- .../containers/HashTables/HashTable/HashTable.H | 4 ++++ .../HashTables/StaticHashTable/StaticHashTable.C | 11 +++++++++++ .../HashTables/StaticHashTable/StaticHashTable.H | 4 ++++ src/OpenFOAM/containers/NamedEnum/NamedEnum.H | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index 2c8167adcee..9bf799d6b34 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -74,7 +74,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size) { table_ = new hashedEntry*[tableSize_]; - for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) + for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx) { table_[hashIdx] = nullptr; } @@ -203,6 +203,17 @@ Foam::HashTable<T, Key, Hash>::find ( const Key& key ) const +{ + return this->cfind(key); +} + + +template<class T, class Key, class Hash> +typename Foam::HashTable<T, Key, Hash>::const_iterator +Foam::HashTable<T, Key, Hash>::cfind +( + const Key& key +) const { if (nElmts_) { @@ -912,7 +923,7 @@ bool Foam::HashTable<T, Key, Hash>::operator== for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { - const_iterator other = find(iter.key()); + const_iterator other = this->cfind(iter.key()); if (!other.found() || other.object() != iter.object()) { diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 3b3338084d9..3f9a2df85fc 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -327,6 +327,10 @@ public: // If not found iterator = end() const_iterator find(const Key& key) const; + //- Find and return an const_iterator set at the hashed entry + // If not found iterator = end() + const_iterator cfind(const Key& key) const; + //- Return hashed entry if it exists, or return the given default inline const T& lookup(const Key& key, const T& deflt) const; diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index ec30dfc20a0..c024ff335e2 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -158,6 +158,17 @@ Foam::StaticHashTable<T, Key, Hash>::find ( const Key& key ) const +{ + return this->cfind(key); +} + + +template<class T, class Key, class Hash> +typename Foam::StaticHashTable<T, Key, Hash>::const_iterator +Foam::StaticHashTable<T, Key, Hash>::cfind +( + const Key& key +) const { if (nElmts_) { diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 5b0a3ef8a59..850eb1fc43d 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -203,6 +203,10 @@ public: // If not found iterator = end() const_iterator find(const Key& key) const; + //- Find and return an const_iterator set at the hashed entry + // If not found iterator = end() + const_iterator cfind(const Key& key) const; + //- Return the table of contents List<Key> toc() const; diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H index b2705f28050..b9054aee85f 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H @@ -87,10 +87,10 @@ public: //- Read a word from Istream and return the corresponding // enumeration element - Enum read(Istream&) const; + Enum read(Istream& is) const; //- Write the name representation of the enumeration to an Ostream - void write(const Enum e, Ostream&) const; + void write(const Enum e, Ostream& os) const; //- The set of names as a list of strings static stringList strings(); -- GitLab