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

StaticHashTableIO.C

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 \*---------------------------------------------------------------------------*/
00025 
00026 #include "StaticHashTable.H"
00027 #include <OpenFOAM/Istream.H>
00028 #include <OpenFOAM/Ostream.H>
00029 
00030 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00031 
00032 template<class T, class Key, class Hash>
00033 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
00034 (
00035     Istream& is,
00036     const label size
00037 )
00038 :
00039     StaticHashTableName(),
00040     keys_(size),
00041     objects_(size),
00042     nElmts_(0),
00043     endIter_(*this, keys_.size(), 0),
00044     endConstIter_(*this, keys_.size(), 0)
00045 {
00046     if (size < 1)
00047     {
00048         FatalErrorIn
00049         (
00050             "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
00051         )   << "Illegal size " << size << " for StaticHashTable."
00052             << " Minimum size is 1" << abort(FatalError);
00053     }
00054 
00055     operator>>(is, *this);
00056 }
00057 
00058 
00059 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00060 
00061 template<class T, class Key, class Hash>
00062 Foam::Ostream&
00063 Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
00064 {
00065     label used = 0;
00066     label maxChain = 0;
00067     unsigned avgChain = 0;
00068 
00069     // Find first non-empty entry
00070     forAll(keys_, hashIdx)
00071     {
00072         const label count = keys_[hashIdx].size();
00073         if (count)
00074         {
00075             ++used;
00076             avgChain += count;
00077 
00078             if (maxChain < count)
00079             {
00080                 maxChain = count;
00081             }
00082         }
00083     }
00084 
00085     os  << "StaticHashTable<T,Key,Hash>"
00086         << " elements:" << size() << " slots:" << used << "/" << keys_.size()
00087         << " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
00088         << "/" << maxChain << endl;
00089 
00090     return os;
00091 }
00092 
00093 
00094 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00095 
00096 template<class T, class Key, class Hash>
00097 Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
00098 {
00099     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
00100 
00101     // Anull list
00102     L.clear();
00103 
00104     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
00105 
00106     token firstToken(is);
00107 
00108     is.fatalCheck
00109     (
00110         "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
00111         "reading first token"
00112     );
00113 
00114     if (firstToken.isLabel())
00115     {
00116         label s = firstToken.labelToken();
00117 
00118         // Read beginning of contents
00119         char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
00120 
00121         if (s)
00122         {
00123             if (2*s > L.keys_.size())
00124             {
00125                 L.resize(2*s);
00126             }
00127 
00128             if (delimiter == token::BEGIN_LIST)
00129             {
00130                 for (label i=0; i<s; i++)
00131                 {
00132                     Key key;
00133                     is >> key;
00134                     L.insert(key, pTraits<T>(is));
00135 
00136                     is.fatalCheck
00137                     (
00138                         "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)"
00139                         " : reading entry"
00140                     );
00141                 }
00142             }
00143             else
00144             {
00145                 FatalIOErrorIn
00146                 (
00147                     "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
00148                     is
00149                 )   << "incorrect first token, '(', found " << firstToken.info()
00150                     << exit(FatalIOError);
00151             }
00152         }
00153 
00154         // Read end of contents
00155         is.readEndList("StaticHashTable");
00156     }
00157     else if (firstToken.isPunctuation())
00158     {
00159         if (firstToken.pToken() != token::BEGIN_LIST)
00160         {
00161             FatalIOErrorIn
00162             (
00163                 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
00164                 is
00165             )   << "incorrect first token, '(', found " << firstToken.info()
00166                 << exit(FatalIOError);
00167         }
00168 
00169         token lastToken(is);
00170         while
00171         (
00172            !(
00173                 lastToken.isPunctuation()
00174              && lastToken.pToken() == token::END_LIST
00175             )
00176         )
00177         {
00178             is.putBack(lastToken);
00179 
00180             Key key;
00181             is >> key;
00182 
00183             T element;
00184             is >> element;
00185 
00186             L.insert(key, element);
00187 
00188             is.fatalCheck
00189             (
00190                 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
00191                 "reading entry"
00192             );
00193 
00194             is >> lastToken;
00195         }
00196     }
00197     else
00198     {
00199         FatalIOErrorIn
00200         (
00201             "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
00202             is
00203         )   << "incorrect first token, expected <int> or '(', found "
00204             << firstToken.info()
00205             << exit(FatalIOError);
00206     }
00207 
00208     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
00209 
00210     return is;
00211 }
00212 
00213 
00214 template<class T, class Key, class Hash>
00215 Foam::Ostream& Foam::operator<<
00216 (
00217     Ostream& os,
00218     const StaticHashTable<T, Key, Hash>& L)
00219 {
00220     // Write size and start delimiter
00221     os << nl << L.size() << nl << token::BEGIN_LIST << nl;
00222 
00223     // Write contents
00224     for
00225     (
00226         typename StaticHashTable<T, Key, Hash>::const_iterator iter = L.begin();
00227         iter != L.end();
00228         ++iter
00229     )
00230     {
00231         os << iter.key() << token::SPACE << iter() << nl;
00232     }
00233 
00234     // Write end delimiter
00235     os << token::END_LIST;
00236 
00237     // Check state of IOstream
00238     os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
00239 
00240     return os;
00241 }
00242 
00243 
00244 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines