Commit a237b7ce authored by Mark Olesen's avatar Mark Olesen
Browse files

HashTbl - iterator/const_iterator implemented in terms of an iteratorBase

parent 3a0c427c
......@@ -65,9 +65,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(const label size)
HashTblName(),
nElmts_(0),
tableSize_(canonicalSize(size)),
table_(NULL),
endIter_(),
endConstIter_()
table_(NULL)
{
if (tableSize_)
{
......@@ -87,9 +85,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(const HashTbl<T, Key, Hash>& ht)
HashTblName(),
nElmts_(0),
tableSize_(ht.tableSize_),
table_(NULL),
endIter_(),
endConstIter_()
table_(NULL)
{
if (tableSize_)
{
......@@ -116,9 +112,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl
HashTblName(),
nElmts_(0),
tableSize_(0),
table_(NULL),
endIter_(),
endConstIter_()
table_(NULL)
{
transfer(ht());
}
......@@ -344,10 +338,10 @@ bool Foam::HashTbl<T, Key, Hash>::set
template<class T, class Key, class Hash>
bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
{
// note: elmtPtr_ is NULL for end(), so this catches that too
if (elmtPtr_)
// note: entryPtr_ is NULL for end(), so this catches that too
if (entryPtr_)
{
// Search element before elmtPtr_
// Search element before entryPtr_
hashedEntry* prev = 0;
for
......@@ -357,7 +351,7 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
ep = ep->next_
)
{
if (ep == elmtPtr_)
if (ep == entryPtr_)
{
break;
}
......@@ -366,19 +360,20 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
if (prev)
{
// has an element before elmtPtr - reposition to there
prev->next_ = elmtPtr_->next_;
delete elmtPtr_;
elmtPtr_ = prev;
// has an element before entryPtr - reposition to there
prev->next_ = entryPtr_->next_;
delete entryPtr_;
entryPtr_ = prev;
}
else
{
// elmtPtr was first element on SLList
hashTable_->table_[hashIndex_] = elmtPtr_->next_;
delete elmtPtr_;
// entryPtr was first element on SLList
hashTable_->table_[hashIndex_] = entryPtr_->next_;
delete entryPtr_;
// assign any non-NULL value so it doesn't look like end()/cend()
elmtPtr_ = reinterpret_cast<hashedEntry*>(hashTable_);
// assign any non-NULL pointer value so it doesn't look
// like end()/cend()
entryPtr_ = reinterpret_cast<hashedEntry*>(this);
// Mark with special hashIndex value to signal it has been rewound.
// The next increment will bring it back to the present location.
......@@ -411,92 +406,15 @@ bool Foam::HashTbl<T, Key, Hash>::iteratorBase::erase()
template<class T, class Key, class Hash>
bool Foam::HashTbl<T, Key, Hash>::erase(const iterator& cit)
{
// note: elmtPtr_ is NULL for end(), so this catches that too
if (cit.elmtPtr_)
{
// Search element before elmtPtr_
hashedEntry* prev = 0;
for (hashedEntry* ep = table_[cit.hashIndex_]; ep; ep = ep->next_)
{
if (ep == cit.elmtPtr_)
{
break;
}
prev = ep;
}
// adjust iterator after erase
iterator& iter = const_cast<iterator&>(cit);
if (prev)
{
// has an element before elmtPtr - reposition to there
prev->next_ = iter.elmtPtr_->next_;
delete iter.elmtPtr_;
iter.elmtPtr_ = prev;
}
else
{
// elmtPtr was first element on SLList
table_[iter.hashIndex_] = iter.elmtPtr_->next_;
delete iter.elmtPtr_;
// assign an non-NULL value so it doesn't look like end()/cend()
iter.elmtPtr_ = reinterpret_cast<hashedEntry*>(this);
// Mark with special hashIndex value to signal it has been rewound.
// The next increment will bring it back to the present location.
//
// For the current position 'X', mark it as '-(X+1)', which is
// written as '-X-1' to avoid overflow.
// The extra '-1' is needed to avoid ambiguity for position '0'.
// To retrieve the previous position 'X-1' we would later
// use '-(-X-1) - 2'
iter.hashIndex_ = -iter.hashIndex_ - 1;
}
nElmts_--;
# ifdef FULLDEBUG
if (debug)
{
Info<< "HashTbl<T, Key, Hash>::erase(const iterator&) : "
<< "hashedEntry " << iter.elmtPtr_->key_ << " removed.\n";
}
# endif
return true;
}
else
{
# ifdef FULLDEBUG
if (debug)
{
Info<< "HashTbl<T, Key, Hash>::erase(const iterator&) : "
<< "cannot remove hashedEntry from hash table\n";
}
# endif
return false;
}
// adjust iterator after erase
return const_cast<iterator&>(cit).erase();
}
template<class T, class Key, class Hash>
bool Foam::HashTbl<T, Key, Hash>::erase(const Key& key)
{
// we could also simply do: erase(find(key));
iterator fnd = find(key);
if (fnd != end())
{
return erase(fnd);
}
else
{
return false;
}
return erase(find(key));
}
......@@ -560,22 +478,22 @@ void Foam::HashTbl<T, Key, Hash>::resize(const label sz)
return;
}
HashTbl<T, Key, Hash>* newTable = new HashTbl<T, Key, Hash>(newSize);
HashTbl<T, Key, Hash>* tmpTable = new HashTbl<T, Key, Hash>(newSize);
for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{
newTable->insert(iter.key(), *iter);
tmpTable->insert(iter.key(), *iter);
}
label oldTableSize = tableSize_;
tableSize_ = newTable->tableSize_;
newTable->tableSize_ = oldTableSize;
label oldSize = tableSize_;
tableSize_ = tmpTable->tableSize_;
tmpTable->tableSize_ = oldSize;
hashedEntry** oldTable = table_;
table_ = newTable->table_;
newTable->table_ = oldTable;
table_ = tmpTable->table_;
tmpTable->table_ = oldTable;
delete newTable;
delete tmpTable;
}
......@@ -687,7 +605,7 @@ bool Foam::HashTbl<T, Key, Hash>::operator==
const HashTbl<T, Key, Hash>& rhs
) const
{
// sizes (ie, number of keys) must match
// sizes (number of keys) must match
if (size() != rhs.size())
{
return false;
......
......@@ -88,6 +88,7 @@ class HashTbl
{
// Private data type for table entries
//- Structure to hold a hashed entry with SLList for collisions
struct hashedEntry
{
//- The lookup key
......@@ -99,18 +100,15 @@ class HashTbl
//- The data object
T obj_;
//- Constructors
//- Construct from key, next pointer and object
inline hashedEntry(const Key&, hashedEntry* next, const T&);
//- Construct given key, next pointer and object
inline hashedEntry
(
const Key&,
hashedEntry* next,
const T& newEntry
);
private:
//- Disallow default bitwise copy construct
hashedEntry(const hashedEntry&);
//- Dissallow construction as copy
hashedEntry(const hashedEntry&);
//- Disallow default bitwise assignment
void operator=(const hashedEntry&);
};
......@@ -222,7 +220,7 @@ public:
inline bool set(const Key&, const T& newElmt);
//- Erase a hashedEntry specified by given iterator
// This invalidates the iterator until the operator++
// This invalidates the iterator until the next operator++
bool erase(const iterator&);
//- Erase a hashedEntry specified by the given key
......@@ -235,7 +233,7 @@ public:
//- Remove entries given by the given keys from this HashTbl
// Return the number of elements removed.
// The parameter HashTbl needs the same type of key, but the
// type of values held and the hashing function is arbitrary.
// type of values held and the hashing function are arbitrary.
template<class AnyType, class AnyHash>
label erase(const HashTbl<AnyType, Key, AnyHash>&);
......@@ -306,24 +304,23 @@ public:
//- The iterator base for HashTbl
// Note: data and functions are protected, to allow reuse by iterator
// and prevent most external usage.
// iterator and const_iterator have the same size, allowing
// us to reinterpret_cast between them (if desired)
class iteratorBase
{
friend class HashTbl;
public:
// Protected Data
// Private Data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
HashTbl<T, Key, Hash>* hashTable_;
//- Current element
hashedEntry* elmtPtr_;
hashedEntry* entryPtr_;
//- Current hash index
label hashIndex_;
protected:
// Protected Member Functions
......@@ -333,22 +330,32 @@ public:
inline iteratorBase();
//- Construct from hash table, moving to its 'begin' position
inline iteratorBase
inline explicit iteratorBase
(
const HashTbl<T, Key, Hash>* curHashTbl
);
//- Construct from hash table, element and hash index
inline iteratorBase
inline explicit iteratorBase
(
const HashTbl<T, Key, Hash>* curHashTbl,
const hashedEntry* elmt,
const label hashIndex
);
inline void incr();
//- Increment to the next position
inline void increment();
//- Erase the HashTbl element at the current position
bool erase();
//- Return non-const access to referenced object
inline T& object();
//- Return const access to referenced object
inline const T& cobject() const;
public:
// Member operators
......@@ -358,9 +365,6 @@ public:
//- Return the Key corresponding to the iterator
inline const Key& key() const;
//- Return referenced object
inline const T& object() const;
//- Compare hashedEntry element pointers
inline bool operator==(const iteratorBase&) const;
inline bool operator!=(const iteratorBase&) const;
......@@ -369,26 +373,21 @@ public:
//- An STL-conforming iterator
class iterator
:
public iteratorBase
{
friend class HashTbl;
friend class const_iterator;
// Private data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
HashTbl<T, Key, Hash>* hashTable_;
//- Current element
hashedEntry* elmtPtr_;
//- Current hash index
label hashIndex_;
// Private Member Functions
//- Construct from hash table, moving to its 'begin' position
inline explicit iterator
(
HashTbl<T, Key, Hash>* curHashTbl
);
//- Construct from hash table, element and hash index
inline iterator
inline explicit iterator
(
HashTbl<T, Key, Hash>* curHashTbl,
hashedEntry* elmt,
......@@ -404,18 +403,10 @@ public:
// Member operators
// Access
//- Return the Key corresponding to the iterator
inline const Key& key() const;
//- Compare hashedEntry element pointers
inline bool operator==(const iterator&) const;
inline bool operator!=(const iterator&) const;
//- Conversion to a const_iterator
inline operator const_iterator() const;
//- Compare hashedEntry element pointers
inline bool operator==(const const_iterator&) const;
inline bool operator!=(const const_iterator&) const;
// Access
//- Return referenced hash value
inline T& operator*();
......@@ -429,9 +420,6 @@ public:
inline iterator operator++(int);
};
//- iterator set to the begining of the HashTbl
inline iteratorBase beginBase();
//- iterator set to the begining of the HashTbl
inline iterator begin();
......@@ -443,26 +431,21 @@ public:
//- An STL-conforming const_iterator
class const_iterator
:
public iteratorBase
{
friend class HashTbl;
friend class iterator;
// Private data
//- Pointer to the HashTbl for which this is an iterator
// This also lets us use the default bitwise copy/assignment
const HashTbl<T, Key, Hash>* hashTable_;
//- Current element
const hashedEntry* elmtPtr_;
//- Current hash index
label hashIndex_;
// Private Member Functions
//- Construct from hash table, moving to its 'begin' position
inline explicit const_iterator
(
const HashTbl<T, Key, Hash>* curHashTbl
);
//- Construct from hash table, element and hash index
inline const_iterator
inline explicit const_iterator
(
const HashTbl<T, Key, Hash>* curHashTbl,
const hashedEntry* elmt,
......@@ -476,25 +459,10 @@ public:
//- Construct null (end iterator)
inline const_iterator();
//- Construct from the non-const iterator
inline const_iterator(const iterator&);
// Member operators
// Access
//- Return the Key corresponding to the iterator
inline const Key& key() const;
//- Compare hashedEntry element pointers
inline bool operator==(const const_iterator&) const;
inline bool operator!=(const const_iterator&) const;
//- Compare hashedEntry element pointers
inline bool operator==(const iterator&) const;
inline bool operator!=(const iterator&) const;
//- Return referenced hash value
inline const T& operator*() const;
inline const T& operator()() const;
......
......@@ -33,12 +33,12 @@ inline Foam::HashTbl<T, Key, Hash>::hashedEntry::hashedEntry
(
const Key& key,
hashedEntry* next,
const T& newEntry
const T& obj
)
:
key_(key),
next_(next),
obj_(newEntry)
obj_(obj)
{}
......@@ -159,13 +159,13 @@ inline T& Foam::HashTbl<T, Key, Hash>::operator()(const Key& key)
}
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase()
:
hashTable_(0),
elmtPtr_(0),
entryPtr_(0),
hashIndex_(0)
{}
......@@ -177,7 +177,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
)
:
hashTable_(const_cast<HashTbl<T, Key, Hash>*>(hashTbl)),
elmtPtr_(0),
entryPtr_(0),
hashIndex_(0)
{
if (hashTable_->nElmts_ && hashTable_->table_)
......@@ -185,7 +185,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
// find first non-NULL table entry
while
(
!(elmtPtr_ = hashTable_->table_[hashIndex_])
!(entryPtr_ = hashTable_->table_[hashIndex_])
&& ++hashIndex_ < hashTable_->tableSize_
)
{}
......@@ -193,7 +193,7 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
if (hashIndex_ >= hashTable_->tableSize_)
{
// make into an end iterator
elmtPtr_ = 0;
entryPtr_ = 0;
hashIndex_ = 0;
}
else
......@@ -213,22 +213,14 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
)
:
hashTable_(const_cast<HashTbl<T, Key, Hash>*>(hashTbl)),
elmtPtr_(const_cast<hashedEntry*>(elmt)),
entryPtr_(const_cast<hashedEntry*>(elmt)),
hashIndex_(hashIndex)
{}
template<class T, class Key, class Hash>
inline typename Foam::HashTbl<T, Key, Hash>::iteratorBase
Foam::HashTbl<T, Key, Hash>::beginBase()
{
return iteratorBase(this);
}
template<class T, class Key, class Hash>
inline void
Foam::HashTbl<T, Key, Hash>::iteratorBase::incr()
Foam::HashTbl<T, Key, Hash>::iteratorBase::increment()
{
// A negative index is a special value from erase
if (hashIndex_ < 0)
......@@ -237,10 +229,10 @@ Foam::HashTbl<T, Key, Hash>::iteratorBase::incr()
// but we wish to continue at 'X-1'
hashIndex_ = -hashIndex_ - 2;
}
else if (elmtPtr_ && elmtPtr_->next_)
else if (entryPtr_ && entryPtr_->next_)
{
// Do we have additional elements on the SLList?
elmtPtr_ = elmtPtr_->next_;
// Move to next element on the SLList
entryPtr_ = entryPtr_->next_;
return;
}
......@@ -248,14 +240,14 @@ Foam::HashTbl<T, Key, Hash>::iteratorBase::incr()
while
(
++hashIndex_ < hashTable_->tableSize_
&& !(elmtPtr_ = hashTable_->table_[hashIndex_])
&& !(entryPtr_ = hashTable_->table_[hashIndex_])
)
{}
if (hashIndex_ >= hashTable_->tableSize_)
{
// make into an end iterator
elmtPtr_ = 0;
entryPtr_ = 0;
hashIndex_ = 0;
}
}
......@@ -265,15 +257,23 @@ template<class T, class Key, class Hash>
inline
const Key& Foam::HashTbl<T, Key, Hash>::iteratorBase::key() const
{
return elmtPtr_->key_;
return entryPtr_->key_;
}
template<class T, class Key, class Hash>
inline T&
Foam::HashTbl<T, Key, Hash>::iteratorBase::object()
{
return entryPtr_->obj_;
}
template<class T, class Key, class Hash>
inline const T&
Foam::HashTbl<T, Key, Hash>::iteratorBase::object() const
Foam::HashTbl<T, Key, Hash>::iteratorBase::cobject() const
{
return elmtPtr_->obj_;
return entryPtr_->obj_;
}
......@@ -283,7 +283,7 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator==
const iteratorBase& iter
) const
{
return elmtPtr_ == iter.elmtPtr_;
return entryPtr_ == iter.entryPtr_;
}
......@@ -293,70 +293,49 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator!=