29#ifndef Foam_HashTable_C
30#define Foam_HashTable_C
39template<
class T,
class Key,
class Hash>
46template<
class T,
class Key,
class Hash>
57 for (label i=0; i < capacity_; ++i)
65template<
class T,
class Key,
class Hash>
70 for (const_iterator iter = ht.
cbegin(); iter != ht.
cend(); ++iter)
72 insert(iter.key(), iter.val());
77template<
class T,
class Key,
class Hash>
82 capacity_(rhs.capacity_),
91template<
class T,
class Key,
class Hash>
94 std::initializer_list<std::pair<Key, T>> list
99 for (
const auto& keyval : list)
101 set(keyval.first, keyval.second);
108template<
class T,
class Key,
class Hash>
121template<
class T,
class Key,
class Hash>
129 list[count++] = iter.key();
136template<
class T,
class Key,
class Hash>
146template<
class T,
class Key,
class Hash>
147template<
class Compare>
160template<
class T,
class Key,
class Hash>
170 result.
set(count++, iter.node());
179template<
class T,
class Key,
class Hash>
187template<
class T,
class Key,
class Hash>
195 for (
iterator iter = begin(); iter != end(); ++iter)
197 result.
set(count++, iter.node());
207template<
class T,
class Key,
class Hash>
208template<
class UnaryPredicate>
211 const UnaryPredicate& pred,
222 list[count++] = iter.key();
233template<
class T,
class Key,
class Hash>
234template<
class UnaryPredicate>
237 const UnaryPredicate& pred,
244 for (const_iterator iter = cbegin(); iter != cend(); ++iter)
248 list[count++] = iter.key();
259template<
class T,
class Key,
class Hash>
260template<
class BinaryPredicate>
263 const BinaryPredicate& pred,
274 list[count++] = iter.key();
285template<
class T,
class Key,
class Hash>
286template<
class UnaryPredicate>
289 const UnaryPredicate& pred,
307template<
class T,
class Key,
class Hash>
308template<
class UnaryPredicate>
311 const UnaryPredicate& pred,
329template<
class T,
class Key,
class Hash>
330template<
class BinaryPredicate>
333 const BinaryPredicate& pred,
351template<
class T,
class Key,
class Hash>
352template<
class... Args>
355 const bool overwrite,
365 const label index = hashKeyIndex(key);
370 for (
node_type* ep = table_[index]; ep; ep = ep->next_)
372 if (key == ep->key())
384 new node_type(table_[index], key, std::forward<Args>(
args)...);
387 if (
double(size_)/capacity_ > 0.8 && capacity_ < maxTableSize)
402 if (!node_type::stores_value())
439template<
class T,
class Key,
class Hash>
453template<
class T,
class Key,
class Hash>
461template<
class T,
class Key,
class Hash>
462template<
class InputIter>
473 const label nTotal = this->size();
474 changed < nTotal && first != last;
478 if (this->
erase(*first))
488template<
class T,
class Key,
class Hash>
491 std::initializer_list<Key> keys
494 return erase(keys.begin(), keys.end());
498template<
class T,
class Key,
class Hash>
509template<
class T,
class Key,
class Hash>
519template<
class T,
class Key,
class Hash>
520template<
class AnyType,
class AnyHash>
526 const label nTotal = this->size();
529 if (other.
size() <= nTotal)
536 changed < nTotal && iter != other.
cend();
540 if (
erase(iter.key()))
551 iterator iter = begin();
552 changed < nTotal && iter != end();
567template<
class T,
class Key,
class Hash>
568template<
class AnyType,
class AnyHash>
574 const label nTotal = this->size();
587 for (iterator iter = begin(); iter != end(); ++iter)
600template<
class T,
class Key,
class Hash>
604 const label oldCapacity = capacity_;
606 if (newCapacity == oldCapacity)
614 else if (!newCapacity)
620 <<
"HashTable contains " << size_ <<
" cannot resize(0)" <<
nl;
638 auto oldTable = table_;
639 capacity_ = newCapacity;
642 for (label i=0; i < capacity_; ++i)
650 for (label i=0; nMove && i < oldCapacity; ++i)
658 const label newIdx = hashKeyIndex(ep->key());
660 ep->next_ = table_[newIdx];
667 oldTable[i] =
nullptr;
677template<
class T,
class Key,
class Hash>
680 for (label i=0; size_ && i<capacity_; ++i)
696template<
class T,
class Key,
class Hash>
704template<
class T,
class Key,
class Hash>
712 std::swap(size_, rhs.size_);
713 std::swap(capacity_, rhs.capacity_);
714 std::swap(table_, rhs.table_);
718template<
class T,
class Key,
class Hash>
731template<
class T,
class Key,
class Hash>
732template<
class UnaryPredicate>
735 const UnaryPredicate& pred,
741 for (
iterator iter = begin(); iter != end(); ++iter)
746 (pred(iter.key()) ? pruning : !pruning)
758template<
class T,
class Key,
class Hash>
759template<
class UnaryPredicate>
762 const UnaryPredicate& pred,
768 for (
iterator iter = begin(); iter != end(); ++iter)
773 (pred(iter.val()) ? pruning : !pruning)
785template<
class T,
class Key,
class Hash>
786template<
class BinaryPredicate>
789 const BinaryPredicate& pred,
795 for (
iterator iter = begin(); iter != end(); ++iter)
800 (pred(iter.key(), iter.val()) ? pruning : !pruning)
814template<
class T,
class Key,
class Hash>
835 for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
837 insert(iter.key(), iter.val());
842template<
class T,
class Key,
class Hash>
845 std::initializer_list<std::pair<Key, T>> rhs
858 for (
const auto& keyval : rhs)
860 set(keyval.first, keyval.second);
865template<
class T,
class Key,
class Hash>
880template<
class T,
class Key,
class Hash>
887 if (size() != rhs.size())
892 for (
const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
896 if (!other.
good() || other.
val() != iter.val())
906template<
class T,
class Key,
class Hash>
916template<
class T,
class Key,
class Hash>
923 if (rhs.size() ||
this != &rhs)
927 for (
const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
929 insert(iter.key(), iter.val());
virtual bool resize()
Resize the ODE solver.
A 1D vector of objects of type <T> with a fixed length <N>.
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
bool good() const noexcept
True if iterator points to an entry.
node_type * entry_
The selected entry.
label index_
Index within the hash-table data.
Forward iterator with const access.
reference val() const
Const access to referenced object (value)
Forward iterator with non-const access.
A HashTable similar to std::unordered_map.
List< Key > tocEntries(const BinaryPredicate &pred, const bool invert=false) const
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
label countEntries(const BinaryPredicate &pred, const bool invert=false) const
Count the number of entries that satisfy the binary predicate.
label countKeys(const UnaryPredicate &pred, const bool invert=false) const
Count the number of keys that satisfy the unary predicate.
List< Key > toc() const
The table of contents (the keys) in unsorted order.
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
label retain(const HashTable< AnyType, Key, AnyHash > &other)
Retain table entries given by keys of the other hash-table.
bool empty() const noexcept
True if the hash table is empty.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
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.
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
label filterValues(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their values.
List< Key > tocValues(const UnaryPredicate &pred, const bool invert=false) const
std::conditional< std::is_same< zero::null, typenamestd::remove_cv< T >::type >::value, Detail::HashTableSingle< Key >, Detail::HashTablePair< Key, T > >::type node_type
label size() const noexcept
The number of elements in table.
void swap(HashTable< T, Key, Hash > &rhs)
Swap contents into this table.
label filterEntries(const BinaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their key/value.
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
label countValues(const UnaryPredicate &pred, const bool invert=false) const
Count the number of values that satisfy the unary predicate.
UPtrList< const node_type > sorted() const
HashTable()
Default construct with default (128) table capacity.
UPtrList< const node_type > csorted() const
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
void clear()
Clear all entries from table.
List< Key > tocKeys(const UnaryPredicate &pred, const bool invert=false) const
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
void resize(const label len)
Adjust allocated size of list.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
const T * set(const label i) const
transferModelList & transfer()
Transfer.
patchWriters resize(patchIds.size())
#define WarningInFunction
Report a warning using Foam::Warning.
#define DebugInFunction
Report an information message using Foam::Info.
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
void sort(UList< T > &list)
Sort the list.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
constexpr char nl
The newline '\n' character (0x0a)
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::argList args(argc, argv)
Bits that are independent of HashTable template parameters.
static label canonicalSize(const label requested_size)
Return a canonical (power-of-two) of the requested size.
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...