Skip to content
Snippets Groups Projects
Commit b8300759 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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.
parent 2af602c2
No related branches found
No related tags found
No related merge requests found
...@@ -74,7 +74,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size) ...@@ -74,7 +74,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size)
{ {
table_ = new hashedEntry*[tableSize_]; table_ = new hashedEntry*[tableSize_];
for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
{ {
table_[hashIdx] = nullptr; table_[hashIdx] = nullptr;
} }
...@@ -203,6 +203,17 @@ Foam::HashTable<T, Key, Hash>::find ...@@ -203,6 +203,17 @@ Foam::HashTable<T, Key, Hash>::find
( (
const Key& key const Key& key
) const ) 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_) if (nElmts_)
{ {
...@@ -912,7 +923,7 @@ bool Foam::HashTable<T, Key, Hash>::operator== ...@@ -912,7 +923,7 @@ bool Foam::HashTable<T, Key, Hash>::operator==
for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) 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()) if (!other.found() || other.object() != iter.object())
{ {
......
...@@ -327,6 +327,10 @@ public: ...@@ -327,6 +327,10 @@ public:
// If not found iterator = end() // If not found iterator = end()
const_iterator find(const Key& key) const; 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 //- Return hashed entry if it exists, or return the given default
inline const T& lookup(const Key& key, const T& deflt) const; inline const T& lookup(const Key& key, const T& deflt) const;
......
...@@ -158,6 +158,17 @@ Foam::StaticHashTable<T, Key, Hash>::find ...@@ -158,6 +158,17 @@ Foam::StaticHashTable<T, Key, Hash>::find
( (
const Key& key const Key& key
) const ) 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_) if (nElmts_)
{ {
......
...@@ -203,6 +203,10 @@ public: ...@@ -203,6 +203,10 @@ public:
// If not found iterator = end() // If not found iterator = end()
const_iterator find(const Key& key) const; 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 //- Return the table of contents
List<Key> toc() const; List<Key> toc() const;
......
...@@ -87,10 +87,10 @@ public: ...@@ -87,10 +87,10 @@ public:
//- Read a word from Istream and return the corresponding //- Read a word from Istream and return the corresponding
// enumeration element // enumeration element
Enum read(Istream&) const; Enum read(Istream& is) const;
//- Write the name representation of the enumeration to an Ostream //- 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 //- The set of names as a list of strings
static stringList strings(); static stringList strings();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment