1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
30 template<class T, class Key, class Hash>
31 inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry
44 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
46 template<class T, class Key, class Hash>
48 Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
50 // size is power of two - this is the modulus
51 return Hash()(key) & (tableSize_ - 1);
55 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
57 template<class T, class Key, class Hash>
58 inline Foam::label Foam::HashTable<T, Key, Hash>::capacity() const
64 template<class T, class Key, class Hash>
65 inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
71 template<class T, class Key, class Hash>
72 inline bool Foam::HashTable<T, Key, Hash>::empty() const
78 template<class T, class Key, class Hash>
79 inline bool Foam::HashTable<T, Key, Hash>::insert
85 return this->set(key, newEntry, true);
89 template<class T, class Key, class Hash>
90 inline bool Foam::HashTable<T, Key, Hash>::set
96 return this->set(key, newEntry, false);
100 template<class T, class Key, class Hash>
101 inline Foam::Xfer<Foam::HashTable<T, Key, Hash> >
102 Foam::HashTable<T, Key, Hash>::xfer()
104 return xferMove(*this);
108 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
110 template<class T, class Key, class Hash>
111 inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
113 iterator iter = this->find(key);
115 if (iter == this->end())
117 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
118 << key << " not found in table. Valid entries: "
127 template<class T, class Key, class Hash>
128 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
130 const_iterator iter = this->find(key);
132 if (iter == this->cend())
134 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
135 << key << " not found in table. Valid entries: "
144 template<class T, class Key, class Hash>
145 inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
147 iterator iter = this->find(key);
149 if (iter == this->end())
151 this->insert(key, T());
161 // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
163 template<class T, class Key, class Hash>
164 inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase()
172 template<class T, class Key, class Hash>
173 inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase
175 const HashTable<T, Key, Hash>* hashTbl
178 hashTable_(const_cast<HashTable<T, Key, Hash>*>(hashTbl)),
182 if (hashTable_->nElmts_)
184 // find first non-NULL table entry
187 !(entryPtr_ = hashTable_->table_[hashIndex_])
188 && ++hashIndex_ < hashTable_->tableSize_
192 if (hashIndex_ >= hashTable_->tableSize_)
194 // make into an end iterator
202 template<class T, class Key, class Hash>
203 inline Foam::HashTable<T, Key, Hash>::iteratorBase::iteratorBase
205 const HashTable<T, Key, Hash>* hashTbl,
206 const hashedEntry* elmt,
207 const label hashIndex
210 hashTable_(const_cast<HashTable<T, Key, Hash>*>(hashTbl)),
211 entryPtr_(const_cast<hashedEntry*>(elmt)),
212 hashIndex_(hashIndex)
216 template<class T, class Key, class Hash>
218 Foam::HashTable<T, Key, Hash>::iteratorBase::increment()
220 // A negative index is a special value from erase
223 // the markPos='-curPos-1', but we wish to continue at 'curPos-1'
224 // thus use '-(markPos+1) -1'
225 hashIndex_ = -(hashIndex_+1) - 1;
229 if (entryPtr_->next_)
231 // Move to next element on the SLList
232 entryPtr_ = entryPtr_->next_;
238 // // if we reach here (entryPtr_ is NULL) it is already at the end()
239 // // we should probably stop
243 // Step to the next table entry
246 ++hashIndex_ < hashTable_->tableSize_
247 && !(entryPtr_ = hashTable_->table_[hashIndex_])
251 if (hashIndex_ >= hashTable_->tableSize_)
253 // make into an end iterator
260 template<class T, class Key, class Hash>
262 const Key& Foam::HashTable<T, Key, Hash>::iteratorBase::key() const
264 return entryPtr_->key_;
268 template<class T, class Key, class Hash>
270 Foam::HashTable<T, Key, Hash>::iteratorBase::object()
272 return entryPtr_->obj_;
276 template<class T, class Key, class Hash>
278 Foam::HashTable<T, Key, Hash>::iteratorBase::cobject() const
280 return entryPtr_->obj_;
284 template<class T, class Key, class Hash>
285 inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator==
287 const iteratorBase& iter
290 return entryPtr_ == iter.entryPtr_;
294 template<class T, class Key, class Hash>
295 inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator!=
297 const iteratorBase& iter
300 return entryPtr_ != iter.entryPtr_;
304 template<class T, class Key, class Hash>
305 inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator==
314 template<class T, class Key, class Hash>
315 inline bool Foam::HashTable<T, Key, Hash>::iteratorBase::operator!=
324 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
326 template<class T, class Key, class Hash>
327 inline Foam::HashTable<T, Key, Hash>::iterator::iterator()
333 template<class T, class Key, class Hash>
334 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
343 template<class T, class Key, class Hash>
344 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
346 HashTable<T, Key, Hash>* hashTbl
349 iteratorBase(hashTbl)
353 template<class T, class Key, class Hash>
354 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
356 HashTable<T, Key, Hash>* hashTbl,
358 const label hashIndex
361 iteratorBase(hashTbl, elmt, hashIndex)
365 template<class T, class Key, class Hash>
367 Foam::HashTable<T, Key, Hash>::iterator::operator*()
369 return this->object();
373 template<class T, class Key, class Hash>
375 Foam::HashTable<T, Key, Hash>::iterator::operator()()
377 return this->object();
381 template<class T, class Key, class Hash>
383 Foam::HashTable<T, Key, Hash>::iterator::operator*() const
385 return this->cobject();
389 template<class T, class Key, class Hash>
391 Foam::HashTable<T, Key, Hash>::iterator::operator()() const
393 return this->cobject();
397 template<class T, class Key, class Hash>
399 typename Foam::HashTable<T, Key, Hash>::iterator&
400 Foam::HashTable<T, Key, Hash>::iterator::operator++()
407 template<class T, class Key, class Hash>
408 inline typename Foam::HashTable<T, Key, Hash>::iterator
409 Foam::HashTable<T, Key, Hash>::iterator::operator++(int)
411 iterator old = *this;
417 template<class T, class Key, class Hash>
418 inline typename Foam::HashTable<T, Key, Hash>::iterator
419 Foam::HashTable<T, Key, Hash>::begin()
421 return iterator(this);
425 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
427 template<class T, class Key, class Hash>
428 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator()
434 template<class T, class Key, class Hash>
435 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
437 const HashTable<T, Key, Hash>::iterator& iter
444 template<class T, class Key, class Hash>
445 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
454 template<class T, class Key, class Hash>
455 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
457 const HashTable<T, Key, Hash>* hashTbl
460 iteratorBase(hashTbl)
464 template<class T, class Key, class Hash>
465 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
467 const HashTable<T, Key, Hash>* hashTbl,
468 const hashedEntry* elmt,
469 const label hashIndex
472 iteratorBase(hashTbl, elmt, hashIndex)
476 template<class T, class Key, class Hash>
478 Foam::HashTable<T, Key, Hash>::const_iterator::operator*() const
480 return this->cobject();
484 template<class T, class Key, class Hash>
486 Foam::HashTable<T, Key, Hash>::const_iterator::operator()() const
488 return this->cobject();
492 template<class T, class Key, class Hash>
494 typename Foam::HashTable<T, Key, Hash>::const_iterator&
495 Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
502 template<class T, class Key, class Hash>
503 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
504 Foam::HashTable<T, Key, Hash>::const_iterator::operator++(int)
506 const_iterator old = *this;
512 template<class T, class Key, class Hash>
513 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
514 Foam::HashTable<T, Key, Hash>::cbegin() const
516 return const_iterator(this);
520 template<class T, class Key, class Hash>
521 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
522 Foam::HashTable<T, Key, Hash>::begin() const
524 return this->cbegin();
528 // ************************************************************************* //