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

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