initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / HashTables / HashTable / HashTableI.H
blob5212d57f5168d289e5fcdc6b9fdedef1e9569632
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
29 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
31 template<class T, class Key, class Hash>
32 inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry
34     const Key& key,
35     hashedEntry* next,
36     const T& newEntry
39     key_(key),
40     next_(next),
41     obj_(newEntry)
45 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
47 template<class T, class Key, class Hash>
48 inline Foam::label
49 Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
51     // size is power of two - this is the modulus
52     return Hash()(key) & (tableSize_ - 1);
56 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
58 template<class T, class Key, class Hash>
59 inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
61     return nElmts_;
65 template<class T, class Key, class Hash>
66 inline bool Foam::HashTable<T, Key, Hash>::empty() const
68     return !nElmts_;
72 template<class T, class Key, class Hash>
73 inline bool Foam::HashTable<T, Key, Hash>::insert
75     const Key& key,
76     const T& newEntry
79     return set(key, newEntry, true);
83 template<class T, class Key, class Hash>
84 inline bool Foam::HashTable<T, Key, Hash>::set
86     const Key& key,
87     const T& newEntry
90     return set(key, newEntry, false);
94 template<class T, class Key, class Hash>
95 inline Foam::Xfer<Foam::HashTable<T, Key, Hash> >
96 Foam::HashTable<T, Key, Hash>::xfer()
98     return xferMove(*this);
102 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
104 template<class T, class Key, class Hash>
105 inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
107     iterator iter = find(key);
109     if (iter == end())
110     {
111         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
112             << key << " not found in table.  Valid entries: "
113             << toc()
114             << exit(FatalError);
115     }
117     return *iter;
121 template<class T, class Key, class Hash>
122 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
124     const_iterator iter = find(key);
126     if (iter == cend())
127     {
128         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
129             << key << " not found in table.  Valid entries: "
130             << toc()
131             << exit(FatalError);
132     }
134     return *iter;
138 template<class T, class Key, class Hash>
139 inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
141     iterator iter = find(key);
143     if (iter == end())
144     {
145         insert(key, T());
146         return *find(key);
147     }
148     else
149     {
150         return *iter;
151     }
155 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
157 template<class T, class Key, class Hash>
158 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
160     HashTable<T, Key, Hash>& hashTbl,
161     hashedEntry* elmt,
162     label hashIndex
165     hashTable_(hashTbl),
166     elmtPtr_(elmt),
167     hashIndex_(hashIndex)
171 template<class T, class Key, class Hash>
172 inline void Foam::HashTable<T, Key, Hash>::iterator::operator=
174     const iterator& iter
177     elmtPtr_ = iter.elmtPtr_;
178     hashIndex_ = iter.hashIndex_;
182 template<class T, class Key, class Hash>
183 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
185     const iterator& iter
186 ) const
188     return elmtPtr_ == iter.elmtPtr_;
192 template<class T, class Key, class Hash>
193 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
195     const iterator& iter
196 ) const
198     return elmtPtr_ != iter.elmtPtr_;
202 template<class T, class Key, class Hash>
203 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
205     const const_iterator& iter
206 ) const
208     return elmtPtr_ == iter.elmtPtr_;
212 template<class T, class Key, class Hash>
213 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
215     const const_iterator& iter
216 ) const
218     return elmtPtr_ != iter.elmtPtr_;
222 template<class T, class Key, class Hash>
223 inline T&
224 Foam::HashTable<T, Key, Hash>::iterator::operator*()
226     return elmtPtr_->obj_;
230 template<class T, class Key, class Hash>
231 inline T&
232 Foam::HashTable<T, Key, Hash>::iterator::operator()()
234     return elmtPtr_->obj_;
238 template<class T, class Key, class Hash>
239 inline const T&
240 Foam::HashTable<T, Key, Hash>::iterator::operator*() const
242     return elmtPtr_->obj_;
246 template<class T, class Key, class Hash>
247 inline const T&
248 Foam::HashTable<T, Key, Hash>::iterator::operator()() const
250     return elmtPtr_->obj_;
254 template<class T, class Key, class Hash>
255 inline
256 typename Foam::HashTable<T, Key, Hash>::iterator&
257 Foam::HashTable<T, Key, Hash>::iterator::operator++()
259     // Check for special value from erase. (sets hashIndex to -1)
260     if (hashIndex_ >= 0)
261     {
262         // Do we have additional elements on the SLList?
263         if (elmtPtr_ && elmtPtr_->next_)
264         {
265             elmtPtr_ = elmtPtr_->next_;
266             return *this;
267         }
268     }
270     // Step to the next table entry
271     while
272     (
273         ++hashIndex_ < hashTable_.tableSize_
274      && !(elmtPtr_ = hashTable_.table_[hashIndex_])
275     )
276     {}
278     if (hashIndex_ == hashTable_.tableSize_)
279     {
280         // make end iterator
281         elmtPtr_ = 0;
282         hashIndex_ = 0;
283     }
284     return *this;
288 template<class T, class Key, class Hash>
289 inline typename Foam::HashTable<T, Key, Hash>::iterator
290 Foam::HashTable<T, Key, Hash>::iterator::operator++
292     int
295     iterator tmp = *this;
296     ++*this;
297     return tmp;
301 template<class T, class Key, class Hash>
302 inline
303 const Key& Foam::HashTable<T, Key, Hash>::iterator::key() const
305     return elmtPtr_->key_;
309 template<class T, class Key, class Hash>
310 inline typename Foam::HashTable<T, Key, Hash>::iterator
311 Foam::HashTable<T, Key, Hash>::begin()
313     label i = 0;
315     if (nElmts_)
316     {
317         while (table_ && !table_[i] && ++i < tableSize_)
318         {}
319     }
320     else
321     {
322         i = tableSize_;
323     }
325     if (i == tableSize_)
326     {
327 #       ifdef FULLDEBUG
328         if (debug)
329         {
330             Info<< "HashTable is empty\n";
331         }
332 #       endif
334         return HashTable<T, Key, Hash>::endIter_;
335     }
336     else
337     {
338         return iterator(*this, table_[i], i);
339     }
343 template<class T, class Key, class Hash>
344 inline const typename Foam::HashTable<T, Key, Hash>::iterator&
345 Foam::HashTable<T, Key, Hash>::end()
347     return HashTable<T, Key, Hash>::endIter_;
351 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
353 template<class T, class Key, class Hash>
354 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
356     const HashTable<T, Key, Hash>& hashTbl,
357     const hashedEntry* elmt,
358     label hashIndex
361     hashTable_(hashTbl),
362     elmtPtr_(elmt),
363     hashIndex_(hashIndex)
367 template<class T, class Key, class Hash>
368 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
370     const iterator& iter
373     hashTable_(iter.hashTable_),
374     elmtPtr_(iter.elmtPtr_),
375     hashIndex_(iter.hashIndex_)
379 template<class T, class Key, class Hash>
380 inline void Foam::HashTable<T, Key, Hash>::const_iterator::operator=
382     const const_iterator& iter
385     elmtPtr_ = iter.elmtPtr_;
386     hashIndex_ = iter.hashIndex_;
390 template<class T, class Key, class Hash>
391 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
393     const const_iterator& iter
394 ) const
396     return elmtPtr_ == iter.elmtPtr_;
400 template<class T, class Key, class Hash>
401 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
403     const const_iterator& iter
404 ) const
406     return elmtPtr_ != iter.elmtPtr_;
410 template<class T, class Key, class Hash>
411 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
413     const iterator& iter
414 ) const
416     return elmtPtr_ == iter.elmtPtr_;
420 template<class T, class Key, class Hash>
421 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
423     const iterator& iter
424 ) const
426     return elmtPtr_ != iter.elmtPtr_;
430 template<class T, class Key, class Hash>
431 inline const T&
432 Foam::HashTable<T, Key, Hash>::const_iterator::operator*() const
434     return elmtPtr_->obj_;
437 template<class T, class Key, class Hash>
438 inline const T&
439 Foam::HashTable<T, Key, Hash>::const_iterator::operator()() const
441     return elmtPtr_->obj_;
445 template<class T, class Key, class Hash>
446 inline
447 typename Foam::HashTable<T, Key, Hash>::const_iterator&
448 Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
450     if
451     (
452         !(elmtPtr_ = elmtPtr_->next_)
453      && ++hashIndex_ < hashTable_.tableSize_
454      && !(elmtPtr_ = hashTable_.table_[hashIndex_])
455     )
456     {
457         while
458         (
459             ++hashIndex_ < hashTable_.tableSize_
460          && !(elmtPtr_ = hashTable_.table_[hashIndex_])
461         )
462         {}
463     }
465     return *this;
469 template<class T, class Key, class Hash>
470 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
471 Foam::HashTable<T, Key, Hash>::const_iterator::operator++
473     int
476     const_iterator tmp = *this;
477     ++*this;
478     return tmp;
482 template<class T, class Key, class Hash>
483 inline
484 const Key& Foam::HashTable<T, Key, Hash>::const_iterator::key() const
486     return elmtPtr_->key_;
490 template<class T, class Key, class Hash>
491 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
492 Foam::HashTable<T, Key, Hash>::cbegin() const
494     label i = 0;
496     if (nElmts_)
497     {
498         while (table_ && !table_[i] && ++i < tableSize_)
499         {}
500     }
501     else
502     {
503         i = tableSize_;
504     }
506     if (i == tableSize_)
507     {
508 #       ifdef FULLDEBUG
509         if (debug)
510         {
511             Info<< "HashTable is empty\n";
512         }
513 #       endif
515         return HashTable<T, Key, Hash>::endConstIter_;
516     }
517     else
518     {
519         return const_iterator(*this, table_[i], i);
520     }
524 template<class T, class Key, class Hash>
525 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
526 Foam::HashTable<T, Key, Hash>::cend() const
528     return HashTable<T, Key, Hash>::endConstIter_;
532 template<class T, class Key, class Hash>
533 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
534 Foam::HashTable<T, Key, Hash>::begin() const
536     return this->cbegin();
540 template<class T, class Key, class Hash>
541 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
542 Foam::HashTable<T, Key, Hash>::end() const
544     return HashTable<T, Key, Hash>::endConstIter_;
548 // ************************************************************************* //