HashPtrTable< T, Key, Hash > Class Template Reference

A HashTable of pointers to objects of type <T>, with deallocation management of the pointers. More...

Inheritance diagram for HashPtrTable< T, Key, Hash >:
Collaboration diagram for HashPtrTable< T, Key, Hash >:

Public Types

typedef HashPtrTable< T, Key, Hashthis_type
 The template instance used for this table. More...
 
typedef HashTable< T *, Key, Hashparent_type
 The template instance used for the parent HashTable. More...
 
using iterator = typename parent_type::iterator
 
using const_iterator = typename parent_type::const_iterator
 
- Public Types inherited from HashTable< T *, Key, Hash >
typedef HashTable< T *, Key, Hashthis_type
 The template instance used for this HashTable. More...
 
typedef 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 hash collisions. More...
 
typedef Key key_type
 The second template parameter, type of keys used. More...
 
typedef Tmapped_type
 The first template parameter, type of objects contained. More...
 
typedef Tvalue_type
 Same as mapped_type for OpenFOAM HashTables. More...
 
typedef Hash hasher
 The third template parameter, the hash index method. More...
 
typedef T * * pointer
 Pointer type for storing into value_type objects. More...
 
typedef T * & reference
 Reference to the stored value_type. More...
 
typedef const T * * const_pointer
 Const pointer type for the stored value_type. More...
 
typedef const T * & const_reference
 Const reference to the stored value_type. More...
 
typedef label difference_type
 The type to represent the difference between two iterators. More...
 
typedef label size_type
 The type that can represent the size of a HashTable. More...
 
using key_iterator = key_iterator_base< iterator >
 Forward iterator returning the key. More...
 
using const_key_iterator = key_iterator_base< const_iterator >
 Forward const iterator returning the key. More...
 

Public Member Functions

 HashPtrTable ()=default
 Default construct with default table capacity. More...
 
 HashPtrTable (const Foam::zero) noexcept
 Construct with zero table capacity. More...
 
 HashPtrTable (const label initialCapacity)
 Construct given initial table capacity. More...
 
 HashPtrTable (const this_type &rhs)
 Copy construct, making a copy of each element. More...
 
 HashPtrTable (this_type &&rhs) noexcept
 Move construct. More...
 
template<class INew >
 HashPtrTable (Istream &is, const INew &inew)
 Construct from Istream using given Istream constructor class. More...
 
 HashPtrTable (Istream &is)
 Construct from Istream using default Istream constructor class. More...
 
 HashPtrTable (const dictionary &dict)
 Construct from dictionary with default dictionary constructor class. More...
 
 ~HashPtrTable ()
 Destructor. More...
 
const Ttest (const Key &key) const
 Return const pointer associated with given entry or a nullptr if the key does not exist in the table. More...
 
const Tget (const Key &key) const
 Return const pointer associated with given entry or a nullptr if the key does not exist in the table. More...
 
autoPtr< Trelease (iterator &iter)
 Release ownership of the pointer and replace with a nullptr. More...
 
autoPtr< Trelease (const Key &key)
 Release ownership of the pointer and replace with a nullptr. More...
 
autoPtr< Tremove (iterator &iter)
 Remove entry specified by given iterator. More...
 
autoPtr< Tremove (const Key &key)
 Remove entry specified by given key. More...
 
bool erase (iterator &iter)
 Erase entry specified by given iterator and delete the allocated pointer. More...
 
bool erase (const Key &key)
 Erase entry specified by given key and delete the allocated pointer. More...
 
void clear ()
 Clear all entries from table and delete any allocated pointers. More...
 
void merge (HashPtrTable< T, Key, Hash > &source)
 Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged. More...
 
void merge (HashPtrTable< T, Key, Hash > &&source)
 Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged. More...
 
void write (Ostream &os) const
 Invoke write() on each non-null entry. More...
 
void operator= (const this_type &rhs)
 Copy assignment. More...
 
void operator= (this_type &&rhs)
 Move assignment. More...
 
template<class... Args>
bool emplace (const Key &key, Args &&... args)
 Emplace insert a new entry, not overwriting existing entries. More...
 
template<class... Args>
Templace_set (const Key &key, Args &&... args)
 Emplace set an entry, overwriting any existing entries. More...
 
template<class... Args>
Ttry_emplace (const Key &key, Args &&... args)
 Like emplace_set() but will not overwrite an occupied (non-null) location. More...
 
bool insert (const Key &, T *)=delete
 No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set() More...
 
bool insert (const Key &key, std::unique_ptr< T > &&ptr)
 Insert a new entry, not overwriting existing entries. More...
 
bool insert (const Key &key, autoPtr< T > &&ptr)
 Insert a new entry, not overwriting existing entries. More...
 
bool set (const Key &key, T *ptr)
 Assign a new entry, overwrites existing. More...
 
bool set (const Key &key, std::unique_ptr< T > &&ptr)
 Assign a new entry, overwrites existing. More...
 
bool set (const Key &key, autoPtr< T > &&ptr)
 Assign a new entry, overwrites existing. More...
 
bool set (const Key &key, const refPtr< T > &ptr)
 Assign a new entry from refPtr (move or clone), overwrites existing. More...
 
bool set (const Key &key, const tmp< T > &ptr)
 Assign a new entry from tmp (move or clone), overwrites existing. More...
 
bool insert (const Key &key, autoPtr< T > &ptr)
 Insert a new entry, not overwriting existing entries. More...
 
bool set (const Key &key, autoPtr< T > &ptr)
 Assign a new entry, overwrites existing. More...
 
- Public Member Functions inherited from HashTable< T *, Key, Hash >
 HashTable () noexcept
 Default construct: empty without allocation (capacity=0) More...
 
 HashTable (const Foam::zero) noexcept
 Construct empty without allocation (capacity=0) More...
 
 HashTable (const label initialCapacity)
 Construct empty with initial table capacity. More...
 
 HashTable (Istream &is)
 Construct from Istream. More...
 
 HashTable (const this_type &ht)
 Copy construct. More...
 
 HashTable (this_type &&rhs) noexcept
 Move construct. More...
 
 HashTable (std::initializer_list< std::pair< Key, T * >> list)
 Construct from an initializer list. More...
 
 ~HashTable ()
 Destructor. More...
 
bool empty () const noexcept
 True if the hash table is empty. More...
 
label size () const noexcept
 The number of elements in table. More...
 
label capacity () const noexcept
 The size of the underlying table (the number of buckets) More...
 
T * & at (const Key &key)
 Find and return a hashed entry. FatalError if it does not exist. More...
 
const T * & at (const Key &key) const
 Find and return a hashed entry. FatalError if it does not exist. More...
 
bool contains (const Key &key) const
 True if hashed key is contained (found) in table. More...
 
iterator find (const Key &key)
 Find and return an iterator set at the hashed entry. More...
 
const_iterator find (const Key &key) const
 Find and return an const_iterator set at the hashed entry. More...
 
const_iterator cfind (const Key &key) const
 Find and return an const_iterator set at the hashed entry. More...
 
const T * & lookup (const Key &key, const T * &deflt) const
 Return hashed entry if it exists, or return the given default. More...
 
List< Key > toc () const
 The table of contents (the keys) in unsorted order. More...
 
List< Key > sortedToc () const
 The table of contents (the keys) in sorted order. More...
 
List< Key > sortedToc (const Compare &comp) const
 The table of contents (the keys) sorted according to the specified comparator. More...
 
Foam::List< Key > sortedToc (const Compare &comp) const
 
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. More...
 
Foam::List< Key > tocKeys (const UnaryPredicate &pred, const bool invert) 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. More...
 
Foam::List< Key > tocValues (const UnaryPredicate &pred, const bool invert) const
 
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 values. More...
 
Foam::List< Key > tocEntries (const BinaryPredicate &pred, const bool invert) const
 
UPtrList< const node_typecsorted () const
 Const access to the hash-table contents in sorted order (sorted by keys). More...
 
UPtrList< node_typesorted ()
 Non-const access to the hash-table contents in sorted order (sorted by keys). More...
 
UPtrList< const node_typesorted () const
 Deprecated(2023-07) use csorted() method. More...
 
label countKeys (const UnaryPredicate &pred, const bool invert=false) const
 Count the number of keys that satisfy the unary predicate. More...
 
Foam::label countKeys (const UnaryPredicate &pred, const bool invert) const
 
label countValues (const UnaryPredicate &pred, const bool invert=false) const
 Count the number of values that satisfy the unary predicate. More...
 
Foam::label countValues (const UnaryPredicate &pred, const bool invert) const
 
label countEntries (const BinaryPredicate &pred, const bool invert=false) const
 Count the number of entries that satisfy the binary predicate. More...
 
Foam::label countEntries (const BinaryPredicate &pred, const bool invert) const
 
bool emplace (const Key &key, Args &&... args)
 Emplace insert a new entry, not overwriting existing entries. More...
 
bool emplace_set (const Key &key, Args &&... args)
 Emplace set an entry, overwriting any existing entries. More...
 
bool insert (const Key &key, const T * &obj)
 Copy insert a new entry, not overwriting existing entries. More...
 
