90 #ifndef Foam_HashTable_H 91 #define Foam_HashTable_H 109 template<
class T>
class List;
110 template<
class T>
class UList;
112 template<
class T,
unsigned N>
class FixedList;
113 template<
class T,
class Key,
class Hash>
class HashTable;
115 template<
class T,
class Key,
class Hash>
118 template<
class T,
class Key,
class Hash>
119 Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
125 template<
class T,
class Key=word,
class Hash=Foam::Hash<Key>>
139 typedef typename std::conditional
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;
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;
321 List<Key>
toc()
const;
328 template<
class Compare>
329 List<Key>
sortedToc(
const Compare& comp)
const;
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>
423 template<
class... Args>
428 inline bool insert(
const Key&
key,
const 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);
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>;
663 using node_type =
typename std::conditional
725 return *
reinterpret_cast<const Iterator<Any>*
>(
this);
741 const Key&
key()
const {
return entry_->key(); }
744 inline Ostream&
print(Ostream&
os)
const;
758 return (
entry_ == iter.entry_);
764 return (
entry_ != iter.entry_);
773 public Iterator<false>
847 public Iterator<true>
918 return this->
operator=
1054 #ifndef NoHashTableC // Excluded from token.H
reference operator*() const
Return the key.
label countValues(const UnaryPredicate &pred, const bool invert=false) const
Count the number of values that satisfy the unary predicate.
node_type * entry_
The selected entry.
void increment()
Increment to the next position.
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...
std::forward_iterator_tag iterator_category
reference val() const
Const access to referenced object (value)
Factory class for creating a begin/end pair for any const iterator.
const_reference operator()() const
label index_
Index within the hash-table data.
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
const node_type * node() const noexcept
Const access to the entry (node)
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
this_type & operator+=(const this_type &rhs)
Add entries into this HashTable.
Internally used base for iterator and const_iterator.
Forward iterator with const access.
label filterValues(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their values.
A 1D vector of objects of type <T> with a fixed length <N>.
const_iterator & operator++()
std::forward_iterator_tag iterator_category
label retain(const HashTable< AnyType, Key, AnyHash > &other)
Retain table entries given by keys of the other hash-table.
bool found(const Key &key) const
Same as contains()
this_type::value_type value_type
Internal storage type for HashSet entries.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
this_type::reference reference
iterator()=default
Default construct (end iterator)
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
label size_type
The type that can represent the size of a HashTable.
Hash hasher
The third template parameter, the hash index method.
UPtrList< const node_type > csorted() const
Const access to the hash-table contents in sorted order (sorted by keys).
HashTable< T, Key, Hash > this_type
The template instance used for this HashTable.
void setCapacity(label newCapacity)
Change the hash table capacity (number of buckets).
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
const_reference val() const
Const access to referenced object (value)
std::conditional< std::is_same< Foam::zero, typename std::remove_cv< T >::type >::value, Detail::HashTableSingle< Key >, Detail::HashTablePair< Key, T > >::type node_type
A table entry (node) that encapsulates the key/val tuple with an additional linked-list entry for has...
HashTable() noexcept
Default construct: empty without allocation (capacity=0)
iterator end() noexcept
iterator to signal the end (for any HashTable)
const_reference operator*() const
Const access to referenced object (value)
UPtrList< node_type > sorted()
Non-const access to the hash-table contents in sorted order (sorted by keys).
const_iterator & operator=(const const_iterator &)=default
Copy assignment.
bool operator==(const this_type &rhs) const
Equality. Tables are equal if all keys and values are equal, independent of order or underlying stora...
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...
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
T * pointer
Pointer type for storing into value_type objects.
Forward iterator with non-const access.
const node_type * node() const noexcept
Const access to the entry (node)
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
label size() const noexcept
The number of elements in table.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
label countEntries(const BinaryPredicate &pred, const bool invert=false) const
Count the number of entries that satisfy the binary predicate.
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
reference operator*() const
Const access to referenced object (value)
reference operator()() const
label capacity() const noexcept
The size of the underlying table (the number of buckets)
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Write unordered keys (list), with line-breaks when length exceeds shortLen.
void operator=(const this_type &rhs)
Copy assign.
A class for handling words, derived from Foam::string.
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
label filterEntries(const BinaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their key/value.
Ostream & printInfo(Ostream &os) const
Print information.
Istream & operator>>(Istream &, directionInfo &)
void clear()
Remove all entries from table.
this_type::node_type node_type
constexpr key_iterator_base() noexcept
Default construct (end iterator)
T & operator()(const Key &key)
Return existing entry or create a new entry.
this_type::const_pointer const_pointer
T mapped_type
The first template parameter, type of objects contained.
bool found() const noexcept
True if iterator points to an entry - same as good()
this_type::pointer pointer
label difference_type
The type to represent the difference between two iterators.
iterator begin()
iterator set to the beginning of the HashTable
const_iterator()=default
Default construct (end iterator)
this_type::key_type key_type
label countKeys(const UnaryPredicate &pred, const bool invert=false) const
Count the number of keys that satisfy the unary predicate.
A HashTable similar to std::unordered_map.
const Key & key() const
The key associated with the iterator.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
key_iterator_base & operator++()
void resize(label newCapacity)
Rehash the hash table with new number of buckets. Currently identical to setCapacity() ...
typename std::conditional< Const, const this_type, this_type >::type table_type
The HashTable container type.
const T & const_reference
Const reference to the stored value_type.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
bool empty() const noexcept
True if the hash table is empty.
An iterator wrapper for returning a reference to the key.
this_type::const_reference const_reference
T value_type
Same as mapped_type for OpenFOAM HashTables.
Key key_type
The second template parameter, type of keys used.
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool operator!=(const Iterator< Any > &iter) const noexcept
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
std::forward_iterator_tag iterator_category
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
table_type * container_
The hash-table container being iterated on.
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Bits that are independent of HashTable template parameters.
constexpr Iterator() noexcept
Default construct. Also the same as the end iterator.
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
void merge(HashTable< T, Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
T & reference
Reference to the stored value_type.
Internal storage type for HashTable entries.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
void swap(HashTable< T, Key, Hash > &rhs) noexcept
Swap contents into this table.
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
this_type::difference_type difference_type
friend class Iterator< false >
Allow iterator access to HashTable internals.
const T * const_pointer
Const pointer type for the stored value_type.
Ostream & print(Ostream &os) const
Write the (key, val) pair.
List< Key > toc() const
The table of contents (the keys) in unsorted order.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
this_type::mapped_type mapped_type
bool good() const noexcept
True if iterator points to an entry.
Foam::argList args(argc, argv)
bool operator==(const Iterator< Any > &iter) const noexcept
Compare hash-entry element pointers.
const_iterator_pair< const_key_iterator, this_type > keys() const
A const iterator begin/end pair for iterating over keys.
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
A keyword and a list of tokens is an 'entry'.
void clearStorage()
Remove all entries from table and the table itself.
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
reference operator()() const
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...