/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. \\/ 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" // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * // template inline Foam::HashTable::hashedEntry::hashedEntry ( const Key& key, hashedEntry* next, const T& obj ) : key_(key), next_(next), obj_(obj) {} // * * * * * * * * * * * * 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& newEntry ) { return set(key, newEntry, true); } template inline bool Foam::HashTable::set ( const Key& key, const T& newEntry ) { return set(key, newEntry, false); } template inline Foam::Xfer > Foam::HashTable::xfer() { return xferMove(*this); } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template inline T& Foam::HashTable::operator[](const Key& key) { iterator iter = find(key); if (iter == end()) { FatalErrorIn("HashTable::operator[](const Key&)") << key << " not found in table. Valid entries: " << toc() << exit(FatalError); } return *iter; } template inline const T& Foam::HashTable::operator[](const Key& key) const { const_iterator iter = find(key); if (iter == cend()) { FatalErrorIn("HashTable::operator[](const Key&) const") << key << " not found in table. Valid entries: " << toc() << exit(FatalError); } return *iter; } template inline T& Foam::HashTable::operator()(const Key& key) { iterator iter = find(key); if (iter == end()) { insert(key, T()); return *find(key); } else { return *iter; } } // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * // template inline Foam::HashTable::iteratorBase::iteratorBase() : hashTable_(0), entryPtr_(0), hashIndex_(0) {} template inline Foam::HashTable::iteratorBase::iteratorBase ( const HashTable* hashTbl ) : hashTable_(const_cast*>(hashTbl)), entryPtr_(0), hashIndex_(0) { if (hashTable_->nElmts_) { // find first non-NULL table entry while ( !(entryPtr_ = hashTable_->table_[hashIndex_]) && ++hashIndex_ < hashTable_->tableSize_ ) {} if (hashIndex_ >= hashTable_->tableSize_) { // make into an end iterator entryPtr_ = 0; hashIndex_ = 0; } } } template inline Foam::HashTable::iteratorBase::iteratorBase ( const HashTable* hashTbl, const hashedEntry* elmt, const label hashIndex ) : hashTable_(const_cast*>(hashTbl)), entryPtr_(const_cast(elmt)), hashIndex_(hashIndex) {} template inline void Foam::HashTable::iteratorBase::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 NULL) 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_ = 0; hashIndex_ = 0; } } template inline const Key& Foam::HashTable::iteratorBase::key() const { return entryPtr_->key_; } template inline T& Foam::HashTable::iteratorBase::object() { return entryPtr_->obj_; } template inline const T& Foam::HashTable::iteratorBase::cobject() const { return entryPtr_->obj_; } template inline bool Foam::HashTable::iteratorBase::operator== ( const iteratorBase& iter ) const { return entryPtr_ == iter.entryPtr_; } template inline bool Foam::HashTable::iteratorBase::operator!= ( const iteratorBase& iter ) const { return entryPtr_ != iter.entryPtr_; } template inline bool Foam::HashTable::iteratorBase::operator== ( const iteratorEnd& ) const { return !entryPtr_; } template inline bool Foam::HashTable::iteratorBase::operator!= ( const iteratorEnd& ) const { return entryPtr_; } // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // template inline Foam::HashTable::iterator::iterator() : iteratorBase() {} template inline Foam::HashTable::iterator::iterator ( const iteratorEnd& ) : iteratorBase() {} template inline Foam::HashTable::iterator::iterator ( HashTable* hashTbl ) : iteratorBase(hashTbl) {} template inline Foam::HashTable::iterator::iterator ( HashTable* hashTbl, hashedEntry* elmt, const label hashIndex ) : iteratorBase(hashTbl, elmt, hashIndex) {} template inline Foam::HashTable::iterator::operator typename Foam::HashTable::const_iterator() const { return *reinterpret_cast < const typename Foam::HashTable::const_iterator* >(this); } template inline T& Foam::HashTable::iterator::operator*() { return this->object(); } template inline T& Foam::HashTable::iterator::operator()() { return this->object(); } template inline const T& Foam::HashTable::iterator::operator*() const { return this->cobject(); } template inline const T& Foam::HashTable::iterator::operator()() const { return this->cobject(); } 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; } template inline typename Foam::HashTable::iterator Foam::HashTable::begin() { return iterator(this); } // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * // template inline Foam::HashTable::const_iterator::const_iterator() : iteratorBase() {} template inline Foam::HashTable::const_iterator::const_iterator ( const iteratorEnd& ) : iteratorBase() {} template inline Foam::HashTable::const_iterator::const_iterator ( const HashTable* hashTbl ) : iteratorBase(hashTbl) {} template inline Foam::HashTable::const_iterator::const_iterator ( const HashTable* hashTbl, const hashedEntry* elmt, const label hashIndex ) : iteratorBase(hashTbl, elmt, hashIndex) {} template inline const T& Foam::HashTable::const_iterator::operator*() const { return this->cobject(); } template inline const T& Foam::HashTable::const_iterator::operator()() const { return this->cobject(); } 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::const_iterator Foam::HashTable::cbegin() const { return const_iterator(this); } template inline typename Foam::HashTable::const_iterator Foam::HashTable::begin() const { return this->cbegin(); } // ************************************************************************* //