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 | Copyright (C) 2011 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_(NULL)
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 Operators * * * * * * * * * * * * //
94 
95 template<class T, class Key, class Hash>
96 Foam::Istream& Foam::operator>>
97 (
98  Istream& is,
100 )
101 {
102  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
103 
104  // Anull list
105  L.clear();
106 
107  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
108 
109  token firstToken(is);
110 
111  is.fatalCheck
112  (
113  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
114  "reading first token"
115  );
116 
117  if (firstToken.isLabel())
118  {
119  label s = firstToken.labelToken();
120 
121  // Read beginning of contents
122  char delimiter = is.readBeginList("HashTable<T, Key, Hash>");
123 
124  if (s)
125  {
126  if (2*s > L.tableSize_)
127  {
128  L.resize(2*s);
129  }
130 
131  if (delimiter == token::BEGIN_LIST)
132  {
133  for (label i=0; i<s; i++)
134  {
135  Key key;
136  is >> key;
137  L.insert(key, pTraits<T>(is));
138 
139  is.fatalCheck
140  (
141  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
142  "reading entry"
143  );
144  }
145  }
146  else
147  {
149  (
150  "operator>>(Istream&, HashTable<T, Key, Hash>&)",
151  is
152  ) << "incorrect first token, '(', found " << firstToken.info()
153  << exit(FatalIOError);
154  }
155  }
156 
157  // Read end of contents
158  is.readEndList("HashTable");
159  }
160  else if (firstToken.isPunctuation())
161  {
162  if (firstToken.pToken() != token::BEGIN_LIST)
163  {
165  (
166  "operator>>(Istream&, HashTable<T, Key, Hash>&)",
167  is
168  ) << "incorrect first token, '(', found " << firstToken.info()
169  << exit(FatalIOError);
170  }
171 
172  token lastToken(is);
173  while
174  (
175  !(
176  lastToken.isPunctuation()
177  && lastToken.pToken() == token::END_LIST
178  )
179  )
180  {
181  is.putBack(lastToken);
182 
183  Key key;
184  is >> key;
185 
186  T element;
187  is >> element;
188 
189  L.insert(key, element);
190 
191  is.fatalCheck
192  (
193  "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
194  "reading entry"
195  );
196 
197  is >> lastToken;
198  }
199  }
200  else
201  {
203  (
204  "operator>>(Istream&, HashTable<T, Key, Hash>&)",
205  is
206  ) << "incorrect first token, expected <int> or '(', found "
207  << firstToken.info()
208  << exit(FatalIOError);
209  }
210 
211  is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
212 
213  return is;
214 }
215 
216 
217 template<class T, class Key, class Hash>
218 Foam::Ostream& Foam::operator<<
219 (
220  Ostream& os,
221  const HashTable<T, Key, Hash>& L
222 )
223 {
224  // Write size and start delimiter
225  os << nl << L.size() << nl << token::BEGIN_LIST << nl;
226 
227  // Write contents
228  for
229  (
230  typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin();
231  iter != L.cend();
232  ++iter
233  )
234  {
235  os << iter.key() << token::SPACE << iter() << nl;
236  }
237 
238  // Write end delimiter
239  os << token::END_LIST;
240 
241  // Check state of IOstream
242  os.check("Ostream& operator<<(Ostream&, const HashTable&)");
243 
244  return os;
245 }
246 
247 
248 // ************************************************************************* //
Ostream & printInfo(Ostream &) const
Print information.
Definition: HashTableIO.C:58
bool isLabel() const
Definition: tokenI.H:262
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 ))
An STL-conforming const_iterator.
Definition: HashTable.H:470
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
An STL-conforming hash table.
Definition: HashTable.H:61
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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
bool isPunctuation() const
Definition: tokenI.H:203
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
HashTable(const label size=128)
Construct given initial table size.
Definition: HashTable.C:35
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
A token holds items read from Istream.
Definition: token.H:67
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
label labelToken() const
Definition: tokenI.H:267
Traits class for primitives.
Definition: pTraits.H:50
friend Istream & operator>>(Istream &, HashTable< T, Key, Hash > &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Template-invariant bits for HashTable.
Definition: HashTable.H:76
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325