HashTableIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "HashTable.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T, class Key, class Hash>
34 :
35  HashTableCore(),
36  nElmts_(0),
37  tableSize_(HashTableCore::canonicalSize(size)),
38  table_(nullptr)
39 {
40  if (tableSize_)
41  {
42  table_ = new hashedEntry*[tableSize_];
43 
44  for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
45  {
46  table_[hashIdx] = 0;
47  }
48  }
49 
50  operator>>(is, *this);
51 }
52 
53 
54 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
55 
56 template<class T, class Key, class Hash>
59 {
60  label used = 0;
61  label maxChain = 0;
62  unsigned avgChain = 0;
63 
64  for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
65  {
66  label count = 0;
67  for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
68  {
69  ++count;
70  }
71 
72  if (count)
73  {
74  ++used;
75  avgChain += count;
76 
77  if (maxChain < count)
78  {
79  maxChain = count;
80  }
81  }
82  }
83 
84  os << "HashTable<T,Key,Hash>"
85  << " elements:" << size() << " slots:" << used << "/" << tableSize_
86  << " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
87  << "/" << maxChain << endl;
88 
89  return os;
90 }
91 
92 
93 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
94 
95 template<class T, class Key, class Hash>
97 {
98  os << ht;
99 }
100 
101 
102 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
103 
104 template<class T, class Key, class Hash>
105 Foam::Istream& Foam::operator>>
106 (
107  Istream& is,
109 )
110 {
111  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
112 
113  // Anull list
114  L.clear();
115 
116  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
117 
118  token firstToken(is);
119 
120  is.fatalCheck
121  (
122  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
123  "reading first token"
124  );
125 
126  if (firstToken.isLabel())
127  {
128  label s = firstToken.labelToken();
129 
130  // Read beginning of contents
131  char delimiter = is.readBeginList("HashTable<T, Key, Hash>");
132 
133  if (s)
134  {
135  if (2*s > L.tableSize_)
136  {
137  L.resize(2*s);
138  }
139 
140  if (delimiter == token::BEGIN_LIST)
141  {
142  for (label i=0; i<s; i++)
143  {
144  Key key;
145  is >> key;
146  L.insert(key, pTraits<T>(is));
147 
148  is.fatalCheck
149  (
150  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
151  "reading entry"
152  );
153  }
154  }
155  else
156  {
158  (
159  is
160  ) << "incorrect first token, '(', found " << firstToken.info()
161  << exit(FatalIOError);
162  }
163  }
164 
165  // Read end of contents
166  is.readEndList("HashTable");
167  }
168  else if (firstToken.isPunctuation())
169  {
170  if (firstToken.pToken() != token::BEGIN_LIST)
171  {
173  (
174  is
175  ) << "incorrect first token, '(', found " << firstToken.info()
176  << exit(FatalIOError);
177  }
178 
179  token lastToken(is);
180  while
181  (
182  !(
183  lastToken.isPunctuation()
184  && lastToken.pToken() == token::END_LIST
185  )
186  )
187  {
188  is.putBack(lastToken);
189 
190  Key key;
191  is >> key;
192 
193  T element;
194  is >> element;
195 
196  L.insert(key, element);
197 
198  is.fatalCheck
199  (
200  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
201  "reading entry"
202  );
203 
204  is >> lastToken;
205  }
206  }
207  else
208  {
210  (
211  is
212  ) << "incorrect first token, expected <int> or '(', found "
213  << firstToken.info()
214  << exit(FatalIOError);
215  }
216 
217  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
218 
219  return is;
220 }
221 
222 
223 template<class T, class Key, class Hash>
224 Foam::Ostream& Foam::operator<<
225 (
226  Ostream& os,
227  const HashTable<T, Key, Hash>& L
228 )
229 {
230  // Write size and start delimiter
231  os << nl << L.size() << nl << token::BEGIN_LIST << nl;
232 
233  // Write contents
234  for
235  (
236  typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin();
237  iter != L.cend();
238  ++iter
239  )
240  {
241  os << iter.key() << token::SPACE << iter() << nl;
242  }
243 
244  // Write end delimiter
245  os << token::END_LIST;
246 
247  // Check state of IOstream
248  os.check("Ostream& operator<<(Ostream&, const HashTable&)");
249 
250  return os;
251 }
252 
253 
254 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
punctuationToken pToken() const
Definition: tokenI.H:217
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
An STL-conforming const_iterator.
Definition: HashTable.H:481
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & printInfo(Ostream &) const
Print information.
Definition: HashTableIO.C:58
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:371
HashTable(const label size=128)
Construct given initial table size.
Definition: HashTable.C:36
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Traits class for primitives.
Definition: pTraits.H:50
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Istream & operator>>(Istream &, directionInfo &)
An STL-conforming hash table.
Definition: HashTable.H:61
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
const volScalarField & T
Template-invariant bits for HashTable.
Definition: HashTable.H:82
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
label labelToken() const
Definition: tokenI.H:276
bool isPunctuation() const
Definition: tokenI.H:212
IOerror FatalIOError