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 -------------------------------------------------------------------------------
11 License
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 
35 template<class T, class Key, class Hash>
37 :
38  HashTableCore(),
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 
59 template<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 
95 template<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 
139  os.check(FUNCTION_NAME);
140  return os;
141 }
142 
143 
144 template<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 }
239 
240 
241 template<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
267  os << len << token::BEGIN_LIST << token::END_LIST;
268  }
269 
270  os.check(FUNCTION_NAME);
271  return os;
272 }
273 
274 
275 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
276 
277 template<class T, class Key, class Hash>
278 Foam::Istream& Foam::operator>>
279 (
280  Istream& is,
281  HashTable<T, Key, Hash>& tbl
282 )
283 {
284  return tbl.readTable(is);
285 }
286 
287 
288 template<class T, class Key, class Hash>
289 Foam::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 // ************************************************************************* //
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
HashTable.H
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::glTF::key
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
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::HashTable::resize
void resize(const label sz)
Resize the hash table for efficiency.
Definition: HashTable.C:553
Foam::HashTable::HashTable
HashTable()
Default construct with default (128) table capacity.
Definition: HashTable.C:39
Istream.H
Foam::HashTableCore
Bits that are independent of HashTable template parameters.
Definition: HashTableCore.H:56
os
OBJstream os(runTime.globalPath()/outputName)
T
const volScalarField & T
Definition: createFieldRefs.H:2
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::HashTable::writeKeys
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Definition: HashTableIO.C:97
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Ostream.H
Foam::HashTable::capacity
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::HashTable::printInfo
Ostream & printInfo(Ostream &os) const
Print information.
Definition: HashTableIO.C:60
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:113
Foam::HashTable::clear
void clear()
Clear all entries from table.
Definition: HashTable.C:630
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::HashSetOps::used
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56