00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "error.H"
00028
00029
00030
00031 template<class T, class Key, class Hash>
00032 inline Foam::HashTable<T, Key, Hash>::hashedEntry::hashedEntry
00033 (
00034 const Key& key,
00035 hashedEntry* next,
00036 const T& newEntry
00037 )
00038 :
00039 key_(key),
00040 next_(next),
00041 obj_(newEntry)
00042 {}
00043
00044
00045
00046
00047 template<class T, class Key, class Hash>
00048 inline Foam::label
00049 Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
00050 {
00051
00052 return Hash()(key) & (tableSize_ - 1);
00053 }
00054
00055
00056
00057
00058 template<class T, class Key, class Hash>
00059 inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
00060 {
00061 return nElmts_;
00062 }
00063
00064
00065 template<class T, class Key, class Hash>
00066 inline bool Foam::HashTable<T, Key, Hash>::empty() const
00067 {
00068 return !nElmts_;
00069 }
00070
00071
00072 template<class T, class Key, class Hash>
00073 inline bool Foam::HashTable<T, Key, Hash>::insert
00074 (
00075 const Key& key,
00076 const T& newEntry
00077 )
00078 {
00079 return set(key, newEntry, true);
00080 }
00081
00082
00083 template<class T, class Key, class Hash>
00084 inline bool Foam::HashTable<T, Key, Hash>::set
00085 (
00086 const Key& key,
00087 const T& newEntry
00088 )
00089 {
00090 return set(key, newEntry, false);
00091 }
00092
00093
00094 template<class T, class Key, class Hash>
00095 inline Foam::Xfer<Foam::HashTable<T, Key, Hash> >
00096 Foam::HashTable<T, Key, Hash>::xfer()
00097 {
00098 return xferMove(*this);
00099 }
00100
00101
00102
00103
00104 template<class T, class Key, class Hash>
00105 inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
00106 {
00107 iterator iter = find(key);
00108
00109 if (iter == end())
00110 {
00111 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&)")
00112 << key << " not found in table. Valid entries: "
00113 << toc()
00114 << exit(FatalError);
00115 }
00116
00117 return *iter;
00118 }
00119
00120
00121 template<class T, class Key, class Hash>
00122 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
00123 {
00124 const_iterator iter = find(key);
00125
00126 if (iter == cend())
00127 {
00128 FatalErrorIn("HashTable<T, Key, Hash>::operator[](const Key&) const")
00129 << key << " not found in table. Valid entries: "
00130 << toc()
00131 << exit(FatalError);
00132 }
00133
00134 return *iter;
00135 }
00136
00137
00138 template<class T, class Key, class Hash>
00139 inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
00140 {
00141 iterator iter = find(key);
00142
00143 if (iter == end())
00144 {
00145 insert(key, T());
00146 return *find(key);
00147 }
00148 else
00149 {
00150 return *iter;
00151 }
00152 }
00153
00154
00155
00156
00157 template<class T, class Key, class Hash>
00158 inline Foam::HashTable<T, Key, Hash>::iterator::iterator
00159 (
00160 HashTable<T, Key, Hash>& hashTbl,
00161 hashedEntry* elmt,
00162 label hashIndex
00163 )
00164 :
00165 hashTable_(hashTbl),
00166 elmtPtr_(elmt),
00167 hashIndex_(hashIndex)
00168 {}
00169
00170
00171 template<class T, class Key, class Hash>
00172 inline void Foam::HashTable<T, Key, Hash>::iterator::operator=
00173 (
00174 const iterator& iter
00175 )
00176 {
00177 elmtPtr_ = iter.elmtPtr_;
00178 hashIndex_ = iter.hashIndex_;
00179 }
00180
00181
00182 template<class T, class Key, class Hash>
00183 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
00184 (
00185 const iterator& iter
00186 ) const
00187 {
00188 return elmtPtr_ == iter.elmtPtr_;
00189 }
00190
00191
00192 template<class T, class Key, class Hash>
00193 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
00194 (
00195 const iterator& iter
00196 ) const
00197 {
00198 return elmtPtr_ != iter.elmtPtr_;
00199 }
00200
00201
00202 template<class T, class Key, class Hash>
00203 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator==
00204 (
00205 const const_iterator& iter
00206 ) const
00207 {
00208 return elmtPtr_ == iter.elmtPtr_;
00209 }
00210
00211
00212 template<class T, class Key, class Hash>
00213 inline bool Foam::HashTable<T, Key, Hash>::iterator::operator!=
00214 (
00215 const const_iterator& iter
00216 ) const
00217 {
00218 return elmtPtr_ != iter.elmtPtr_;
00219 }
00220
00221
00222 template<class T, class Key, class Hash>
00223 inline T&
00224 Foam::HashTable<T, Key, Hash>::iterator::operator*()
00225 {
00226 return elmtPtr_->obj_;
00227 }
00228
00229
00230 template<class T, class Key, class Hash>
00231 inline T&
00232 Foam::HashTable<T, Key, Hash>::iterator::operator()()
00233 {
00234 return elmtPtr_->obj_;
00235 }
00236
00237
00238 template<class T, class Key, class Hash>
00239 inline const T&
00240 Foam::HashTable<T, Key, Hash>::iterator::operator*() const
00241 {
00242 return elmtPtr_->obj_;
00243 }
00244
00245
00246 template<class T, class Key, class Hash>
00247 inline const T&
00248 Foam::HashTable<T, Key, Hash>::iterator::operator()() const
00249 {
00250 return elmtPtr_->obj_;
00251 }
00252
00253
00254 template<class T, class Key, class Hash>
00255 inline
00256 typename Foam::HashTable<T, Key, Hash>::iterator&
00257 Foam::HashTable<T, Key, Hash>::iterator::operator++()
00258 {
00259
00260 if (hashIndex_ >= 0)
00261 {
00262
00263 if (elmtPtr_ && elmtPtr_->next_)
00264 {
00265 elmtPtr_ = elmtPtr_->next_;
00266 return *this;
00267 }
00268 }
00269
00270
00271 while
00272 (
00273 ++hashIndex_ < hashTable_.tableSize_
00274 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
00275 )
00276 {}
00277
00278 if (hashIndex_ == hashTable_.tableSize_)
00279 {
00280
00281 elmtPtr_ = 0;
00282 hashIndex_ = 0;
00283 }
00284 return *this;
00285 }
00286
00287
00288 template<class T, class Key, class Hash>
00289 inline typename Foam::HashTable<T, Key, Hash>::iterator
00290 Foam::HashTable<T, Key, Hash>::iterator::operator++
00291 (
00292 int
00293 )
00294 {
00295 iterator tmp = *this;
00296 ++*this;
00297 return tmp;
00298 }
00299
00300
00301 template<class T, class Key, class Hash>
00302 inline
00303 const Key& Foam::HashTable<T, Key, Hash>::iterator::key() const
00304 {
00305 return elmtPtr_->key_;
00306 }
00307
00308
00309 template<class T, class Key, class Hash>
00310 inline typename Foam::HashTable<T, Key, Hash>::iterator
00311 Foam::HashTable<T, Key, Hash>::begin()
00312 {
00313 label i = 0;
00314
00315 if (nElmts_)
00316 {
00317 while (table_ && !table_[i] && ++i < tableSize_)
00318 {}
00319 }
00320 else
00321 {
00322 i = tableSize_;
00323 }
00324
00325 if (i == tableSize_)
00326 {
00327 # ifdef FULLDEBUG
00328 if (debug)
00329 {
00330 Info<< "HashTable is empty\n";
00331 }
00332 # endif
00333
00334 return HashTable<T, Key, Hash>::endIter_;
00335 }
00336 else
00337 {
00338 return iterator(*this, table_[i], i);
00339 }
00340 }
00341
00342
00343 template<class T, class Key, class Hash>
00344 inline const typename Foam::HashTable<T, Key, Hash>::iterator&
00345 Foam::HashTable<T, Key, Hash>::end()
00346 {
00347 return HashTable<T, Key, Hash>::endIter_;
00348 }
00349
00350
00351
00352
00353 template<class T, class Key, class Hash>
00354 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
00355 (
00356 const HashTable<T, Key, Hash>& hashTbl,
00357 const hashedEntry* elmt,
00358 label hashIndex
00359 )
00360 :
00361 hashTable_(hashTbl),
00362 elmtPtr_(elmt),
00363 hashIndex_(hashIndex)
00364 {}
00365
00366
00367 template<class T, class Key, class Hash>
00368 inline Foam::HashTable<T, Key, Hash>::const_iterator::const_iterator
00369 (
00370 const iterator& iter
00371 )
00372 :
00373 hashTable_(iter.hashTable_),
00374 elmtPtr_(iter.elmtPtr_),
00375 hashIndex_(iter.hashIndex_)
00376 {}
00377
00378
00379 template<class T, class Key, class Hash>
00380 inline void Foam::HashTable<T, Key, Hash>::const_iterator::operator=
00381 (
00382 const const_iterator& iter
00383 )
00384 {
00385 elmtPtr_ = iter.elmtPtr_;
00386 hashIndex_ = iter.hashIndex_;
00387 }
00388
00389
00390 template<class T, class Key, class Hash>
00391 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
00392 (
00393 const const_iterator& iter
00394 ) const
00395 {
00396 return elmtPtr_ == iter.elmtPtr_;
00397 }
00398
00399
00400 template<class T, class Key, class Hash>
00401 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
00402 (
00403 const const_iterator& iter
00404 ) const
00405 {
00406 return elmtPtr_ != iter.elmtPtr_;
00407 }
00408
00409
00410 template<class T, class Key, class Hash>
00411 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator==
00412 (
00413 const iterator& iter
00414 ) const
00415 {
00416 return elmtPtr_ == iter.elmtPtr_;
00417 }
00418
00419
00420 template<class T, class Key, class Hash>
00421 inline bool Foam::HashTable<T, Key, Hash>::const_iterator::operator!=
00422 (
00423 const iterator& iter
00424 ) const
00425 {
00426 return elmtPtr_ != iter.elmtPtr_;
00427 }
00428
00429
00430 template<class T, class Key, class Hash>
00431 inline const T&
00432 Foam::HashTable<T, Key, Hash>::const_iterator::operator*() const
00433 {
00434 return elmtPtr_->obj_;
00435 }
00436
00437 template<class T, class Key, class Hash>
00438 inline const T&
00439 Foam::HashTable<T, Key, Hash>::const_iterator::operator()() const
00440 {
00441 return elmtPtr_->obj_;
00442 }
00443
00444
00445 template<class T, class Key, class Hash>
00446 inline
00447 typename Foam::HashTable<T, Key, Hash>::const_iterator&
00448 Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
00449 {
00450 if
00451 (
00452 !(elmtPtr_ = elmtPtr_->next_)
00453 && ++hashIndex_ < hashTable_.tableSize_
00454 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
00455 )
00456 {
00457 while
00458 (
00459 ++hashIndex_ < hashTable_.tableSize_
00460 && !(elmtPtr_ = hashTable_.table_[hashIndex_])
00461 )
00462 {}
00463 }
00464
00465 return *this;
00466 }
00467
00468
00469 template<class T, class Key, class Hash>
00470 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
00471 Foam::HashTable<T, Key, Hash>::const_iterator::operator++
00472 (
00473 int
00474 )
00475 {
00476 const_iterator tmp = *this;
00477 ++*this;
00478 return tmp;
00479 }
00480
00481
00482 template<class T, class Key, class Hash>
00483 inline
00484 const Key& Foam::HashTable<T, Key, Hash>::const_iterator::key() const
00485 {
00486 return elmtPtr_->key_;
00487 }
00488
00489
00490 template<class T, class Key, class Hash>
00491 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
00492 Foam::HashTable<T, Key, Hash>::cbegin() const
00493 {
00494 label i = 0;
00495
00496 if (nElmts_)
00497 {
00498 while (table_ && !table_[i] && ++i < tableSize_)
00499 {}
00500 }
00501 else
00502 {
00503 i = tableSize_;
00504 }
00505
00506 if (i == tableSize_)
00507 {
00508 # ifdef FULLDEBUG
00509 if (debug)
00510 {
00511 Info<< "HashTable is empty\n";
00512 }
00513 # endif
00514
00515 return HashTable<T, Key, Hash>::endConstIter_;
00516 }
00517 else
00518 {
00519 return const_iterator(*this, table_[i], i);
00520 }
00521 }
00522
00523
00524 template<class T, class Key, class Hash>
00525 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
00526 Foam::HashTable<T, Key, Hash>::cend() const
00527 {
00528 return HashTable<T, Key, Hash>::endConstIter_;
00529 }
00530
00531
00532 template<class T, class Key, class Hash>
00533 inline typename Foam::HashTable<T, Key, Hash>::const_iterator
00534 Foam::HashTable<T, Key, Hash>::begin() const
00535 {
00536 return this->cbegin();
00537 }
00538
00539
00540 template<class T, class Key, class Hash>
00541 inline const typename Foam::HashTable<T, Key, Hash>::const_iterator&
00542 Foam::HashTable<T, Key, Hash>::end() const
00543 {
00544 return HashTable<T, Key, Hash>::endConstIter_;
00545 }
00546
00547
00548