bool insert (const Key &key, T * &&obj)
 Move insert a new entry, not overwriting existing entries. More...
 
bool set (const Key &key, const T * &obj)
 Copy assign a new entry, overwriting existing entries. More...
 
bool set (const Key &key, T * &&obj)
 Move assign a new entry, overwriting existing entries. More...
 
bool erase (const iterator &iter)
 Erase an entry specified by given iterator. More...
 
bool erase (const Key &key)
 Erase an entry specified by the given key. More...
 
label erase (const HashTable< AnyType, Key, AnyHash > &other)
 Remove table entries given by keys of the other hash-table. More...
 
label erase (std::initializer_list< Key > keys)
 Remove table entries given by the listed keys. More...
 
label erase (InputIter first, InputIter last)
 Remove multiple entries using an iterator range of keys. More...
 
label erase (const FixedList< Key, N > &keys)
 Remove table entries given by the listed keys. More...
 
label erase (const UList< Key > &keys)
 Remove table entries given by the listed keys. More...
 
Foam::label erase (InputIter first, InputIter last)
 
Foam::label erase (const FixedList< Key, N > &keys)
 
Foam::label erase (const HashTable< AnyType, Key, AnyHash > &other)
 
label retain (const HashTable< AnyType, Key, AnyHash > &other)
 Retain table entries given by keys of the other hash-table. More...
 
Foam::label retain (const HashTable< AnyType, Key, AnyHash > &other)
 
label filterKeys (const UnaryPredicate &pred, const bool pruning=false)
 Generalized means to filter table entries based on their keys. More...
 
Foam::label filterKeys (const UnaryPredicate &pred, const bool pruning)
 
label filterValues (const UnaryPredicate &pred, const bool pruning=false)
 Generalized means to filter table entries based on their values. More...
 
Foam::label filterValues (const UnaryPredicate &pred, const bool pruning)
 
label filterEntries (const BinaryPredicate &pred, const bool pruning=false)
 Generalized means to filter table entries based on their key/value. More...
 
Foam::label filterEntries (const BinaryPredicate &pred, const bool pruning)
 
void clear ()
 Remove all entries from table. More...
 
void clearStorage ()
 Remove all entries from table and the table itself. More...
 
void setCapacity (label newCapacity)
 Change the hash table capacity (number of buckets). More...
 
void resize (label newCapacity)
 Rehash the hash table with new number of buckets. Currently identical to setCapacity() More...
 
void reserve (label numEntries)
 Reserve space for at least the specified number of elements (not the number of buckets) and regenerates the hash table. More...
 
void swap (HashTable< T *, Key, Hash > &rhs) noexcept
 Swap contents into this table. More...
 
void transfer (HashTable< T *, Key, Hash > &rhs)
 Transfer contents into this table. More...
 
void merge (HashTable< T *, Key, Hash > &source)
 Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged. More...
 
void merge (HashTable< T *, Key, Hash > &&source)
 Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged. More...
 
T * & operator[] (const Key &key)
 Find and return a hashed entry. FatalError if it does not exist. More...
 
const T * & operator[] (const Key &key) const
 Find and return a hashed entry. FatalError if it does not exist. More...
 
T * & operator() (const Key &key)
 Return existing entry or create a new entry. More...
 
T * & operator() (const Key &key, const T * &deflt)
 Return existing entry or insert a new entry. More...
 
void operator= (const this_type &rhs)
 Copy assign. More...
 
void operator= (std::initializer_list< std::pair< Key, T * >> rhs)
 Copy assign from an initializer list. More...
 
void operator= (this_type &&rhs)
 Move assign. More...
 
bool operator== (const this_type &rhs) const
 Equality. Tables are equal if all keys and values are equal, independent of order or underlying storage size. More...
 
bool operator!= (const this_type &rhs) const
 The opposite of the equality operation. More...
 
this_typeoperator+= (const this_type &rhs)
 Add entries into this HashTable. More...
 
const_iterator_pair< const_key_iterator, this_typekeys () const
 A const iterator begin/end pair for iterating over keys. More...
 
iterator begin ()
 iterator set to the beginning of the HashTable More...
 
const_iterator begin () const
 const_iterator set to the beginning of the HashTable More...
 
const_iterator cbegin () const
 const_iterator set to the beginning of the HashTable More...
 
iterator end () noexcept
 iterator to signal the end (for any HashTable) More...
 
const_iterator end () const noexcept
 const_iterator to signal the end (for any HashTable) More...
 
constexpr const_iterator cend () const noexcept
 const_iterator to signal the end (for any HashTable) More...
 
