26 #ifndef StaticHashTable_C
27 #define StaticHashTable_C
35 template<
class T,
class Key,
class Hash>
44 unsigned int goodSize = size;
46 if (goodSize & (goodSize - 1))
50 while (goodSize <
unsigned(size))
63 template<
class T,
class Key,
class Hash>
66 StaticHashTableName(),
67 keys_(canonicalSize(size)),
68 objects_(keys_.size()),
70 endIter_(*this, keys_.size(), 0),
71 endConstIter_(*this, keys_.size(), 0)
77 "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
78 ) <<
"Illegal size " << size <<
" for StaticHashTable."
85 template<
class T,
class Key,
class Hash>
91 StaticHashTableName(),
93 objects_(ht.objects_),
95 endIter_(*
this, keys_.size(), 0),
96 endConstIter_(*
this, keys_.size(), 0)
101 template<
class T,
class Key,
class Hash>
107 StaticHashTableName(),
111 endIter_(*
this, 0, 0),
112 endConstIter_(*
this, 0, 0)
120 template<
class T,
class Key,
class Hash>
127 template<
class T,
class Key,
class Hash>
132 const label hashIdx = hashKeyIndex(key);
133 const List<Key>& localKeys = keys_[hashIdx];
135 forAll(localKeys, elemIdx)
137 if (key == localKeys[elemIdx])
147 Info<<
"StaticHashTable<T, Key, Hash>::found(const Key&) : "
148 <<
"Entry " << key <<
" not found in hash table\n";
156 template<
class T,
class Key,
class Hash>
165 const label hashIdx = hashKeyIndex(key);
166 const List<Key>& localKeys = keys_[hashIdx];
168 forAll(localKeys, elemIdx)
170 if (key == localKeys[elemIdx])
172 return iterator(*
this, hashIdx, elemIdx);
180 Info<<
"StaticHashTable<T, Key, Hash>::find(const Key&) : "
181 <<
"Entry " << key <<
" not found in hash table\n";
189 template<
class T,
class Key,
class Hash>
198 const label hashIdx = hashKeyIndex(key);
199 const List<Key>& localKeys = keys_[hashIdx];
201 forAll(localKeys, elemIdx)
203 if (key == localKeys[elemIdx])
213 Info<<
"StaticHashTable<T, Key, Hash>::find(const Key&) const : "
214 <<
"Entry " << key <<
" not found in hash table\n";
223 template<
class T,
class Key,
class Hash>
231 tofc[i++] = iter.key();
238 template<
class T,
class Key,
class Hash>
246 const label hashIdx = hashKeyIndex(key);
249 label existing = localKeys.
size();
250 forAll(localKeys, elemIdx)
252 if (key == localKeys[elemIdx])
259 if (existing == localKeys.
size())
262 List<T>& localObjects = objects_[hashIdx];
265 localObjects.setSize(existing+1);
267 localKeys[existing] = key;
268 localObjects[existing] = newEntry;
279 Info<<
"StaticHashTable<T, Key, Hash>::set"
280 "(const Key& key, T newEntry, true) : "
281 "Cannot insert " << key <<
" already in hash table\n";
290 objects_[hashIdx][existing] = newEntry;
297 template<
class T,
class Key,
class Hash>
302 List<Key>& localKeys = keys_[cit.hashIndex_];
303 List<T>& localObjects = objects_[cit.hashIndex_];
306 for (label i = cit.elemIndex_+1; i < localKeys.
size(); i++)
308 localKeys[i-1] = localKeys[i];
309 localObjects[i-1] = localObjects[i];
318 if (it.elemIndex_ < 0)
323 while (--it.hashIndex_ >= 0 && !objects_[it.hashIndex_].size())
326 if (it.hashIndex_ >= 0)
329 it.elemIndex_ = objects_[it.hashIndex_].size() - 1;
346 Info<<
"StaticHashTable<T, Key, Hash>::erase(iterator&) : "
347 <<
"hashedEntry removed.\n";
358 Info<<
"StaticHashTable<T, Key, Hash>::erase(iterator&) : "
359 <<
"cannot remove hashedEntry from hash table\n";
368 template<
class T,
class Key,
class Hash>
384 template<
class T,
class Key,
class Hash>
394 for (
iterator iter = this->begin(); iter != this->end(); ++iter)
396 if (rhs.
found(iter.key()) && erase(iter))
406 template<
class T,
class Key,
class Hash>
409 label newSize = canonicalSize(sz);
411 if (newSize == keys_.size())
416 Info<<
"StaticHashTable<T, Key, Hash>::resize(const label) : "
417 <<
"new table size == old table size\n";
428 "StaticHashTable<T, Key, Hash>::resize(const label)"
429 ) <<
"Illegal size " << newSize <<
" for StaticHashTable."
438 newTable.
insert(iter.key(), *iter);
444 endIter_.hashIndex_ = keys_.size();
445 endConstIter_.hashIndex_ = keys_.size();
449 template<
class T,
class Key,
class Hash>
454 keys_[hashIdx].clear();
455 objects_[hashIdx].clear();
462 template<
class T,
class Key,
class Hash>
471 template<
class T,
class Key,
class Hash>
481 keys_.transfer(ht.keys_);
482 objects_.transfer(ht.objects_);
484 nElmts_ = ht.nElmts_;
488 endIter_.hashIndex_ = keys_.size();
489 endConstIter_.hashIndex_ = keys_.size();
491 ht.endIter_.hashIndex_ = 0;
492 ht.endConstIter_.hashIndex_ = 0;
498 template<
class T,
class Key,
class Hash>
509 "StaticHashTable<T, Key, Hash>::operator="
510 "(const StaticHashTable<T, Key, Hash>&)"
511 ) <<
"attempted assignment to self"
519 keys_.setSize(rhs.keys_.
size());
520 objects_.setSize(keys_.size());
523 endIter_.hashIndex_ = keys_.size();
524 endConstIter_.hashIndex_ = keys_.size();
535 insert(iter.key(), *iter);
539 template<
class T,
class Key,
class Hash>
540 bool Foam::StaticHashTable<T, Key, Hash>::operator==
550 if (fnd == rhs.
cend() || fnd() != iter())
561 if (fnd == cend() || fnd() != iter())
570 template<
class T,
class Key,
class Hash>
571 bool Foam::StaticHashTable<T, Key, Hash>::operator!=