OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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     HashTable
00027 
00028 Description
00029     STL conforming hash table.
00030 
00031 SourceFiles
00032     HashTableI.H
00033     HashTable.C
00034     HashTableIO.C
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef HashTable_H
00039 #define HashTable_H
00040 
00041 #include "label.H"
00042 #include "word.H"
00043 #include "className.H"
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
00051 
00052 template<class T>
00053 class List;
00054 
00055 template<class T, class Key, class Hash> class HashTable;
00056 
00057 template<class T, class Key, class Hash> Istream& operator>>
00058 (
00059     Istream&,
00060     HashTable<T, Key, Hash>&
00061 );
00062 
00063 template<class T, class Key, class Hash> Ostream& operator<<
00064 (
00065     Ostream&,
00066     const HashTable<T, Key, Hash>&
00067 );
00068 
00069 
00070 /*---------------------------------------------------------------------------*\
00071                         Class HashTableName Declaration
00072 \*---------------------------------------------------------------------------*/
00073 
00074 TemplateName(HashTable);
00075 
00076 
00077 /*---------------------------------------------------------------------------*\
00078                           Class HashTable Declaration
00079 \*---------------------------------------------------------------------------*/
00080 
00081 template<class T, class Key=word, class Hash=string::hash>
00082 class HashTable
00083 :
00084     public HashTableName
00085 {
00086     // Private data type for table entries
00087 
00088         struct hashedEntry
00089         {
00090             //- The lookup key
00091             Key key_;
00092 
00093             //- Pointer to next hashedEntry in sub-list
00094             hashedEntry* next_;
00095 
00096             //- The data object
00097             T obj_;
00098 
00099             //- Constructors
00100 
00101                 //- Construct given key, next pointer and object
00102                 inline hashedEntry
00103                 (
00104                     const Key& key,
00105                     hashedEntry* next,
00106                     const T& newEntry
00107                 );
00108 
00109                 //- Dissallow construction as copy
00110                 hashedEntry(const hashedEntry&);
00111         };
00112 
00113 
00114     // Private data: size of table, the table and current number of elements
00115 
00116         //- Number of primary entries allocated in table (not necessarily used)
00117         label tableSize_;
00118 
00119         //- The table of primary entries
00120         hashedEntry** table_;
00121 
00122         //- The current number of elements in table
00123         label nElmts_;
00124 
00125 
00126 public:
00127 
00128         //- Declare friendship with the HashPtrTable class
00129         template<class T2, class Key2, class Hash2>
00130         friend class HashPtrTable;
00131 
00132 
00133     // Forward declaration of STL iterators
00134 
00135         class iterator;
00136         friend class iterator;
00137 
00138         class const_iterator;
00139         friend class const_iterator;
00140 
00141 
00142     // Constructors
00143 
00144         //- Construct given initial table size
00145         HashTable(const label size = 100);
00146 
00147         //- Construct from Istream
00148         HashTable(Istream&, const label size = 100);
00149 
00150         //- Construct as copy
00151         HashTable(const HashTable<T, Key, Hash>&);
00152 
00153 
00154     // Destructor
00155 
00156         ~HashTable();
00157 
00158 
00159     // Member Functions
00160 
00161         // Access
00162 
00163             //- Return number of elements in table.
00164             inline label size() const;
00165 
00166             //- Return true if hashedEntry is found in table
00167             bool found(const Key& key) const;
00168 
00169             //- Find and return an iterator set at the hashedEntry
00170             //  If not found iterator = end()
00171             iterator find(const Key& key);
00172 
00173             //- Find and return an const_iterator set at the hashedEntry
00174             //  If not found iterator = end()
00175             const_iterator find(const Key& key) const;
00176 
00177             //- Return the table of contents
00178             List<Key> toc() const;
00179 
00180 
00181         // Edit
00182 
00183             //- Insert a new hashedEntry
00184             bool insert(const Key& key, const T& newElmt);
00185 
00186             //- Erase an hashedEntry specified by given iterator
00187             bool erase(const iterator& it);
00188 
00189             //- Erase an hashedEntry specified by given key if in table
00190             bool erase(const Key& key);
00191 
00192             //- Resize the hash table for efficiency
00193             void resize(const label newSize);
00194 
00195             //- Clear all entries from table
00196             void clear();
00197 
00198             //- Transfer the contents of the argument table into this table
00199             //  and annull the argument table.
00200             void transfer(HashTable<T, Key, Hash>&);
00201 
00202 
00203     // Member Operators
00204 
00205         //- Find and return an hashedEntry
00206         inline T& operator[](const Key& key);
00207 
00208         //- Find and return an hashedEntry
00209         inline const T& operator[](const Key& key) const;
00210 
00211         //- Find and return an hashedEntry and
00212         //  if it is not present create it null.
00213         inline T& operator()(const Key& key);
00214 
00215         //- Assignment
00216         void operator=(const HashTable<T, Key, Hash>&);
00217 
00218 
00219     // STL type definitions
00220 
00221         //- Type of values the HashTable contains.
00222         typedef T value_type;
00223 
00224         //- Type that can be used for storing into HashTable::value_type
00225         //  objects.  This type is usually List::value_type&.
00226         typedef T& reference;
00227 
00228         //- Type that can be used for storing into constant
00229         //  HashTable::value_type objects.  This type is usually const
00230         //  HashTable::value_type&.
00231         typedef const T& const_reference;
00232 
00233         //- The type that can represent the size of a HashTable.
00234         typedef label size_type;
00235 
00236 
00237     // STL iterator
00238 
00239         class iterator
00240         {
00241             friend class HashTable;
00242             friend class const_iterator;
00243 
00244             // Private data
00245 
00246                 //- Reference to the HashTable this is an iterator for
00247                 HashTable<T, Key, Hash>& curHashTable_;
00248 
00249                 //- Current element
00250                 hashedEntry* elmtPtr_;
00251 
00252                 //- Current hash index
00253                 label hashIndex_;
00254 
00255 
00256         public:
00257 
00258             // Constructors
00259 
00260                 //- Construct from hash table, element and hash index
00261                 inline iterator
00262                 (
00263                     HashTable<T, Key, Hash>& curHashTable,
00264                     hashedEntry* elmt,
00265                     label hashIndex
00266                 );
00267 
00268 
00269             // Member operators
00270 
00271                 inline void operator=(const iterator& iter);
00272 
00273                 inline bool operator==(const iterator& iter) const;
00274                 inline bool operator!=(const iterator& iter) const;
00275 
00276                 inline bool operator==(const const_iterator& iter) const;
00277                 inline bool operator!=(const const_iterator& iter) const;
00278 
00279                 inline T& operator*();
00280                 inline T& operator()();
00281 
00282                 inline iterator& operator++();
00283                 inline iterator operator++(int);
00284 
00285                 inline const Key& key();
00286         };
00287 
00288 
00289         //- iterator set to the begining of the HashTable
00290         inline iterator begin();
00291 
00292         //- iterator set to beyond the end of the HashTable
00293         inline const iterator& end();
00294 
00295 
00296     // STL const_iterator
00297 
00298         class const_iterator
00299         {
00300             friend class iterator;
00301 
00302             // Private data
00303 
00304                 //- Reference to the HashTable this is an iterator for
00305                 const HashTable<T, Key, Hash>& curHashTable_;
00306 
00307                 //- Current element
00308                 const hashedEntry* elmtPtr_;
00309 
00310                 //- Current hash index
00311                 label hashIndex_;
00312 
00313 
00314         public:
00315 
00316             // Constructors
00317 
00318                 //- Construct from hash table, element and hash index
00319                 inline const_iterator
00320                 (
00321                     const HashTable<T, Key, Hash>& curHashTable,
00322                     const hashedEntry* elmt,
00323                     label hashIndex
00324                 );
00325 
00326                 //- Construct from the non-const iterator
00327                 inline const_iterator(const iterator&);
00328 
00329 
00330             // Member operators
00331 
00332                 inline void operator=(const const_iterator& iter);
00333 
00334                 inline bool operator==(const const_iterator& iter) const;
00335                 inline bool operator!=(const const_iterator& iter) const;
00336 
00337                 inline bool operator==(const iterator& iter) const;
00338                 inline bool operator!=(const iterator& iter) const;
00339 
00340                 inline const T& operator*();
00341                 inline const T& operator()();
00342 
00343                 inline const_iterator& operator++();
00344                 inline const_iterator operator++(int);
00345 
00346                 inline const Key& key();
00347         };
00348 
00349 
00350         //- const_iterator set to the begining of the HashTable
00351         inline const_iterator begin() const;
00352 
00353         //- const_iterator set to beyond the end of the HashTable
00354         inline const const_iterator& end() const;
00355 
00356 
00357     // IOstream Operator
00358 
00359         friend Istream& operator>> <T, Key, Hash>
00360         (
00361             Istream&,
00362             HashTable<T, Key, Hash>&
00363         );
00364 
00365         friend Ostream& operator<< <T, Key, Hash>
00366         (
00367             Ostream&,
00368             const HashTable<T, Key, Hash>&
00369         );
00370 
00371 
00372 private:
00373 
00374         //- iterator returned by end()
00375         iterator endIter_;
00376 
00377         //- const_iterator returned by end()
00378         const_iterator endConstIter_;
00379 };
00380 
00381 
00382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00383 
00384 } // End namespace Foam
00385 
00386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00387 
00388 #   include "HashTableI.H"
00389 
00390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00391 
00392 #ifndef NoHashTableC
00393 #ifdef NoRepository
00394 #   include "HashTable.C"
00395 #endif
00396 #endif
00397 
00398 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00399 
00400 #endif
00401 
00402 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd