ListHashTableI.H
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 "IOstreams.H"
27 
28 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
29 
30 template<class T, class Key, class Hash>
31 inline Foam::label
33 {
34  // size is power of two - this is the modulus
35  return Hash()(key) & (keys_.size() - 1);
36 }
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
41 template<class T, class Key, class Hash>
43 {
44  return nElmts_;
45 }
46 
47 
48 template<class T, class Key, class Hash>
50 {
51  return !nElmts_;
52 }
53 
54 
55 template<class T, class Key, class Hash>
57 (
58  const Key& key,
59  const T& newEntry
60 )
61 {
62  return set(key, newEntry, true);
63 }
64 
65 
66 template<class T, class Key, class Hash>
68 (
69  const Key& key,
70  const T& newEntry
71 )
72 {
73  return set(key, newEntry, false);
74 }
75 
76 
77 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
78 
79 template<class T, class Key, class Hash>
81 {
82  iterator iter = find(key);
83 
84  if (iter == end())
85  {
87  << toc()
88  << exit(FatalError);
89  }
90 
91  return *iter;
92 }
93 
94 
95 template<class T, class Key, class Hash>
97 (
98  const Key& key
99 ) const
100 {
101  const_iterator iter = find(key);
102 
103  if (iter == cend())
104  {
106  << toc()
107  << exit(FatalError);
108  }
109 
110  return *iter;
111 }
112 
113 
114 template<class T, class Key, class Hash>
116 {
117  iterator iter = find(key);
118 
119  if (iter == end())
120  {
121  insert(key, T());
122  return *find(key);
123  }
124  else
125  {
126  return *iter;
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
132 
133 template<class T, class Key, class Hash>
134 template<class TRef, class TableRef>
136 (
137  TableRef hashTbl,
138  label hashIndex,
139  label elemIndex
140 )
141 :
142  hashTable_(hashTbl),
143  hashIndex_(hashIndex),
144  elemIndex_(elemIndex)
145 {}
146 
147 
148 template<class T, class Key, class Hash>
149 template<class TRef, class TableRef>
151 (
152  const iterator& iter
153 )
154 :
155  hashTable_(iter.hashTable_),
156  hashIndex_(iter.hashIndex_),
157  elemIndex_(iter.elemIndex_)
158 {}
159 
160 
161 template<class T, class Key, class Hash>
162 template<class TRef, class TableRef>
163 inline void
164 Foam::ListHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator=
165 (
166  const iterator& iter
167 )
168 {
169  this->hashIndex_ = iter.hashIndex_;
170  this->elemIndex_ = iter.elemIndex_;
171 }
172 
173 
174 template<class T, class Key, class Hash>
175 template<class TRef, class TableRef>
176 inline bool
177 Foam::ListHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
178 (
179  const iterator& iter
180 ) const
181 {
182  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
183 }
184 
185 
186 template<class T, class Key, class Hash>
187 template<class TRef, class TableRef>
188 inline bool
189 Foam::ListHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
190 (
191  const const_iterator& iter
192 ) const
193 {
194  return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
195 }
196 
197 
198 template<class T, class Key, class Hash>
199 template<class TRef, class TableRef>
200 inline bool
201 Foam::ListHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
202 (
203  const iterator& iter
204 ) const
205 {
206  return !operator==(iter);
207 }
208 
209 
210 template<class T, class Key, class Hash>
211 template<class TRef, class TableRef>
212 inline bool
213 Foam::ListHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
214 (
215  const const_iterator& iter
216 ) const
217 {
218  return !operator==(iter);
219 }
220 
221 
222 template<class T, class Key, class Hash>
223 template<class TRef, class TableRef>
224 inline TRef
226 {
227  return hashTable_.objects_[hashIndex_][elemIndex_];
228 }
229 
230 
231 template<class T, class Key, class Hash>
232 template<class TRef, class TableRef>
233 inline TRef
235 {
236  return operator*();
237 }
238 
239 
240 template<class T, class Key, class Hash>
241 template<class TRef, class TableRef>
242 inline
244 <
245  TRef,
246  TableRef
247 >&
249 <
250  TRef,
251  TableRef
252 >::operator++()
253 {
254  // A negative index is a special value from erase
255  // (see notes in HashTable)
256  if (hashIndex_ < 0)
257  {
258  hashIndex_ = -(hashIndex_+1) - 1;
259  }
260  else
261  {
262  // Try the next element on the local list
263  elemIndex_++;
264 
265  if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
266  {
267  return *this;
268  }
269  }
270 
271  // Step to the next table entry
272  elemIndex_ = 0;
273 
274  while
275  (
276  ++hashIndex_ < hashTable_.objects_.size()
277  && !hashTable_.objects_[hashIndex_].size()
278  )
279  {}
280 
281 
282  if (hashIndex_ >= hashTable_.objects_.size())
283  {
284  // make end iterator
285  hashIndex_ = hashTable_.keys_.size();
286  }
287 
288  return *this;
289 }
290 
291 
292 template<class T, class Key, class Hash>
293 template<class TRef, class TableRef>
294 inline
296 <
297  TRef,
298  TableRef
299 >
301 <
302  TRef,
303  TableRef
304 >::operator++
305 (
306  int
307 )
308 {
309  iterator tmp = *this;
310  ++*this;
311  return tmp;
312 }
313 
314 
315 template<class T, class Key, class Hash>
316 template<class TRef, class TableRef>
317 inline const Key&
319 {
320  return hashTable_.keys_[hashIndex_][elemIndex_];
321 }
322 
323 
324 template<class T, class Key, class Hash>
327 {
328  // Find first non-empty entry
329  forAll(keys_, hashIdx)
330  {
331  if (keys_[hashIdx].size())
332  {
333  return iterator(*this, hashIdx, 0);
334  }
335  }
336 
337  #ifdef FULLDEBUG
338  if (debug)
339  {
340  Info<< "ListHashTable is empty\n";
341  }
342  #endif
343 
345 }
346 
347 
348 template<class T, class Key, class Hash>
349 inline const typename Foam::ListHashTable<T, Key, Hash>::iterator&
351 {
353 }
354 
355 
356 template<class T, class Key, class Hash>
359 {
360  // Find first non-empty entry
361  forAll(keys_, hashIdx)
362  {
363  if (keys_[hashIdx].size())
364  {
365  return const_iterator(*this, hashIdx, 0);
366  }
367  }
368 
369  #ifdef FULLDEBUG
370  if (debug)
371  {
372  Info<< "ListHashTable is empty\n";
373  }
374  #endif
375 
377 }
378 
379 
380 template<class T, class Key, class Hash>
383 {
385 }
386 
387 
388 template<class T, class Key, class Hash>
391 {
392  return this->cbegin();
393 }
394 
395 
396 template<class T, class Key, class Hash>
399 {
401 }
402 
403 
404 // ************************************************************************* //
T & operator[](const Key &)
Find and return an hashed entry.
tmp< fvMatrix< Type > > operator*(const volScalarField::Internal &, const fvMatrix< Type > &)
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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 empty() const
Return true if the hash table is empty.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
const iterator & end()
Iterator set to beyond the end of the ListHashTable.
iterator begin()
Iterator set to the beginning of the ListHashTable.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
label size() const
Return number of elements in table.
const volScalarField & T
STL conforming hash table using contiguous lists rather than linked lists.
Definition: ListHashTable.H:56
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
const const_iterator & cend() const
const_iterator set to beyond the end of the ListHashTable
messageStream Info
const_iterator cbegin() const
const_iterator set to the beginning of the ListHashTable
A class for managing temporary objects.
Definition: PtrList.H:53
T & operator()(const Key &)
Find and return an hashed entry, create it null if not present.