/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
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"
// * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
template
inline Foam::HashTable::hashedEntry::hashedEntry
(
const Key& key,
const T& obj,
hashedEntry* next
)
:
key_(key),
obj_(obj),
next_(next)
{}
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template
inline Foam::label
Foam::HashTable::hashKeyIndex(const Key& key) const
{
// size is power of two - this is the modulus
return Hash()(key) & (tableSize_ - 1);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
inline Foam::label Foam::HashTable::capacity() const
{
return tableSize_;
}
template
inline Foam::label Foam::HashTable::size() const
{
return nElmts_;
}
template
inline bool Foam::HashTable::empty() const
{
return !nElmts_;
}
template
inline bool Foam::HashTable::insert
(
const Key& key,
const T& obj
)
{
return this->set(key, obj, true);
}
template
inline bool Foam::HashTable::set
(
const Key& key,
const T& obj
)
{
return this->set(key, obj, false);
}
template
inline Foam::Xfer>
Foam::HashTable::xfer()
{
return xferMove(*this);
}
template
inline const T& Foam::HashTable::lookup
(
const Key& key,
const T& deflt
) const
{
const_iterator iter = this->find(key);
return iter.found() ? iter.object() : deflt;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
inline T& Foam::HashTable::operator[](const Key& key)
{
iterator iter = this->find(key);
if (!iter.found())
{
FatalErrorInFunction
<< key << " not found in table. Valid entries: "
<< toc()
<< exit(FatalError);
}
return iter.object();
}
template
inline const T& Foam::HashTable::operator[](const Key& key) const
{
const_iterator iter = this->find(key);
if (!iter.found())
{
FatalErrorInFunction
<< key << " not found in table. Valid entries: "
<< toc()
<< exit(FatalError);
}
return iter.object();
}
template
inline T& Foam::HashTable::operator()(const Key& key)
{
iterator iter = this->find(key);
if (iter.found())
{
return iter.object();
}
this->insert(key, T());
return find(key).object();
}
template
inline T& Foam::HashTable::operator()
(
const Key& key,
const T& deflt
)
{
iterator iter = this->find(key);
if (iter.found())
{
return iter.object();
}
this->insert(key, deflt);
return find(key).object();
}
template
inline const T& Foam::HashTable::operator()
(
const Key& key,
const T& deflt
) const
{
return this->lookup(key, deflt);
}
// * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
template
inline Foam::HashTable::iterator_base::iterator_base()
:
entryPtr_(nullptr),
hashTable_(nullptr),
hashIndex_(0)
{}
template
inline Foam::HashTable::iterator_base::iterator_base
(
const table_type* hashTbl,
const entry_type* elmt,
const label hashIndex
)
:
entryPtr_(const_cast(elmt)),
hashTable_(const_cast(hashTbl)),
hashIndex_(hashIndex)
{}
template
inline Foam::HashTable::iterator_base::iterator_base
(
const table_type* hashTbl
)
:
entryPtr_(nullptr),
hashTable_(const_cast(hashTbl)),
hashIndex_(0)
{
if (hashTable_ && hashTable_->nElmts_)
{
// find first non-nullptr table entry
while
(
!(entryPtr_ = hashTable_->table_[hashIndex_])
&& ++hashIndex_ < hashTable_->tableSize_
)
{}
if (hashIndex_ >= hashTable_->tableSize_)
{
// make into an end iterator
entryPtr_ = nullptr;
hashIndex_ = 0;
}
}
}
template
inline void
Foam::HashTable::iterator_base::increment()
{
// A negative index is a special value from erase
if (hashIndex_ < 0)
{
// the markPos='-curPos-1', but we wish to continue at 'curPos-1'
// thus use '-(markPos+1) -1'
hashIndex_ = -(hashIndex_+1) - 1;
}
else if (entryPtr_)
{
if (entryPtr_->next_)
{
// Move to next element on the SLList
entryPtr_ = entryPtr_->next_;
return;
}
}
// else
// {
// // if we reach here (entryPtr_ is nullptr) it is already at the end()
// // we should probably stop
// }
// Step to the next table entry
while
(
++hashIndex_ < hashTable_->tableSize_
&& !(entryPtr_ = hashTable_->table_[hashIndex_])
)
{}
if (hashIndex_ >= hashTable_->tableSize_)
{
// make into an end iterator
entryPtr_ = nullptr;
hashIndex_ = 0;
}
}
template
inline bool
Foam::HashTable::iterator_base::found() const
{
return entryPtr_;
}
template
inline const Key& Foam::HashTable::iterator_base::key() const
{
return entryPtr_->key_;
}
template
inline T& Foam::HashTable::iterator_base::element() const
{
return entryPtr_->obj_;
}
template
inline bool Foam::HashTable::iterator_base::operator==
(
const iterator_base& iter
) const
{
return entryPtr_ == iter.entryPtr_;
}
template
inline bool Foam::HashTable::iterator_base::operator!=
(
const iterator_base& iter
) const
{
return entryPtr_ != iter.entryPtr_;
}
// * * * * * * * * * * * * * * key iterator base * * * * * * * * * * * * * * //
template
template
inline Foam::HashTable::key_iterator_base
::key_iterator_base
(
const WrappedIterator& iter
)
:
WrappedIterator(iter)
{}
template
template
inline const Key&
Foam::HashTable::key_iterator_base
::operator*() const
{
return this->key();
}
template
template
inline const Key&
Foam::HashTable::key_iterator_base
::operator()() const
{
return this->key();
}
template
template
inline Foam::HashTable::key_iterator_base&
Foam::HashTable::key_iterator_base
::operator++()
{
this->increment();
return *this;
}
template
template
inline Foam::HashTable::key_iterator_base
Foam::HashTable::key_iterator_base
::operator++(int)
{
key_iterator_base old = *this;
this->increment();
return old;
}
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
template
inline Foam::HashTable::iterator::iterator()
:
iterator_base()
{}
template
inline Foam::HashTable::iterator::iterator
(
table_type* hashTbl
)
:
iterator_base(hashTbl)
{}
template
inline Foam::HashTable::iterator::iterator
(
table_type* hashTbl,
entry_type* elmt,
const label hashIndex
)
:
iterator_base(hashTbl, elmt, hashIndex)
{}
template
inline T&
Foam::HashTable::iterator::object() const
{
return this->element();
}
template
inline T&
Foam::HashTable::iterator::operator*() const
{
return this->object();
}
template
inline T&
Foam::HashTable::iterator::operator()() const
{
return this->object();
}
template
inline typename Foam::HashTable::iterator&
Foam::HashTable::iterator::operator++()
{
this->increment();
return *this;
}
template
inline typename Foam::HashTable::iterator
Foam::HashTable::iterator::operator++(int)
{
iterator old = *this;
this->increment();
return old;
}
// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
template
inline Foam::HashTable::const_iterator::const_iterator()
:
iterator_base()
{}
template
inline Foam::HashTable::const_iterator::const_iterator
(
const HashTable::iterator& iter
)
:
iterator_base(iter)
{}
template
inline Foam::HashTable::const_iterator::const_iterator
(
table_type* hashTbl
)
:
iterator_base(hashTbl)
{}
template
inline Foam::HashTable::const_iterator::const_iterator
(
table_type* hashTbl,
entry_type* elmt,
const label hashIndex
)
:
iterator_base(hashTbl, elmt, hashIndex)
{}
template
inline const T&
Foam::HashTable::const_iterator::object() const
{
return this->element();
}
template
inline const T&
Foam::HashTable::const_iterator::operator*() const
{
return this->object();
}
template
inline const T&
Foam::HashTable::const_iterator::operator()() const
{
return this->object();
}
template
inline typename Foam::HashTable::const_iterator&
Foam::HashTable::const_iterator::operator++()
{
this->increment();
return *this;
}
template
inline typename Foam::HashTable::const_iterator
Foam::HashTable::const_iterator::operator++(int)
{
const_iterator old = *this;
this->increment();
return old;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template
inline typename Foam::HashTable::iterator
Foam::HashTable::begin()
{
return iterator(this);
}
template
inline typename Foam::HashTable::const_iterator
Foam::HashTable::begin() const
{
return const_iterator(this);
}
template
inline typename Foam::HashTable::const_iterator
Foam::HashTable::cbegin() const
{
return const_iterator(this);
}
template
inline const typename Foam::HashTable::iterator&
Foam::HashTable::end()
{
return iterator_end();
}
template
inline const typename Foam::HashTable::const_iterator&
Foam::HashTable::end() const
{
return iterator_end();
}
template
inline const typename Foam::HashTable::const_iterator&
Foam::HashTable::cend() const
{
return iterator_cend();
}
// ************************************************************************* //