HashTableIO.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "HashTable.H"
30#include "Istream.H"
31#include "Ostream.H"
32
33// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34
35template<class T, class Key, class Hash>
37:
39 size_(0),
40 capacity_(HashTableCore::canonicalSize(size)),
41 table_(nullptr)
42{
43 if (capacity_)
44 {
45 table_ = new node_type*[capacity_];
46
47 for (label i=0; i < capacity_; ++i)
48 {
49 table_[i] = nullptr;
50 }
51 }
52
53 operator>>(is, *this);
54}
55
56
57// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
58
59template<class T, class Key, class Hash>
61{
62 label used = 0;
63 label maxChain = 0;
64 unsigned avgChain = 0;
65
66 for (label i=0; i < capacity_; ++i)
67 {
68 label count = 0;
69 for (node_type* ep = table_[i]; ep; ep = ep->next_)
70 {
71 ++count;
72 }
73
74 if (count)
75 {
76 ++used;
77 avgChain += count;
78
79 if (maxChain < count)
80 {
81 maxChain = count;
82 }
83 }
84 }
85
86 os << "HashTable<T,Key,Hash>"
87 << " elements:" << size() << " slots:" << used << "/" << capacity_
88 << " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
89 << "/" << maxChain << endl;
90
91 return os;
92}
93
94
95template<class T, class Key, class Hash>
97(
98 Ostream& os,
99 const label shortLen
100) const
101{
102 // Similar to UList::writeList version except the following:
103 // - the keys can never be uniform
104 // - never write in binary
105
106 label i = this->size();
107
108 if
109 (
110 (i <= 1 || !shortLen)
111 || (i <= shortLen)
112 )
113 {
114 // Write size and start delimiter
115 os << i << token::BEGIN_LIST;
116
117 i = 0;
118 for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
119 {
120 if (i++) os << token::SPACE;
121 os << iter.key();
122 }
123
124 os << token::END_LIST; // End delimiter
125 }
126 else
127 {
128 // Write size and start delimiter
129 os << nl << i << nl << token::BEGIN_LIST << nl;
130
131 for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
132 {
133 os << iter.key() << nl;
134 }
135
136 os << token::END_LIST << nl; // End delimiter
137 }
138
140 return os;
141}
142
143
144template<class T, class Key, class Hash>
146(
147 Istream& is
148)
149{
150 HashTable<T, Key, Hash>& tbl = *this;
151
152 // Anull existing table
153 tbl.clear();
154
156
157 token tok(is);
158
159 is.fatalCheck
160 (
161 "operator>>(Istream&, HashTable&) : "
162 "reading first token"
163 );
164
165 if (tok.isLabel())
166 {
167 const label len = tok.labelToken();
168
169 // Read beginning of contents
170 const char delimiter = is.readBeginList("HashTable");
171
172 if (len)
173 {
174 if (delimiter != token::BEGIN_LIST)
175 {
177 << "incorrect first token, '(', found "
178 << tok.info() << nl
179 << exit(FatalIOError);
180 }
181
182 if (2*len > tbl.capacity())
183 {
184 tbl.resize(2*len);
185 }
186
187 for (label i=0; i<len; ++i)
188 {
189 Key key;
190
191 is >> key; // Read the key
192 T& val = tbl(key); // Insert nameless T() into table
193 is >> val; // Read directly into the table value
194
195 is.fatalCheck
196 (
197 "operator>>(Istream&, HashTable&) : "
198 "reading entry"
199 );
200 }
201 }
202
203 // Read end of contents
204 is.readEndList("HashTable");
205 }
206 else if (tok.isPunctuation(token::BEGIN_LIST))
207 {
208 is >> tok;
209 while (!tok.isPunctuation(token::END_LIST))
210 {
211 is.putBack(tok);
212
213 Key key;
214
215 is >> key; // Read the key
216 T& val = tbl(key); // Insert nameless T() into table
217 is >> val; // Read directly into the table value
218
219 is.fatalCheck
220 (
221 "operator>>(Istream&, HashTable&) : "
222 "reading entry"
223 );
224
225 is >> tok;
226 }
227 }
228 else
229 {
231 << "incorrect first token, expected <int> or '(', found "
232 << tok.info() << nl
233 << exit(FatalIOError);
234 }
235
237 return is;
238}
240
241template<class T, class Key, class Hash>
243(
244 Ostream& os
245) const
246{
247 const HashTable<T, Key, Hash>& tbl = *this;
248
249 const label len = tbl.size();
250
251 if (len)
252 {
253 // Size and start list delimiter
254 os << nl << len << nl << token::BEGIN_LIST << nl;
255
256 // Contents
257 for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
258 {
259 iter.print(os) << nl;
260 }
261
262 os << token::END_LIST; // End list delimiter
263 }
264 else
265 {
266 // Empty hash table
268 }
269
271 return os;
272}
273
274
275// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
276
277template<class T, class Key, class Hash>
278Foam::Istream& Foam::operator>>
279(
280 Istream& is,
282)
283{
284 return tbl.readTable(is);
285}
286
287
288template<class T, class Key, class Hash>
289Foam::Ostream& Foam::operator<<
290(
291 Ostream& os,
292 const HashTable<T, Key, Hash>& tbl
293)
294{
295 return tbl.writeTable(os);
296}
297
298
299// ************************************************************************* //
Forward iterator with const access.
Definition: HashTable.H:794
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Definition: HashTableIO.C:97
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
void resize(const label sz)
Resize the hash table for efficiency.
Definition: HashTable.C:601
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
std::conditional< std::is_same< zero::null, typenamestd::remove_cv< T >::type >::value, Detail::HashTableSingle< Key >, Detail::HashTablePair< Key, T > >::type node_type
Definition: HashTable.H:138
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
HashTable()
Default construct with default (128) table capacity.
Definition: HashTable.C:40
void clear()
Clear all entries from table.
Definition: HashTable.C:678
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
friend Istream & operator>>(Istream &, HashTable< T, Key, Hash > &tbl)
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
void printInfo() const
Print general information about the mesh.
Definition: ccmReader.C:522
A token holds an item read from Istream.
Definition: token.H:69
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ END_LIST
End list [isseparator].
Definition: token.H:156
@ SPACE
Space [isspace].
Definition: token.H:125
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Bits that are independent of HashTable template parameters.
Definition: HashTableCore.H:57