1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
36 template<class T, class Key, class Hash>
37 inline HashTable<T, Key, Hash>::hashedEntry::hashedEntry
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 template<class T, class Key, class Hash>
53 inline label HashTable<T, Key, Hash>::size() const
59 template<class T, class Key, class Hash>
60 inline bool HashTable<T, Key, Hash>::insert(const Key& key, const T& newEntry)
62 return set(key, newEntry, true);
66 template<class T, class Key, class Hash>
67 inline bool HashTable<T, Key, Hash>::set(const Key& key, const T& newEntry)
69 return set(key, newEntry, false);
72 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
74 template<class T, class Key, class Hash>
75 inline T& HashTable<T, Key, Hash>::operator[](const Key& key)
77 iterator iter = find(key);
81 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
82 << key << " not found in table. Valid entries are "
90 template<class T, class Key, class Hash>
91 inline const T& HashTable<T, Key, Hash>::operator[](const Key& key) const
93 const_iterator iter = find(key);
97 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
98 << key << " not found in table. Valid entries are "
107 template<class T, class Key, class Hash>
108 inline T& HashTable<T, Key, Hash>::operator()(const Key& key)
110 iterator iter = find(key);
124 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
126 template<class T, class Key, class Hash>
127 inline HashTable<T, Key, Hash>::iterator::iterator
129 HashTable<T, Key, Hash>& curHashTable,
134 curHashTable_(curHashTable),
136 hashIndex_(hashIndex)
140 template<class T, class Key, class Hash>
141 inline void HashTable<T, Key, Hash>::iterator::operator=(const iterator& iter)
143 elmtPtr_ = iter.elmtPtr_;
144 hashIndex_ = iter.hashIndex_;
148 template<class T, class Key, class Hash>
149 inline bool HashTable<T, Key, Hash>::iterator::operator==
154 return elmtPtr_ == iter.elmtPtr_;
158 template<class T, class Key, class Hash>
159 inline bool HashTable<T, Key, Hash>::iterator::operator!=
164 return elmtPtr_ != iter.elmtPtr_;
168 template<class T, class Key, class Hash>
169 inline bool HashTable<T, Key, Hash>::iterator::operator==
171 const const_iterator& iter
174 return elmtPtr_ == iter.elmtPtr_;
178 template<class T, class Key, class Hash>
179 inline bool HashTable<T, Key, Hash>::iterator::operator!=
181 const const_iterator& iter
184 return elmtPtr_ != iter.elmtPtr_;
188 template<class T, class Key, class Hash>
189 inline T& HashTable<T, Key, Hash>::iterator::operator*()
191 return elmtPtr_->obj_;
195 template<class T, class Key, class Hash>
196 inline T& HashTable<T, Key, Hash>::iterator::operator()()
202 template<class T, class Key, class Hash>
204 typename HashTable<T, Key, Hash>::iterator&
205 HashTable<T, Key, Hash>::iterator::operator++()
207 // Check for special value from erase. (sets hashIndex to -1)
210 // Do we have additional elements on the singly linked list?
211 if (elmtPtr_ && elmtPtr_->next_)
213 elmtPtr_ = elmtPtr_->next_;
218 // Step to the next table entry
221 ++hashIndex_ < curHashTable_.tableSize_
222 && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
226 if (hashIndex_ == curHashTable_.tableSize_)
236 template<class T, class Key, class Hash>
237 inline typename HashTable<T, Key, Hash>::iterator
238 HashTable<T, Key, Hash>::iterator::operator++
243 iterator tmp = *this;
249 template<class T, class Key, class Hash>
251 const Key& HashTable<T, Key, Hash>::iterator::key()
253 return elmtPtr_->key_;
257 template<class T, class Key, class Hash>
258 inline typename HashTable<T, Key, Hash>::iterator
259 HashTable<T, Key, Hash>::begin()
263 while (table_ && !table_[i] && ++i < tableSize_)
271 Info<< "HashTable is empty\n";
275 return HashTable<T, Key, Hash>::endIter_;
279 return iterator(*this, table_[i], i);
284 template<class T, class Key, class Hash>
285 inline const typename HashTable<T, Key, Hash>::iterator&
286 HashTable<T, Key, Hash>::end()
288 return HashTable<T, Key, Hash>::endIter_;
292 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
294 template<class T, class Key, class Hash>
295 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
297 const HashTable<T, Key, Hash>& curHashTable,
298 const hashedEntry* elmt,
302 curHashTable_(curHashTable),
304 hashIndex_(hashIndex)
308 template<class T, class Key, class Hash>
309 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
314 curHashTable_(iter.curHashTable_),
315 elmtPtr_(iter.elmtPtr_),
316 hashIndex_(iter.hashIndex_)
320 template<class T, class Key, class Hash>
321 inline void HashTable<T, Key, Hash>::const_iterator::operator=
323 const const_iterator& iter
326 elmtPtr_ = iter.elmtPtr_;
327 hashIndex_ = iter.hashIndex_;
331 template<class T, class Key, class Hash>
332 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
334 const const_iterator& iter
337 return elmtPtr_ == iter.elmtPtr_;
341 template<class T, class Key, class Hash>
342 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
344 const const_iterator& iter
347 return elmtPtr_ != iter.elmtPtr_;
351 template<class T, class Key, class Hash>
352 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
357 return elmtPtr_ == iter.elmtPtr_;
361 template<class T, class Key, class Hash>
362 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
367 return elmtPtr_ != iter.elmtPtr_;
371 template<class T, class Key, class Hash>
372 inline const T& HashTable<T, Key, Hash>::const_iterator::operator*()
374 return elmtPtr_->obj_;
378 template<class T, class Key, class Hash>
379 inline const T& HashTable<T, Key, Hash>::const_iterator::operator()()
385 template<class T, class Key, class Hash>
387 typename HashTable<T, Key, Hash>::const_iterator&
388 HashTable<T, Key, Hash>::const_iterator::operator++()
392 !(elmtPtr_ = elmtPtr_->next_)
393 && ++hashIndex_ < curHashTable_.tableSize_
394 && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
399 ++hashIndex_ < curHashTable_.tableSize_
400 && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
409 template<class T, class Key, class Hash>
410 inline typename HashTable<T, Key, Hash>::const_iterator
411 HashTable<T, Key, Hash>::const_iterator::operator++
416 const_iterator tmp = *this;
422 template<class T, class Key, class Hash>
424 const Key& HashTable<T, Key, Hash>::const_iterator::key()
426 return elmtPtr_->key_;
430 template<class T, class Key, class Hash>
431 inline typename HashTable<T, Key, Hash>::const_iterator
432 HashTable<T, Key, Hash>::begin() const
436 while (table_ && !table_[i] && ++i < tableSize_)
444 Info<< "HashTable is empty\n";
448 return HashTable<T, Key, Hash>::endConstIter_;
452 return const_iterator(*this, table_[i], i);
457 template<class T, class Key, class Hash>
458 inline const typename HashTable<T, Key, Hash>::const_iterator&
459 HashTable<T, Key, Hash>::end() const
461 return HashTable<T, Key, Hash>::endConstIter_;
465 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 } // End namespace Foam
469 // ************************************************************************* //