mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
HashTbl - iterator/const_iterator implemented in terms of an iteratorBase
This commit is contained in:
@ -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,19 +403,11 @@ public:
|
||||
|
||||
// Member operators
|
||||
|
||||
//- Conversion to a const_iterator
|
||||
inline operator const_iterator() const;
|
||||
|
||||
// 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;
|
||||
|
||||
//- Compare hashedEntry element pointers
|
||||
inline bool operator==(const const_iterator&) const;
|
||||
inline bool operator!=(const const_iterator&) const;
|
||||
|
||||
//- Return referenced hash value
|
||||
inline T& operator*();
|
||||
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,10 +293,29 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator!=
|
||||
const iteratorBase& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ != iter.elmtPtr_;
|
||||
return entryPtr_ != iter.entryPtr_;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator()
|
||||
:
|
||||
iteratorBase()
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator
|
||||
(
|
||||
HashTbl<T, Key, Hash>* hashTbl
|
||||
)
|
||||
:
|
||||
iteratorBase(hashTbl)
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator
|
||||
(
|
||||
@ -305,58 +324,18 @@ inline Foam::HashTbl<T, Key, Hash>::iterator::iterator
|
||||
const label hashIndex
|
||||
)
|
||||
:
|
||||
hashTable_(hashTbl),
|
||||
elmtPtr_(elmt),
|
||||
hashIndex_(hashIndex)
|
||||
iteratorBase(hashTbl, elmt, hashIndex)
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator()
|
||||
:
|
||||
hashTable_(0),
|
||||
elmtPtr_(0),
|
||||
hashIndex_(0)
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::iterator::operator==
|
||||
(
|
||||
const iterator& iter
|
||||
) const
|
||||
inline Foam::HashTbl<T, Key, Hash>::iterator::operator
|
||||
typename Foam::HashTbl<T, Key, Hash>::const_iterator() const
|
||||
{
|
||||
return elmtPtr_ == iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::iterator::operator!=
|
||||
(
|
||||
const iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ != iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::iterator::operator==
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ == iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::iterator::operator!=
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ != iter.elmtPtr_;
|
||||
return *reinterpret_cast
|
||||
<
|
||||
const typename Foam::HashTbl<T, Key, Hash>::const_iterator*
|
||||
>(this);
|
||||
}
|
||||
|
||||
|
||||
@ -364,7 +343,7 @@ template<class T, class Key, class Hash>
|
||||
inline T&
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator*()
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->object();
|
||||
}
|
||||
|
||||
|
||||
@ -372,7 +351,7 @@ template<class T, class Key, class Hash>
|
||||
inline T&
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator()()
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->object();
|
||||
}
|
||||
|
||||
|
||||
@ -380,7 +359,7 @@ template<class T, class Key, class Hash>
|
||||
inline const T&
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator*() const
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->cobject();
|
||||
}
|
||||
|
||||
|
||||
@ -388,7 +367,7 @@ template<class T, class Key, class Hash>
|
||||
inline const T&
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator()() const
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->cobject();
|
||||
}
|
||||
|
||||
|
||||
@ -397,56 +376,18 @@ inline
|
||||
typename Foam::HashTbl<T, Key, Hash>::iterator&
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator++()
|
||||
{
|
||||
// A negative index is a special value from erase
|
||||
if (hashIndex_ < 0)
|
||||
{
|
||||
// old position 'X' was marked as '-(X+1)'
|
||||
// but we wish to continue at 'X-1'
|
||||
hashIndex_ = -hashIndex_ - 2;
|
||||
}
|
||||
else if (elmtPtr_ && elmtPtr_->next_)
|
||||
{
|
||||
// Do we have additional elements on the SLList?
|
||||
elmtPtr_ = elmtPtr_->next_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Step to the next table entry
|
||||
while
|
||||
(
|
||||
++hashIndex_ < hashTable_->tableSize_
|
||||
&& !(elmtPtr_ = hashTable_->table_[hashIndex_])
|
||||
)
|
||||
{}
|
||||
|
||||
if (hashIndex_ >= hashTable_->tableSize_)
|
||||
{
|
||||
// make an end iterator
|
||||
elmtPtr_ = 0;
|
||||
hashIndex_ = 0;
|
||||
}
|
||||
this->increment();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline typename Foam::HashTbl<T, Key, Hash>::iterator
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator++
|
||||
(
|
||||
int
|
||||
)
|
||||
Foam::HashTbl<T, Key, Hash>::iterator::operator++(int)
|
||||
{
|
||||
iterator tmp = *this;
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline
|
||||
const Key& Foam::HashTbl<T, Key, Hash>::iterator::key() const
|
||||
{
|
||||
return elmtPtr_->key_;
|
||||
iterator old = *this;
|
||||
this->increment();
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
@ -454,33 +395,7 @@ template<class T, class Key, class Hash>
|
||||
inline typename Foam::HashTbl<T, Key, Hash>::iterator
|
||||
Foam::HashTbl<T, Key, Hash>::begin()
|
||||
{
|
||||
label hashIdx = 0;
|
||||
|
||||
if (nElmts_)
|
||||
{
|
||||
while (table_ && !table_[hashIdx] && ++hashIdx < tableSize_)
|
||||
{}
|
||||
}
|
||||
else
|
||||
{
|
||||
hashIdx = tableSize_;
|
||||
}
|
||||
|
||||
if (hashIdx >= tableSize_)
|
||||
{
|
||||
# ifdef FULLDEBUG
|
||||
if (debug)
|
||||
{
|
||||
Info<< "HashTbl is empty\n";
|
||||
}
|
||||
# endif
|
||||
|
||||
return HashTbl<T, Key, Hash>::endIter_;
|
||||
}
|
||||
else
|
||||
{
|
||||
return iterator(this, table_[hashIdx], hashIdx);
|
||||
}
|
||||
return iterator(this);
|
||||
}
|
||||
|
||||
|
||||
@ -497,9 +412,17 @@ Foam::HashTbl<T, Key, Hash>::end()
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator()
|
||||
:
|
||||
hashTable_(0),
|
||||
elmtPtr_(0),
|
||||
hashIndex_(0)
|
||||
iteratorBase()
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator
|
||||
(
|
||||
const HashTbl<T, Key, Hash>* hashTbl
|
||||
)
|
||||
:
|
||||
iteratorBase(hashTbl)
|
||||
{}
|
||||
|
||||
|
||||
@ -511,76 +434,23 @@ inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator
|
||||
const label hashIndex
|
||||
)
|
||||
:
|
||||
hashTable_(hashTbl),
|
||||
elmtPtr_(elmt),
|
||||
hashIndex_(hashIndex)
|
||||
iteratorBase(hashTbl, elmt, hashIndex)
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator
|
||||
(
|
||||
const iterator& iter
|
||||
)
|
||||
:
|
||||
hashTable_(iter.hashTable_),
|
||||
elmtPtr_(iter.elmtPtr_),
|
||||
hashIndex_(iter.hashIndex_)
|
||||
{}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::const_iterator::operator==
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ == iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::const_iterator::operator!=
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ != iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::const_iterator::operator==
|
||||
(
|
||||
const iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ == iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTbl<T, Key, Hash>::const_iterator::operator!=
|
||||
(
|
||||
const iterator& iter
|
||||
) const
|
||||
{
|
||||
return elmtPtr_ != iter.elmtPtr_;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline const T&
|
||||
Foam::HashTbl<T, Key, Hash>::const_iterator::operator*() const
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->cobject();
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline const T&
|
||||
Foam::HashTbl<T, Key, Hash>::const_iterator::operator()() const
|
||||
{
|
||||
return elmtPtr_->obj_;
|
||||
return this->cobject();
|
||||
}
|
||||
|
||||
|
||||
@ -589,43 +459,18 @@ inline
|
||||
typename Foam::HashTbl<T, Key, Hash>::const_iterator&
|
||||
Foam::HashTbl<T, Key, Hash>::const_iterator::operator++()
|
||||
{
|
||||
if
|
||||
(
|
||||
!(elmtPtr_ = elmtPtr_->next_)
|
||||
&& ++hashIndex_ < hashTable_->tableSize_
|
||||
&& !(elmtPtr_ = hashTable_->table_[hashIndex_])
|
||||
)
|
||||
{
|
||||
while
|
||||
(
|
||||
++hashIndex_ < hashTable_->tableSize_
|
||||
&& !(elmtPtr_ = hashTable_->table_[hashIndex_])
|
||||
)
|
||||
{}
|
||||
}
|
||||
|
||||
this->increment();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline typename Foam::HashTbl<T, Key, Hash>::const_iterator
|
||||
Foam::HashTbl<T, Key, Hash>::const_iterator::operator++
|
||||
(
|
||||
int
|
||||
)
|
||||
Foam::HashTbl<T, Key, Hash>::const_iterator::operator++(int)
|
||||
{
|
||||
const_iterator tmp = *this;
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline
|
||||
const Key& Foam::HashTbl<T, Key, Hash>::const_iterator::key() const
|
||||
{
|
||||
return elmtPtr_->key_;
|
||||
const_iterator old = *this;
|
||||
this->increment();
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
@ -633,33 +478,7 @@ template<class T, class Key, class Hash>
|
||||
inline typename Foam::HashTbl<T, Key, Hash>::const_iterator
|
||||
Foam::HashTbl<T, Key, Hash>::cbegin() const
|
||||
{
|
||||
label hashIdx = 0;
|
||||
|
||||
if (nElmts_)
|
||||
{
|
||||
while (table_ && !table_[hashIdx] && ++hashIdx < tableSize_)
|
||||
{}
|
||||
}
|
||||
else
|
||||
{
|
||||
hashIdx = tableSize_;
|
||||
}
|
||||
|
||||
if (hashIdx >= tableSize_)
|
||||
{
|
||||
# ifdef FULLDEBUG
|
||||
if (debug)
|
||||
{
|
||||
Info<< "HashTbl is empty\n";
|
||||
}
|
||||
# endif
|
||||
|
||||
return HashTbl<T, Key, Hash>::endConstIter_;
|
||||
}
|
||||
else
|
||||
{
|
||||
return const_iterator(this, table_[hashIdx], hashIdx);
|
||||
}
|
||||
return const_iterator(this);
|
||||
}
|
||||
|
||||
|
||||
@ -683,7 +502,7 @@ template<class T, class Key, class Hash>
|
||||
inline const typename Foam::HashTbl<T, Key, Hash>::const_iterator&
|
||||
Foam::HashTbl<T, Key, Hash>::end() const
|
||||
{
|
||||
return HashTbl<T, Key, Hash>::endConstIter_;
|
||||
return this->cend();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -36,13 +36,16 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(Istream& is, const label size)
|
||||
HashTblName(),
|
||||
nElmts_(0),
|
||||
tableSize_(canonicalSize(size)),
|
||||
table_(new hashedEntry*[tableSize_]),
|
||||
endIter_(),
|
||||
endConstIter_()
|
||||
table_(NULL)
|
||||
{
|
||||
for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
|
||||
if (tableSize_)
|
||||
{
|
||||
table_[hashIdx] = 0;
|
||||
table_ = new hashedEntry*[tableSize_];
|
||||
|
||||
for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
|
||||
{
|
||||
table_[hashIdx] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
operator>>(is, *this);
|
||||
|
||||
Reference in New Issue
Block a user