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

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