/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template
inline Foam::label
Foam::StaticHashTable::hashKeyIndex(const Key& key) const
{
// size is power of two - this is the modulus
return Hash()(key) & (keys_.size() - 1);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
inline Foam::label Foam::StaticHashTable::size() const
{
return nElmts_;
}
template
inline bool Foam::StaticHashTable::empty() const
{
return !nElmts_;
}
template
inline bool Foam::StaticHashTable::insert
(
const Key& key,
const T& newEntry
)
{
return set(key, newEntry, true);
}
template
inline bool Foam::StaticHashTable::set
(
const Key& key,
const T& newEntry
)
{
return set(key, newEntry, false);
}
template
inline Foam::Xfer>
Foam::StaticHashTable::xfer()
{
return xferMove(*this);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
inline T& Foam::StaticHashTable::operator[](const Key& key)
{
iterator iter = find(key);
if (iter == end())
{
FatalErrorInFunction
<< toc()
<< exit(FatalError);
}
return *iter;
}
template
inline const T& Foam::StaticHashTable::operator[]
(
const Key& key
) const
{
const_iterator iter = find(key);
if (iter == cend())
{
FatalErrorInFunction
<< toc()
<< exit(FatalError);
}
return *iter;
}
template
inline T& Foam::StaticHashTable::operator()(const Key& key)
{
iterator iter = find(key);
if (iter == end())
{
insert(key, T());
return *find(key);
}
else
{
return *iter;
}
}
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
template
template
inline Foam::StaticHashTable::Iterator::Iterator
(
TableRef hashTbl,
label hashIndex,
label elemIndex
)
:
hashTable_(hashTbl),
hashIndex_(hashIndex),
elemIndex_(elemIndex)
{}
template
template
inline Foam::StaticHashTable::Iterator::Iterator
(
const iterator& iter
)
:
hashTable_(iter.hashTable_),
hashIndex_(iter.hashIndex_),
elemIndex_(iter.elemIndex_)
{}
template
template
inline void
Foam::StaticHashTable::Iterator::operator=
(
const iterator& iter
)
{
this->hashIndex_ = iter.hashIndex_;
this->elemIndex_ = iter.elemIndex_;
}
template
template
inline bool
Foam::StaticHashTable::Iterator::operator==
(
const iterator& iter
) const
{
return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
}
template
template
inline bool
Foam::StaticHashTable::Iterator::operator==
(
const const_iterator& iter
) const
{
return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
}
template
template
inline bool
Foam::StaticHashTable::Iterator::operator!=
(
const iterator& iter
) const
{
return !operator==(iter);
}
template
template
inline bool
Foam::StaticHashTable::Iterator::operator!=
(
const const_iterator& iter
) const
{
return !operator==(iter);
}
template
template
inline TRef
Foam::StaticHashTable::Iterator::operator*()
{
return hashTable_.objects_[hashIndex_][elemIndex_];
}
template
template
inline TRef
Foam::StaticHashTable::Iterator::operator()()
{
return operator*();
}
template
template
inline
typename Foam::StaticHashTable::template Iterator
<
TRef,
TableRef
>&
Foam::StaticHashTable::Iterator
<
TRef,
TableRef
>::operator++()
{
// A negative index is a special value from erase
// (see notes in HashTable)
if (hashIndex_ < 0)
{
hashIndex_ = -(hashIndex_+1) - 1;
}
else
{
// Try the next element on the local list
elemIndex_++;
if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
{
return *this;
}
}
// Step to the next table entry
elemIndex_ = 0;
while
(
++hashIndex_ < hashTable_.objects_.size()
&& !hashTable_.objects_[hashIndex_].size()
)
{}
if (hashIndex_ >= hashTable_.objects_.size())
{
// make end iterator
hashIndex_ = hashTable_.keys_.size();
}
return *this;
}
template
template
inline
typename Foam::StaticHashTable::template Iterator
<
TRef,
TableRef
>
Foam::StaticHashTable::Iterator
<
TRef,
TableRef
>::operator++
(
int
)
{
iterator tmp = *this;
++*this;
return tmp;
}
template
template
inline const Key&
Foam::StaticHashTable::Iterator::key() const
{
return hashTable_.keys_[hashIndex_][elemIndex_];
}
template
inline typename Foam::StaticHashTable::iterator
Foam::StaticHashTable::begin()
{
// Find first non-empty entry
forAll(keys_, hashIdx)
{
if (keys_[hashIdx].size())
{
return iterator(*this, hashIdx, 0);
}
}
#ifdef FULLDEBUG
if (debug)
{
Info<< "StaticHashTable is empty\n";
}
#endif
return StaticHashTable::endIter_;
}
template
inline const typename Foam::StaticHashTable::iterator&
Foam::StaticHashTable::end()
{
return StaticHashTable::endIter_;
}
template
inline typename Foam::StaticHashTable::const_iterator
Foam::StaticHashTable::cbegin() const
{
// Find first non-empty entry
forAll(keys_, hashIdx)
{
if (keys_[hashIdx].size())
{
return const_iterator(*this, hashIdx, 0);
}
}
#ifdef FULLDEBUG
if (debug)
{
Info<< "StaticHashTable is empty\n";
}
#endif
return StaticHashTable::endConstIter_;
}
template
inline const typename Foam::StaticHashTable::const_iterator&
Foam::StaticHashTable::cend() const
{
return StaticHashTable::endConstIter_;
}
template
inline typename Foam::StaticHashTable::const_iterator
Foam::StaticHashTable::begin() const
{
return this->cbegin();
}
template
inline const typename Foam::StaticHashTable::const_iterator&
Foam::StaticHashTable::end() const
{
return StaticHashTable::endConstIter_;
}
// ************************************************************************* //