26 #ifndef StaticHashTable_C 27 #define StaticHashTable_C 43 unsigned int goodSize = size;
45 if (goodSize & (goodSize - 1))
49 while (goodSize <
unsigned(size))
62 template<
class T,
class Key,
class Hash>
67 objects_(keys_.size()),
69 endIter_(*this, keys_.size(), 0),
70 endConstIter_(*this, keys_.size(), 0)
76 "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)" 77 ) <<
"Illegal size " << size <<
" for StaticHashTable." 84 template<
class T,
class Key,
class Hash>
92 objects_(ht.objects_),
94 endIter_(*
this, keys_.size(), 0),
95 endConstIter_(*
this, keys_.size(), 0)
99 template<
class T,
class Key,
class Hash>
109 endIter_(*
this, 0, 0),
110 endConstIter_(*
this, 0, 0)
118 template<
class T,
class Key,
class Hash>
125 template<
class T,
class Key,
class Hash>
130 const label hashIdx = hashKeyIndex(key);
131 const List<Key>& localKeys = keys_[hashIdx];
133 forAll(localKeys, elemIdx)
135 if (key == localKeys[elemIdx])
145 Info<<
"StaticHashTable<T, Key, Hash>::found(const Key&) : " 146 <<
"Entry " << key <<
" not found in hash table\n";
154 template<
class T,
class Key,
class Hash>
163 const label hashIdx = hashKeyIndex(key);
164 const List<Key>& localKeys = keys_[hashIdx];
166 forAll(localKeys, elemIdx)
168 if (key == localKeys[elemIdx])
170 return iterator(*
this, hashIdx, elemIdx);
178 Info<<
"StaticHashTable<T, Key, Hash>::find(const Key&) : " 179 <<
"Entry " << key <<
" not found in hash table\n";
187 template<
class T,
class Key,
class Hash>
196 const label hashIdx = hashKeyIndex(key);
197 const List<Key>& localKeys = keys_[hashIdx];
199 forAll(localKeys, elemIdx)
201 if (key == localKeys[elemIdx])
211 Info<<
"StaticHashTable<T, Key, Hash>::find(const Key&) const : " 212 <<
"Entry " << key <<
" not found in hash table\n";
221 template<
class T,
class Key,
class Hash>
229 keys[keyI++] = iter.key();
236 template<
class T,
class Key,
class Hash>
244 const label hashIdx = hashKeyIndex(key);
248 forAll(localKeys, elemIdx)
250 if (key == localKeys[elemIdx])
257 if (existing == localKeys.
size())
260 List<T>& localObjects = objects_[hashIdx];
263 localObjects.
setSize(existing+1);
265 localKeys[existing] = key;
266 localObjects[existing] = newEntry;
277 Info<<
"StaticHashTable<T, Key, Hash>::set" 278 "(const Key& key, T newEntry, true) : " 279 "Cannot insert " << key <<
" already in hash table\n";
288 objects_[hashIdx][existing] = newEntry;
295 template<
class T,
class Key,
class Hash>
300 List<Key>& localKeys = keys_[cit.hashIndex_];
301 List<T>& localObjects = objects_[cit.hashIndex_];
304 for (
label i = cit.elemIndex_+1; i < localKeys.
size(); i++)
306 localKeys[i-1] = localKeys[i];
307 localObjects[i-1] = localObjects[i];
316 if (it.elemIndex_ < 0)
320 it.hashIndex_ = -it.hashIndex_ - 1;
329 Info<<
"StaticHashTable<T, Key, Hash>::erase(iterator&) : " 330 <<
"hashedEntry removed.\n";
341 Info<<
"StaticHashTable<T, Key, Hash>::erase(iterator&) : " 342 <<
"cannot remove hashedEntry from hash table\n";
351 template<
class T,
class Key,
class Hash>
367 template<
class T,
class Key,
class Hash>
389 template<
class T,
class Key,
class Hash>
394 if (newSize == keys_.size())
399 Info<<
"StaticHashTable<T, Key, Hash>::resize(const label) : " 400 <<
"new table size == old table size\n";
411 "StaticHashTable<T, Key, Hash>::resize(const label)" 412 ) <<
"Illegal size " << newSize <<
" for StaticHashTable." 421 newTable.
insert(iter.key(), *iter);
427 endIter_.hashIndex_ = keys_.size();
428 endConstIter_.hashIndex_ = keys_.size();
432 template<
class T,
class Key,
class Hash>
437 keys_[hashIdx].clear();
438 objects_[hashIdx].clear();
445 template<
class T,
class Key,
class Hash>
453 template<
class T,
class Key,
class Hash>
463 keys_.transfer(ht.keys_);
464 objects_.transfer(ht.objects_);
466 nElmts_ = ht.nElmts_;
470 endIter_.hashIndex_ = keys_.size();
471 endConstIter_.hashIndex_ = keys_.size();
473 ht.endIter_.hashIndex_ = 0;
474 ht.endConstIter_.hashIndex_ = 0;
480 template<
class T,
class Key,
class Hash>
481 void Foam::StaticHashTable<T, Key, Hash>::operator=
491 "StaticHashTable<T, Key, Hash>::operator=" 492 "(const StaticHashTable<T, Key, Hash>&)" 493 ) <<
"attempted assignment to self" 501 keys_.setSize(rhs.keys_.size());
502 objects_.setSize(keys_.size());
505 endIter_.hashIndex_ = keys_.size();
506 endConstIter_.hashIndex_ = keys_.size();
517 insert(iter.key(), *iter);
521 template<
class T,
class Key,
class Hash>
522 bool Foam::StaticHashTable<T, Key, Hash>::operator==
533 if (fnd ==
cend() || fnd() != iter())
543 template<
class T,
class Key,
class Hash>
544 bool Foam::StaticHashTable<T, Key, Hash>::operator!=
STL conforming hash table.
static label canonicalSize(const label)
Return a canonical (power-of-two) size.
Iterator< const T &, const StaticHashTable< T, Key, Hash > & > const_iterator
iterator begin()
Iterator set to the beginning of the StaticHashTable.
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Iterator< T &, StaticHashTable< T, Key, Hash > & > iterator
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
StaticHashTableCore()
Construct null.
bool operator==(const StaticHashTable< T, Key, Hash > &) const
Equality. Two hash tables are equal if all contents of first are.
A simple container for copying or transferring objects of type <T>.
StaticHashTable(const label size=128)
Construct given initial table size.
~StaticHashTable()
Destructor.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
void size(const label)
Override size to be inconsistent with allocated storage.
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void setSize(const label)
Reset size of List.
List< Key > toc() const
Return the table of contents.
const iterator & end()
Iterator set to beyond the end of the StaticHashTable.
void clear()
Clear all entries from table.
errorManip< error > abort(error &err)
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Template-invariant bits for StaticHashTable.
void resize(const label newSize)
Resize the hash table for efficiency.
bool found(const Key &key) const
Return true if hashed entry is found in table.
void clearStorage()
Clear the table entries and the table itself.
void transfer(StaticHashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
const const_iterator & cend() const
const_iterator set to beyond the end of the StaticHashTable
bool erase(const iterator &it)
Erase an hashed entry specified by given iterator.
const_iterator cbegin() const
const_iterator set to the beginning of the StaticHashTable