OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

StaticHashTableI.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-2009 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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 #include "IOstreams.H"
00029 
00030 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
00031 
00032 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00033 
00034 template<class T, class Key, class Hash>
00035 inline Foam::label
00036 Foam::StaticHashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
00037 {
00038     // size is power of two - this is the modulus
00039     return Hash()(key) & (keys_.size() - 1);
00040 }
00041 
00042 
00043 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
00044 
00045 template<class T, class Key, class Hash>
00046 inline Foam::label Foam::StaticHashTable<T, Key, Hash>::size() const
00047 {
00048     return nElmts_;
00049 }
00050 
00051 
00052 template<class T, class Key, class Hash>
00053 inline bool Foam::StaticHashTable<T, Key, Hash>::empty() const
00054 {
00055     return !nElmts_;
00056 }
00057 
00058 
00059 template<class T, class Key, class Hash>
00060 inline bool Foam::StaticHashTable<T, Key, Hash>::insert
00061 (
00062     const Key& key,
00063     const T& newEntry
00064 )
00065 {
00066     return set(key, newEntry, true);
00067 }
00068 
00069 
00070 template<class T, class Key, class Hash>
00071 inline bool Foam::StaticHashTable<T, Key, Hash>::set
00072 (
00073     const Key& key,
00074     const T& newEntry
00075 )
00076 {
00077     return set(key, newEntry, false);
00078 }
00079 
00080 
00081 template<class T, class Key, class Hash>
00082 inline Foam::Xfer< Foam::StaticHashTable<T, Key, Hash> >
00083 Foam::StaticHashTable<T, Key, Hash>::xfer()
00084 {
00085     return xferMove(*this);
00086 }
00087 
00088 
00089 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00090 
00091 template<class T, class Key, class Hash>
00092 inline T& Foam::StaticHashTable<T, Key, Hash>::operator[](const Key& key)
00093 {
00094     iterator iter = find(key);
00095 
00096     if (iter == end())
00097     {
00098         FatalErrorIn("StaticHashTable<T, Key, Hash>::operator[](const Key&)")
00099             << key << " not found in table.  Valid entries: "
00100             << toc()
00101             << exit(FatalError);
00102     }
00103 
00104     return *iter;
00105 }
00106 
00107 
00108 template<class T, class Key, class Hash>
00109 inline const T& Foam::StaticHashTable<T, Key, Hash>::operator[]
00110 (
00111     const Key& key
00112 ) const
00113 {
00114     const_iterator iter = find(key);
00115 
00116     if (iter == cend())
00117     {
00118         FatalErrorIn
00119         (
00120             "StaticHashTable<T, Key, Hash>::operator[](const Key&) const"
00121         )   << key << " not found in table.  Valid entries: "
00122             << toc()
00123             << exit(FatalError);
00124     }
00125 
00126     return *iter;
00127 }
00128 
00129 
00130 template<class T, class Key, class Hash>
00131 inline T& Foam::StaticHashTable<T, Key, Hash>::operator()(const Key& key)
00132 {
00133     iterator iter = find(key);
00134 
00135     if (iter == end())
00136     {
00137         insert(key, T());
00138         return *find(key);
00139     }
00140     else
00141     {
00142         return *iter;
00143     }
00144 }
00145 
00146 
00147 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
00148 
00149 template<class T, class Key, class Hash>
00150 template<class TRef, class TableRef>
00151 inline Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::Iterator
00152 (
00153     TableRef hashTbl,
00154     label hashIndex,
00155     label elemIndex
00156 )
00157 :
00158     hashTable_(hashTbl),
00159     hashIndex_(hashIndex),
00160     elemIndex_(elemIndex)
00161 {}
00162 
00163 
00164 template<class T, class Key, class Hash>
00165 template<class TRef, class TableRef>
00166 inline Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::Iterator
00167 (
00168     const iterator& iter
00169 )
00170 :
00171     hashTable_(iter.hashTable_),
00172     hashIndex_(iter.hashIndex_),
00173     elemIndex_(iter.elemIndex_)
00174 {}
00175 
00176 
00177 template<class T, class Key, class Hash>
00178 template<class TRef, class TableRef>
00179 inline void
00180 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator=
00181 (
00182     const iterator& iter
00183 )
00184 {
00185     this->hashIndex_ = iter.hashIndex_;
00186     this->elemIndex_ = iter.elemIndex_;
00187 }
00188 
00189 
00190 template<class T, class Key, class Hash>
00191 template<class TRef, class TableRef>
00192 inline bool
00193 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
00194 (
00195     const iterator& iter
00196 ) const
00197 {
00198     return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
00199 }
00200 
00201 
00202 template<class T, class Key, class Hash>
00203 template<class TRef, class TableRef>
00204 inline bool
00205 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
00206 (
00207     const const_iterator& iter
00208 ) const
00209 {
00210     return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
00211 }
00212 
00213 
00214 template<class T, class Key, class Hash>
00215 template<class TRef, class TableRef>
00216 inline bool
00217 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
00218 (
00219     const iterator& iter
00220 ) const
00221 {
00222     return !operator==(iter);
00223 }
00224 
00225 
00226 template<class T, class Key, class Hash>
00227 template<class TRef, class TableRef>
00228 inline bool
00229 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
00230 (
00231     const const_iterator& iter
00232 ) const
00233 {
00234     return !operator==(iter);
00235 }
00236 
00237 
00238 template<class T, class Key, class Hash>
00239 template<class TRef, class TableRef>
00240 inline TRef
00241 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator*()
00242 {
00243     return hashTable_.objects_[hashIndex_][elemIndex_];
00244 }
00245 
00246 
00247 template<class T, class Key, class Hash>
00248 template<class TRef, class TableRef>
00249 inline TRef
00250 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator()()
00251 {
00252     return operator*();
00253 }
00254 
00255 
00256 template<class T, class Key, class Hash>
00257 template<class TRef, class TableRef>
00258 inline
00259 typename Foam::StaticHashTable<T, Key, Hash>::template Iterator
00260 <
00261     TRef,
00262     TableRef
00263 >&
00264 Foam::StaticHashTable<T, Key, Hash>::Iterator
00265 <
00266     TRef,
00267     TableRef
00268 >::operator++()
00269 {
00270     // Check for special value from erase. (sets hashIndex to -1)
00271     if (hashIndex_ >= 0)
00272     {
00273         // Try the next element on the local list
00274         elemIndex_++;
00275 
00276         if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
00277         {
00278             return *this;
00279         }
00280     }
00281 
00282     // Step to the next table entry
00283     elemIndex_ = 0;
00284 
00285     while
00286     (
00287         ++hashIndex_ < hashTable_.objects_.size()
00288      && !hashTable_.objects_[hashIndex_].size()
00289     )
00290     {}
00291 
00292 
00293     if (hashIndex_ >= hashTable_.objects_.size())
00294     {
00295         // make end iterator
00296         hashIndex_ = hashTable_.keys_.size();
00297     }
00298 
00299     return *this;
00300 }
00301 
00302 
00303 template<class T, class Key, class Hash>
00304 template<class TRef, class TableRef>
00305 inline
00306 typename Foam::StaticHashTable<T, Key, Hash>::template Iterator
00307 <
00308     TRef,
00309     TableRef
00310 >
00311 Foam::StaticHashTable<T, Key, Hash>::Iterator
00312 <
00313     TRef,
00314     TableRef
00315 >::operator++
00316 (
00317     int
00318 )
00319 {
00320     iterator tmp = *this;
00321     ++*this;
00322     return tmp;
00323 }
00324 
00325 
00326 template<class T, class Key, class Hash>
00327 template<class TRef, class TableRef>
00328 inline const Key&
00329 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::key() const
00330 {
00331     return hashTable_.keys_[hashIndex_][elemIndex_];
00332 }
00333 
00334 
00335 template<class T, class Key, class Hash>
00336 inline typename Foam::StaticHashTable<T, Key, Hash>::iterator
00337 Foam::StaticHashTable<T, Key, Hash>::begin()
00338 {
00339     // Find first non-empty entry
00340     forAll(keys_, hashIdx)
00341     {
00342         if (keys_[hashIdx].size())
00343         {
00344             return iterator(*this, hashIdx, 0);
00345         }
00346     }
00347 
00348 #   ifdef FULLDEBUG
00349     if (debug)
00350     {
00351         Info<< "StaticHashTable is empty\n";
00352     }
00353 #   endif
00354 
00355     return StaticHashTable<T, Key, Hash>::endIter_;
00356 }
00357 
00358 
00359 template<class T, class Key, class Hash>
00360 inline const typename Foam::StaticHashTable<T, Key, Hash>::iterator&
00361 Foam::StaticHashTable<T, Key, Hash>::end()
00362 {
00363     return StaticHashTable<T, Key, Hash>::endIter_;
00364 }
00365 
00366 
00367 template<class T, class Key, class Hash>
00368 inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
00369 Foam::StaticHashTable<T, Key, Hash>::cbegin() const
00370 {
00371     // Find first non-empty entry
00372     forAll(keys_, hashIdx)
00373     {
00374         if (keys_[hashIdx].size())
00375         {
00376             return const_iterator(*this, hashIdx, 0);
00377         }
00378     }
00379 
00380 #   ifdef FULLDEBUG
00381     if (debug)
00382     {
00383         Info<< "StaticHashTable is empty\n";
00384     }
00385 #   endif
00386 
00387     return StaticHashTable<T, Key, Hash>::endConstIter_;
00388 }
00389 
00390 
00391 template<class T, class Key, class Hash>
00392 inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
00393 Foam::StaticHashTable<T, Key, Hash>::cend() const
00394 {
00395     return StaticHashTable<T, Key, Hash>::endConstIter_;
00396 }
00397 
00398 
00399 template<class T, class Key, class Hash>
00400 inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
00401 Foam::StaticHashTable<T, Key, Hash>::begin() const
00402 {
00403     return this->cbegin();
00404 }
00405 
00406 
00407 template<class T, class Key, class Hash>
00408 inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
00409 Foam::StaticHashTable<T, Key, Hash>::end() const
00410 {
00411     return StaticHashTable<T, Key, Hash>::endConstIter_;
00412 }
00413 
00414 
00415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00416 
00417 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd