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

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