mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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.
This commit is contained in:
@ -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())
|
||||
{
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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_)
|
||||
{
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user