OstreamprintInfo (Ostream &os) const
 Print information. More...
 
OstreamwriteKeys (Ostream &os, const label shortLen=0) const
 Write unordered keys (list), with line-breaks when length exceeds shortLen. More...
 
bool found (const Key &key) const
 Same as contains() More...
 
- Public Member Functions inherited from HashTableCore
 ClassName ("HashTable")
 Declare type-name (with debug switch) More...
 
 HashTableCore () noexcept=default
 Default construct. More...
 

Friends

Istreamoperator>> (Istream &is, HashPtrTable< T, Key, Hash > &tbl)
 Clear table and read from Istream. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from HashTableCore
static label canonicalSize (const label requested_size)
 Return a canonical (power-of-two) of the requested size. More...
 
- Static Public Attributes inherited from HashTableCore
static const label maxTableSize
 Maximum allowable internal table size. Approximately labelMax/4. More...
 

Detailed Description

template<class T, class Key = word, class Hash = Foam::Hash<Key>>
class Foam::HashPtrTable< T, Key, Hash >

A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.

Source files

Definition at line 51 of file HashPtrTable.H.

Member Typedef Documentation

◆ this_type

typedef HashPtrTable<T, Key, Hash> this_type

The template instance used for this table.

Definition at line 96 of file HashPtrTable.H.

◆ parent_type

typedef HashTable<T*, Key, Hash> parent_type

The template instance used for the parent HashTable.

Definition at line 101 of file HashPtrTable.H.

◆ iterator

using iterator = typename parent_type::iterator

Definition at line 103 of file HashPtrTable.H.

◆ const_iterator

using const_iterator = typename parent_type::const_iterator

Definition at line 104 of file HashPtrTable.H.

Constructor & Destructor Documentation

◆ HashPtrTable() [1/8]

HashPtrTable ( )
default

Default construct with default table capacity.

◆ HashPtrTable() [2/8]

HashPtrTable ( const Foam::zero  )
inlineexplicitnoexcept

Construct with zero table capacity.

Definition at line 117 of file HashPtrTable.H.

◆ HashPtrTable() [3/8]

HashPtrTable ( const label  initialCapacity)
inlineexplicit

Construct given initial table capacity.

Definition at line 125 of file HashPtrTable.H.

◆ HashPtrTable() [4/8]

HashPtrTable ( const this_type rhs)

Copy construct, making a copy of each element.

◆ HashPtrTable() [5/8]

HashPtrTable ( this_type &&  rhs)
inlinenoexcept

Move construct.

Definition at line 138 of file HashPtrTable.H.

◆ HashPtrTable() [6/8]

HashPtrTable ( Istream is,
const INew inew 
)

Construct from Istream using given Istream constructor class.

Definition at line 152 of file HashPtrTableIO.C.

◆ HashPtrTable() [7/8]

HashPtrTable ( Istream is)

Construct from Istream using default Istream constructor class.

Definition at line 159 of file HashPtrTableIO.C.

◆ HashPtrTable() [8/8]

HashPtrTable ( const dictionary dict)
explicit

Construct from dictionary with default dictionary constructor class.

Definition at line 166 of file HashPtrTableIO.C.

◆ ~HashPtrTable()

Destructor.

Definition at line 56 of file HashPtrTable.C.

Member Function Documentation

◆ test()

const T * test ( const Key &  key) const
inline

Return const pointer associated with given entry or a nullptr if the key does not exist in the table.

Definition at line 28 of file HashPtrTableI.H.

◆ get()

const T * get ( const Key &  key) const
inline

Return const pointer associated with given entry or a nullptr if the key does not exist in the table.

Definition at line 41 of file HashPtrTableI.H.

◆ release() [1/2]

Foam::autoPtr< T > release ( iterator iter)

Release ownership of the pointer and replace with a nullptr.

Includes a safeguard against the end-iterator.

Returns
the entry's old value as an encapsulated pointer.

Definition at line 65 of file HashPtrTable.C.

◆ release() [2/2]

Foam::autoPtr< T > release ( const Key &  key)

Release ownership of the pointer and replace with a nullptr.

Returns
the entry's old value as an encapsulated pointer.

Definition at line 79 of file HashPtrTable.C.

◆ remove() [1/2]

Foam::autoPtr< T > remove ( iterator iter)

Remove entry specified by given iterator.

Includes a safeguard against the end-iterator.

Returns
the entry's old value as an encapsulated pointer.

Definition at line 87 of file HashPtrTable.C.

◆ remove() [2/2]

Foam::autoPtr< T > remove ( const Key &  key)

Remove entry specified by given key.

Returns
the entry's old value as an encapsulated pointer.

