OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
00101 
00102 template<class T, class Key, class Hash>
00103 template<class TRef, class TableRef, class HashedEntryPtr>
00104 inline HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00105 Iterator
00106 (
00107     TableRef curHashTable,
00108     HashedEntryPtr elmt,
00109     HashedEntryPtr prev,
00110     label hashIndex
00111 )
00112 :
00113     curHashTable_(curHashTable),
00114     elmtPtr_(elmt),
00115     prevElmtPtr_(prev),
00116     hashIndex_(hashIndex)
00117 {}
00118 
00119 
00120 template<class T, class Key, class Hash>
00121 template<class TRef, class TableRef, class HashedEntryPtr>
00122 inline HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00123 Iterator(const iterator& iter)
00124 :
00125     curHashTable_(iter.curHashTable_),
00126     elmtPtr_(iter.elmtPtr_),
00127     prevElmtPtr_(iter.prevElmtPtr_),
00128     hashIndex_(iter.hashIndex_)
00129 {}
00130 
00131 
00132 template<class T, class Key, class Hash>
00133 template<class TRef, class TableRef, class HashedEntryPtr>
00134 inline void HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00135 operator=(const iterator& iter)
00136 {
00137     this->elmtPtr_ = iter.elmtPtr_;
00138     this->prevElmtPtr_ = iter.prevElmtPtr_;
00139     this->hashIndex_ = iter.hashIndex_;
00140 }
00141 
00142 
00143 template<class T, class Key, class Hash>
00144 template<class TRef, class TableRef, class HashedEntryPtr>
00145 inline bool HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00146 operator==(const iterator& iter) const
00147 {
00148     return elmtPtr_ == iter.elmtPtr_;
00149 }
00150 
00151 template<class T, class Key, class Hash>
00152 template<class TRef, class TableRef, class HashedEntryPtr>
00153 inline bool HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00154 operator==(const const_iterator& iter) const
00155 {
00156     return elmtPtr_ == iter.elmtPtr_;
00157 }
00158 
00159 
00160 template<class T, class Key, class Hash>
00161 template<class TRef, class TableRef, class HashedEntryPtr>
00162 inline bool HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00163 operator!=(const iterator& iter) const
00164 {
00165     return elmtPtr_ != iter.elmtPtr_;
00166 }
00167 
00168 template<class T, class Key, class Hash>
00169 template<class TRef, class TableRef, class HashedEntryPtr>
00170 inline bool HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00171 operator!=(const const_iterator& iter) const
00172 {
00173     return elmtPtr_ != iter.elmtPtr_;
00174 }
00175 
00176 
00177 template<class T, class Key, class Hash>
00178 template<class TRef, class TableRef, class HashedEntryPtr>
00179 inline TRef HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00180 operator*()
00181 {
00182     return elmtPtr_->obj_;
00183 }
00184 
00185 
00186 template<class T, class Key, class Hash>
00187 template<class TRef, class TableRef, class HashedEntryPtr>
00188 inline TRef HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00189 operator()()
00190 {
00191     return operator*();
00192 }
00193 
00194 
00195 template<class T, class Key, class Hash>
00196 template<class TRef, class TableRef, class HashedEntryPtr>
00197 inline
00198 typename HashTable<T, Key, Hash>::template Iterator
00199 <
00200     TRef,
00201     TableRef,
00202     HashedEntryPtr
00203 >&
00204 HashTable<T, Key, Hash>::Iterator
00205 <
00206     TRef,
00207     TableRef,
00208     HashedEntryPtr
00209 >::operator++()
00210 {
00211     prevElmtPtr_ = elmtPtr_;
00212 
00213     if
00214     (
00215         !(elmtPtr_ = elmtPtr_->next_)
00216      && ++hashIndex_ < curHashTable_.tableSize_
00217      && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
00218     )
00219     {
00220         prevElmtPtr_ = 0;
00221 
00222         while
00223         (
00224             ++hashIndex_ < curHashTable_.tableSize_
00225          && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
00226         );
00227     }
00228 
00229     return *this;
00230 }
00231 
00232 
00233 template<class T, class Key, class Hash>
00234 template<class TRef, class TableRef, class HashedEntryPtr>
00235 inline
00236 typename HashTable<T, Key, Hash>::template Iterator
00237 <
00238     TRef,
00239     TableRef,
00240     HashedEntryPtr
00241 >
00242 HashTable<T, Key, Hash>::Iterator
00243 <
00244     TRef,
00245     TableRef,
00246     HashedEntryPtr
00247 >::operator++
00248 (
00249     int
00250 )
00251 {
00252     iterator tmp = *this;
00253     ++*this;
00254     return tmp;
00255 }
00256 
00257 
00258 template<class T, class Key, class Hash>
00259 template<class TRef, class TableRef, class HashedEntryPtr>
00260 inline
00261 const Key& HashTable<T, Key, Hash>::Iterator<TRef, TableRef, HashedEntryPtr>::
00262 key()
00263 {
00264     return elmtPtr_->key_;
00265 }
00266 
00267 
00268 template<class T, class Key, class Hash>
00269 inline typename HashTable<T, Key, Hash>::iterator
00270 HashTable<T, Key, Hash>::begin()
00271 {
00272     label i = 0;
00273 
00274     while (table_ && !table_[i] && ++i < tableSize_);
00275 
00276     if (i == tableSize_)
00277     {
00278 #       ifdef FULLDEBUG
00279         if (debug)
00280         {
00281             Info<< "HashTable is empty\n";
00282         }
00283 #       endif
00284 
00285         return HashTable<T, Key, Hash>::endIter_;
00286     }
00287     else
00288     {
00289         return iterator(*this, table_[i], 0, i);
00290     }
00291 }
00292 
00293 
00294 template<class T, class Key, class Hash>
00295 inline const typename HashTable<T, Key, Hash>::iterator&
00296 HashTable<T, Key, Hash>::end()
00297 {
00298     return HashTable<T, Key, Hash>::endIter_;
00299 }
00300 
00301 
00302 template<class T, class Key, class Hash>
00303 inline typename HashTable<T, Key, Hash>::const_iterator
00304 HashTable<T, Key, Hash>::begin() const
00305 {
00306     label i = 0;
00307 
00308     while (table_ && !table_[i] && ++i < tableSize_);
00309 
00310     if (i == tableSize_)
00311     {
00312 #       ifdef FULLDEBUG
00313         if (debug)
00314         {
00315             Info<< "HashTable is empty\n";
00316         }
00317 #       endif
00318 
00319         return HashTable<T, Key, Hash>::endConstIter_;
00320     }
00321     else
00322     {
00323         return const_iterator(*this, table_[i], 0, i);
00324     }
00325 }
00326 
00327 
00328 template<class T, class Key, class Hash>
00329 inline const typename HashTable<T, Key, Hash>::const_iterator&
00330 HashTable<T, Key, Hash>::end() const
00331 {
00332     return HashTable<T, Key, Hash>::endConstIter_;
00333 }
00334 
00335 
00336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00337 
00338 } // End namespace Foam
00339 
00340 // ************************************************************************* //
For further information go to www.openfoam.org