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 "HashTable.H"
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 template<class T, class Key, class Hash>
34 Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size)
38 tableSize_(canonicalSize(size)),
39 table_(new hashedEntry*[tableSize_]),
40 endIter_(*this, NULL, 0),
41 endConstIter_(*this, NULL, 0)
43 for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
48 operator>>(is, *this);
52 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
54 template<class T, class Key, class Hash>
56 Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
60 unsigned avgChain = 0;
62 for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
65 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
82 os << "HashTable<T,Key,Hash>"
83 << " elements:" << size() << " slots:" << used << "/" << tableSize_
84 << " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
85 << "/" << maxChain << endl;
91 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
93 template<class T, class Key, class Hash>
94 Foam::Istream& Foam::operator>>
97 HashTable<T, Key, Hash>& L
100 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
105 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
107 token firstToken(is);
111 "operator>>(Istream&, HashTable<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("HashTable<T, Key, Hash>");
124 if (2*s > L.tableSize_)
129 if (delimiter == token::BEGIN_LIST)
131 for (label i=0; i<s; i++)
135 L.insert(key, pTraits<T>(is));
139 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
148 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
150 ) << "incorrect first token, '(', found " << firstToken.info()
151 << exit(FatalIOError);
155 // Read end of contents
156 is.readEndList("HashTable");
158 else if (firstToken.isPunctuation())
160 if (firstToken.pToken() != token::BEGIN_LIST)
164 "operator>>(Istream&, HashTable<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&, HashTable<T, Key, Hash>&) : "
202 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
204 ) << "incorrect first token, expected <int> or '(', found "
206 << exit(FatalIOError);
209 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
215 template<class T, class Key, class Hash>
216 Foam::Ostream& Foam::operator<<
219 const HashTable<T, Key, Hash>& L
222 // Write size and start delimiter
223 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
228 typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin();
233 os << iter.key() << token::SPACE << iter() << nl;
236 // Write end delimiter
237 os << token::END_LIST;
239 // Check state of IOstream
240 os.check("Ostream& operator<<(Ostream&, const HashTable&)");
246 // ************************************************************************* //