1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "HashTable.H"
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 template<class T, class Key, class Hash>
33 Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size)
37 tableSize_(HashTableCore::canonicalSize(size)),
42 table_ = new hashedEntry*[tableSize_];
44 for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
50 operator>>(is, *this);
54 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
56 template<class T, class Key, class Hash>
58 Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
62 unsigned avgChain = 0;
64 for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
67 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
84 os << "HashTable<T,Key,Hash>"
85 << " elements:" << size() << " slots:" << used << "/" << tableSize_
86 << " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
87 << "/" << maxChain << endl;
93 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
95 template<class T, class Key, class Hash>
96 Foam::Istream& Foam::operator>>
99 HashTable<T, Key, Hash>& L
102 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
107 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
109 token firstToken(is);
113 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
114 "reading first token"
117 if (firstToken.isLabel())
119 label s = firstToken.labelToken();
121 // Read beginning of contents
122 char delimiter = is.readBeginList("HashTable<T, Key, Hash>");
126 if (2*s > L.tableSize_)
131 if (delimiter == token::BEGIN_LIST)
133 for (label i=0; i<s; i++)
137 L.insert(key, pTraits<T>(is));
141 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
150 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
152 ) << "incorrect first token, '(', found " << firstToken.info()
153 << exit(FatalIOError);
157 // Read end of contents
158 is.readEndList("HashTable");
160 else if (firstToken.isPunctuation())
162 if (firstToken.pToken() != token::BEGIN_LIST)
166 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
168 ) << "incorrect first token, '(', found " << firstToken.info()
169 << exit(FatalIOError);
176 lastToken.isPunctuation()
177 && lastToken.pToken() == token::END_LIST
181 is.putBack(lastToken);
189 L.insert(key, element);
193 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
204 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
206 ) << "incorrect first token, expected <int> or '(', found "
208 << exit(FatalIOError);
211 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
217 template<class T, class Key, class Hash>
218 Foam::Ostream& Foam::operator<<
221 const HashTable<T, Key, Hash>& L
224 // Write size and start delimiter
225 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
230 typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin();
235 os << iter.key() << token::SPACE << iter() << nl;
238 // Write end delimiter
239 os << token::END_LIST;
241 // Check state of IOstream
242 os.check("Ostream& operator<<(Ostream&, const HashTable&)");
248 // ************************************************************************* //