26 #ifndef StaticHashTable_C 27 #define StaticHashTable_C 43 unsigned int goodSize = size;
45 if (goodSize & (goodSize - 1))
49 while (goodSize <
unsigned(size))
61 template<
class T,
class Key,
class Hash>
66 objects_(keys_.size()),
68 endIter_(*this, keys_.size(), 0),
69 endConstIter_(*this, keys_.size(), 0)
74 <<
"Illegal size " << size <<
" for StaticHashTable." 80 template<
class T,
class Key,
class Hash>
88 objects_(ht.objects_),
90 endIter_(*
this, keys_.size(), 0),
91 endConstIter_(*
this, keys_.size(), 0)
95 template<
class T,
class Key,
class Hash>
105 endIter_(*
this, 0, 0),
106 endConstIter_(*
this, 0, 0)
114 template<
class T,
class Key,
class Hash>
121 template<
class T,
class Key,
class Hash>
126 const label hashIdx = hashKeyIndex(key);
127 const List<Key>& localKeys = keys_[hashIdx];
129 forAll(localKeys, elemIdx)
131 if (key == localKeys[elemIdx])
141 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
149 template<
class T,
class Key,
class Hash>
158 const label hashIdx = hashKeyIndex(key);
159 const List<Key>& localKeys = keys_[hashIdx];
161 forAll(localKeys, elemIdx)
163 if (key == localKeys[elemIdx])
165 return iterator(*
this, hashIdx, elemIdx);
173 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
181 template<
class T,
class Key,
class Hash>
190 const label hashIdx = hashKeyIndex(key);
191 const List<Key>& localKeys = keys_[hashIdx];
193 forAll(localKeys, elemIdx)
195 if (key == localKeys[elemIdx])
205 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
213 template<
class T,
class Key,
class Hash>
221 keys[keyI++] = iter.key();
228 template<
class T,
class Key,
class Hash>
236 const label hashIdx = hashKeyIndex(key);
240 forAll(localKeys, elemIdx)
242 if (key == localKeys[elemIdx])
249 if (existing == localKeys.
size())
252 List<T>& localObjects = objects_[hashIdx];
255 localObjects.
setSize(existing+1);
257 localKeys[existing] = key;
258 localObjects[existing] = newEntry;
270 <<
"Cannot insert " << key <<
" already in hash table\n";
279 objects_[hashIdx][existing] = newEntry;
286 template<
class T,
class Key,
class Hash>
291 List<Key>& localKeys = keys_[cit.hashIndex_];
292 List<T>& localObjects = objects_[cit.hashIndex_];
295 for (
label i = cit.elemIndex_+1; i < localKeys.
size(); i++)
297 localKeys[i-1] = localKeys[i];
298 localObjects[i-1] = localObjects[i];
307 if (it.elemIndex_ < 0)
311 it.hashIndex_ = -it.hashIndex_ - 1;
332 <<
"Cannot remove hashedEntry from hash table\n";
341 template<
class T,
class Key,
class Hash>
357 template<
class T,
class Key,
class Hash>
379 template<
class T,
class Key,
class Hash>
384 if (newSize == keys_.size())
399 <<
"Illegal size " << newSize <<
" for StaticHashTable." 408 newTable.
insert(iter.key(), *iter);
414 endIter_.hashIndex_ = keys_.size();
415 endConstIter_.hashIndex_ = keys_.size();
419 template<
class T,
class Key,
class Hash>
424 keys_[hashIdx].clear();
425 objects_[hashIdx].clear();
432 template<
class T,
class Key,
class Hash>
440 template<
class T,
class Key,
class Hash>
450 keys_.transfer(ht.keys_);
451 objects_.transfer(ht.objects_);
453 nElmts_ = ht.nElmts_;
457 endIter_.hashIndex_ = keys_.size();
458 endConstIter_.hashIndex_ = keys_.size();
460 ht.endIter_.hashIndex_ = 0;
461 ht.endConstIter_.hashIndex_ = 0;
467 template<
class T,
class Key,
class Hash>
468 void Foam::StaticHashTable<T, Key, Hash>::operator=
477 <<
"attempted assignment to self" 485 keys_.setSize(rhs.keys_.size());
486 objects_.setSize(keys_.size());
489 endIter_.hashIndex_ = keys_.size();
490 endConstIter_.hashIndex_ = keys_.size();
501 insert(iter.key(), *iter);
505 template<
class T,
class Key,
class Hash>
506 bool Foam::StaticHashTable<T, Key, Hash>::operator==
517 if (fnd ==
cend() || fnd() != iter())
527 template<
class T,
class Key,
class Hash>
528 bool Foam::StaticHashTable<T, Key, Hash>::operator!=
Iterator< const T &, const StaticHashTable< T, Key, Hash > &> const_iterator
~StaticHashTable()
Destructor.
A simple container for copying or transferring objects of type <T>.
#define forAll(list, i)
Loop across all elements in list.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
iterator begin()
Iterator set to the beginning of the StaticHashTable.
void clearStorage()
Clear the table entries and the table itself.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
void size(const label)
Override size to be inconsistent with allocated storage.
void transfer(StaticHashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
const_iterator cbegin() const
const_iterator set to the beginning of the StaticHashTable
bool operator==(const StaticHashTable< T, Key, Hash > &) const
Equality. Two hash tables are equal if all contents of first are.
static label canonicalSize(const label)
Return a canonical (power-of-two) size.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool erase(const iterator &it)
Erase an hashed entry specified by given iterator.
STL conforming hash table.
bool insert(const Key &key, const T &newElmt)
Insert a new hashed entry.
StaticHashTableCore()
Construct null.
Template-invariant bits for StaticHashTable.
StaticHashTable(const label size=128)
Construct given initial table size.
errorManip< error > abort(error &err)
void clear()
Clear all entries from table.
bool found(const Key &key) const
Return true if hashed entry is found in table.
const const_iterator & cend() const
const_iterator set to beyond the end of the StaticHashTable
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Iterator< T &, StaticHashTable< T, Key, Hash > &> iterator
void setSize(const label)
Reset size of List.
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
List< Key > toc() const
Return the table of contents.
void resize(const label newSize)
Resize the hash table for efficiency.
const iterator & end()
Iterator set to beyond the end of the StaticHashTable.
#define InfoInFunction
Report an information message using Foam::Info.