90#ifndef Foam_HashTable_H
91#define Foam_HashTable_H
109template<
class T>
class List;
110template<
class T>
class UList;
112template<
class T,
unsigned N>
class FixedList;
113template<
class T,
class Key,
class Hash>
class HashTable;
115template<
class T,
class Key,
class Hash>
118template<
class T,
class Key,
class Hash>
125template<
class T,
class Key=word,
class Hash=Foam::Hash<Key>>
141 std::is_same_v<Foam::zero, std::remove_cv_t<T>>,
187 class const_iterator;
208 inline label hashKeyIndex(
const Key& key)
const;
212 template<
class... Args>
213 bool setEntry(
const bool overwrite,
const Key& key, Args&&...
args);
221 bool iterator_erase(iterator& iter);
224 node_type* iterator_extract(iterator& iter);
244 explicit HashTable(
const label initialCapacity);
259 std::initializer_list<std::pair<Key, T>> list,
260 const bool overwrite =
false
269 const bool overwrite =
false
294 inline T&
at(
const Key& key);
297 inline const T&
at(
const Key& key)
const;
300 inline bool contains(
const Key& key)
const;
304 inline iterator
find(
const Key& key);
308 inline const_iterator
find(
const Key& key)
const;
312 inline const_iterator
cfind(
const Key& key)
const;
315 inline const T&
lookup(
const Key& key,
const T& deflt)
const;
328 template<
class Compare>
336 template<
class UnaryPredicate>
339 const UnaryPredicate& pred,
348 template<
class UnaryPredicate>
351 const UnaryPredicate& pred,
360 template<
class BinaryPredicate>
363 const BinaryPredicate& pred,
386 template<
class UnaryPredicate>
389 const UnaryPredicate& pred,
396 template<
class UnaryPredicate>
399 const UnaryPredicate& pred,
406 template<
class BinaryPredicate>
409 const BinaryPredicate& pred,
418 template<
class... Args>
419 inline bool emplace(
const Key& key, Args&&...
args);
423 template<
class... Args>
428 inline bool insert(
const Key& key,
const T& obj);
432 inline bool insert(
const Key& key,
T&& obj);
436 inline bool set(
const Key& key,
const T& obj);
440 inline bool set(
const Key& key,
T&& obj);
454 bool erase(
const iterator& iter);
458 bool erase(
const Key& key);
466 template<
class AnyType,
class AnyHash>
471 inline label
erase(std::initializer_list<Key>
keys);
474 template<
class InputIter>
475 inline label
erase(InputIter first, InputIter last);
492 template<
class AnyType,
class AnyHash>
512 template<
class UnaryPredicate>
515 const UnaryPredicate& pred,
516 const bool pruning =
false
527 template<
class UnaryPredicate>
530 const UnaryPredicate& pred,
531 const bool pruning =
false
542 template<
class BinaryPredicate>
545 const BinaryPredicate& pred,
546 const bool pruning =
false
564 void resize(label newCapacity);
568 void reserve(label numEntries);
610 void operator=(std::initializer_list<std::pair<Key, T>>
rhs);
631 template<
bool Const>
class Iterator;
634 friend class Iterator<true>;
637 friend class Iterator<false>;
723 explicit
operator const Iterator<Any>&()
const
725 return *
reinterpret_cast<const Iterator<Any>*
>(
this);
741 const Key&
key()
const {
return entry_->key(); }
756 bool operator==(
const Iterator<Any>& iter)
const noexcept
758 return (
entry_ == iter.entry_);
764 return (
entry_ != iter.entry_);
773 public Iterator<false>
797 explicit iterator(
const Iterator<false>& iter)
799 Iterator<false>(iter)
847 public Iterator<true>
918 return this->
operator=
A 1D vector of objects of type <T> with a fixed length <N>.
Factory class for creating a begin/end pair for any const iterator type, normally associated with a H...
bool operator!=(const Iterator< Any > &iter) const noexcept
this_type::key_type key_type
The key type.
bool found() const noexcept
True if iterator points to an entry - same as good().
bool good() const noexcept
True if iterator points to an entry.
std::forward_iterator_tag iterator_category
Ostream & print(Ostream &os) const
Write the (key, val) pair.
node_type * entry_
The selected entry.
constexpr Iterator() noexcept
Default construct. Also the same as the end iterator.
std::conditional_t< Const, const this_type, this_type > table_type
The HashTable container type.
std::conditional_t< Const, const this_type::node_type, this_type::node_type > node_type
The node-type being addressed.
this_type::difference_type difference_type
label index_
Index within the hash-table data.
const Key & key() const
The key associated with the iterator.
bool operator==(const Iterator< Any > &iter) const noexcept
Compare hash-entry element pointers.
table_type * container_
The hash-table container being iterated on.
void increment()
Increment to the next position.
std::conditional_t< Const, const this_type::mapped_type, this_type::mapped_type > mapped_type
The object type being addressed.
Forward iterator with const access.
const node_type * node() const noexcept
Const access to the entry (node).
this_type::key_type key_type
reference val() const
Const access to referenced object (value).
reference operator()() const
const this_type::mapped_type mapped_type
const_iterator & operator++()
const_iterator()=default
Default construct (end iterator).
std::forward_iterator_tag iterator_category
this_type::const_pointer pointer
const_iterator(const const_iterator &)=default
Copy construct.
this_type::node_type node_type
const_iterator & operator=(const const_iterator &)=default
Copy assignment.
this_type::difference_type difference_type
reference operator*() const
Const access to referenced object (value).
this_type::const_reference reference
const_iterator(const iterator &iter)
Implicit conversion from dissimilar access type.
const this_type::value_type value_type
const_iterator(const Iterator< Any > &iter)
Copy construct from any access type.
const_iterator & operator=(const iterator &iter)
Forward iterator with non-const access.
const node_type * node() const noexcept
Const access to the entry (node).
this_type::mapped_type mapped_type
const_reference operator()() const
this_type::key_type key_type
iterator()=default
Default construct (end iterator).
this_type::value_type value_type
reference operator*()
Non-const access to referenced object (value).
const_reference val() const
Const access to referenced object (value).
node_type * node() noexcept
Non-const access to the entry (node).
std::forward_iterator_tag iterator_category
this_type::const_reference const_reference
this_type::reference reference
this_type::node_type node_type
this_type::difference_type difference_type
this_type::const_pointer const_pointer
reference val()
Non-const access to referenced object (value).
const_reference operator*() const
Const access to referenced object (value).
this_type::pointer pointer
iterator(const Iterator< false > &iter)
Copy construct from similar access type.
An iterator wrapper for returning a reference to the key.
key_iterator_base(const Iter &iter)
Copy construct with implicit conversion.
reference operator()() const
reference operator*() const
Return the key.
key_iterator_base operator++(int)
constexpr key_iterator_base() noexcept
Default construct (end iterator).
key_iterator_base & operator++()
this_type::key_type value_type
A HashTable similar to std::unordered_map.
List< Key > tocEntries(const BinaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the binary predicate applied to the keys and v...
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
label erase(const HashTable< AnyType, Key, AnyHash > &other)
Remove table entries given by keys of the other hash-table.
label countEntries(const BinaryPredicate &pred, const bool invert=false) const
Count the number of entries that satisfy the binary predicate.
HashTable(const UList< Key > &keys, const UList< T > &values, const bool overwrite=false)
Construct from key/value pairs.
void swap(HashTable< T, Key, Hash > &rhs) noexcept
Swap contents into this table.
const_iterator_pair< const_key_iterator, this_type > keys() const
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
T & operator()(const Key &key)
Return existing entry or create a new entry.
const_iterator begin() const
const_iterator set to the beginning of the HashTable
void operator=(std::initializer_list< std::pair< Key, T > > rhs)
Copy assign from an initializer list.
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.
HashTable< T, Key, HashType > this_type
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
bool insert(const Key &key, T &&obj)
Move insert a new entry, not overwriting existing entries.
label retain(const HashTable< AnyType, Key, AnyHash > &other)
Retain table entries given by keys of the other hash-table.
T & operator()(const Key &key, const T &deflt)
Return existing entry or insert a new entry.
label erase(const UList< Key > &keys)
Remove table entries given by the listed keys.
Ostream & printInfo(Ostream &os) const
bool empty() const noexcept
True if the hash table is empty.
void merge(HashTable< T, Key, Hash > &&source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
bool erase(const Key &key)
Erase an entry specified by the given key.
iterator begin()
iterator set to the beginning of the HashTable
const T & at(const Key &key) const
Find and return a hashed entry. FatalError if it does not exist.
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
key_iterator_base< iterator > key_iterator
label erase(InputIter first, InputIter last)
Remove multiple entries using an iterator range of keys.
bool found(const Key &key) const
bool set(const Key &key, T &&obj)
Move assign a new entry, overwriting existing entries.
void merge(HashTable< T, Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
void operator=(this_type &&rhs)
Move assign.
bool operator==(const this_type &rhs) const
Equality. Tables are equal if all keys and values are equal, independent of order or underlying stora...
void clearStorage()
Remove all entries from table and the table itself.
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
HashTable(this_type &&rhs) noexcept
Move construct.
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
key_iterator_base< const_iterator > const_key_iterator
label filterValues(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their values.
HashTable(std::initializer_list< std::pair< Key, T > > list, const bool overwrite=false)
Construct from key/value pairs in initializer list.
List< Key > tocValues(const UnaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the unary predicate applied to the values.
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
HashTable(Istream &is)
Construct from Istream.
label filterEntries(const BinaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their key/value.
UPtrList< node_type > sorted()
Non-const access to the hash-table contents in sorted order (sorted by keys).
HashTable(const label initialCapacity)
Construct empty with initial table capacity.
void resize(label newCapacity)
Rehash the hash table with new number of buckets. Currently identical to setCapacity().
label erase(std::initializer_list< Key > keys)
Remove table entries given by the listed keys.
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
label capacity() const noexcept
The size of the underlying table (the number of buckets).
HashTable(const this_type &ht)
Copy construct.
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
label countValues(const UnaryPredicate &pred, const bool invert=false) const
Count the number of values that satisfy the unary predicate.
label size() const noexcept
The number of elements in table.
UPtrList< const node_type > csorted() const
Const access to the hash-table contents in sorted order (sorted by keys).
const_iterator find(const Key &key) const
Find and return an const_iterator set at the hashed entry.
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
void operator=(const this_type &rhs)
Copy assign.
std::conditional_t< std::is_same_v< Foam::zero, std::remove_cv_t< T > >, Detail::HashTableSingle< Key >, Detail::HashTablePair< Key, T > > node_type
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
void clear()
Remove all entries from table.
this_type & operator+=(const this_type &rhs)
Add entries into this HashTable.
iterator end() noexcept
iterator to signal the end (for any HashTable)
const T & operator[](const Key &key) const
Find and return a hashed entry. FatalError if it does not exist.
List< Key > tocKeys(const UnaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the unary predicate applied to the keys.
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
constexpr const_iterator cend() const noexcept
void setCapacity(label newCapacity)
Change the hash table capacity (number of buckets).
label erase(const FixedList< Key, N > &keys)
Remove table entries given by the listed keys.
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
const T & const_reference
List< Key > sortedToc(const Compare &comp) const
The table of contents (the keys) sorted according to the specified comparator.
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
constexpr HashTable() noexcept
Default construct: empty without allocation (capacity=0).
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
A keyword and a list of tokens is an 'entry'.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
OBJstream os(runTime.globalPath()/outputName)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
nonInt insert("surfaceSum(((S|magSf)*S)")
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...
#define FOAM_DEPRECATED_FOR(since, replacement)
Internal storage type for HashTable entries.
Internal storage type for HashSet entries.
constexpr HashTableCore() noexcept=default
Default construct.
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...