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-2019 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 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
145 
146 template<class T, class Key, class Hash>
147 Foam::Istream& Foam::operator>>
148 (
149  Istream& is,
151 )
152 {
153  // Anull existing table
154  tbl.clear();
155 
157 
158  token firstToken(is);
159 
160  is.fatalCheck
161  (
162  "operator>>(Istream&, HashTable&) : "
163  "reading first token"
164  );
165 
166  if (firstToken.isLabel())
167  {
168  const label len = firstToken.labelToken();
169 
170  // Read beginning of contents
171  const char delimiter = is.readBeginList("HashTable");
172 
173  if (len)
174  {
175  if (delimiter != token::BEGIN_LIST)
176  {
178  << "incorrect first token, '(', found " << firstToken.info()
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 (firstToken.isPunctuation())
207  {
208  if (firstToken.pToken() != token::BEGIN_LIST)
209  {
211  << "incorrect first token, '(', found " << firstToken.info()
212  << exit(FatalIOError);
213  }
214 
215  token lastToken(is);
216  while
217  (
218  !(
219  lastToken.isPunctuation()
220  && lastToken.pToken() == token::END_LIST
221  )
222  )
223  {
224  is.putBack(lastToken);
225 
226  Key key;
227 
228  is >> key; // Read the key
229  T& val = tbl(key); // Insert nameless T() into table
230  is >> val; // Read directly into the table value
231 
232  is.fatalCheck
233  (
234  "operator>>(Istream&, HashTable&) : "
235  "reading entry"
236  );
237 
238  is >> lastToken;
239  }
240  }
241  else
242  {
244  << "incorrect first token, expected <int> or '(', found "
245  << firstToken.info()
246  << exit(FatalIOError);
247  }
248 
249  is.fatalCheck(FUNCTION_NAME);
250  return is;
251 }
252 
253 
254 template<class T, class Key, class Hash>
255 Foam::Ostream& Foam::operator<<
256 (
257  Ostream& os,
258  const HashTable<T, Key, Hash>& tbl
259 )
260 {
261  const label len = tbl.size();
262 
263  if (len)
264  {
265  // Size and start list delimiter
266  os << nl << len << nl << token::BEGIN_LIST << nl;
267 
268  // Contents
269  for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
270  {
271  iter.print(os) << nl;
272  }
273 
274  os << token::END_LIST; // End list delimiter
275  }
276  else
277  {
278  // Empty hash table
279  os << len << token::BEGIN_LIST << token::END_LIST;
280  }
281 
282  os.check(FUNCTION_NAME);
283  return os;
284 }
285 
286 
287 // ************************************************************************* //
HashTable.H
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
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:350
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::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
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
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::nl
constexpr char nl
Definition: Ostream.H:385
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::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
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56