Commit 39159a92 authored by Mark Olesen's avatar Mark Olesen
Browse files

HashTbl: change allocation of end iterators

- the end iterators and canonicalSize are independent of the templated
  parameters, so move them to HashTblCore

- this avoid allocating separate end iterator at all.

  end(), cend() return a iteratorEnd.
  iterator == iteratorEnd just checks for non-NULL pointer on iterator.
  The compiler should optimize most of it away.
parent a237b7ce
......@@ -61,7 +61,7 @@ $(sha1)/SHA1Digest.C
primitives/random/Random.C
containers/HashTables/HashTbl/HashTblName.C
containers/HashTables/HashTbl/HashTblCore.C
containers/HashTables/HashTable/HashTableName.C
containers/HashTables/StaticHashTable/StaticHashTableName.C
containers/Lists/SortableList/ParSortableListName.C
......
......@@ -32,8 +32,7 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::label Foam::HashTbl<T, Key, Hash>::canonicalSize(const label size)
Foam::label Foam::HashTblCore::canonicalSize(const label size)
{
if (size < 1)
{
......@@ -62,9 +61,9 @@ Foam::label Foam::HashTbl<T, Key, Hash>::canonicalSize(const label size)
template<class T, class Key, class Hash>
Foam::HashTbl<T, Key, Hash>::HashTbl(const label size)
:
HashTblName(),
HashTblCore(),
nElmts_(0),
tableSize_(canonicalSize(size)),
tableSize_(HashTblCore::canonicalSize(size)),
table_(NULL)
{
if (tableSize_)
......@@ -82,7 +81,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl(const label size)
template<class T, class Key, class Hash>
Foam::HashTbl<T, Key, Hash>::HashTbl(const HashTbl<T, Key, Hash>& ht)
:
HashTblName(),
HashTblCore(),
nElmts_(0),
tableSize_(ht.tableSize_),
table_(NULL)
......@@ -109,7 +108,7 @@ Foam::HashTbl<T, Key, Hash>::HashTbl
const Xfer<HashTbl<T, Key, Hash> >& ht
)
:
HashTblName(),
HashTblCore(),
nElmts_(0),
tableSize_(0),
table_(NULL)
......@@ -189,7 +188,7 @@ Foam::HashTbl<T, Key, Hash>::find
}
# endif
return end();
return iterator();
}
......@@ -221,7 +220,7 @@ Foam::HashTbl<T, Key, Hash>::find
}
# endif
return cend();
return const_iterator();
}
......@@ -463,7 +462,7 @@ Foam::label Foam::HashTbl<T, Key, Hash>::erase
template<class T, class Key, class Hash>
void Foam::HashTbl<T, Key, Hash>::resize(const label sz)
{
label newSize = canonicalSize(sz);
label newSize = HashTblCore::canonicalSize(sz);
if (newSize == tableSize_)
{
......@@ -532,7 +531,7 @@ void Foam::HashTbl<T, Key, Hash>::clearStorage()
template<class T, class Key, class Hash>
void Foam::HashTbl<T, Key, Hash>::shrink()
{
const label newSize = canonicalSize(nElmts_);
const label newSize = HashTblCore::canonicalSize(nElmts_);
if (newSize < tableSize_)
{
......
......@@ -71,20 +71,53 @@ Ostream& operator<<(Ostream&, const HashTbl<T, Key, Hash>&);
/*---------------------------------------------------------------------------*\
Class HashTblName Declaration
Class HashTblCore Declaration
\*---------------------------------------------------------------------------*/
TemplateName(HashTbl);
//- Template-invariant bits for HashTbl
struct HashTblCore
{
//- Return a canonical (power-of-two) size
static label canonicalSize(const label);
//- Construct null
HashTblCore()
{}
//- Define template name and debug
ClassName("HashTbl");
//- A zero-sized end iterator
struct iteratorEnd
{
//- Construct null
iteratorEnd()
{}
};
//- iteratorEnd set to beyond the end of any HashTbl
static iteratorEnd cend()
{
return iteratorEnd();
}
//- iteratorEnd set to beyond the end of any HashTbl
static iteratorEnd end()
{
return iteratorEnd();
}
};
/*---------------------------------------------------------------------------*\
Class HashTbl Declaration
Class HashTbl Declaration
\*---------------------------------------------------------------------------*/
template<class T, class Key=word, class Hash=string::hash>
class HashTbl
:
public HashTblName
public HashTblCore
{
// Private data type for table entries
......@@ -368,6 +401,10 @@ public:
//- Compare hashedEntry element pointers
inline bool operator==(const iteratorBase&) const;
inline bool operator!=(const iteratorBase&) const;
//- Compare hashedEntry to iteratorEnd pointers
inline bool operator==(const iteratorEnd&) const;
inline bool operator!=(const iteratorEnd&) const;
};
......@@ -401,6 +438,9 @@ public:
//- Construct null (end iterator)
inline iterator();
//- Construct end iterator
inline iterator(const iteratorEnd&);
// Member operators
//- Conversion to a const_iterator
......@@ -423,9 +463,6 @@ public:
//- iterator set to the begining of the HashTbl
inline iterator begin();
//- iterator set to beyond the end of the HashTbl
inline const iterator& end();
// STL const_iterator
......@@ -459,6 +496,9 @@ public:
//- Construct null (end iterator)
inline const_iterator();
//- Construct end iterator
inline const_iterator(const iteratorEnd&);
// Member operators
// Access
......@@ -476,15 +516,9 @@ public:
//- const_iterator set to the beginning of the HashTbl
inline const_iterator cbegin() const;
//- const_iterator set to beyond the end of the HashTbl
inline const const_iterator& cend() const;
//- const_iterator set to the beginning of the HashTbl
inline const_iterator begin() const;
//- const_iterator set to beyond the end of the HashTbl
inline const const_iterator& end() const;
// IOstream Operator
......@@ -500,14 +534,6 @@ public:
const HashTbl<T, Key, Hash>&
);
private:
//- iterator returned by end()
iterator endIter_;
//- const_iterator returned by end()
const_iterator endConstIter_;
};
......
......@@ -28,6 +28,6 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(Foam::HashTblName, 0);
defineTypeNameAndDebug(Foam::HashTblCore, 0);
// ************************************************************************* //
......@@ -196,10 +196,6 @@ inline Foam::HashTbl<T, Key, Hash>::iteratorBase::iteratorBase
entryPtr_ = 0;
hashIndex_ = 0;
}
else
{
Info<<"OK key is " << this->key() << endl;
}
}
}
......@@ -229,12 +225,21 @@ Foam::HashTbl<T, Key, Hash>::iteratorBase::increment()
// but we wish to continue at 'X-1'
hashIndex_ = -hashIndex_ - 2;
}
else if (entryPtr_ && entryPtr_->next_)
else if (entryPtr_)
{
// Move to next element on the SLList
entryPtr_ = entryPtr_->next_;
return;
if (entryPtr_->next_)
{
// Move to next element on the SLList
entryPtr_ = entryPtr_->next_;
return;
}
}
// else
// {
// // if we reach here (entryPtr_ is NULL) it is already at the end()
// // we should probably stop
// }
// Step to the next table entry
while
......@@ -297,6 +302,26 @@ inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator!=
}
template<class T, class Key, class Hash>
inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator==
(
const iteratorEnd&
) const
{
return !entryPtr_;
}
template<class T, class Key, class Hash>
inline bool Foam::HashTbl<T, Key, Hash>::iteratorBase::operator!=
(
const iteratorEnd&
) const
{
return entryPtr_;
}
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......@@ -306,6 +331,16 @@ inline Foam::HashTbl<T, Key, Hash>::iterator::iterator()
{}
template<class T, class Key, class Hash>
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator
(
const iteratorEnd&
)
:
iteratorBase()
{}
template<class T, class Key, class Hash>
inline Foam::HashTbl<T, Key, Hash>::iterator::iterator
(
......@@ -399,14 +434,6 @@ Foam::HashTbl<T, Key, Hash>::begin()
}
template<class T, class Key, class Hash>
inline const typename Foam::HashTbl<T, Key, Hash>::iterator&
Foam::HashTbl<T, Key, Hash>::end()
{
return HashTbl<T, Key, Hash>::endIter_;
}
// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......@@ -416,6 +443,16 @@ inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator()
{}
template<class T, class Key, class Hash>
inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator
(
const iteratorEnd&
)
:
iteratorBase()
{}
template<class T, class Key, class Hash>
inline Foam::HashTbl<T, Key, Hash>::const_iterator::const_iterator
(
......@@ -482,14 +519,6 @@ Foam::HashTbl<T, Key, Hash>::cbegin() const
}
template<class T, class Key, class Hash>
inline const typename Foam::HashTbl<T, Key, Hash>::const_iterator&
Foam::HashTbl<T, Key, Hash>::cend() const
{
return HashTbl<T, Key, Hash>::endConstIter_;
}
template<class T, class Key, class Hash>
inline typename Foam::HashTbl<T, Key, Hash>::const_iterator
Foam::HashTbl<T, Key, Hash>::begin() const
......@@ -498,12 +527,4 @@ Foam::HashTbl<T, Key, Hash>::begin() const
}
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 this->cend();
}
// ************************************************************************* //
......@@ -33,9 +33,9 @@ License
template<class T, class Key, class Hash>
Foam::HashTbl<T, Key, Hash>::HashTbl(Istream& is, const label size)
:
HashTblName(),
HashTblCore(),
nElmts_(0),
tableSize_(canonicalSize(size)),
tableSize_(HashTblCore::canonicalSize(size)),
table_(NULL)
{
if (tableSize_)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment