1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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"
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 template<class T, class Key, class Hash>
34 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
40 StaticHashTableName(),
44 endIter_(*this, keys_.size(), 0),
45 endConstIter_(*this, keys_.size(), 0)
51 "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
52 ) << "Illegal size " << size << " for StaticHashTable."
53 << " Minimum size is 1" << abort(FatalError);
56 operator>>(is, *this);
60 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
62 template<class T, class Key, class Hash>
64 Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
68 unsigned avgChain = 0;
70 // Find first non-empty entry
71 forAll(keys_, hashIdx)
73 const label count = keys_[hashIdx].size();
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;
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>&)");
105 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
107 token firstToken(is);
111 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
112 "reading first token"
115 if (firstToken.isLabel())
117 label s = firstToken.labelToken();
119 // Read beginning of contents
120 char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
124 if (2*s > L.keys_.size())
129 if (delimiter == token::BEGIN_LIST)
131 for (label i=0; i<s; i++)
135 L.insert(key, pTraits<T>(is));
139 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)"
148 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
150 ) << "incorrect first token, '(', found " << firstToken.info()
151 << exit(FatalIOError);
155 // Read end of contents
156 is.readEndList("StaticHashTable");
158 else if (firstToken.isPunctuation())
160 if (firstToken.pToken() != token::BEGIN_LIST)
164 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
166 ) << "incorrect first token, '(', found " << firstToken.info()
167 << exit(FatalIOError);
174 lastToken.isPunctuation()
175 && lastToken.pToken() == token::END_LIST
179 is.putBack(lastToken);
187 L.insert(key, element);
191 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
202 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
204 ) << "incorrect first token, expected <int> or '(', found "
206 << exit(FatalIOError);
209 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
215 template<class T, class Key, class Hash>
216 Foam::Ostream& Foam::operator<<
219 const StaticHashTable<T, Key, Hash>& L)
221 // Write size and start delimiter
222 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
227 typename StaticHashTable<T, Key, Hash>::const_iterator iter = L.begin();
232 os << iter.key() << token::SPACE << iter() << nl;
235 // Write end delimiter
236 os << token::END_LIST;
238 // Check state of IOstream
239 os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
245 // ************************************************************************* //