initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / HashTables / StaticHashTable / StaticHashTableIO.C
blob3280ad738995f5af13e5aa31fd400be1593abbb2
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "StaticHashTable.H"
28 #include "Istream.H"
29 #include "Ostream.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 template<class T, class Key, class Hash>
34 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
36     Istream& is,
37     const label size
40     StaticHashTableName(),
41     keys_(size),
42     objects_(size),
43     nElmts_(0),
44     endIter_(*this, keys_.size(), 0),
45     endConstIter_(*this, keys_.size(), 0)
47     if (size < 1)
48     {
49         FatalErrorIn
50         (
51             "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
52         )   << "Illegal size " << size << " for StaticHashTable."
53             << " Minimum size is 1" << abort(FatalError);
54     }
56     operator>>(is, *this);
60 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
62 template<class T, class Key, class Hash>
63 Foam::Ostream&
64 Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
66     label used = 0;
67     label maxChain = 0;
68     unsigned avgChain = 0;
70     // Find first non-empty entry
71     forAll(keys_, hashIdx)
72     {
73         const label count = keys_[hashIdx].size();
74         if (count)
75         {
76             ++used;
77             avgChain += count;
79             if (maxChain < count)
80             {
81                 maxChain = count;
82             }
83         }
84     }
86     os  << "StaticHashTable<T,Key,Hash>"
87         << " elements:" << size() << " slots:" << used << "/" << keys_.size()
88         << " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
89         << "/" << maxChain << endl;
91     return os;
95 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
97 template<class T, class Key, class Hash>
98 Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
100     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
102     // Anull list
103     L.clear();
105     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
107     token firstToken(is);
109     is.fatalCheck
110     (
111         "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
112         "reading first token"
113     );
115     if (firstToken.isLabel())
116     {
117         label s = firstToken.labelToken();
119         // Read beginning of contents
120         char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
122         if (s)
123         {
124             if (2*s > L.keys_.size())
125             {
126                 L.resize(2*s);
127             }
129             if (delimiter == token::BEGIN_LIST)
130             {
131                 for (label i=0; i<s; i++)
132                 {
133                     Key key;
134                     is >> key;
135                     L.insert(key, pTraits<T>(is));
137                     is.fatalCheck
138                     (
139                         "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)"
140                         " : reading entry"
141                     );
142                 }
143             }
144             else
145             {
146                 FatalIOErrorIn
147                 (
148                     "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
149                     is
150                 )   << "incorrect first token, '(', found " << firstToken.info()
151                     << exit(FatalIOError);
152             }
153         }
155         // Read end of contents
156         is.readEndList("StaticHashTable");
157     }
158     else if (firstToken.isPunctuation())
159     {
160         if (firstToken.pToken() != token::BEGIN_LIST)
161         {
162             FatalIOErrorIn
163             (
164                 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
165                 is
166             )   << "incorrect first token, '(', found " << firstToken.info()
167                 << exit(FatalIOError);
168         }
170         token lastToken(is);
171         while
172         (
173            !(
174                 lastToken.isPunctuation()
175              && lastToken.pToken() == token::END_LIST
176             )
177         )
178         {
179             is.putBack(lastToken);
181             Key key;
182             is >> key;
184             T element;
185             is >> element;
187             L.insert(key, element);
189             is.fatalCheck
190             (
191                 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
192                 "reading entry"
193             );
195             is >> lastToken;
196         }
197     }
198     else
199     {
200         FatalIOErrorIn
201         (
202             "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
203             is
204         )   << "incorrect first token, expected <int> or '(', found "
205             << firstToken.info()
206             << exit(FatalIOError);
207     }
209     is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
211     return is;
215 template<class T, class Key, class Hash>
216 Foam::Ostream& Foam::operator<<
218     Ostream& os,
219     const StaticHashTable<T, Key, Hash>& L)
221     // Write size and start delimiter
222     os << nl << L.size() << nl << token::BEGIN_LIST << nl;
224     // Write contents
225     for
226     (
227         typename StaticHashTable<T, Key, Hash>::const_iterator iter = L.begin();
228         iter != L.end();
229         ++iter
230     )
231     {
232         os << iter.key() << token::SPACE << iter() << nl;
233     }
235     // Write end delimiter
236     os << token::END_LIST;
238     // Check state of IOstream
239     os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
241     return os;
245 // ************************************************************************* //