StaticHashTableI.H
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 "error.H"
27 #include "IOstreams.H"
28 
29 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
30 
31 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 
33 template<class T, class Key, class Hash>
34 inline Foam::label
36 {
37  // size is power of two - this is the modulus
38  return Hash()(key) & (keys_.size() - 1);
39 }
40 
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
44 template<class T, class Key, class Hash>
46 {
47  return nElmts_;
48 }
49 
50 
51 template<class T, class Key, class Hash>
53 {
54  return !nElmts_;
55 }
56 
57 
58 template<class T, class Key, class Hash>
60 (
61  const Key& key,
62  const T& newEntry
63 )
64 {
65  return set(key, newEntry, true);
66 }
67 
68 
69 template<class T, class Key, class Hash>
71 (
72  const Key& key,
73  const T& newEntry
74 )
75 {
76  return set(key, newEntry, false);
77 }
78 
79 
80 template<class T, class Key, class Hash>
83 {
84  return xferMove(*this);
85 }
86 
87 
88 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
89 
90 template<class T, class Key, class Hash>
92 {
93  iterator iter = find(key);
94 
95  if (iter == end())
96  {
97  FatalErrorIn("StaticHashTable<T, Key, Hash>::operator[](const Key&)")
98  << key << " not found in table. Valid entries: "
99  << toc()
100  << exit(FatalError);
101  }
102 
103  return *iter;
104 }
105 
106 
107 template<class T, class Key, class Hash>
109 (
110  const Key& key
111 ) const
112 {
113  const_iterator iter = find(key);
114 
115  if (iter == cend())
116  {
118  (
119  "StaticHashTable<T, Key, Hash>::operator[](const Key&) const"
120  ) << key << " not found in table. Valid entries: "
121  << toc()
122  << exit(FatalError);
123  }
124 
125  return *iter;
126 }
127 
128 
129 template<class T, class Key, class Hash>
131 {
132  iterator iter = find(key);
133 
134  if (iter == end())
135  {
136  insert(key, T());
137  return *find(key);
138  }
139  else
140  {
141  return *iter;
142  }
143 }
144 
145 
146 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
147 
148 template<class T, class Key, class Hash>
149 template<class TRef, class TableRef>
151 (
152  TableRef hashTbl,
153  label hashIndex,
154  label elemIndex
155 )
156 :
157  hashTable_(hashTbl),
158  hashIndex_(hashIndex),
159  elemIndex_(elemIndex)
160 {}
161 
162 
163 template<class T, class Key, class Hash>
164 template<class TRef, class TableRef>
166 (
167  const iterator& iter
168 )
169 :
170  hashTable_(iter.hashTable_),
171  hashIndex_(iter.hashIndex_),
172  elemIndex_(iter.elemIndex_)
173 {}
174 
175 
176 template<class T, class Key, class Hash>
177 template<class TRef, class TableRef>
178 inline void
180 (
181  const iterator& iter
182 )
183 {
184  this->hashIndex_ = iter.hashIndex_;
185  this->elemIndex_ = iter.elemIndex_;
186 }
187 
188 
189 template<class T, class Key, class Hash>
190 template<class TRef, class TableRef>
191 inline bool
193 (
194  const iterator& iter
195 ) const
196 {
197  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
198 }
199 
200 
201 template<class T, class Key, class Hash>
202 template<class TRef, class TableRef>
203 inline bool
205 (
206  const const_iterator& iter
207 ) const
208 {
209  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
210 }
211 
212 
213 template<class T, class Key, class Hash>
214 template<class TRef, class TableRef>
215 inline bool
217 (
218  const iterator& iter
219 ) const
220 {
221  return !operator==(iter);
222 }
223 
224 
225 template<class T, class Key, class Hash>
226 template<class TRef, class TableRef>
227 inline bool
229 (
230  const const_iterator& iter
231 ) const
232 {
233  return !operator==(iter);
234 }
235 
236 
237 template<class T, class Key, class Hash>
238 template<class TRef, class TableRef>
239 inline TRef
241 {
242  return hashTable_.objects_[hashIndex_][elemIndex_];
243 }
244 
245 
246 template<class T, class Key, class Hash>
247 template<class TRef, class TableRef>
248 inline TRef
250 {
251  return operator*();
252 }
253 
254 
255 template<class T, class Key, class Hash>
256 template<class TRef, class TableRef>
257 inline
259 <
260  TRef,
261  TableRef
262 >&
264 <
265  TRef,
266  TableRef
267 >::operator++()
268 {
269  // A negative index is a special value from erase
270  // (see notes in HashTable)
271  if (hashIndex_ < 0)
272  {
273  hashIndex_ = -(hashIndex_+1) - 1;
274  }
275  else
276  {
277  // Try the next element on the local list
278  elemIndex_++;
279 
280  if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
281  {
282  return *this;
283  }
284  }
285 
286  // Step to the next table entry
287  elemIndex_ = 0;
288 
289  while
290  (
291  ++hashIndex_ < hashTable_.objects_.size()
292  && !hashTable_.objects_[hashIndex_].size()
293  )
294  {}
295 
296 
297  if (hashIndex_ >= hashTable_.objects_.size())
298  {
299  // make end iterator
300  hashIndex_ = hashTable_.keys_.size();
301  }
302 
303  return *this;
304 }
305 
306 
307 template<class T, class Key, class Hash>
308 template<class TRef, class TableRef>
309 inline
311 <
312  TRef,
313  TableRef
314 >
316 <
317  TRef,
318  TableRef
319 >::operator++
320 (
321  int
322 )
323 {
324  iterator tmp = *this;
325  ++*this;
326  return tmp;
327 }
328 
329 
330 template<class T, class Key, class Hash>
331 template<class TRef, class TableRef>
332 inline const Key&
334 {
335  return hashTable_.keys_[hashIndex_][elemIndex_];
336 }
337 
338 
339 template<class T, class Key, class Hash>
342 {
343  // Find first non-empty entry
344  forAll(keys_, hashIdx)
345  {
346  if (keys_[hashIdx].size())
347  {
348  return iterator(*this, hashIdx, 0);
349  }
350  }
351 
352 # ifdef FULLDEBUG
353  if (debug)
354  {
355  Info<< "StaticHashTable is empty\n";
356  }
357 # endif
358 
360 }
361 
362 
363 template<class T, class Key, class Hash>
366 {
368 }
369 
370 
371 template<class T, class Key, class Hash>
374 {
375  // Find first non-empty entry
376  forAll(keys_, hashIdx)
377  {
378  if (keys_[hashIdx].size())
379  {
380  return const_iterator(*this, hashIdx, 0);
381  }
382  }
383 
384 # ifdef FULLDEBUG
385  if (debug)
386  {
387  Info<< "StaticHashTable is empty\n";
388  }
389 # endif
390 
392 }
393 
394 
395 template<class T, class Key, class Hash>
398 {
400 }
401 
402 
403 template<class T, class Key, class Hash>
406 {
407  return this->cbegin();
408 }
409 
410 
411 template<class T, class Key, class Hash>
414 {
416 }
417 
418 
419 // ************************************************************************* //
tmp< fvMatrix< Type > > operator*(const DimensionedField< scalar, volMesh > &, const fvMatrix< Type > &)
STL conforming hash table.
iterator begin()
Iterator set to the beginning of the StaticHashTable.
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
bool empty() const
Return true if the hash table is empty.
Xfer< StaticHashTable< T, Key, Hash > > xfer()
Transfer contents to the Xfer container.
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
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.
messageStream Info
Xfer< T > xferMove(T &)
label size() const
Return number of elements in table.
timeIndices insert(timeIndex, timeDirs[timeI].value())
const volScalarField & T
Definition: createFields.H:25
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
#define forAll(list, i)
Definition: UList.H:421
const iterator & end()
Iterator set to beyond the end of the StaticHashTable.
T & operator()(const Key &)
Find and return an hashed entry, create it null if not present.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
T & operator[](const Key &)
Find and return an hashed entry.
error FatalError
dimensionedScalar TRef("TRef", dimTemperature, laminarTransport)
A class for managing temporary objects.
Definition: PtrList.H:118
const const_iterator & cend() const
const_iterator set to beyond the end of the StaticHashTable
const_iterator cbegin() const
const_iterator set to the beginning of the StaticHashTable