Definition at line 101 of file HashPtrTable.C.

◆ erase() [1/2]

bool erase ( iterator iter)

Erase entry specified by given iterator and delete the allocated pointer.

Includes a safeguard against the end-iterator.

Returns
True if item was removed

Definition at line 109 of file HashPtrTable.C.

◆ erase() [2/2]

bool erase ( const Key &  key)

Erase entry specified by given key and delete the allocated pointer.

Returns
True if item was removed

Definition at line 127 of file HashPtrTable.C.

◆ clear()

void clear ( )

Clear all entries from table and delete any allocated pointers.

Definition at line 135 of file HashPtrTable.C.

◆ merge() [1/2]

void merge ( HashPtrTable< T, Key, Hash > &  source)

Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged.

Definition at line 148 of file HashPtrTable.C.

◆ merge() [2/2]

void merge ( HashPtrTable< T, Key, Hash > &&  source)

Attempts to extract entries from source parameter and insert them into this, does not overwrite existing entries. The source will contains any items that could not be merged.

Definition at line 160 of file HashPtrTable.C.

◆ write()

void write ( Ostream os) const

Invoke write() on each non-null entry.

Definition at line 135 of file HashPtrTableIO.C.

◆ operator=() [1/2]

void operator= ( const this_type rhs)

Copy assignment.

◆ operator=() [2/2]

void operator= ( this_type &&  rhs)

Move assignment.

◆ emplace()

bool emplace ( const Key &  key,
Args &&...  args 
)
inline

Emplace insert a new entry, not overwriting existing entries.

Returns
True if the entry did not previously exist in the table.

Definition at line 56 of file HashPtrTableI.H.

◆ emplace_set()

T & emplace_set ( const Key &  key,
Args &&...  args 
)
inline

Emplace set an entry, overwriting any existing entries.

Returns
Reference to the new element.

Definition at line 109 of file HashPtrTableI.H.

◆ try_emplace()

T & try_emplace ( const Key &  key,
Args &&...  args 
)
inline

Like emplace_set() but will not overwrite an occupied (non-null) location.

Parameters
key- the location to set (unless already defined)
argsarguments to forward to the constructor of the element
Returns
reference to the existing or the new element.

Definition at line 122 of file HashPtrTableI.H.

◆ insert() [1/4]

bool insert ( const Key &  ,
T  
)
inlinedelete

No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set()

Referenced by HashPtrTable< exprResult >::insert().

Here is the caller graph for this function:

◆ insert() [2/4]

bool insert ( const Key &  key,
std::unique_ptr< T > &&  ptr 
)
inline

Insert a new entry, not overwriting existing entries.

Returns
True if the entry inserted (not previously in table)

Definition at line 134 of file HashPtrTableI.H.

◆ insert() [3/4]

bool insert ( const Key &  key,
autoPtr< T > &&  ptr 
)
inline

Insert a new entry, not overwriting existing entries.

Returns
True if the entry inserted (not previously in table)

Definition at line 151 of file HashPtrTableI.H.

◆ set() [1/6]

◆ set() [2/6]

bool set ( const Key &  key,
std::unique_ptr< T > &&  ptr 
)
inline

Assign a new entry, overwrites existing.

Definition at line 192 of file HashPtrTableI.H.

◆ set() [3/6]

bool set ( const Key &  key,
autoPtr< T > &&  ptr 
)
inline

Assign a new entry, overwrites existing.

Definition at line 203 of file HashPtrTableI.H.

◆ set() [4/6]

bool set ( const Key &  key,
const refPtr< T > &  ptr 
)
inline

Assign a new entry from refPtr (move or clone), overwrites existing.

Definition at line 214 of file HashPtrTableI.H.

◆ set() [5/6]

bool set ( const Key &  key,
const tmp< T > &  ptr 
)
inline

Assign a new entry from tmp (move or clone), overwrites existing.

Definition at line 225 of file HashPtrTableI.H.

◆ insert() [4/4]

bool insert ( const Key &  key,
autoPtr< T > &  ptr 
)
inline

Insert a new entry, not overwriting existing entries.

Returns
True if the entry inserted (not previously in table)

Definition at line 373 of file HashPtrTable.H.

◆ set() [6/6]

bool set ( const Key &  key,
autoPtr< T > &  ptr 
)
inline

Assign a new entry, overwrites existing.

Definition at line 381 of file HashPtrTable.H.

Friends And Related Function Documentation

◆ operator>>

Istream& operator>> ( Istream is,
HashPtrTable< T, Key, Hash > &  tbl 
)
friend

Clear table and read from Istream.


The documentation for this class was generated from the following files: