35 template<
class T,
class Key,
class Hash>
45 table_ =
new hashedEntry*[tableSize_];
47 for (
label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
55 template<
class T,
class Key,
class Hash>
67 template<
class T,
class Key,
class Hash>
82 template<
class T,
class Key,
class Hash>
91 if (keyList.
size() != elmtList.
size())
94 <<
"Lists of keys and elements have different sizes" <<
nl
95 <<
" number of keys: " << keyList.
size()
96 <<
", number of elements: " << elmtList.
size()
102 insert(keyList[i], elmtList[i]);
107 template<
class T,
class Key,
class Hash>
117 insert(pair.first(), pair.second());
124 template<
class T,
class Key,
class Hash>
137 template<
class T,
class Key,
class Hash>
142 const label hashIdx = hashKeyIndex(key);
144 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
156 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
164 template<
class T,
class Key,
class Hash>
173 const label hashIdx = hashKeyIndex(key);
175 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
187 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
195 template<
class T,
class Key,
class Hash>
204 const label hashIdx = hashKeyIndex(key);
206 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
210 return const_iterator(
this, ep, hashIdx);
218 InfoInFunction <<
"Entry " << key <<
" not found in hash table\n";
222 return const_iterator();
226 template<
class T,
class Key,
class Hash>
234 keys[keyI++] = iter.key();
241 template<
class T,
class Key,
class Hash>
251 template<
class T,
class Key,
class Hash>
258 for (const_iterator iter = cbegin(); iter != cend(); ++iter)
260 sortedLst[i++] = iter;
268 const const_iterator& a,
269 const const_iterator&
b
272 return a.key() <
b.key();
280 template<
class T,
class Key,
class Hash>
293 const label hashIdx = hashKeyIndex(key);
295 hashedEntry* existing = 0;
296 hashedEntry* prev = 0;
298 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
311 table_[hashIdx] =
new hashedEntry(key, table_[hashIdx], newEntry);
314 if (
double(nElmts_)/tableSize_ > 0.8 && tableSize_ < maxTableSize)
334 <<
"Cannot insert " << key <<
" already in hash table\n";
343 hashedEntry* ep =
new hashedEntry(key, existing->next_, newEntry);
352 table_[hashIdx] = ep;
362 template<
class T,
class Key,
class Hash>
367 insert(iter.key(), *iter);
372 template<
class T,
class Key,
class Hash>
377 set(iter.key(), *iter);
382 template<
class T,
class Key,
class Hash>
389 hashedEntry* prev = 0;
393 hashedEntry* ep = hashTable_->table_[hashIndex_];
408 prev->next_ = entryPtr_->next_;
415 hashTable_->table_[hashIndex_] = entryPtr_->next_;
420 entryPtr_ =
reinterpret_cast<hashedEntry*
>(
this);
430 hashIndex_ = -hashIndex_ - 1;
433 hashTable_->nElmts_--;
444 template<
class T,
class Key,
class Hash>
456 template<
class T,
class Key,
class Hash>
459 return erase(find(key));
463 template<
class T,
class Key,
class Hash>
466 const label nTotal = nElmts_;
470 for (
label keyI = 0;
count < nTotal && keyI < keys.
size(); ++keyI)
472 if (
erase(keys[keyI]))
482 template<
class T,
class Key,
class Hash>
483 template<
class AnyType,
class AnyHash>
493 for (
iterator iter = begin(); iter != end(); ++iter)
505 template<
class T,
class Key,
class Hash>
510 if (newSize == tableSize_)
526 tmpTable->
insert(iter.key(), *iter);
529 label oldSize = tableSize_;
530 tableSize_ = tmpTable->tableSize_;
531 tmpTable->tableSize_ = oldSize;
533 hashedEntry** oldTable = table_;
534 table_ = tmpTable->table_;
535 tmpTable->table_ = oldTable;
541 template<
class T,
class Key,
class Hash>
546 for (
label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
550 hashedEntry* ep = table_[hashIdx];
551 while (hashedEntry* next = ep->next_)
565 template<
class T,
class Key,
class Hash>
573 template<
class T,
class Key,
class Hash>
578 if (newSize < tableSize_)
581 resize(newSize ? newSize : 2);
586 template<
class T,
class Key,
class Hash>
596 tableSize_ = ht.tableSize_;
602 nElmts_ = ht.nElmts_;
609 template<
class T,
class Key,
class Hash>
619 <<
"attempted assignment to self"
633 for (
const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
635 insert(iter.key(), *iter);
640 template<
class T,
class Key,
class Hash>
650 <<
"attempted assignment to self"
658 template<
class T,
class Key,
class Hash>
661 std::initializer_list<Tuple2<Key, T>> lst
676 insert(pair.first(), pair.second());
681 template<
class T,
class Key,
class Hash>
688 if (size() != rhs.size())
693 for (
const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
697 if (fnd == cend() || fnd() != iter())
707 template<
class T,
class Key,
class Hash>
#define forAll(list, i)
Loop across all elements in list.
An STL-conforming const_iterator.
bool erase()
Erase the HashTable element at the current position.
An STL-conforming iterator.
An STL-conforming hash table.
List< const_iterator > sorted() const
Return a sorted list of constant iterators.
List< Key > sortedToc() const
Return the table of contents as a sorted list.
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
void shrink()
Shrink the allocated table to approx. twice number of elements.
List< Key > toc() const
Return the table of contents.
void transfer(HashTable< 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 HashTable
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
void clearStorage()
Clear the table entries and the table itself.
bool found(const Key &) const
Return true if hashedEntry is found in table.
void clear()
Clear all entries from table.
HashTable(const label size=128)
Construct given initial table size.
void resize(const label newSize)
Resize the hash table for efficiency.
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
A 2-tuple for storing two objects of different types.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
label size() const
Return the number of elements in the UList.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define InfoInFunction
Report an information message using Foam::Info.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
errorManip< error > abort(error &err)
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
triSurfaceToAgglom resize(surfacesMesh.size())
Template-invariant bits for HashTable.
static label canonicalSize(const label)
Return a canonical (power-of-two) size.
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable