1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include "IOstreams.H"
29 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
31 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
33 template<class T, class Key, class Hash>
35 Foam::StaticHashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
37 // size is power of two - this is the modulus
38 return Hash()(key) & (keys_.size() - 1);
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 template<class T, class Key, class Hash>
45 inline Foam::label Foam::StaticHashTable<T, Key, Hash>::size() const
51 template<class T, class Key, class Hash>
52 inline bool Foam::StaticHashTable<T, Key, Hash>::empty() const
58 template<class T, class Key, class Hash>
59 inline bool Foam::StaticHashTable<T, Key, Hash>::insert
65 return set(key, newEntry, true);
69 template<class T, class Key, class Hash>
70 inline bool Foam::StaticHashTable<T, Key, Hash>::set
76 return set(key, newEntry, false);
80 template<class T, class Key, class Hash>
81 inline Foam::Xfer<Foam::StaticHashTable<T, Key, Hash> >
82 Foam::StaticHashTable<T, Key, Hash>::xfer()
84 return xferMove(*this);
88 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
90 template<class T, class Key, class Hash>
91 inline T& Foam::StaticHashTable<T, Key, Hash>::operator[](const Key& key)
93 iterator iter = find(key);
97 FatalErrorIn("StaticHashTable<T, Key, Hash>::operator[](const Key&)")
98 << key << " not found in table. Valid entries: "
107 template<class T, class Key, class Hash>
108 inline const T& Foam::StaticHashTable<T, Key, Hash>::operator[]
113 const_iterator iter = find(key);
119 "StaticHashTable<T, Key, Hash>::operator[](const Key&) const"
120 ) << key << " not found in table. Valid entries: "
129 template<class T, class Key, class Hash>
130 inline T& Foam::StaticHashTable<T, Key, Hash>::operator()(const Key& key)
132 iterator iter = find(key);
146 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
148 template<class T, class Key, class Hash>
149 template<class TRef, class TableRef>
150 inline Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::Iterator
158 hashIndex_(hashIndex),
159 elemIndex_(elemIndex)
163 template<class T, class Key, class Hash>
164 template<class TRef, class TableRef>
165 inline Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::Iterator
170 hashTable_(iter.hashTable_),
171 hashIndex_(iter.hashIndex_),
172 elemIndex_(iter.elemIndex_)
176 template<class T, class Key, class Hash>
177 template<class TRef, class TableRef>
179 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator=
184 this->hashIndex_ = iter.hashIndex_;
185 this->elemIndex_ = iter.elemIndex_;
189 template<class T, class Key, class Hash>
190 template<class TRef, class TableRef>
192 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
197 return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
201 template<class T, class Key, class Hash>
202 template<class TRef, class TableRef>
204 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator==
206 const const_iterator& iter
209 return hashIndex_ == iter.hashIndex_ && elemIndex_ == iter.elemIndex_;
213 template<class T, class Key, class Hash>
214 template<class TRef, class TableRef>
216 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
221 return !operator==(iter);
225 template<class T, class Key, class Hash>
226 template<class TRef, class TableRef>
228 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator!=
230 const const_iterator& iter
233 return !operator==(iter);
237 template<class T, class Key, class Hash>
238 template<class TRef, class TableRef>
240 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator*()
242 return hashTable_.objects_[hashIndex_][elemIndex_];
246 template<class T, class Key, class Hash>
247 template<class TRef, class TableRef>
249 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::operator()()
255 template<class T, class Key, class Hash>
256 template<class TRef, class TableRef>
258 typename Foam::StaticHashTable<T, Key, Hash>::template Iterator
263 Foam::StaticHashTable<T, Key, Hash>::Iterator
269 // A negative index is a special value from erase
270 // (see notes in HashTable)
273 hashIndex_ = -(hashIndex_+1) - 1;
277 // Try the next element on the local list
280 if (elemIndex_ < hashTable_.objects_[hashIndex_].size())
286 // Step to the next table entry
291 ++hashIndex_ < hashTable_.objects_.size()
292 && !hashTable_.objects_[hashIndex_].size()
297 if (hashIndex_ >= hashTable_.objects_.size())
300 hashIndex_ = hashTable_.keys_.size();
307 template<class T, class Key, class Hash>
308 template<class TRef, class TableRef>
310 typename Foam::StaticHashTable<T, Key, Hash>::template Iterator
315 Foam::StaticHashTable<T, Key, Hash>::Iterator
324 iterator tmp = *this;
330 template<class T, class Key, class Hash>
331 template<class TRef, class TableRef>
333 Foam::StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::key() const
335 return hashTable_.keys_[hashIndex_][elemIndex_];
339 template<class T, class Key, class Hash>
340 inline typename Foam::StaticHashTable<T, Key, Hash>::iterator
341 Foam::StaticHashTable<T, Key, Hash>::begin()
343 // Find first non-empty entry
344 forAll(keys_, hashIdx)
346 if (keys_[hashIdx].size())
348 return iterator(*this, hashIdx, 0);
355 Info<< "StaticHashTable is empty\n";
359 return StaticHashTable<T, Key, Hash>::endIter_;
363 template<class T, class Key, class Hash>
364 inline const typename Foam::StaticHashTable<T, Key, Hash>::iterator&
365 Foam::StaticHashTable<T, Key, Hash>::end()
367 return StaticHashTable<T, Key, Hash>::endIter_;
371 template<class T, class Key, class Hash>
372 inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
373 Foam::StaticHashTable<T, Key, Hash>::cbegin() const
375 // Find first non-empty entry
376 forAll(keys_, hashIdx)
378 if (keys_[hashIdx].size())
380 return const_iterator(*this, hashIdx, 0);
387 Info<< "StaticHashTable is empty\n";
391 return StaticHashTable<T, Key, Hash>::endConstIter_;
395 template<class T, class Key, class Hash>
396 inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
397 Foam::StaticHashTable<T, Key, Hash>::cend() const
399 return StaticHashTable<T, Key, Hash>::endConstIter_;
403 template<class T, class Key, class Hash>
404 inline typename Foam::StaticHashTable<T, Key, Hash>::const_iterator
405 Foam::StaticHashTable<T, Key, Hash>::begin() const
407 return this->cbegin();
411 template<class T, class Key, class Hash>
412 inline const typename Foam::StaticHashTable<T, Key, Hash>::const_iterator&
413 Foam::StaticHashTable<T, Key, Hash>::end() const
415 return StaticHashTable<T, Key, Hash>::endConstIter_;
419 // ************************************************************************* //