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 "StaticHashTable.H"
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 template<class T, class Key, class Hash>
33 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
39 StaticHashTableCore(),
40 keys_(StaticHashTableCore::canonicalSize(size)),
41 objects_(StaticHashTableCore::canonicalSize(size)),
43 endIter_(*this, keys_.size(), 0),
44 endConstIter_(*this, keys_.size(), 0)
50 "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
51 ) << "Illegal size " << size << " for StaticHashTable."
52 << " Minimum size is 1" << abort(FatalError);
55 operator>>(is, *this);
59 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
61 template<class T, class Key, class Hash>
63 Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
67 unsigned avgChain = 0;
69 // Find first non-empty entry
70 forAll(keys_, hashIdx)
72 const label count = keys_[hashIdx].size();
85 os << "StaticHashTable<T,Key,Hash>"
86 << " elements:" << size() << " slots:" << used << "/" << keys_.size()
87 << " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
88 << "/" << maxChain << endl;
94 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
96 template<class T, class Key, class Hash>
97 Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
99 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
104 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
106 token firstToken(is);
110 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
111 "reading first token"
114 if (firstToken.isLabel())
116 label s = firstToken.labelToken();
118 // Read beginning of contents
119 char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
123 if (2*s > L.keys_.size())
128 if (delimiter == token::BEGIN_LIST)
130 for (label i=0; i<s; i++)
134 L.insert(key, pTraits<T>(is));
138 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)"
147 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
149 ) << "incorrect first token, '(', found " << firstToken.info()
150 << exit(FatalIOError);
154 // Read end of contents
155 is.readEndList("StaticHashTable");
157 else if (firstToken.isPunctuation())
159 if (firstToken.pToken() != token::BEGIN_LIST)
163 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
165 ) << "incorrect first token, '(', found " << firstToken.info()
166 << exit(FatalIOError);
173 lastToken.isPunctuation()
174 && lastToken.pToken() == token::END_LIST
178 is.putBack(lastToken);
186 L.insert(key, element);
190 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
201 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
203 ) << "incorrect first token, expected <int> or '(', found "
205 << exit(FatalIOError);
208 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
214 template<class T, class Key, class Hash>
215 Foam::Ostream& Foam::operator<<
218 const StaticHashTable<T, Key, Hash>& L)
220 // Write size and start delimiter
221 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
226 typename StaticHashTable<T, Key, Hash>::const_iterator iter = L.begin();
231 os << iter.key() << token::SPACE << iter() << nl;
234 // Write end delimiter
235 os << token::END_LIST;
237 // Check state of IOstream
238 os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
244 // ************************************************************************* //