FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

HashTable.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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::HashTable
00026 
00027 Description
00028     An STL-conforming hash table.
00029 
00030 Note
00031     Hashing index collisions are handled via chaining using a singly-linked
00032     list with the colliding entry being added to the head of the linked
00033     list. Thus copying the hash table (or indeed even resizing it) will
00034     often result in a different hash order. Use a sorted table-of-contents
00035     when the hash order is important.
00036 
00037 SourceFiles
00038     HashTableI.H
00039     HashTable.C
00040     HashTableIO.C
00041 
00042 \*---------------------------------------------------------------------------*/
00043 
00044 #ifndef HashTable_H
00045 #define HashTable_H
00046 
00047 #include <OpenFOAM/label.H>
00048 #include <OpenFOAM/uLabel.H>
00049 #include <OpenFOAM/word.H>
00050 #include <OpenFOAM/Xfer.H>
00051 #include <OpenFOAM/className.H>
00052 
00053 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00054 
00055 namespace Foam
00056 {
00057 
00058 // Forward declaration of friend functions and operators
00059 
00060 template<class T> class List;
00061 template<class T> class UList;
00062 template<class T, class Key, class Hash> class HashTable;
00063 template<class T, class Key, class Hash> class HashPtrTable;
00064 
00065 template<class T, class Key, class Hash>
00066 Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
00067 
00068 template<class T, class Key, class Hash>
00069 Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
00070 
00071 
00072 /*---------------------------------------------------------------------------*\
00073                         Class HashTableName Declaration
00074 \*---------------------------------------------------------------------------*/
00075 
00076 TemplateName(HashTable);
00077 
00078 
00079 /*---------------------------------------------------------------------------*\
00080                           Class HashTable Declaration
00081 \*---------------------------------------------------------------------------*/
00082 
00083 template<class T, class Key=word, class Hash=string::hash>
00084 class HashTable
00085 :
00086     public HashTableName
00087 {
00088     // Private data type for table entries
00089 
00090         struct hashedEntry
00091         {
00092             //- The lookup key
00093             Key key_;
00094 
00095             //- Pointer to next hashedEntry in sub-list
00096             hashedEntry* next_;
00097 
00098             //- The data object
00099             T obj_;
00100 
00101             //- Constructors
00102 
00103                 //- Construct given key, next pointer and object
00104                 inline hashedEntry
00105                 (
00106                     const Key&,
00107                     hashedEntry* next,
00108                     const T& newEntry
00109                 );
00110 
00111                 //- Dissallow construction as copy
00112                 hashedEntry(const hashedEntry&);
00113         };
00114 
00115 
00116     // Private data: size of table, the table and current number of elements
00117 
00118         //- The current number of elements in table
00119         label nElmts_;
00120 
00121         //- Number of primary entries allocated in table (not necessarily used)
00122         label tableSize_;
00123 
00124         //- The table of primary entries
00125         hashedEntry** table_;
00126 
00127 
00128     // Private Member Functions
00129 
00130         //- Return a canonical (power-of-two) size
00131         static label canonicalSize(const label);
00132 
00133         //- Return the hash index of the Key within the current table size.
00134         //  No checks for zero-sized tables.
00135         inline label hashKeyIndex(const Key&) const;
00136 
00137         //- Assign a new hashedEntry to a possibly already existing key
00138         bool set(const Key&, const T& newElmt, bool protect);
00139 
00140 
00141 public:
00142 
00143         //- Declare friendship with the HashPtrTable class
00144         template<class T2, class Key2, class Hash2>
00145         friend class HashPtrTable;
00146 
00147 
00148     // Forward declaration of STL iterators
00149 
00150         class iterator;
00151         friend class iterator;
00152 
00153         class const_iterator;
00154         friend class const_iterator;
00155 
00156 
00157     // Constructors
00158 
00159         //- Construct given initial table size
00160         HashTable(const label size = 128);
00161 
00162         //- Construct from Istream
00163         HashTable(Istream&, const label size = 128);
00164 
00165         //- Construct as copy
00166         HashTable(const HashTable<T, Key, Hash>&);
00167 
00168         //- Construct by transferring the parameter contents
00169         HashTable(const Xfer<HashTable<T, Key, Hash> >&);
00170 
00171 
00172     // Destructor
00173 
00174         ~HashTable();
00175 
00176 
00177     // Member Functions
00178 
00179         // Access
00180 
00181             //- Return number of elements in table.
00182             inline label size() const;
00183 
00184             //- Return true if the hash table is empty
00185             inline bool empty() const;
00186 
00187             //- Return true if hashedEntry is found in table
00188             bool found(const Key&) const;
00189 
00190             //- Find and return an iterator set at the hashedEntry
00191             //  If not found iterator = end()
00192             iterator find(const Key&);
00193 
00194             //- Find and return an const_iterator set at the hashedEntry
00195             //  If not found iterator = end()
00196             const_iterator find(const Key&) const;
00197 
00198             //- Return the table of contents
00199             List<Key> toc() const;
00200 
00201             //- Return the table of contents as a sorted list
00202             List<Key> sortedToc() const;
00203 
00204             //- Print information
00205             Ostream& printInfo(Ostream&) const;
00206 
00207         // Edit
00208 
00209             //- Insert a new hashedEntry
00210             inline bool insert(const Key&, const T& newElmt);
00211 
00212             //- Assign a new hashedEntry, overwriting existing entries
00213             inline bool set(const Key&, const T& newElmt);
00214 
00215             //- Erase an hashedEntry specified by given iterator
00216             bool erase(const iterator&);
00217 
00218             //- Erase an hashedEntry specified by given key if in table
00219             bool erase(const Key&);
00220 
00221             //- Remove entries given by the listed keys from this HashTable
00222             //  Return the number of elements removed
00223             label erase(const UList<Key>&);
00224 
00225             //- Remove entries given by the given keys from this HashTable
00226             //  Return the number of elements removed.
00227             //  The parameter HashTable needs the same type of key, but the
00228             //  type of values held and the hashing function are arbitrary.
00229             template<class AnyType, class AnyHash>
00230             label erase(const HashTable<AnyType, Key, AnyHash>&);
00231 
00232             //- Resize the hash table for efficiency
00233             void resize(const label newSize);
00234 
00235             //- Clear all entries from table
00236             void clear();
00237 
00238             //- Clear the table entries and the table itself.
00239             //  Equivalent to clear() followed by resize(0)
00240             void clearStorage();
00241 
00242             //- Transfer the contents of the argument table into this table
00243             //  and annull the argument table.
00244             void transfer(HashTable<T, Key, Hash>&);
00245 
00246             //- Transfer contents to the Xfer container
00247             inline Xfer<HashTable<T, Key, Hash> > xfer();
00248 
00249 
00250     // Member Operators
00251 
00252         //- Find and return an hashedEntry
00253         inline T& operator[](const Key&);
00254 
00255         //- Find and return an hashedEntry
00256         inline const T& operator[](const Key&) const;
00257 
00258         //- Find and return an hashedEntry, create it null if not present.
00259         inline T& operator()(const Key&);
00260 
00261         //- Assignment
00262         void operator=(const HashTable<T, Key, Hash>&);
00263 
00264         //- Equality. Two hash tables are equal if all contents of first are
00265         //  also in second and vice versa. So does not depend on table size or
00266         //  order!
00267         bool operator==(const HashTable<T, Key, Hash>&) const;
00268 
00269         //- The opposite of the equality operation. Takes linear time.
00270         bool operator!=(const HashTable<T, Key, Hash>&) const;
00271 
00272 
00273     // STL type definitions
00274 
00275         //- Type of values the HashTable contains.
00276         typedef T value_type;
00277 
00278         //- Type that can be used for storing into HashTable::value_type
00279         //  objects.  This type is usually List::value_type&.
00280         typedef T& reference;
00281 
00282         //- Type that can be used for storing into constant
00283         //  HashTable::value_type objects.  This type is usually const
00284         //  HashTable::value_type&.
00285         typedef const T& const_reference;
00286 
00287         //- The type that can represent the size of a HashTable.
00288         typedef label size_type;
00289 
00290 
00291     // STL iterator
00292 
00293         //- An STL-conforming iterator
00294         class iterator
00295         {
00296             friend class HashTable;
00297             friend class const_iterator;
00298 
00299             // Private data
00300 
00301                 //- Reference to the HashTable this is an iterator for
00302                 HashTable<T, Key, Hash>& hashTable_;
00303 
00304                 //- Current element
00305                 hashedEntry* elmtPtr_;
00306 
00307                 //- Current hash index
00308                 label hashIndex_;
00309 
00310         public:
00311 
00312             // Constructors
00313 
00314                 //- Construct from hash table, element and hash index
00315                 inline iterator
00316                 (
00317                     HashTable<T, Key, Hash>& curHashTable,
00318                     hashedEntry* elmt,
00319                     label hashIndex
00320                 );
00321 
00322             // Member operators
00323 
00324                 inline void operator=(const iterator&);
00325 
00326                 inline bool operator==(const iterator&) const;
00327                 inline bool operator!=(const iterator&) const;
00328 
00329                 inline bool operator==(const const_iterator&) const;
00330                 inline bool operator!=(const const_iterator&) const;
00331 
00332                 inline T& operator*();
00333                 inline T& operator()();
00334 
00335                 inline const T& operator*() const;
00336                 inline const T& operator()() const;
00337 
00338                 inline iterator& operator++();
00339                 inline iterator operator++(int);
00340 
00341                 inline const Key& key() const;
00342         };
00343 
00344 
00345         //- iterator set to the begining of the HashTable
00346         inline iterator begin();
00347 
00348         //- iterator set to beyond the end of the HashTable
00349         inline const iterator& end();
00350 
00351 
00352     // STL const_iterator
00353 
00354         //- An STL-conforming const_iterator
00355         class const_iterator
00356         {
00357             friend class iterator;
00358 
00359             // Private data
00360 
00361                 //- Reference to the HashTable this is an iterator for
00362                 const HashTable<T, Key, Hash>& hashTable_;
00363 
00364                 //- Current element
00365                 const hashedEntry* elmtPtr_;
00366 
00367                 //- Current hash index
00368                 label hashIndex_;
00369 
00370 
00371         public:
00372 
00373             // Constructors
00374 
00375                 //- Construct from hash table, element and hash index
00376                 inline const_iterator
00377                 (
00378                     const HashTable<T, Key, Hash>& curHashTable,
00379                     const hashedEntry* elmt,
00380                     label hashIndex
00381                 );
00382 
00383                 //- Construct from the non-const iterator
00384                 inline const_iterator(const iterator&);
00385 
00386 
00387             // Member operators
00388 
00389                 inline void operator=(const const_iterator&);
00390 
00391                 inline bool operator==(const const_iterator&) const;
00392                 inline bool operator!=(const const_iterator&) const;
00393 
00394                 inline bool operator==(const iterator&) const;
00395                 inline bool operator!=(const iterator&) const;
00396 
00397                 inline const T& operator*() const;
00398                 inline const T& operator()() const;
00399 
00400                 inline const_iterator& operator++();
00401                 inline const_iterator operator++(int);
00402 
00403                 inline const Key& key() const;
00404         };
00405 
00406 
00407         //- const_iterator set to the beginning of the HashTable
00408         inline const_iterator cbegin() const;
00409 
00410         //- const_iterator set to beyond the end of the HashTable
00411         inline const const_iterator& cend() const;
00412 
00413         //- const_iterator set to the beginning of the HashTable
00414         inline const_iterator begin() const;
00415 
00416         //- const_iterator set to beyond the end of the HashTable
00417         inline const const_iterator& end() const;
00418 
00419 
00420     // IOstream Operator
00421 
00422         friend Istream& operator>> <T, Key, Hash>
00423         (
00424             Istream&,
00425             HashTable<T, Key, Hash>&
00426         );
00427 
00428         friend Ostream& operator<< <T, Key, Hash>
00429         (
00430             Ostream&,
00431             const HashTable<T, Key, Hash>&
00432         );
00433 
00434 
00435 private:
00436 
00437         //- iterator returned by end()
00438         iterator endIter_;
00439 
00440         //- const_iterator returned by end()
00441         const_iterator endConstIter_;
00442 };
00443 
00444 
00445 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00446 
00447 } // End namespace Foam
00448 
00449 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00450 
00451 #   include <OpenFOAM/HashTableI.H>
00452 
00453 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00454 
00455 #ifndef NoHashTableC
00456 #ifdef NoRepository
00457 #   include <OpenFOAM/HashTable.C>
00458 #endif
00459 #endif
00460 
00461 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00462 
00463 #endif
00464 
00465 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines