StaticHashTableIO.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 "StaticHashTable.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T, class Key, class Hash>
34 (
35  Istream& is,
36  const label size
37 )
38 :
40  keys_(StaticHashTableCore::canonicalSize(size)),
41  objects_(StaticHashTableCore::canonicalSize(size)),
42  nElmts_(0),
43  endIter_(*this, keys_.size(), 0),
44  endConstIter_(*this, keys_.size(), 0)
45 {
46  if (size < 1)
47  {
49  (
50  "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
51  ) << "Illegal size " << size << " for StaticHashTable."
52  << " Minimum size is 1" << abort(FatalError);
53  }
54 
55  operator>>(is, *this);
56 }
57 
58 
59 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
60 
61 template<class T, class Key, class Hash>
64 {
65  label used = 0;
66  label maxChain = 0;
67  unsigned avgChain = 0;
68 
69  // Find first non-empty entry
70  forAll(keys_, hashIdx)
71  {
72  const label count = keys_[hashIdx].size();
73  if (count)
74  {
75  ++used;
76  avgChain += count;
77 
78  if (maxChain < count)
79  {
80  maxChain = count;
81  }
82  }
83  }
84 
85  os << "StaticHashTable<T,Key,Hash>"
86  << " elements:" << size() << " slots:" << used << "/" << keys_.size()
87  << " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
88  << "/" << maxChain << endl;
89 
90  return os;
91 }
92 
93 
94 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
95 
96 template<class T, class Key, class Hash>
98 {
99  is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
100 
101  // Anull list
102  L.clear();
103 
104  is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
105 
106  token firstToken(is);
107 
108  is.fatalCheck
109  (
110  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
111  "reading first token"
112  );
113 
114  if (firstToken.isLabel())
115  {
116  label s = firstToken.labelToken();
117 
118  // Read beginning of contents
119  char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
120 
121  if (s)
122  {
123  if (2*s > L.keys_.size())
124  {
125  L.resize(2*s);
126  }
127 
128  if (delimiter == token::BEGIN_LIST)
129  {
130  for (label i=0; i<s; i++)
131  {
132  Key key;
133  is >> key;
134  L.insert(key, pTraits<T>(is));
135 
136  is.fatalCheck
137  (
138  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)"
139  " : reading entry"
140  );
141  }
142  }
143  else
144  {
146  (
147  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
148  is
149  ) << "incorrect first token, '(', found " << firstToken.info()
150  << exit(FatalIOError);
151  }
152  }
153 
154  // Read end of contents
155  is.readEndList("StaticHashTable");
156  }
157  else if (firstToken.isPunctuation())
158  {
159  if (firstToken.pToken() != token::BEGIN_LIST)
160  {
162  (
163  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
164  is
165  ) << "incorrect first token, '(', found " << firstToken.info()
166  << exit(FatalIOError);
167  }
168 
169  token lastToken(is);
170  while
171  (
172  !(
173  lastToken.isPunctuation()
174  && lastToken.pToken() == token::END_LIST
175  )
176  )
177  {
178  is.putBack(lastToken);
179 
180  Key key;
181  is >> key;
182 
183  T element;
184  is >> element;
185 
186  L.insert(key, element);
187 
188  is.fatalCheck
189  (
190  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&) : "
191  "reading entry"
192  );
193 
194  is >> lastToken;
195  }
196  }
197  else
198  {
200  (
201  "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
202  is
203  ) << "incorrect first token, expected <int> or '(', found "
204  << firstToken.info()
205  << exit(FatalIOError);
206  }
207 
208  is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
209 
210  return is;
211 }
212 
213 
214 template<class T, class Key, class Hash>
215 Foam::Ostream& Foam::operator<<
216 (
217  Ostream& os,
219 {
220  // Write size and start delimiter
221  os << nl << L.size() << nl << token::BEGIN_LIST << nl;
222 
223  // Write contents
224  for
225  (
227  iter != L.end();
228  ++iter
229  )
230  {
231  os << iter.key() << token::SPACE << iter() << nl;
232  }
233 
234  // Write end delimiter
235  os << token::END_LIST;
236 
237  // Check state of IOstream
238  os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
239 
240  return os;
241 }
242 
243 
244 // ************************************************************************* //
STL conforming hash table.
iterator begin()
Iterator set to the beginning of the StaticHashTable.
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 ))
Ostream & printInfo(Ostream &) const
Print information.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
char readBeginList(const char *funcName)
Definition: Istream.C:131
StaticHashTable(const label size=128)
Construct given initial table size.
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
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
label size() const
Return number of elements in table.
const volScalarField & T
Definition: createFields.H:25
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
char readEndList(const char *funcName)
Definition: Istream.C:152
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
#define forAll(list, i)
Definition: UList.H:421
const iterator & end()
Iterator set to beyond the end of the StaticHashTable.
A token holds items read from Istream.
Definition: token.H:67
void clear()
Clear all entries from table.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
label labelToken() const
Definition: tokenI.H:267
Template-invariant bits for StaticHashTable.
Traits class for primitives.
Definition: pTraits.H:50
error FatalError
void resize(const label newSize)
Resize the hash table for efficiency.
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325