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

StaticHashTable.C

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 #ifndef StaticHashTable_C
00028 #define StaticHashTable_C
00029 
00030 #include "StaticHashTable.H"
00031 #include "List.H"
00032 #include "IOstreams.H"
00033 
00034 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00035 
00036 template<class T, class Key, class Hash>
00037 Foam::label Foam::StaticHashTable<T, Key, Hash>::canonicalSize(const label size)
00038 {
00039     if (size < 1)
00040     {
00041         return 0;
00042     }
00043 
00044     // enforce power of two
00045     unsigned int goodSize = size;
00046 
00047     if (goodSize & (goodSize - 1))
00048     {
00049         // brute-force is fast enough
00050         goodSize = 1;
00051         while (goodSize < unsigned(size))
00052         {
00053             goodSize <<= 1;
00054         }
00055     }
00056 
00057     return goodSize;
00058 }
00059 
00060 
00061 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00062 
00063 // Construct given initial table size
00064 template<class T, class Key, class Hash>
00065 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)
00066 :
00067     StaticHashTableName(),
00068     keys_(canonicalSize(size)),
00069     objects_(keys_.size()),
00070     nElmts_(0),
00071     endIter_(*this, keys_.size(), 0),
00072     endConstIter_(*this, keys_.size(), 0)
00073 {
00074     if (size < 1)
00075     {
00076         FatalErrorIn
00077         (
00078             "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
00079         )   << "Illegal size " << size << " for StaticHashTable."
00080             << " Minimum size is 1" << abort(FatalError);
00081     }
00082 }
00083 
00084 
00085 // Construct as copy
00086 template<class T, class Key, class Hash>
00087 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
00088 (
00089     const StaticHashTable<T, Key, Hash>& ht
00090 )
00091 :
00092     StaticHashTableName(),
00093     keys_(ht.keys_),
00094     objects_(ht.objects_),
00095     nElmts_(ht.nElmts_),
00096     endIter_(*this, keys_.size(), 0),
00097     endConstIter_(*this, keys_.size(), 0)
00098 {}
00099 
00100 
00101 
00102 template<class T, class Key, class Hash>
00103 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
00104 (
00105     const Xfer< StaticHashTable<T, Key, Hash> >& ht
00106 )
00107 :
00108     StaticHashTableName(),
00109     keys_(0),
00110     objects_(0),
00111     nElmts_(0),
00112     endIter_(*this, 0, 0),
00113     endConstIter_(*this, 0, 0)
00114 {
00115     transfer(ht());
00116 }
00117 
00118 
00119 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00120 
00121 template<class T, class Key, class Hash>
00122 Foam::StaticHashTable<T, Key, Hash>::~StaticHashTable()
00123 {}
00124 
00125 
00126 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00127 
00128 template<class T, class Key, class Hash>
00129 bool Foam::StaticHashTable<T, Key, Hash>::found(const Key& key) const
00130 {
00131     if (nElmts_)
00132     {
00133         const label hashIdx = hashKeyIndex(key);
00134         const List<Key>& localKeys = keys_[hashIdx];
00135 
00136         forAll(localKeys, elemIdx)
00137         {
00138             if (key == localKeys[elemIdx])
00139             {
00140                 return true;
00141             }
00142         }
00143     }
00144 
00145 #   ifdef FULLDEBUG
00146     if (debug)
00147     {
00148         Info<< "StaticHashTable<T, Key, Hash>::found(const Key&) : "
00149             << "Entry " << key << " not found in hash table\n";
00150     }
00151 #   endif
00152 
00153     return false;
00154 }
00155 
00156 
00157 template<class T, class Key, class Hash>
00158 typename Foam::StaticHashTable<T, Key, Hash>::iterator
00159 Foam::StaticHashTable<T, Key, Hash>::find
00160 (
00161     const Key& key
00162 )
00163 {
00164     if (nElmts_)
00165     {
00166         const label hashIdx = hashKeyIndex(key);
00167         const List<Key>& localKeys = keys_[hashIdx];
00168 
00169         forAll(localKeys, elemIdx)
00170         {
00171             if (key == localKeys[elemIdx])
00172             {
00173                 return iterator(*this, hashIdx, elemIdx);
00174             }
00175         }
00176     }
00177 
00178 #   ifdef FULLDEBUG
00179     if (debug)
00180     {
00181         Info<< "StaticHashTable<T, Key, Hash>::find(const Key&) : "
00182             << "Entry " << key << " not found in hash table\n";
00183     }
00184 #   endif
00185 
00186     return end();
00187 }
00188 
00189 
00190 template<class T, class Key, class Hash>
00191 typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
00192 Foam::StaticHashTable<T, Key, Hash>::find
00193 (
00194     const Key& key
00195 ) const
00196 {
00197     if (nElmts_)
00198     {
00199         const label hashIdx = hashKeyIndex(key);
00200         const List<Key>& localKeys = keys_[hashIdx];
00201 
00202         forAll(localKeys, elemIdx)
00203         {
00204             if (key == localKeys[elemIdx])
00205             {
00206                 return const_iterator(*this, hashIdx, elemIdx);
00207             }
00208         }
00209     }
00210 
00211 #   ifdef FULLDEBUG
00212     if (debug)
00213     {
00214         Info<< "StaticHashTable<T, Key, Hash>::find(const Key&) const : "
00215             << "Entry " << key << " not found in hash table\n";
00216     }
00217 #   endif
00218 
00219     return cend();
00220 }
00221 
00222 
00223 // Return the table of contents
00224 template<class T, class Key, class Hash>
00225 Foam::List<Key> Foam::StaticHashTable<T, Key, Hash>::toc() const
00226 {
00227     List<Key> tofc(nElmts_);
00228     label i = 0;
00229 
00230     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00231     {
00232         tofc[i++] = iter.key();
00233     }
00234 
00235     return tofc;
00236 }
00237 
00238 
00239 template<class T, class Key, class Hash>
00240 bool Foam::StaticHashTable<T, Key, Hash>::set
00241 (
00242     const Key& key,
00243     const T& newEntry,
00244     const bool protect
00245 )
00246 {
00247     const label hashIdx = hashKeyIndex(key);
00248     List<Key>& localKeys = keys_[hashIdx];
00249 
00250     label existing = localKeys.size();
00251     forAll(localKeys, elemIdx)
00252     {
00253         if (key == localKeys[elemIdx])
00254         {
00255             existing = elemIdx;
00256             break;
00257         }
00258     }
00259 
00260     if (existing == localKeys.size())
00261     {
00262         // not found, append
00263         List<T>& localObjects = objects_[hashIdx];
00264 
00265         localKeys.setSize(existing+1);
00266         localObjects.setSize(existing+1);
00267 
00268         localKeys[existing] = key;
00269         localObjects[existing] = newEntry;
00270 
00271         nElmts_++;
00272     }
00273     else if (protect)
00274     {
00275         // found - but protected from overwriting
00276         // this corresponds to the STL 'insert' convention
00277 #       ifdef FULLDEBUG
00278         if (debug)
00279         {
00280             Info<< "StaticHashTable<T, Key, Hash>::set"
00281                 "(const Key& key, T newEntry, true) : "
00282                 "Cannot insert " << key << " already in hash table\n";
00283         }
00284 #       endif
00285         return false;
00286     }
00287     else
00288     {
00289         // found - overwrite existing entry
00290         // this corresponds to the Perl convention
00291         objects_[hashIdx][existing] = newEntry;
00292     }
00293 
00294     return true;
00295 }
00296 
00297 
00298 template<class T, class Key, class Hash>
00299 bool Foam::StaticHashTable<T, Key, Hash>::erase(const iterator& cit)
00300 {
00301     if (cit != end())
00302     {
00303         List<Key>& localKeys = keys_[cit.hashIndex_];
00304         List<T>& localObjects = objects_[cit.hashIndex_];
00305 
00306         // Copy down
00307         for (label i = cit.elemIndex_+1; i < localKeys.size(); i++)
00308         {
00309             localKeys[i-1] = localKeys[i];
00310             localObjects[i-1] = localObjects[i];
00311         }
00312         localKeys.setSize(localKeys.size()-1);
00313         localObjects.setSize(localObjects.size()-1);
00314 
00315         // adjust iterator after erase
00316         iterator& it = const_cast<iterator&>(cit);
00317 
00318         it.elemIndex_--;
00319         if (it.elemIndex_ < 0)
00320         {
00321             // No previous element in the local list
00322 
00323             // Search back for previous non-zero table entry
00324             while (--it.hashIndex_ >= 0 && !objects_[it.hashIndex_].size())
00325             {}
00326 
00327             if (it.hashIndex_ >= 0)
00328             {
00329                 // The last element in the local list
00330                 it.elemIndex_ = objects_[it.hashIndex_].size() - 1;
00331             }
00332             else
00333             {
00334                 // No previous found. Mark with special value which is
00335                 // - not end()
00336                 // - handled by operator++
00337                 it.hashIndex_ = -1;
00338                 it.elemIndex_ = 0;
00339             }
00340         }
00341 
00342         nElmts_--;
00343 
00344 #       ifdef FULLDEBUG
00345         if (debug)
00346         {
00347             Info<< "StaticHashTable<T, Key, Hash>::erase(iterator&) : "
00348                 << "hashedEntry removed.\n";
00349         }
00350 #       endif
00351 
00352         return true;
00353     }
00354     else
00355     {
00356 #       ifdef FULLDEBUG
00357         if (debug)
00358         {
00359             Info<< "StaticHashTable<T, Key, Hash>::erase(iterator&) : "
00360                 << "cannot remove hashedEntry from hash table\n";
00361         }
00362 #       endif
00363 
00364         return false;
00365     }
00366 }
00367 
00368 
00369 template<class T, class Key, class Hash>
00370 bool Foam::StaticHashTable<T, Key, Hash>::erase(const Key& key)
00371 {
00372     iterator it = find(key);
00373 
00374     if (it != end())
00375     {
00376         return erase(it);
00377     }
00378     else
00379     {
00380         return false;
00381     }
00382 }
00383 
00384 
00385 template<class T, class Key, class Hash>
00386 Foam::label Foam::StaticHashTable<T, Key, Hash>::erase
00387 (
00388     const StaticHashTable<T, Key, Hash>& rhs
00389 )
00390 {
00391     label count = 0;
00392 
00393     // Remove rhs elements from this table
00394     // NOTE: could optimize depending on which hash is smaller
00395     for (iterator iter = this->begin(); iter != this->end(); ++iter)
00396     {
00397         if (rhs.found(iter.key()) && erase(iter))
00398         {
00399             count++;
00400         }
00401     }
00402 
00403    return count;
00404 }
00405 
00406 
00407 template<class T, class Key, class Hash>
00408 void Foam::StaticHashTable<T, Key, Hash>::resize(const label sz)
00409 {
00410     label newSize = canonicalSize(sz);
00411     
00412     if (newSize == keys_.size())
00413     {
00414 #       ifdef FULLDEBUG
00415         if (debug)
00416         {
00417             Info<< "StaticHashTable<T, Key, Hash>::resize(const label) : "
00418                 << "new table size == old table size\n";
00419         }
00420 #       endif
00421 
00422         return;
00423     }
00424 
00425     if (newSize < 1)
00426     {
00427         FatalErrorIn
00428         (
00429             "StaticHashTable<T, Key, Hash>::resize(const label)"
00430         )   << "Illegal size " << newSize << " for StaticHashTable."
00431             << " Minimum size is 1" << abort(FatalError);
00432     }
00433 
00434 
00435     StaticHashTable<T, Key, Hash> newTable(newSize);
00436 
00437     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00438     {
00439         newTable.insert(iter.key(), *iter);
00440     }
00441 
00442     transfer(newTable);
00443 
00444     // Adapt end() iterators
00445     endIter_.hashIndex_ = keys_.size();
00446     endConstIter_.hashIndex_ = keys_.size();
00447 }
00448 
00449 
00450 template<class T, class Key, class Hash>
00451 void Foam::StaticHashTable<T, Key, Hash>::clear()
00452 {
00453     forAll(keys_, hashIdx)
00454     {
00455         keys_[hashIdx].clear();
00456         objects_[hashIdx].clear();
00457     }
00458 
00459     nElmts_ = 0;
00460 }
00461 
00462 
00463 template<class T, class Key, class Hash>
00464 void Foam::StaticHashTable<T, Key, Hash>::clearStorage()
00465 {
00466     clear();
00467     resize(1);
00468 }
00469 
00470 
00471 
00472 template<class T, class Key, class Hash>
00473 void Foam::StaticHashTable<T, Key, Hash>::transfer
00474 (
00475     StaticHashTable<T, Key, Hash>& ht
00476 )
00477 {
00478     // Remove existing elements
00479     clear();
00480 
00481     // Copy data from ht
00482     keys_.transfer(ht.keys_);
00483     objects_.transfer(ht.objects_);
00484 
00485     nElmts_ = ht.nElmts_;
00486     ht.nElmts_ = 0;
00487 
00488     // Adapt end() iterators
00489     endIter_.hashIndex_ = keys_.size();
00490     endConstIter_.hashIndex_ = keys_.size();
00491 
00492     ht.endIter_.hashIndex_ = 0;
00493     ht.endConstIter_.hashIndex_ = 0;
00494 }
00495 
00496 
00497 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00498 
00499 template<class T, class Key, class Hash>
00500 void Foam::StaticHashTable<T, Key, Hash>::operator=
00501 (
00502     const StaticHashTable<T, Key, Hash>& rhs
00503 )
00504 {
00505     // Check for assignment to self
00506     if (this == &rhs)
00507     {
00508         FatalErrorIn
00509         (
00510             "StaticHashTable<T, Key, Hash>::operator="
00511             "(const StaticHashTable<T, Key, Hash>&)"
00512         )   << "attempted assignment to self"
00513             << abort(FatalError);
00514     }
00515 
00516 
00517     // keys could be empty from a previous transfer()
00518     if (keys_.empty())
00519     {
00520         keys_.setSize(rhs.keys_.size());
00521         objects_.setSize(keys_.size());
00522 
00523         // Adapt end() iterators
00524         endIter_.hashIndex_ = keys_.size();
00525         endConstIter_.hashIndex_ = keys_.size();
00526     }
00527     else
00528     {
00529         clear();
00530         // keys_.size() does not change so neither does end() iterator.
00531     }
00532 
00533 
00534     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00535     {
00536         insert(iter.key(), *iter);
00537     }
00538 }
00539 
00540 template<class T, class Key, class Hash>
00541 bool Foam::StaticHashTable<T, Key, Hash>::operator==
00542 (
00543     const StaticHashTable<T, Key, Hash>& rhs
00544 ) const
00545 {
00546     // Are all my elements in rhs?
00547     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00548     {
00549         const_iterator fnd = rhs.find(iter.key());
00550 
00551         if (fnd == rhs.cend() || fnd() != iter())
00552         {
00553             return false;
00554         }
00555     }
00556 
00557     // Are all rhs elements in me?
00558     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00559     {
00560         const_iterator fnd = find(iter.key());
00561 
00562         if (fnd == cend() || fnd() != iter())
00563         {
00564             return false;
00565         }
00566     }
00567     return true;
00568 }
00569 
00570 
00571 template<class T, class Key, class Hash>
00572 bool Foam::StaticHashTable<T, Key, Hash>::operator!=
00573 (
00574     const StaticHashTable<T, Key, Hash>& rhs
00575 ) const
00576 {
00577     return !(operator==(rhs));
00578 }
00579 
00580 
00581 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00582 
00583 #include "StaticHashTableIO.C"
00584 
00585 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00586 
00587 #endif
00588 
00589 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd