initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / HashTables / StaticHashTable / StaticHashTableI.H
blob96653c1e6c1c8048b142148058a07bd5c8ffa876
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"
28 #include "IOstreams.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 namespace Foam
35 // * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * //
38 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 template<class T, class Key, class Hash>
41 inline label StaticHashTable<T, Key, Hash>::size() const
43     return nElmts_;
47 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
49 template<class T, class Key, class Hash>
50 inline T& StaticHashTable<T, Key, Hash>::operator[](const Key& key)
52     iterator iter = find(key);
54     if (iter == end())
55     {
56         FatalErrorIn("StaticHashTable<T, Key, Hash>::operator[](const Key&)")
57             << key << " not found in table.  Valid entries are "
58             << toc()
59             << exit(FatalError);
60     }
62     return *iter;
65 template<class T, class Key, class Hash>
66 inline const T& StaticHashTable<T, Key, Hash>::operator[](const Key& key) const
68     const_iterator iter = find(key);
70     if (iter == end())
71     {
72         FatalErrorIn
73         (
74             "StaticHashTable<T, Key, Hash>::operator[](const Key&) const"
75         )   << key << " not found in table.  Valid entries are "
76             << toc()
77             << exit(FatalError);
78     }
80     return *iter;
84 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
86 template<class T, class Key, class Hash>
87 template<class TRef, class TableRef>
88 inline StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
89 Iterator
91     TableRef curStaticHashTable,
92     label hashIndex,
93     label elementIndex
96     curStaticHashTable_(curStaticHashTable),
97     hashIndex_(hashIndex),
98     elementIndex_(elementIndex)
102 template<class T, class Key, class Hash>
103 template<class TRef, class TableRef>
104 inline StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
105 Iterator(const iterator& iter)
107     curStaticHashTable_(iter.curStaticHashTable_),
108     hashIndex_(iter.hashIndex_),
109     elementIndex_(iter.elementIndex_)
113 template<class T, class Key, class Hash>
114 template<class TRef, class TableRef>
115 inline void StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
116 operator=(const iterator& iter)
118     this->hashIndex_ = iter.hashIndex_;
119     this->elementIndex_ = iter.elementIndex_;
123 template<class T, class Key, class Hash>
124 template<class TRef, class TableRef>
125 inline bool StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
126 operator==(const iterator& iter) const
128     return hashIndex_ == iter.hashIndex_ && elementIndex_ == iter.elementIndex_;
131 template<class T, class Key, class Hash>
132 template<class TRef, class TableRef>
133 inline bool StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
134 operator==(const const_iterator& iter) const
136     return hashIndex_ == iter.hashIndex_ && elementIndex_ == iter.elementIndex_;
140 template<class T, class Key, class Hash>
141 template<class TRef, class TableRef>
142 inline bool StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
143 operator!=(const iterator& iter) const
145     return !operator==(iter);
148 template<class T, class Key, class Hash>
149 template<class TRef, class TableRef>
150 inline bool StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
151 operator!=(const const_iterator& iter) const
153     return !operator==(iter);
157 template<class T, class Key, class Hash>
158 template<class TRef, class TableRef>
159 inline TRef StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
160 operator*()
162     return curStaticHashTable_.objects_[hashIndex_][elementIndex_];
166 template<class T, class Key, class Hash>
167 template<class TRef, class TableRef>
168 inline TRef StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
169 operator()()
171     return operator*();
175 template<class T, class Key, class Hash>
176 template<class TRef, class TableRef>
177 inline
178 typename StaticHashTable<T, Key, Hash>::template Iterator
180     TRef,
181     TableRef
183 StaticHashTable<T, Key, Hash>::Iterator
185     TRef,
186     TableRef
187 >::operator++()
189     const List<T>& localObjects = curStaticHashTable_.objects_[hashIndex_];
191     if (elementIndex_ == localObjects.size()-1)
192     {
193         elementIndex_ = 0;
195         // Find first non-zero entry
196         for
197         (
198             hashIndex_++;
199             hashIndex_ < curStaticHashTable_.objects_.size();
200             hashIndex_++
201         )
202         {
203             if (curStaticHashTable_.objects_[hashIndex_].size() > 0)
204             {
205                 break;
206             }
207         }
208     }
209     else
210     {
211         elementIndex_++;
212     }
214     return *this;
218 template<class T, class Key, class Hash>
219 template<class TRef, class TableRef>
220 inline
221 typename StaticHashTable<T, Key, Hash>::template Iterator
223     TRef,
224     TableRef
226 StaticHashTable<T, Key, Hash>::Iterator
228     TRef,
229     TableRef
230 >::operator++
232     int
235     iterator tmp = *this;
236     ++*this;
237     return tmp;
241 template<class T, class Key, class Hash>
242 template<class TRef, class TableRef>
243 inline
244 const Key& StaticHashTable<T, Key, Hash>::Iterator<TRef, TableRef>::
245 key()
247     return curStaticHashTable_.keys_[hashIndex_][elementIndex_];
251 template<class T, class Key, class Hash>
252 inline typename StaticHashTable<T, Key, Hash>::iterator
253 StaticHashTable<T, Key, Hash>::begin()
255     // Find first non-empty entry
256     forAll(keys_, i)
257     {
258         if (keys_[i].size() > 0)
259         {
260             return iterator(*this, i, 0);
261         }
262     }
264 #   ifdef FULLDEBUG
265     if (debug)
266     {
267         Info<< "StaticHashTable is empty\n";
268     }
269 #   endif
271     return StaticHashTable<T, Key, Hash>::endIter_;
275 template<class T, class Key, class Hash>
276 inline const typename StaticHashTable<T, Key, Hash>::iterator&
277 StaticHashTable<T, Key, Hash>::end()
279     return StaticHashTable<T, Key, Hash>::endIter_;
283 template<class T, class Key, class Hash>
284 inline typename StaticHashTable<T, Key, Hash>::const_iterator
285 StaticHashTable<T, Key, Hash>::begin() const
287     // Find first non-empty entry
288     forAll(keys_, i)
289     {
290         if (keys_[i].size() > 0)
291         {
292             return const_iterator(*this, i, 0);
293         }
294     }
296 #   ifdef FULLDEBUG
297     if (debug)
298     {
299         Info<< "StaticHashTable is empty\n";
300     }
301 #   endif
303     return StaticHashTable<T, Key, Hash>::endConstIter_;
307 template<class T, class Key, class Hash>
308 inline const typename StaticHashTable<T, Key, Hash>::const_iterator&
309 StaticHashTable<T, Key, Hash>::end() const
311     return StaticHashTable<T, Key, Hash>::endConstIter_;
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 } // End namespace Foam
319 // ************************************************************************* //