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

StaticHashTable.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::StaticHashTable
00027 
00028 Description
00029     STL conforming hash table.
00030 
00031 Note
00032     Uses straight lists as underlying type.
00033     Is slower to insert than the standard HashTable, but should be more
00034     memory efficient and faster to access.
00035 
00036 SourceFiles
00037     StaticHashTableI.H
00038     StaticHashTable.C
00039     StaticHashTableIO.C
00040 
00041 \*---------------------------------------------------------------------------*/
00042 
00043 #ifndef StaticHashTable_H
00044 #define StaticHashTable_H
00045 
00046 #include "label.H"
00047 #include "uLabel.H"
00048 #include "word.H"
00049 #include "Xfer.H"
00050 #include "className.H"
00051 
00052 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00053 
00054 namespace Foam
00055 {
00056 
00057 // Forward declaration of friend functions and operators
00058 
00059 template<class T> class List;
00060 template<class T, class Key, class Hash> class StaticHashTable;
00061 
00062 template<class T, class Key, class Hash> Istream& operator>>
00063 (
00064     Istream&,
00065     StaticHashTable<T, Key, Hash>&
00066 );
00067 
00068 template<class T, class Key, class Hash> Ostream& operator<<
00069 (
00070     Ostream&,
00071     const StaticHashTable<T, Key, Hash>&
00072 );
00073 
00074 
00075 /*---------------------------------------------------------------------------*\
00076                      Class StaticHashTableName Declaration
00077 \*---------------------------------------------------------------------------*/
00078 
00079 TemplateName(StaticHashTable);
00080 
00081 
00082 /*---------------------------------------------------------------------------*\
00083                        Class StaticHashTable Declaration
00084 \*---------------------------------------------------------------------------*/
00085 
00086 template<class T, class Key=word, class Hash=string::hash>
00087 class StaticHashTable
00088 :
00089     public StaticHashTableName
00090 {
00091     // Private data type for table entries
00092 
00093         //- The lookup keys, ordered per hash value
00094         List<List<Key> > keys_;
00095 
00096         //- For each key the corresponding object.
00097         List<List<T> > objects_;
00098 
00099         //- The current number of elements in table
00100         label nElmts_;
00101 
00102         //- Return a canonical (power-of-two) size
00103         static label canonicalSize(const label);
00104 
00105         //- Return the hash index of the Key within the current table size.
00106         //  No checks for zero-sized tables.
00107         inline label hashKeyIndex(const Key&) const;
00108 
00109         //- Assign a new hashed entry to a possibly already existing key
00110         bool set(const Key&, const T& newElmt, bool protect);
00111 
00112 public:
00113 
00114 
00115     // Forward declaration of STL iterators
00116 
00117         template<class TRef, class TableRef>
00118         class Iterator;
00119 
00120         typedef Iterator
00121         <
00122             T&,
00123             StaticHashTable<T, Key, Hash>&
00124         > iterator;
00125 
00126         typedef Iterator
00127         <
00128             const T&,
00129             const StaticHashTable<T, Key, Hash>&
00130         > const_iterator;
00131 
00132 
00133     // Declare friendship with the iterators
00134 
00135         friend class Iterator
00136         <
00137             T&,
00138             StaticHashTable<T, Key, Hash>&
00139         >;
00140 
00141         friend class Iterator
00142         <
00143             const T&,
00144             const StaticHashTable<T, Key, Hash>&
00145         >;
00146 
00147 
00148     // Constructors
00149 
00150         //- Construct given initial table size
00151         StaticHashTable(const label size = 128);
00152 
00153         //- Construct from Istream
00154         StaticHashTable(Istream&, const label size = 128);
00155 
00156         //- Construct as copy
00157         StaticHashTable(const StaticHashTable<T, Key, Hash>&);
00158 
00159         //- Construct by transferring the parameter contents
00160         StaticHashTable(const Xfer< StaticHashTable<T, Key, Hash> >&);
00161 
00162     // Destructor
00163 
00164         ~StaticHashTable();
00165 
00166 
00167     // Member Functions
00168 
00169         // Access
00170 
00171             //- Return number of elements in table.
00172             inline label size() const;
00173 
00174             //- Return true if the hash table is empty
00175             inline bool empty() const;
00176 
00177             //- Return true if hashed entry is found in table
00178             bool found(const Key& key) const;
00179 
00180             //- Find and return an iterator set at the hashed entry
00181             //  If not found iterator = end()
00182             iterator find(const Key& key);
00183 
00184             //- Find and return an const_iterator set at the hashed entry
00185             //  If not found iterator = end()
00186             const_iterator find(const Key& key) const;
00187 
00188             //- Return the table of contents
00189             List<Key> toc() const;
00190 
00191             //- Print information
00192             Ostream& printInfo(Ostream&) const;
00193 
00194 
00195         // Edit
00196 
00197             //- Insert a new hashed entry
00198             bool insert(const Key& key, const T& newElmt);
00199 
00200             //- Assign a new hashed entry, overwriting existing entries
00201             inline bool set(const Key&, const T& newElmt);
00202 
00203             //- Erase an hashed entry specified by given iterator
00204             bool erase(const iterator& it);
00205 
00206             //- Erase an hashed entry specified by given key if in table
00207             bool erase(const Key& key);
00208 
00209             //- Resize the hash table for efficiency
00210             void resize(const label newSize);
00211 
00212             //- Remove entries in the given hash table from this hash table
00213             //  Return the number of elements removed
00214             label erase(const StaticHashTable<T, Key, Hash>&);
00215 
00216             //- Clear all entries from table
00217             void clear();
00218 
00219             //- Clear the table entries and the table itself.
00220             //  Equivalent to clear() followed by resize(1)
00221             void clearStorage();
00222 
00223             //- Transfer the contents of the argument table into this table
00224             //  and annull the argument table.
00225             void transfer(StaticHashTable<T, Key, Hash>&);
00226 
00227             //- Transfer contents to the Xfer container
00228             inline Xfer< StaticHashTable<T, Key, Hash> > xfer();
00229 
00230 
00231     // Member Operators
00232 
00233         //- Find and return an hashed entry
00234         inline T& operator[](const Key&);
00235 
00236         //- Find and return an hashed entry
00237         inline const T& operator[](const Key&) const;
00238 
00239         //- Find and return an hashed entry, create it null if not present.
00240         inline T& operator()(const Key&);
00241 
00242         //- Assignment
00243         void operator=(const StaticHashTable<T, Key, Hash>&);
00244 
00245         //- Equality. Two hash tables are equal if all contents of first are
00246         //  also in second and vice versa.
00247         bool operator==(const StaticHashTable<T, Key, Hash>&) const;
00248 
00249         //- The opposite of the equality operation.
00250         bool operator!=(const StaticHashTable<T, Key, Hash>&) const;
00251 
00252     // STL type definitions
00253 
00254         //- Type of values the StaticHashTable contains.
00255         typedef T value_type;
00256 
00257         //- Type that can be used for storing into StaticHashTable::value_type
00258         //  objects.  This type is usually List::value_type&.
00259         typedef T& reference;
00260 
00261         //- Type that can be used for storing into constant
00262         //  StaticHashTable::value_type objects.  This type is usually const
00263         //  StaticHashTable::value_type&.
00264         typedef const T& const_reference;
00265 
00266         //- The type that can represent the size of a StaticHashTable.
00267         typedef label size_type;
00268 
00269 
00270     // STL iterator
00271 
00272         //- An STL iterator
00273         template<class TRef, class TableRef>
00274         class Iterator
00275         {
00276             friend class StaticHashTable;
00277 
00278 #           ifndef __INTEL_COMPILER
00279             template<class TRef2, class TableRef2>
00280             friend class Iterator;
00281 #           endif
00282 
00283             // Private data
00284 
00285                 //- Reference to the StaticHashTable this is an iterator for
00286                 TableRef hashTable_;
00287 
00288                 //- Current hash index
00289                 label hashIndex_;
00290 
00291                 //- Index of current element at hashIndex
00292                 label elemIndex_;
00293 
00294         public:
00295 
00296             // Constructors
00297 
00298                 //- Construct from hash table, hash index and element index
00299                 inline Iterator
00300                 (
00301                     TableRef,
00302                     label hashIndex_,
00303                     label elemIndex_
00304                 );
00305 
00306                 //- Construct from the non-const iterator
00307                 inline Iterator(const iterator&);
00308 
00309 
00310             // Member operators
00311 
00312                 inline void operator=(const iterator&);
00313 
00314                 inline bool operator==(const iterator&) const;
00315                 inline bool operator==(const const_iterator&) const;
00316 
00317                 inline bool operator!=(const iterator&) const;
00318                 inline bool operator!=(const const_iterator&) const;
00319 
00320                 inline TRef operator*();
00321                 inline TRef operator()();
00322 
00323                 inline Iterator& operator++();
00324                 inline Iterator operator++(int);
00325 
00326                 inline const Key& key() const;
00327         };
00328 
00329 
00330         //- iterator set to the beginning of the StaticHashTable
00331         inline iterator begin();
00332 
00333         //- iterator set to beyond the end of the StaticHashTable
00334         inline const iterator& end();
00335 
00336         //- const_iterator set to the beginning of the StaticHashTable
00337         inline const_iterator cbegin() const;
00338 
00339         //- const_iterator set to beyond the end of the StaticHashTable
00340         inline const const_iterator& cend() const;
00341 
00342         //- const_iterator set to the beginning of the StaticHashTable
00343         inline const_iterator begin() const;
00344 
00345         //- const_iterator set to beyond the end of the StaticHashTable
00346         inline const const_iterator& end() const;
00347 
00348     // IOstream Operator
00349 
00350         friend Istream& operator>> <T, Key, Hash>
00351         (
00352             Istream&,
00353             StaticHashTable<T, Key, Hash>&
00354         );
00355 
00356         friend Ostream& operator<< <T, Key, Hash>
00357         (
00358             Ostream&,
00359             const StaticHashTable<T, Key, Hash>&
00360         );
00361 
00362 
00363 private:
00364 
00365         //- iterator returned by end()
00366         iterator endIter_;
00367 
00368         //- const_iterator returned by end()
00369         const_iterator endConstIter_;
00370 };
00371 
00372 
00373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00374 
00375 } // End namespace Foam
00376 
00377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00378 
00379 #   include "StaticHashTableI.H"
00380 
00381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00382 
00383 #ifndef NoStaticHashTableC
00384 #ifdef NoRepository
00385 #   include "StaticHashTable.C"
00386 #endif
00387 #endif
00388 
00389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00390 
00391 #endif
00392 
00393 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd