34 template<
class T,
class Key,
class Hash>
44 table_ =
new hashedEntry*[tableSize_];
46 for (
label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
54 template<
class T,
class Key,
class Hash>
59 tableSize_(ht.tableSize_),
64 table_ =
new hashedEntry*[tableSize_];
66 for (
label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
78 template<
class T,
class Key,
class Hash>
95 template<
class T,
class Key,
class Hash>
108 template<
class T,
class Key,
class Hash>
113 const label hashIdx = hashKeyIndex(key);
115 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
127 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
135 template<
class T,
class Key,
class Hash>
144 const label hashIdx = hashKeyIndex(key);
146 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
158 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
166 template<
class T,
class Key,
class Hash>
175 const label hashIdx = hashKeyIndex(key);
177 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
189 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
197 template<
class T,
class Key,
class Hash>
205 keys[keyI++] = iter.key();
212 template<
class T,
class Key,
class Hash>
222 template<
class T,
class Key,
class Hash>
235 const label hashIdx = hashKeyIndex(key);
237 hashedEntry* existing = 0;
238 hashedEntry* prev = 0;
240 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
253 table_[hashIdx] =
new hashedEntry(key, table_[hashIdx], newEntry);
256 if (
double(nElmts_)/tableSize_ > 0.8 && tableSize_ <
maxTableSize)
276 <<
"Cannot insert " << key <<
" already in hash table\n";
285 hashedEntry* ep =
new hashedEntry(key, existing->next_, newEntry);
294 table_[hashIdx] = ep;
304 template<
class T,
class Key,
class Hash>
311 hashedEntry* prev = 0;
315 hashedEntry* ep = hashTable_->table_[hashIndex_];
330 prev->next_ = entryPtr_->next_;
337 hashTable_->table_[hashIndex_] = entryPtr_->next_;
342 entryPtr_ =
reinterpret_cast<hashedEntry*
>(
this);
352 hashIndex_ = -hashIndex_ - 1;
355 hashTable_->nElmts_--;
366 template<
class T,
class Key,
class Hash>
378 template<
class T,
class Key,
class Hash>
385 template<
class T,
class Key,
class Hash>
388 const label nTotal = nElmts_;
392 for (
label keyI = 0; count < nTotal && keyI < keys.
size(); ++keyI)
394 if (
erase(keys[keyI]))
404 template<
class T,
class Key,
class Hash>
405 template<
class AnyType,
class AnyHash>
427 template<
class T,
class Key,
class Hash>
432 if (newSize == tableSize_)
448 tmpTable->
insert(iter.key(), *iter);
451 label oldSize = tableSize_;
452 tableSize_ = tmpTable->tableSize_;
453 tmpTable->tableSize_ = oldSize;
455 hashedEntry** oldTable = table_;
456 table_ = tmpTable->table_;
457 tmpTable->table_ = oldTable;
463 template<
class T,
class Key,
class Hash>
468 for (
label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
472 hashedEntry* ep = table_[hashIdx];
473 while (hashedEntry* next = ep->next_)
487 template<
class T,
class Key,
class Hash>
495 template<
class T,
class Key,
class Hash>
500 if (newSize < tableSize_)
503 resize(newSize ? newSize : 2);
508 template<
class T,
class Key,
class Hash>
518 tableSize_ = ht.tableSize_;
524 nElmts_ = ht.nElmts_;
531 template<
class T,
class Key,
class Hash>
532 void Foam::HashTable<T, Key, Hash>::operator=
541 <<
"attempted assignment to self" 557 insert(iter.key(), *iter);
562 template<
class T,
class Key,
class Hash>
563 bool Foam::HashTable<T, Key, Hash>::operator==
578 if (fnd ==
cend() || fnd() != iter())
588 template<
class T,
class Key,
class Hash>
589 bool Foam::HashTable<T, Key, Hash>::operator!=
A simple container for copying or transferring objects of type <T>.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
An STL-conforming const_iterator.
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
#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...
HashTable(const label size=128)
Construct given initial table size.
label size() const
Return number of elements in table.
void transfer(HashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
HashTableCore()
Construct null.
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
An STL-conforming iterator.
bool operator==(const HashTable< T, Key, Hash > &) const
Equality. Hash tables are equal if the keys and values are equal.
friend class const_iterator
Declare friendship with the const_iterator.
void clear()
Clear all entries from table.
static const label maxTableSize
Maximum allowable table size.
iterator begin()
Iterator set to the beginning of the HashTable.
An STL-conforming hash table.
errorManip< error > abort(error &err)
List< Key > toc() const
Return the table of contents.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
bool found(const Key &) const
Return true if hashedEntry is found in table.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label size() const
Return the number of elements in the UList.
Template-invariant bits for HashTable.
static label canonicalSize(const label)
Return a canonical (power-of-two) size.
List< Key > sortedToc() const
Return the table of contents as a sorted list.
void resize(const label newSize)
Resize the hash table for efficiency.
friend class iterator
Declare friendship with the iterator.
bool erase()
Erase the HashTable element at the current position.
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
void shrink()
Shrink the allocated table to approx. twice number of elements.
void clearStorage()
Clear the table entries and the table itself.
#define InfoInFunction
Report an information message using Foam::Info.