HashTable / StaticHashTable changes

StaticHashTable:
- erase(iterator&) now actually alters the iterator and iterator++() handles
  it properly
- clear() also sets count to zero
- operator=(const StaticHashTable&) doesn't crash after a previous transfer
- operator(), operator==() and operator!=() added

HashTable:
- operator=(const HashTable&) gets tableSize if required, eg, after a
  previous transfer)

HashSet / Map
- add xfer<...> constructor for underlying HashTable
This commit is contained in:
Mark Olesen
2009-01-02 13:24:30 +01:00
parent 973b9ea0ce
commit a010121427
15 changed files with 813 additions and 537 deletions

View File

@ -26,15 +26,10 @@ License
#include "error.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline HashTable<T, Key, Hash>::hashedEntry::hashedEntry
inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry
(
const Key& key,
hashedEntry* next,
@ -50,36 +45,55 @@ inline HashTable<T, Key, Hash>::hashedEntry::hashedEntry
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline label HashTable<T, Key, Hash>::size() const
inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
{
return nElmts_;
}
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::insert(const Key& key, const T& newEntry)
inline bool Foam::HashTable<T, Key, Hash>::insert
(
const Key& key,
const T& newEntry
)
{
return set(key, newEntry, true);
}
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::set(const Key& key, const T& newEntry)
inline bool Foam::HashTable<T, Key, Hash>::set
(
const Key& key,
const T& newEntry
)
{
return set(key, newEntry, false);
}
template<class T, class Key, class Hash>
inline Foam::xfer<Foam::HashTable<T, Key, Hash> >
Foam::HashTable<T, Key, Hash>::transfer()
{
Foam::xfer<HashTable<T, Key, Hash> > xf;
xf().transfer(*this);
return xf;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline T& HashTable<T, Key, Hash>::operator[](const Key& key)
inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
{
iterator iter = find(key);
if (iter == end())
{
FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
<< key << " not found in table. Valid entries are "
<< key << " not found in table. Valid entries: "
<< toc()
<< exit(FatalError);
}
@ -87,15 +101,16 @@ inline T& HashTable<T, Key, Hash>::operator[](const Key& key)
return *iter;
}
template<class T, class Key, class Hash>
inline const T& HashTable<T, Key, Hash>::operator[](const Key& key) const
inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
{
const_iterator iter = find(key);
if (iter == end())
{
FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
<< key << " not found in table. Valid entries are "
<< key << " not found in table. Valid entries: "
<< toc()
<< exit(FatalError);
}
@ -105,7 +120,7 @@ inline const T& HashTable<T, Key, Hash>::operator[](const Key& key) const
template<class T, class Key, class Hash>
inline T& HashTable<T, Key, Hash>::operator()(const Key& key)
inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
{
iterator iter = find(key);
@ -124,7 +139,7 @@ inline T& HashTable<T, Key, Hash>::operator()(const Key& key)
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline HashTable<T, Key, Hash>::iterator::iterator
inline Foam::HashTable<T, Key, Hash>::iterator::iterator
(
HashTable<T, Key, Hash>& curHashTable,
hashedEntry* elmt,
@ -138,7 +153,10 @@ inline HashTable<T, Key, Hash>::iterator::iterator
template<class T, class Key, class Hash>
inline void HashTable<T, Key, Hash>::iterator::operator=(const iterator& iter)
inline void Foam::HashTable<T, Key, Hash>::iterator::operator=
(
const iterator& iter
)
{
elmtPtr_ = iter.elmtPtr_;
hashIndex_ = iter.hashIndex_;
@ -146,7 +164,7 @@ inline void HashTable<T, Key, Hash>::iterator::operator=(const iterator& iter)
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::iterator::operator==
inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
(
const iterator& iter
) const
@ -156,7 +174,7 @@ inline bool HashTable<T, Key, Hash>::iterator::operator==
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::iterator::operator!=
inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
(
const iterator& iter
) const
@ -166,7 +184,7 @@ inline bool HashTable<T, Key, Hash>::iterator::operator!=
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::iterator::operator==
inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
(
const const_iterator& iter
) const
@ -176,7 +194,7 @@ inline bool HashTable<T, Key, Hash>::iterator::operator==
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::iterator::operator!=
inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
(
const const_iterator& iter
) const
@ -186,14 +204,14 @@ inline bool HashTable<T, Key, Hash>::iterator::operator!=
template<class T, class Key, class Hash>
inline T& HashTable<T, Key, Hash>::iterator::operator*()
inline T& Foam::HashTable<T, Key, Hash>::iterator::operator*()
{
return elmtPtr_->obj_;
}
template<class T, class Key, class Hash>
inline T& HashTable<T, Key, Hash>::iterator::operator()()
inline T& Foam::HashTable<T, Key, Hash>::iterator::operator()()
{
return operator*();
}
@ -201,13 +219,13 @@ inline T& HashTable<T, Key, Hash>::iterator::operator()()
template<class T, class Key, class Hash>
inline
typename HashTable<T, Key, Hash>::iterator&
HashTable<T, Key, Hash>::iterator::operator++()
typename Foam::HashTable<T, Key, Hash>::iterator&
Foam::HashTable<T, Key, Hash>::iterator::operator++()
{
// Check for special value from erase. (sets hashIndex to -1)
if (hashIndex_ >= 0)
{
// Do we have additional elements on the singly linked list?
// Do we have additional elements on the SLList?
if (elmtPtr_ && elmtPtr_->next_)
{
elmtPtr_ = elmtPtr_->next_;
@ -226,16 +244,16 @@ HashTable<T, Key, Hash>::iterator::operator++()
if (hashIndex_ == curHashTable_.tableSize_)
{
// make end iterator
hashIndex_ = 0;
elmtPtr_ = 0;
hashIndex_ = 0;
}
return *this;
}
template<class T, class Key, class Hash>
inline typename HashTable<T, Key, Hash>::iterator
HashTable<T, Key, Hash>::iterator::operator++
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::iterator::operator++
(
int
)
@ -248,15 +266,15 @@ HashTable<T, Key, Hash>::iterator::operator++
template<class T, class Key, class Hash>
inline
const Key& HashTable<T, Key, Hash>::iterator::key()
const Key& Foam::HashTable<T, Key, Hash>::iterator::key()
{
return elmtPtr_->key_;
}
template<class T, class Key, class Hash>
inline typename HashTable<T, Key, Hash>::iterator
HashTable<T, Key, Hash>::begin()
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::begin()
{
label i = 0;
@ -282,8 +300,8 @@ HashTable<T, Key, Hash>::begin()
template<class T, class Key, class Hash>
inline const typename HashTable<T, Key, Hash>::iterator&
HashTable<T, Key, Hash>::end()
inline const typename Foam::HashTable<T, Key, Hash>::iterator&
Foam::HashTable<T, Key, Hash>::end()
{
return HashTable<T, Key, Hash>::endIter_;
}
@ -292,7 +310,7 @@ HashTable<T, Key, Hash>::end()
// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline HashTable<T, Key, Hash>::const_iterator::const_iterator
inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
(
const HashTable<T, Key, Hash>& curHashTable,
const hashedEntry* elmt,
@ -306,7 +324,7 @@ inline HashTable<T, Key, Hash>::const_iterator::const_iterator
template<class T, class Key, class Hash>
inline HashTable<T, Key, Hash>::const_iterator::const_iterator
inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
(
const iterator& iter
)
@ -318,7 +336,7 @@ inline HashTable<T, Key, Hash>::const_iterator::const_iterator
template<class T, class Key, class Hash>
inline void HashTable<T, Key, Hash>::const_iterator::operator=
inline void Foam::HashTable<T, Key, Hash>::const_iterator::operator=
(
const const_iterator& iter
)
@ -329,7 +347,7 @@ inline void HashTable<T, Key, Hash>::const_iterator::operator=
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::const_iterator::operator==
inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
(
const const_iterator& iter
) const
@ -339,7 +357,7 @@ inline bool HashTable<T, Key, Hash>::const_iterator::operator==
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
(
const const_iterator& iter
) const
@ -349,7 +367,7 @@ inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::const_iterator::operator==
inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
(
const iterator& iter
) const
@ -359,7 +377,7 @@ inline bool HashTable<T, Key, Hash>::const_iterator::operator==
template<class T, class Key, class Hash>
inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
(
const iterator& iter
) const
@ -369,14 +387,14 @@ inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
template<class T, class Key, class Hash>
inline const T& HashTable<T, Key, Hash>::const_iterator::operator*()
inline const T& Foam::HashTable<T, Key, Hash>::const_iterator::operator*()
{
return elmtPtr_->obj_;
}
#ifndef __CINT__
template<class T, class Key, class Hash>
inline const T& HashTable<T, Key, Hash>::const_iterator::operator()()
inline const T& Foam::HashTable<T, Key, Hash>::const_iterator::operator()()
{
return operator*();
}
@ -384,8 +402,8 @@ inline const T& HashTable<T, Key, Hash>::const_iterator::operator()()
template<class T, class Key, class Hash>
inline
typename HashTable<T, Key, Hash>::const_iterator&
HashTable<T, Key, Hash>::const_iterator::operator++()
typename Foam::HashTable<T, Key, Hash>::const_iterator&
Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
{
if
(
@ -407,8 +425,8 @@ HashTable<T, Key, Hash>::const_iterator::operator++()
template<class T, class Key, class Hash>
inline typename HashTable<T, Key, Hash>::const_iterator
HashTable<T, Key, Hash>::const_iterator::operator++
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::const_iterator::operator++
(
int
)
@ -421,15 +439,15 @@ HashTable<T, Key, Hash>::const_iterator::operator++
template<class T, class Key, class Hash>
inline
const Key& HashTable<T, Key, Hash>::const_iterator::key()
const Key& Foam::HashTable<T, Key, Hash>::const_iterator::key()
{
return elmtPtr_->key_;
}
template<class T, class Key, class Hash>
inline typename HashTable<T, Key, Hash>::const_iterator
HashTable<T, Key, Hash>::begin() const
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::begin() const
{
label i = 0;
@ -455,15 +473,11 @@ HashTable<T, Key, Hash>::begin() const
template<class T, class Key, class Hash>
inline const typename HashTable<T, Key, Hash>::const_iterator&
HashTable<T, Key, Hash>::end() const
inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
Foam::HashTable<T, Key, Hash>::end() const
{
return HashTable<T, Key, Hash>::endConstIter_;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //