1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
31 template<class T, class Key, class Hash>
32 inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry
45 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
47 template<class T, class Key, class Hash>
49 Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
51 // size is power of two - this is the modulus
52 return Hash()(key) & (tableSize_ - 1);
56 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
58 template<class T, class Key, class Hash>
59 inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
65 template<class T, class Key, class Hash>
66 inline bool Foam::HashTable<T, Key, Hash>::empty() const
72 template<class T, class Key, class Hash>
73 inline bool Foam::HashTable<T, Key, Hash>::insert
79 return set(key, newEntry, true);
83 template<class T, class Key, class Hash>
84 inline bool Foam::HashTable<T, Key, Hash>::set
90 return set(key, newEntry, false);
94 template<class T, class Key, class Hash>
95 inline Foam::Xfer<Foam::HashTable<T, Key, Hash> >
96 Foam::HashTable<T, Key, Hash>::xfer()
98 return xferMove(*this);
102 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
104 template<class T, class Key, class Hash>
105 inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
107 iterator iter = find(key);
111 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
112 << key << " not found in table. Valid entries: "
121 template<class T, class Key, class Hash>
122 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
124 const_iterator iter = find(key);
128 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
129 << key << " not found in table. Valid entries: "
138 template<class T, class Key, class Hash>
139 inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
141 iterator iter = find(key);
155 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
157 template<class T, class Key, class Hash>
158 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
160 HashTable<T, Key, Hash>& hashTbl,
167 hashIndex_(hashIndex)
171 template<class T, class Key, class Hash>
172 inline void Foam::HashTable<T, Key, Hash>::iterator::operator=
177 elmtPtr_ = iter.elmtPtr_;
178 hashIndex_ = iter.hashIndex_;
182 template<class T, class Key, class Hash>
183 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
188 return elmtPtr_ == iter.elmtPtr_;
192 template<class T, class Key, class Hash>
193 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
198 return elmtPtr_ != iter.elmtPtr_;
202 template<class T, class Key, class Hash>
203 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
205 const const_iterator& iter
208 return elmtPtr_ == iter.elmtPtr_;
212 template<class T, class Key, class Hash>
213 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
215 const const_iterator& iter
218 return elmtPtr_ != iter.elmtPtr_;
222 template<class T, class Key, class Hash>
224 Foam::HashTable<T, Key, Hash>::iterator::operator*()
226 return elmtPtr_->obj_;
230 template<class T, class Key, class Hash>
232 Foam::HashTable<T, Key, Hash>::iterator::operator()()
234 return elmtPtr_->obj_;
238 template<class T, class Key, class Hash>
240 Foam::HashTable<T, Key, Hash>::iterator::operator*() const
242 return elmtPtr_->obj_;
246 template<class T, class Key, class Hash>
248 Foam::HashTable<T, Key, Hash>::iterator::operator()() const
250 return elmtPtr_->obj_;
254 template<class T, class Key, class Hash>
256 typename Foam::HashTable<T, Key, Hash>::iterator&
257 Foam::HashTable<T, Key, Hash>::iterator::operator++()
259 // Check for special value from erase. (sets hashIndex to -1)
262 // Do we have additional elements on the SLList?
263 if (elmtPtr_ && elmtPtr_->next_)
265 elmtPtr_ = elmtPtr_->next_;
270 // Step to the next table entry
273 ++hashIndex_ < hashTable_.tableSize_
274 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
278 if (hashIndex_ == hashTable_.tableSize_)
288 template<class T, class Key, class Hash>
289 inline typename Foam::HashTable<T, Key, Hash>::iterator
290 Foam::HashTable<T, Key, Hash>::iterator::operator++
295 iterator tmp = *this;
301 template<class T, class Key, class Hash>
303 const Key& Foam::HashTable<T, Key, Hash>::iterator::key() const
305 return elmtPtr_->key_;
309 template<class T, class Key, class Hash>
310 inline typename Foam::HashTable<T, Key, Hash>::iterator
311 Foam::HashTable<T, Key, Hash>::begin()
317 while (table_ && !table_[i] && ++i < tableSize_)
330 Info<< "HashTable is empty\n";
334 return HashTable<T, Key, Hash>::endIter_;
338 return iterator(*this, table_[i], i);
343 template<class T, class Key, class Hash>
344 inline const typename Foam::HashTable<T, Key, Hash>::iterator&
345 Foam::HashTable<T, Key, Hash>::end()
347 return HashTable<T, Key, Hash>::endIter_;
351 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
353 template<class T, class Key, class Hash>
354 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
356 const HashTable<T, Key, Hash>& hashTbl,
357 const hashedEntry* elmt,
363 hashIndex_(hashIndex)
367 template<class T, class Key, class Hash>
368 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
373 hashTable_(iter.hashTable_),
374 elmtPtr_(iter.elmtPtr_),
375 hashIndex_(iter.hashIndex_)
379 template<class T, class Key, class Hash>
380 inline void Foam::HashTable<T, Key, Hash>::const_iterator::operator=
382 const const_iterator& iter
385 elmtPtr_ = iter.elmtPtr_;
386 hashIndex_ = iter.hashIndex_;
390 template<class T, class Key, class Hash>
391 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
393 const const_iterator& iter
396 return elmtPtr_ == iter.elmtPtr_;
400 template<class T, class Key, class Hash>
401 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
403 const const_iterator& iter
406 return elmtPtr_ != iter.elmtPtr_;
410 template<class T, class Key, class Hash>
411 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
416 return elmtPtr_ == iter.elmtPtr_;
420 template<class T, class Key, class Hash>
421 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
426 return elmtPtr_ != iter.elmtPtr_;
430 template<class T, class Key, class Hash>
432 Foam::HashTable<T, Key, Hash>::const_iterator::operator*() const
434 return elmtPtr_->obj_;
437 template<class T, class Key, class Hash>
439 Foam::HashTable<T, Key, Hash>::const_iterator::operator()() const
441 return elmtPtr_->obj_;
445 template<class T, class Key, class Hash>
447 typename Foam::HashTable<T, Key, Hash>::const_iterator&
448 Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
452 !(elmtPtr_ = elmtPtr_->next_)
453 && ++hashIndex_ < hashTable_.tableSize_
454 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
459 ++hashIndex_ < hashTable_.tableSize_
460 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
469 template<class T, class Key, class Hash>
470 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
471 Foam::HashTable<T, Key, Hash>::const_iterator::operator++
476 const_iterator tmp = *this;
482 template<class T, class Key, class Hash>
484 const Key& Foam::HashTable<T, Key, Hash>::const_iterator::key() const
486 return elmtPtr_->key_;
490 template<class T, class Key, class Hash>
491 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
492 Foam::HashTable<T, Key, Hash>::cbegin() const
498 while (table_ && !table_[i] && ++i < tableSize_)
511 Info<< "HashTable is empty\n";
515 return HashTable<T, Key, Hash>::endConstIter_;
519 return const_iterator(*this, table_[i], i);
524 template<class T, class Key, class Hash>
525 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
526 Foam::HashTable<T, Key, Hash>::cend() const
528 return HashTable<T, Key, Hash>::endConstIter_;
532 template<class T, class Key, class Hash>
533 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
534 Foam::HashTable<T, Key, Hash>::begin() const
536 return this->cbegin();
540 template<class T, class Key, class Hash>
541 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
542 Foam::HashTable<T, Key, Hash>::end() const
544 return HashTable<T, Key, Hash>::endConstIter_;
548 // ************************************************************************* //