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-2016 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  {
98  << toc()
99  << exit(FatalError);
100  }
101 
102  return *iter;
103 }
104 
105 
106 template<class T, class Key, class Hash>
108 (
109  const Key& key
110 ) const
111 {
112  const_iterator iter = find(key);
113 
114  if (iter == cend())
115  {
117  << toc()
118  << exit(FatalError);
119  }
120 
121  return *iter;
122 }
123 
124 
125 template<class T, class Key, class Hash>
127 {
128  iterator iter = find(key);
129 
130  if (iter == end())
131  {
132  insert(key, T());
133  return *find(key);
134  }
135  else
136  {
137  return *iter;
138  }
139 }
140 
141 
142 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
143 
144 template<class T, class Key, class Hash>
145 template<class TRef, class TableRef>
147 (
148  TableRef hashTbl,
149  label hashIndex,
150  label elemIndex
151 )
152 :
153  hashTable_(hashTbl),
154  hashIndex_(hashIndex),
155  elemIndex_(elemIndex)
156 {}
157 
158 
159 template<class T, class Key, class Hash>
160 template<class TRef, class TableRef>
162 (
163  const iterator& iter
164 )
165 :
166  hashTable_(iter.hashTable_),
167  hashIndex_(iter.hashIndex_),
168  elemIndex_(iter.elemIndex_)
169 {}
170 
171 
172 template<class T, class Key, class Hash>
173 template<class TRef, class TableRef>
174 inline void
176 (
177  const iterator& iter
178 )
179 {
180  this->hashIndex_ = iter.hashIndex_;
181  this->elemIndex_ = iter.elemIndex_;
182 }
183 
184 
185 template<class T, class Key, class Hash>
186 template<class TRef, class TableRef>
187 inline bool
189 (
190  const iterator& iter
191 ) const
192 {
193  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
194 }
195 
196 
197 template<class T, class Key, class Hash>
198 template<class TRef, class TableRef>
199 inline bool
201 (
202  const const_iterator& iter
203 ) const
204 {
205  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
206 }
207 
208 
209 template<class T, class Key, class Hash>
210 template<class TRef, class TableRef>
211 inline bool
213 (
214  const iterator& iter
215 ) const
216 {
217  return !operator==(iter);
218 }
219 
220 
221 template<class T, class Key, class Hash>
222 template<class TRef, class TableRef>
223 inline bool
225 (
226  const const_iterator& iter
227 ) const
228 {
229  return !operator==(iter);
230 }
231 
232 
233 template<class T, class Key, class Hash>
234 template<class TRef, class TableRef>
235 inline TRef
237 {
238  return hashTable_.objects_[hashIndex_][elemIndex_];
239 }
240 
241 
242 template<class T, class Key, class Hash>
243 template<class TRef, class TableRef>
244 inline TRef
246 {
247  return operator*();
248 }
249 
250 
251 template<class T, class Key, class Hash>
252 template<class TRef, class TableRef>
253 inline
255 <
256  TRef,
257  TableRef
258 >&
260 <
261  TRef,
262  TableRef
263 >::operator++()
264 {
265  // A negative index is a special value from erase
266  // (see notes in HashTable)
267  if (hashIndex_ < 0)
268  {
269  hashIndex_ = -(hashIndex_+1) - 1;
270  }
271  else
272  {
273  // Try the next element on the local list
274  elemIndex_++;
275 
276  if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
277  {
278  return *this;
279  }
280  }
281 
282  // Step to the next table entry
283  elemIndex_ = 0;
284 
285  while
286  (
287  ++hashIndex_ < hashTable_.objects_.size()
288  && !hashTable_.objects_[hashIndex_].size()
289  )
290  {}
291 
292 
293  if (hashIndex_ >= hashTable_.objects_.size())
294  {
295  // make end iterator
296  hashIndex_ = hashTable_.keys_.size();
297  }
298 
299  return *this;
300 }
301 
302 
303 template<class T, class Key, class Hash>
304 template<class TRef, class TableRef>
305 inline
307 <
308  TRef,
309  TableRef
310 >
312 <
313  TRef,
314  TableRef
315 >::operator++
316 (
317  int
318 )
319 {
320  iterator tmp = *this;
321  ++*this;
322  return tmp;
323 }
324 
325 
326 template<class T, class Key, class Hash>
327 template<class TRef, class TableRef>
328 inline const Key&
330 {
331  return hashTable_.keys_[hashIndex_][elemIndex_];
332 }
333 
334 
335 template<class T, class Key, class Hash>
338 {
339  // Find first non-empty entry
340  forAll(keys_, hashIdx)
341  {
342  if (keys_[hashIdx].size())
343  {
344  return iterator(*this, hashIdx, 0);
345  }
346  }
347 
348  #ifdef FULLDEBUG
349  if (debug)
350  {
351  Info<< "StaticHashTable is empty\n";
352  }
353  #endif
354 
356 }
357 
358 
359 template<class T, class Key, class Hash>
362 {
364 }
365 
366 
367 template<class T, class Key, class Hash>
370 {
371  // Find first non-empty entry
372  forAll(keys_, hashIdx)
373  {
374  if (keys_[hashIdx].size())
375  {
376  return const_iterator(*this, hashIdx, 0);
377  }
378  }
379 
380  #ifdef FULLDEBUG
381  if (debug)
382  {
383  Info<< "StaticHashTable is empty\n";
384  }
385  #endif
386 
388 }
389 
390 
391 template<class T, class Key, class Hash>
394 {
396 }
397 
398 
399 template<class T, class Key, class Hash>
402 {
403  return this->cbegin();
404 }
405 
406 
407 template<class T, class Key, class Hash>
410 {
412 }
413 
414 
415 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
iterator begin()
Iterator set to the beginning of the StaticHashTable.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const_iterator cbegin() const
const_iterator set to the beginning of the StaticHashTable
Xfer< StaticHashTable< T, Key, Hash > > xfer()
Transfer contents to the Xfer container.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
tmp< fvMatrix< Type > > operator*(const volScalarField::Internal &, const fvMatrix< Type > &)
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
STL conforming hash table.
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
timeIndices insert(timeIndex, timeDirs[timeI].value())
const const_iterator & cend() const
const_iterator set to beyond the end of the StaticHashTable
const volScalarField & T
T & operator[](const Key &)
Find and return an hashed entry.
T & operator()(const Key &)
Find and return an hashed entry, create it null if not present.
bool empty() const
Return true if the hash table is empty.
messageStream Info
dimensionedScalar TRef("TRef", dimTemperature, laminarTransport)
label size() const
Return number of elements in table.
A class for managing temporary objects.
Definition: PtrList.H:53
const iterator & end()
Iterator set to beyond the end of the StaticHashTable.