OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

HashTableI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2007 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     HashTable
00027 
00028 Description
00029     STL conforming hash table.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "error.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039 
00040 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
00041 
00042 template<class T, class Key, class Hash>
00043 inline HashTable<T, Key, Hash>::hashedEntry::hashedEntry
00044 (
00045     const Key& key,
00046     hashedEntry* next,
00047     const T& newEntry
00048 )
00049 :
00050     key_(key),
00051     next_(next),
00052     obj_(newEntry)
00053 {}
00054 
00055 
00056 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
00057 
00058 template<class T, class Key, class Hash>
00059 inline label HashTable<T, Key, Hash>::size() const
00060 {
00061     return nElmts_;
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00066 
00067 template<class T, class Key, class Hash>
00068 inline T& HashTable<T, Key, Hash>::operator[](const Key& key)
00069 {
00070     iterator iter = find(key);
00071 
00072     if (iter == end())
00073     {
00074         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
00075             << key << " not found in table.  Valid entries are "
00076             << toc()
00077             << exit(FatalError);
00078     }
00079 
00080     return *iter;
00081 }
00082 
00083 template<class T, class Key, class Hash>
00084 inline const T& HashTable<T, Key, Hash>::operator[](const Key& key) const
00085 {
00086     const_iterator iter = find(key);
00087 
00088     if (iter == end())
00089     {
00090         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
00091             << key << " not found in table.  Valid entries are "
00092             << toc()
00093             << exit(FatalError);
00094     }
00095 
00096     return *iter;
00097 }
00098 
00099 
00100 template<class T, class Key, class Hash>
00101 inline T& HashTable<T, Key, Hash>::operator()(const Key& key)
00102 {
00103     iterator iter = find(key);
00104 
00105     if (iter == end())
00106     {
00107         insert(key, T());
00108         return *find(key);
00109     }
00110     else
00111     {
00112         return *iter;
00113     }
00114 }
00115 
00116 
00117 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
00118 
00119 template<class T, class Key, class Hash>
00120 inline HashTable<T, Key, Hash>::iterator::iterator
00121 (
00122     HashTable<T, Key, Hash>& curHashTable,
00123     hashedEntry* elmt,
00124     label hashIndex
00125 )
00126 :
00127     curHashTable_(curHashTable),
00128     elmtPtr_(elmt),
00129     hashIndex_(hashIndex)
00130 {}
00131 
00132 
00133 template<class T, class Key, class Hash>
00134 inline void HashTable<T, Key, Hash>::iterator::operator=(const iterator& iter)
00135 {
00136     elmtPtr_ = iter.elmtPtr_;
00137     hashIndex_ = iter.hashIndex_;
00138 }
00139 
00140 
00141 template<class T, class Key, class Hash>
00142 inline bool HashTable<T, Key, Hash>::iterator::operator==
00143 (
00144     const iterator& iter
00145 ) const
00146 {
00147     return elmtPtr_ == iter.elmtPtr_;
00148 }
00149 
00150 
00151 template<class T, class Key, class Hash>
00152 inline bool HashTable<T, Key, Hash>::iterator::operator!=
00153 (
00154     const iterator& iter
00155 ) const
00156 {
00157     return elmtPtr_ != iter.elmtPtr_;
00158 }
00159 
00160 
00161 template<class T, class Key, class Hash>
00162 inline bool HashTable<T, Key, Hash>::iterator::operator==
00163 (
00164     const const_iterator& iter
00165 ) const
00166 {
00167     return elmtPtr_ == iter.elmtPtr_;
00168 }
00169 
00170 
00171 template<class T, class Key, class Hash>
00172 inline bool HashTable<T, Key, Hash>::iterator::operator!=
00173 (
00174     const const_iterator& iter
00175 ) const
00176 {
00177     return elmtPtr_ != iter.elmtPtr_;
00178 }
00179 
00180 
00181 template<class T, class Key, class Hash>
00182 inline T& HashTable<T, Key, Hash>::iterator::operator*()
00183 {
00184     return elmtPtr_->obj_;
00185 }
00186 
00187 
00188 template<class T, class Key, class Hash>
00189 inline T& HashTable<T, Key, Hash>::iterator::operator()()
00190 {
00191     return operator*();
00192 }
00193 
00194 
00195 template<class T, class Key, class Hash>
00196 inline
00197 typename HashTable<T, Key, Hash>::iterator&
00198 HashTable<T, Key, Hash>::iterator::operator++()
00199 {
00200     // Check for special value from erase. (sets hashIndex to -1)
00201     if (hashIndex_ >= 0)
00202     {
00203         // Do we have additional elements on the singly linked list?
00204         if (elmtPtr_ && elmtPtr_->next_)
00205         {
00206             elmtPtr_ = elmtPtr_->next_;
00207             return *this;
00208         }
00209     }
00210 
00211     // Step to the next table entry
00212     while
00213     (
00214         ++hashIndex_ < curHashTable_.tableSize_
00215      && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
00216     );
00217 
00218     if (hashIndex_ == curHashTable_.tableSize_)
00219     {
00220         // make end iterator
00221         hashIndex_ = 0;
00222         elmtPtr_ = 0;
00223     }
00224     return *this;
00225 }
00226 
00227 
00228 template<class T, class Key, class Hash>
00229 inline typename HashTable<T, Key, Hash>::iterator
00230 HashTable<T, Key, Hash>::iterator::operator++
00231 (
00232     int
00233 )
00234 {
00235     iterator tmp = *this;
00236     ++*this;
00237     return tmp;
00238 }
00239 
00240 
00241 template<class T, class Key, class Hash>
00242 inline
00243 const Key& HashTable<T, Key, Hash>::iterator::key()
00244 {
00245     return elmtPtr_->key_;
00246 }
00247 
00248 
00249 template<class T, class Key, class Hash>
00250 inline typename HashTable<T, Key, Hash>::iterator
00251 HashTable<T, Key, Hash>::begin()
00252 {
00253     label i = 0;
00254 
00255     while (table_ && !table_[i] && ++i < tableSize_);
00256 
00257     if (i == tableSize_)
00258     {
00259 #       ifdef FULLDEBUG
00260         if (debug)
00261         {
00262             Info<< "HashTable is empty\n";
00263         }
00264 #       endif
00265 
00266         return HashTable<T, Key, Hash>::endIter_;
00267     }
00268     else
00269     {
00270         return iterator(*this, table_[i], i);
00271     }
00272 }
00273 
00274 
00275 template<class T, class Key, class Hash>
00276 inline const typename HashTable<T, Key, Hash>::iterator&
00277 HashTable<T, Key, Hash>::end()
00278 {
00279     return HashTable<T, Key, Hash>::endIter_;
00280 }
00281 
00282 
00283 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
00284 
00285 template<class T, class Key, class Hash>
00286 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
00287 (
00288     const HashTable<T, Key, Hash>& curHashTable,
00289     const hashedEntry* elmt,
00290     label hashIndex
00291 )
00292 :
00293     curHashTable_(curHashTable),
00294     elmtPtr_(elmt),
00295     hashIndex_(hashIndex)
00296 {}
00297 
00298 
00299 template<class T, class Key, class Hash>
00300 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
00301 (   
00302     const iterator& iter
00303 )
00304 :
00305     curHashTable_(iter.curHashTable_),
00306     elmtPtr_(iter.elmtPtr_),
00307     hashIndex_(iter.hashIndex_)
00308 {}
00309 
00310 
00311 template<class T, class Key, class Hash>
00312 inline void HashTable<T, Key, Hash>::const_iterator::operator=
00313 (
00314     const const_iterator& iter
00315 )
00316 {
00317     elmtPtr_ = iter.elmtPtr_;
00318     hashIndex_ = iter.hashIndex_;
00319 }
00320 
00321 
00322 template<class T, class Key, class Hash>
00323 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
00324 (
00325     const const_iterator& iter
00326 ) const
00327 {
00328     return elmtPtr_ == iter.elmtPtr_;
00329 }
00330 
00331 
00332 template<class T, class Key, class Hash>
00333 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
00334 (
00335     const const_iterator& iter
00336 ) const
00337 {
00338     return elmtPtr_ != iter.elmtPtr_;
00339 }
00340 
00341 
00342 template<class T, class Key, class Hash>
00343 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
00344 (
00345     const iterator& iter
00346 ) const
00347 {
00348     return elmtPtr_ == iter.elmtPtr_;
00349 }
00350 
00351 
00352 template<class T, class Key, class Hash>
00353 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
00354 (
00355     const iterator& iter
00356 ) const
00357 {
00358     return elmtPtr_ != iter.elmtPtr_;
00359 }
00360 
00361 
00362 template<class T, class Key, class Hash>
00363 inline const T& HashTable<T, Key, Hash>::const_iterator::operator*()
00364 {
00365     return elmtPtr_->obj_;
00366 }
00367 
00368 
00369 template<class T, class Key, class Hash>
00370 inline const T& HashTable<T, Key, Hash>::const_iterator::operator()()
00371 {
00372     return operator*();
00373 }
00374 
00375 
00376 template<class T, class Key, class Hash>
00377 inline
00378 typename HashTable<T, Key, Hash>::const_iterator&
00379 HashTable<T, Key, Hash>::const_iterator::operator++()
00380 {
00381     if
00382     (
00383         !(elmtPtr_ = elmtPtr_->next_)
00384      && ++hashIndex_ < curHashTable_.tableSize_
00385      && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
00386     )
00387     {
00388         while
00389         (
00390             ++hashIndex_ < curHashTable_.tableSize_
00391          && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
00392         );
00393     }
00394 
00395     return *this;
00396 }
00397 
00398 
00399 template<class T, class Key, class Hash>
00400 inline typename HashTable<T, Key, Hash>::const_iterator
00401 HashTable<T, Key, Hash>::const_iterator::operator++
00402 (
00403     int
00404 )
00405 {
00406     const_iterator tmp = *this;
00407     ++*this;
00408     return tmp;
00409 }
00410 
00411 
00412 template<class T, class Key, class Hash>
00413 inline
00414 const Key& HashTable<T, Key, Hash>::const_iterator::key()
00415 {
00416     return elmtPtr_->key_;
00417 }
00418 
00419 
00420 template<class T, class Key, class Hash>
00421 inline typename HashTable<T, Key, Hash>::const_iterator
00422 HashTable<T, Key, Hash>::begin() const
00423 {
00424     label i = 0;
00425 
00426     while (table_ && !table_[i] && ++i < tableSize_);
00427 
00428     if (i == tableSize_)
00429     {
00430 #       ifdef FULLDEBUG
00431         if (debug)
00432         {
00433             Info<< "HashTable is empty\n";
00434         }
00435 #       endif
00436 
00437         return HashTable<T, Key, Hash>::endConstIter_;
00438     }
00439     else
00440     {
00441         return const_iterator(*this, table_[i], i);
00442     }
00443 }
00444 
00445 
00446 template<class T, class Key, class Hash>
00447 inline const typename HashTable<T, Key, Hash>::const_iterator&
00448 HashTable<T, Key, Hash>::end() const
00449 {
00450     return HashTable<T, Key, Hash>::endConstIter_;
00451 }
00452 
00453 
00454 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00455 
00456 } // End namespace Foam
00457 
00458 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd