initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / HashTables / HashTable / HashTableI.H
blob35156c95097559be822c3da27164561251d53dc9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 namespace Foam
34 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
36 template<class T, class Key, class Hash>
37 inline HashTable<T, Key, Hash>::hashedEntry::hashedEntry
39     const Key& key,
40     hashedEntry* next,
41     const T& newEntry
44     key_(key),
45     next_(next),
46     obj_(newEntry)
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 template<class T, class Key, class Hash>
53 inline label HashTable<T, Key, Hash>::size() const
55     return nElmts_;
59 template<class T, class Key, class Hash>
60 inline bool HashTable<T, Key, Hash>::insert(const Key& key, const T& newEntry)
62     return set(key, newEntry, true);
66 template<class T, class Key, class Hash>
67 inline bool HashTable<T, Key, Hash>::set(const Key& key, const T& newEntry)
69     return set(key, newEntry, false);
72 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
74 template<class T, class Key, class Hash>
75 inline T& HashTable<T, Key, Hash>::operator[](const Key& key)
77     iterator iter = find(key);
79     if (iter == end())
80     {
81         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
82             << key << " not found in table.  Valid entries are "
83             << toc()
84             << exit(FatalError);
85     }
87     return *iter;
90 template<class T, class Key, class Hash>
91 inline const T& HashTable<T, Key, Hash>::operator[](const Key& key) const
93     const_iterator iter = find(key);
95     if (iter == end())
96     {
97         FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
98             << key << " not found in table.  Valid entries are "
99             << toc()
100             << exit(FatalError);
101     }
103     return *iter;
107 template<class T, class Key, class Hash>
108 inline T& HashTable<T, Key, Hash>::operator()(const Key& key)
110     iterator iter = find(key);
112     if (iter == end())
113     {
114         insert(key, T());
115         return *find(key);
116     }
117     else
118     {
119         return *iter;
120     }
124 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
126 template<class T, class Key, class Hash>
127 inline HashTable<T, Key, Hash>::iterator::iterator
129     HashTable<T, Key, Hash>& curHashTable,
130     hashedEntry* elmt,
131     label hashIndex
134     curHashTable_(curHashTable),
135     elmtPtr_(elmt),
136     hashIndex_(hashIndex)
140 template<class T, class Key, class Hash>
141 inline void HashTable<T, Key, Hash>::iterator::operator=(const iterator& iter)
143     elmtPtr_ = iter.elmtPtr_;
144     hashIndex_ = iter.hashIndex_;
148 template<class T, class Key, class Hash>
149 inline bool HashTable<T, Key, Hash>::iterator::operator==
151     const iterator& iter
152 ) const
154     return elmtPtr_ == iter.elmtPtr_;
158 template<class T, class Key, class Hash>
159 inline bool HashTable<T, Key, Hash>::iterator::operator!=
161     const iterator& iter
162 ) const
164     return elmtPtr_ != iter.elmtPtr_;
168 template<class T, class Key, class Hash>
169 inline bool HashTable<T, Key, Hash>::iterator::operator==
171     const const_iterator& iter
172 ) const
174     return elmtPtr_ == iter.elmtPtr_;
178 template<class T, class Key, class Hash>
179 inline bool HashTable<T, Key, Hash>::iterator::operator!=
181     const const_iterator& iter
182 ) const
184     return elmtPtr_ != iter.elmtPtr_;
188 template<class T, class Key, class Hash>
189 inline T& HashTable<T, Key, Hash>::iterator::operator*()
191     return elmtPtr_->obj_;
195 template<class T, class Key, class Hash>
196 inline T& HashTable<T, Key, Hash>::iterator::operator()()
198     return operator*();
202 template<class T, class Key, class Hash>
203 inline
204 typename HashTable<T, Key, Hash>::iterator&
205 HashTable<T, Key, Hash>::iterator::operator++()
207     // Check for special value from erase. (sets hashIndex to -1)
208     if (hashIndex_ >= 0)
209     {
210         // Do we have additional elements on the singly linked list?
211         if (elmtPtr_ && elmtPtr_->next_)
212         {
213             elmtPtr_ = elmtPtr_->next_;
214             return *this;
215         }
216     }
218     // Step to the next table entry
219     while
220     (
221         ++hashIndex_ < curHashTable_.tableSize_
222      && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
223     )
224     {}
226     if (hashIndex_ == curHashTable_.tableSize_)
227     {
228         // make end iterator
229         hashIndex_ = 0;
230         elmtPtr_ = 0;
231     }
232     return *this;
236 template<class T, class Key, class Hash>
237 inline typename HashTable<T, Key, Hash>::iterator
238 HashTable<T, Key, Hash>::iterator::operator++
240     int
243     iterator tmp = *this;
244     ++*this;
245     return tmp;
249 template<class T, class Key, class Hash>
250 inline
251 const Key& HashTable<T, Key, Hash>::iterator::key()
253     return elmtPtr_->key_;
257 template<class T, class Key, class Hash>
258 inline typename HashTable<T, Key, Hash>::iterator
259 HashTable<T, Key, Hash>::begin()
261     label i = 0;
263     while (table_ && !table_[i] && ++i < tableSize_)
264     {}
266     if (i == tableSize_)
267     {
268 #       ifdef FULLDEBUG
269         if (debug)
270         {
271             Info<< "HashTable is empty\n";
272         }
273 #       endif
275         return HashTable<T, Key, Hash>::endIter_;
276     }
277     else
278     {
279         return iterator(*this, table_[i], i);
280     }
284 template<class T, class Key, class Hash>
285 inline const typename HashTable<T, Key, Hash>::iterator&
286 HashTable<T, Key, Hash>::end()
288     return HashTable<T, Key, Hash>::endIter_;
292 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
294 template<class T, class Key, class Hash>
295 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
297     const HashTable<T, Key, Hash>& curHashTable,
298     const hashedEntry* elmt,
299     label hashIndex
302     curHashTable_(curHashTable),
303     elmtPtr_(elmt),
304     hashIndex_(hashIndex)
308 template<class T, class Key, class Hash>
309 inline HashTable<T, Key, Hash>::const_iterator::const_iterator
311     const iterator& iter
314     curHashTable_(iter.curHashTable_),
315     elmtPtr_(iter.elmtPtr_),
316     hashIndex_(iter.hashIndex_)
320 template<class T, class Key, class Hash>
321 inline void HashTable<T, Key, Hash>::const_iterator::operator=
323     const const_iterator& iter
326     elmtPtr_ = iter.elmtPtr_;
327     hashIndex_ = iter.hashIndex_;
331 template<class T, class Key, class Hash>
332 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
334     const const_iterator& iter
335 ) const
337     return elmtPtr_ == iter.elmtPtr_;
341 template<class T, class Key, class Hash>
342 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
344     const const_iterator& iter
345 ) const
347     return elmtPtr_ != iter.elmtPtr_;
351 template<class T, class Key, class Hash>
352 inline bool HashTable<T, Key, Hash>::const_iterator::operator==
354     const iterator& iter
355 ) const
357     return elmtPtr_ == iter.elmtPtr_;
361 template<class T, class Key, class Hash>
362 inline bool HashTable<T, Key, Hash>::const_iterator::operator!=
364     const iterator& iter
365 ) const
367     return elmtPtr_ != iter.elmtPtr_;
371 template<class T, class Key, class Hash>
372 inline const T& HashTable<T, Key, Hash>::const_iterator::operator*()
374     return elmtPtr_->obj_;
377 #ifndef __CINT__
378 template<class T, class Key, class Hash>
379 inline const T& HashTable<T, Key, Hash>::const_iterator::operator()()
381     return operator*();
383 #endif
385 template<class T, class Key, class Hash>
386 inline
387 typename HashTable<T, Key, Hash>::const_iterator&
388 HashTable<T, Key, Hash>::const_iterator::operator++()
390     if
391     (
392         !(elmtPtr_ = elmtPtr_->next_)
393      && ++hashIndex_ < curHashTable_.tableSize_
394      && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
395     )
396     {
397         while
398         (
399             ++hashIndex_ < curHashTable_.tableSize_
400          && !(elmtPtr_ = curHashTable_.table_[hashIndex_])
401         )
402         {}
403     }
405     return *this;
409 template<class T, class Key, class Hash>
410 inline typename HashTable<T, Key, Hash>::const_iterator
411 HashTable<T, Key, Hash>::const_iterator::operator++
413     int
416     const_iterator tmp = *this;
417     ++*this;
418     return tmp;
422 template<class T, class Key, class Hash>
423 inline
424 const Key& HashTable<T, Key, Hash>::const_iterator::key()
426     return elmtPtr_->key_;
430 template<class T, class Key, class Hash>
431 inline typename HashTable<T, Key, Hash>::const_iterator
432 HashTable<T, Key, Hash>::begin() const
434     label i = 0;
436     while (table_ && !table_[i] && ++i < tableSize_)
437     {}
439     if (i == tableSize_)
440     {
441 #       ifdef FULLDEBUG
442         if (debug)
443         {
444             Info<< "HashTable is empty\n";
445         }
446 #       endif
448         return HashTable<T, Key, Hash>::endConstIter_;
449     }
450     else
451     {
452         return const_iterator(*this, table_[i], i);
453     }
457 template<class T, class Key, class Hash>
458 inline const typename HashTable<T, Key, Hash>::const_iterator&
459 HashTable<T, Key, Hash>::end() const
461     return HashTable<T, Key, Hash>::endConstIter_;
465 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 } // End namespace Foam
469 // ************************************************************************* //