aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::HashTable< Key, Val > Class Template Reference

The class for generic Hash Tables. More...

#include <agrum/base/core/hashTable.h>

Collaboration diagram for gum::HashTable< Key, Val >:

Public Types

using Bucket = HashTableBucket< Key, Val >
 The buckets where data are stored.
using key_type = Key
 Types for STL compliance.
using mapped_type = Val
 Types for STL compliance.
using value_type = std::pair< const Key, Val >
 Types for STL compliance.
using reference = value_type&
 Types for STL compliance.
using const_reference = const value_type&
 Types for STL compliance.
using pointer = value_type*
 Types for STL compliance.
using const_pointer = const value_type*
 Types for STL compliance.
using size_type = Size
 Types for STL compliance.
using difference_type = std::ptrdiff_t
 Types for STL compliance.
using iterator = HashTableIterator< Key, Val >
 Types for STL compliance.
using const_iterator = HashTableConstIterator< Key, Val >
 Types for STL compliance.
using iterator_safe = HashTableIteratorSafe< Key, Val >
 Types for STL compliance.
using const_iterator_safe = HashTableConstIteratorSafe< Key, Val >
 Types for STL compliance.

Public Member Functions

template<typename... Args>
INLINE HashTable< Key, Val >::value_typeemplace (Args &&... args)
template<typename Mount>
HashTable< Key, Mount > INLINE map (Mount(*f)(Val), Size size, bool resize_pol, bool key_uniqueness_pol) const
template<typename Mount>
HashTable< Key, Mount > INLINE map (Mount(*f)(Val &), Size size, bool resize_pol, bool key_uniqueness_pol) const
template<typename Mount>
HashTable< Key, Mount > INLINE map (Mount(*f)(const Val &), Size size, bool resize_pol, bool key_uniqueness_pol) const
template<typename Mount>
HashTable< Key, Mount > INLINE map (const Mount &val, Size size, bool resize_pol, bool key_uniqueness_pol) const
Constructors / Destructors
 HashTable (Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
 Default constructor.
 HashTable (std::initializer_list< std::pair< Key, Val > > list)
 Initializer list constructor.
 HashTable (const HashTable< Key, Val > &from)
 Copy constructor.
 HashTable (HashTable< Key, Val > &&from)
 Move constructor.
 ~HashTable ()
 Class destructor.
Iterators
const iteratorend () noexcept
 Returns the unsafe iterator pointing to the end of the hashtable.
const const_iteratorend () const noexcept
 Returns the unsafe const_iterator pointing to the end of the hashtable.
const const_iteratorcend () const noexcept
 Returns the unsafe const_iterator pointing to the end of the hashtable.
iterator begin ()
 Returns an unsafe iterator pointing to the beginning of the hashtable.
const_iterator begin () const
 Returns an unsafe const_iterator pointing to the beginning of the hashtable.
const_iterator cbegin () const
 Returns an unsafe const_iterator pointing to the beginning of the hashtable.
const iterator_safeendSafe () noexcept
 Returns the safe iterator pointing to the end of the hashtable.
const const_iterator_safeendSafe () const noexcept
 Returns the safe const_iterator pointing to the end of the hashtable.
const const_iterator_safecendSafe () const noexcept
 Returns the safe const_iterator pointing to the end of the hashtable.
iterator_safe beginSafe ()
 Returns the safe iterator pointing to the beginning of the hashtable.
const_iterator_safe beginSafe () const
 Returns the safe const_iterator pointing to the beginning of the hashtable.
const_iterator_safe cbeginSafe () const
 Returns the safe const_iterator pointing to the beginning of the hashtable.
Operators
HashTable< Key, Val > & operator= (const HashTable< Key, Val > &from)
 Copy operator.
HashTable< Key, Val > & operator= (HashTable< Key, Val > &&from)
 Move operator.
Val & operator[] (const Key &key)
 Returns a reference on the value the key of which is passed in argument.
const Val & operator[] (const Key &key) const
 returns a reference on the value the key of which is passed in argument
bool operator== (const HashTable< Key, Val > &from) const
 Checks whether two hashtables contain the same elements.
bool operator!= (const HashTable< Key, Val > &from) const
 Checks whether two hashtables contain different sets of elements.
Fine tuning
Size capacity () const noexcept
 Returns the number of slots in the 'nodes' vector of the hashtable.
void resize (Size new_size)
 Changes the number of slots in the 'nodes' vector of the hash table.
void setResizePolicy (const bool new_policy) noexcept
 Enables the user to change dynamically the resizing policy.
bool resizePolicy () const noexcept
 Returns the current resizing policy.
void setKeyUniquenessPolicy (const bool new_policy) noexcept
 Enables the user to change dynamically the policy for checking whether there can exist several elements in the table with identical keys.
bool keyUniquenessPolicy () const noexcept
 Returns the current checking policy.
Accessors / Modifiers
Size size () const noexcept
 Returns the number of elements stored into the hashtable.
bool exists (const Key &key) const
 Checks whether there exists an element with a given key in the hashtable.
value_typeinsert (const Key &key, const Val &val)
 Adds a new element (actually a copy of this element) into the hash table.
value_typeinsert (Key &&key, Val &&val)
 Moves a new element in the hash table.
value_typeinsert (const std::pair< Key, Val > &elt)
 Adds a new element (actually a copy of this element) into the hash table.
value_typeinsert (std::pair< Key, Val > &&elt)
 Moves a new element in the hash table.
template<typename... Args>
value_typeemplace (Args &&... args)
 Emplace a new element into the hashTable.
mapped_typegetWithDefault (const Key &key, const Val &default_value)
 Returns a reference on the element the key of which is passed in argument.
mapped_typegetWithDefault (Key &&key, Val &&default_value)
 Returns a reference on the element the key of which is passed in argument.
void set (const Key &key, const Val &default_value)
 Add a new property or modify it if it already existed.
void reset (const Key &key)
 Removes a property (i.e., remove an element).
void erase (const Key &key)
 Removes a given element from the hash table.
void erase (const iterator_safe &iter)
 Removes a given element from the hash table.
void erase (const const_iterator_safe &iter)
 Removes a given element from the hash table.
void eraseByVal (const Val &val)
 Removes a given element from the hash table.
const Key & keyByVal (const Val &val) const
 Returns a reference on the key given a value.
const Key & key (const Key &key) const
 Returns a reference on a given key.
void eraseAllVal (const Val &val)
 Removes all the elements having a certain value from the hash table.
void clear ()
 Removes all the elements in the hash table.
bool empty () const noexcept
 Indicates whether the hash table is empty.
template<typename Mount>
HashTable< Key, Mount > map (Mount(*f)(Val), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
 Transforms a hashtable of vals into a hashtable of mountains.
template<typename Mount>
HashTable< Key, Mount > map (Mount(*f)(Val &), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
 Transforms a hashtable of vals into a hashtable of mountains.
template<typename Mount>
HashTable< Key, Mount > map (Mount(*f)(const Val &), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
 Transforms a hashtable of vals into a hashtable of mountains.
template<typename Mount>
HashTable< Key, Mount > map (const Mount &val, Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
 Creates a hashtable of mounts with a given value from a hashtable of vals.

Private Member Functions

void _erase_ (HashTableBucket< Key, Val > *bucket, Size index)
 Erases a given bucket.
void _copy_ (const HashTable< Key, Val > &table)
 A function used to perform copies of HashTables.
void _create_ (Size size)
 Used by all default constructors (general and specialized).
void _clearIterators_ ()
 Clear all the safe iterators.
void _insert_ (Bucket *bucket)
 Adds a new element (actually a copy of this element) in the hash table.

Private Attributes

std::vector< HashTableList< Key, Val > > _nodes_
 The hash table is represented as a vector of chained lists.
Size _size_
 The number of nodes in vector ' __nodes'.
Size _nb_elements_ {Size(0)}
 Number of elements of type Val stored in the hash table.
HashFunc< Key > _hash_func_
 The function used to hash keys (may change when the table is resized).
bool _resize_policy_ {true}
 Is resizing performed automatically?
bool _key_uniqueness_policy_ {true}
 Shall we check for key uniqueness in the table?
Size _begin_index_ {std::numeric_limits< Size >::max()}
 Returns where the begin index should be.
std::vector< HashTableConstIteratorSafe< Key, Val > * > _safe_iterators_
 The list of safe iterators pointing to the hash table.

Friends

class HashTableIterator< Key, Val >
 Friends to optimize the access to data, iterators must be friends.
class HashTableConstIterator< Key, Val >
 Friends to optimize the access to data, iterators must be friends.
class HashTableIteratorSafe< Key, Val >
 Friends to optimize the access to data, iterators must be friends.
class HashTableConstIteratorSafe< Key, Val >
 Friends to optimize the access to data, iterators must be friends.
template<typename T1, typename T2>
class Bijection
 For bijections to quickly access data.
std::ostream & operator<< (std::ostream &s, const HashTable< Key, Val > &table)
 Prints the content of a gum::HashTable in the stream.
std::ostream & operator<< (std::ostream &s, const HashTable< Key *, Val > &table)
 Prints the content of a gum::HashTable with pointers key in the stream.

Detailed Description

template<typename Key, typename Val>
class gum::HashTable< Key, Val >

The class for generic Hash Tables.

In aGrUM, a hashtable is a vector of chained lists (collision problems are fixed by chaining). Each slot of the vector contains a list of elements sharing the same hashed value. To be computationally efficient, the hash table should not contain too many elements as compared to its number of slots. Therefore, it is sometimes useful to resize the chained lists vector. aGrUM's hash tables are designed to automatically double their size when there is in average more than 3 elements per slot. However, when memory consumption is a concern, this feature can be turned off either by passing false as an optional resize_pol argument to the constructor of the hash table or by using method setResizePolicy when the instance of the class has already been constructed. Similarly, the default number of slots of the hash table may be parameterized as an optional argument of the constructor (size_param). Beware: when inserting elements of a given class into a hash table, unless the element is an r-value, only a copy of this element is stored into the table (this is compulsory if the hashtable is to be generic and can be used to store both complex classes and built-in types like integers). HashTable have different kinds of iterators: HashTableIteratorSafe and HashTableConstIteratorSafe (a.k.a. HashTable<>::iterator_safe and HashTable<>::const_iterator_safe) allow safe parsing of the hash tables. By safe, we mean that whenever the element pointed to by such an iterator is removed from the hashtable, accessing it through the iterator (*iter) does not result in a segmentation fault but rather in an exception being thrown. This safety is ensured at a very low cost (actually, our experiments show that our HashTables and HashTable's safe iterators significantly outperform the standard library unordered_maps). Of course, if there is no possibility for an iterator to point to a deleted element, the user can use "unsafe" iterators HashTableIterator and HashTableConstIterator (a.k.a. HashTable<>::iterator and HashTable<>::const_iterator). These iterators are slightly faster than their safe counterparts. However, as in the standard library, accessing through them a deleted element usually results in a mess (most probably a segfault).

Warning
HashTables guarantee that any element stored within them will have the same location in memory until it is removed from the hashtable (and this holds whatever operation is performed on the hashtable like new insertions, deletions, resizing, etc.).
Usage example:
// creation of an empty hash table
// insert two elements into the hash table
table1.insert (10,"xxx");
table1.insert (20,"yyy");
table1.emplace (30,"zzz");
// creation of a nonempty hashtable using initializer lists
HashTable<int,bool> table { std::make_pair(3,true), std::make_pair(2,false)
};
// display the content of the hash table
cerr << table1;
// get the number of elements stored into the hash table
cerr << "number of elements in table1 = " << table1.size () << endl;
// create two copies of the hash table
HashTable<int,string> table2, table3 = table1;
table2 = table3;
// get the element whose key is 10
cerr << table1[10] << " = xxx" << endl;
// check whether there exists an element with key 20
if (table1.exists (20)) cerr << "element found" << endl;
// transform the hashtable of string into a hashtable of int assuming f is
// defined as: int f (const string& str) { return str.size (); }
HashTable<int,int> table = table1.map (f);
// remove two elements from table1 and table2
table1.erase (10); // key = 10
table1.eraseByVal ("yyy"); // val = "yyy"
table2.clear ();
// check whether the hash table is empty
if (!table1.empty ()) cerr << "table not empty" << endl;
// check whether hashtables contain the same elements
if ((table1 == table2) && (table1 != table3))
cerr << "check for equality/inequality" << endl;
// parse the content of a hashtable using an unsafe iterator
for (HashTable<int,string>::const_iterator iter = table1.cbegin();
iter != table1.cend(); ++iter)
cerr << *iter;
HashTable<int,string>::iterator iter = table1.begin();
iter += 2;
cerr << iter.key () << " " << iter.val ();
// use an iterator to point the element we wish to erase
HashTable<int,string>::iterator_safe iterS = table1.beginSafe ();
table1.erase ( table1.beginSafe () + 4 );
table1.erase ( iterS ); // this is safe because the iterator is safe
// check for iterator's safety
for (HashTable<int,string>::iterator_safe iter = table1.beginSafe ();
iter != table1.endSafe (); ++iter )
table1.eraseByVal ( *iter );
The class for generic Hash Tables.
Definition hashTable.h:637
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition hashTable.h:650
HashTable< Key, Mount > map(Mount(*f)(Val), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
Transforms a hashtable of vals into a hashtable of mountains.
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition hashTable.h:651
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void clear()
Removes all the elements in the hash table.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
HashTable(Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
Default constructor.
value_type & emplace(Args &&... args)
Emplace a new element into the hashTable.
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition hashTable.h:652
Template Parameters
KeyThe type for keys in a gum::HashTable.
ValThe type for values in a gum::HashTable.

Definition at line 637 of file hashTable.h.

Member Typedef Documentation

◆ Bucket

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::Bucket = HashTableBucket< Key, Val >

The buckets where data are stored.

Definition at line 657 of file hashTable.h.

◆ const_iterator

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::const_iterator = HashTableConstIterator< Key, Val >

Types for STL compliance.

Definition at line 651 of file hashTable.h.

◆ const_iterator_safe

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::const_iterator_safe = HashTableConstIteratorSafe< Key, Val >

Types for STL compliance.

Definition at line 653 of file hashTable.h.

◆ const_pointer

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::const_pointer = const value_type*

Types for STL compliance.

Definition at line 647 of file hashTable.h.

◆ const_reference

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::const_reference = const value_type&

Types for STL compliance.

Definition at line 645 of file hashTable.h.

◆ difference_type

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 649 of file hashTable.h.

◆ iterator

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::iterator = HashTableIterator< Key, Val >

Types for STL compliance.

Definition at line 650 of file hashTable.h.

◆ iterator_safe

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::iterator_safe = HashTableIteratorSafe< Key, Val >

Types for STL compliance.

Definition at line 652 of file hashTable.h.

◆ key_type

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::key_type = Key

Types for STL compliance.

Definition at line 641 of file hashTable.h.

◆ mapped_type

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::mapped_type = Val

Types for STL compliance.

Definition at line 642 of file hashTable.h.

◆ pointer

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::pointer = value_type*

Types for STL compliance.

Definition at line 646 of file hashTable.h.

◆ reference

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::reference = value_type&

Types for STL compliance.

Definition at line 644 of file hashTable.h.

◆ size_type

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::size_type = Size

Types for STL compliance.

Definition at line 648 of file hashTable.h.

◆ value_type

template<typename Key, typename Val>
using gum::HashTable< Key, Val >::value_type = std::pair< const Key, Val >

Types for STL compliance.

Definition at line 643 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTable() [1/4]

template<typename Key, typename Val>
gum::HashTable< Key, Val >::HashTable ( Size size_param = HashTableConst::default_size,
bool resize_pol = HashTableConst::default_resize_policy,
bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy )
explicit

Default constructor.

The true capacity (vector's size) of the hashtable will be the lowest number greater than or equal to size_param that is also a power of 2. The second optional argument is the resizing policy. By default, each time there is an average of 3 elements by node, the size of the hashtable is automatically multiplied by 2. But the user may pass false as argument to resize_pol to disable this feature.

Parameters
size_paramThe initial size of the gum::HashTable.
resize_polThe policy for resizing the hashtable when new elements are added (possible values: true = automatic resize and false = manual resize).
key_uniqueness_polUniqueness policy : should we prevent inserting the same key more than once in the table?

Definition at line 302 of file hashTable_tpl.h.

302 :
303 // size must be >= 2 else we lose all the bits of the hash function
306 // for debugging purposes
308
309 // finalize the creation
311 }
bool _resize_policy_
Is resizing performed automatically?
Definition hashTable.h:1481
void _create_(Size size)
Used by all default constructors (general and specialized).
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition hashTable.h:1484
Size _size_
The number of nodes in vector ' __nodes'.
Definition hashTable.h:1472

References HashTable(), _create_(), gum::_hashTableLog2_(), _key_uniqueness_policy_, _resize_policy_, and _size_.

Referenced by HashTable(), HashTable(), HashTable(), HashTable(), ~HashTable(), _copy_(), operator!=(), operator=(), operator=(), and operator==().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HashTable() [2/4]

template<typename Key, typename Val>
gum::HashTable< Key, Val >::HashTable ( std::initializer_list< std::pair< Key, Val > > list)
explicit

Initializer list constructor.

Parameters
listThe initialized list.

Definition at line 314 of file hashTable_tpl.h.

314 :
315 // size must be >= 2 else we lose all the bits of the hash function
317 // for debugging purposes
319
320 // setup the _nodes_ vector (contains only empty lists)
322
323 // insert all the elements
324 for (const auto& elt: list) {
325 insert(elt);
326 }
327 }

References HashTable(), _create_(), gum::_hashTableLog2_(), _size_, insert(), and size().

Here is the call graph for this function:

◆ HashTable() [3/4]

template<typename Key, typename Val>
gum::HashTable< Key, Val >::HashTable ( const HashTable< Key, Val > & from)

Copy constructor.

This creates a new hashtable the content of which is similar to that of the table passed in argument. Beware: similar does not mean that both tables share the same objects, but rather that the objects stored in the newly created table are copies of those of the table passed in argument. In particular, the new hash table inherits the parameters (resize policy, uniqueness policy) of table 'from'.

Parameters
fromThe gum::HashTable to copy.

Definition at line 330 of file hashTable_tpl.h.

330 :
333 // for debugging purposes
335
336 // setup the _nodes_ vector (contains only empty lists)
338
339 // fill with the content of table
340 _copy_(table);
341 }
Size _begin_index_
Returns where the begin index should be.
Definition hashTable.h:1500
void _copy_(const HashTable< Key, Val > &table)
A function used to perform copies of HashTables.

References HashTable(), _begin_index_, _copy_(), _create_(), _key_uniqueness_policy_, _resize_policy_, and _size_.

Here is the call graph for this function:

◆ HashTable() [4/4]

template<typename Key, typename Val>
gum::HashTable< Key, Val >::HashTable ( HashTable< Key, Val > && from)

Move constructor.

Parameters
fromThe gum::HashTable to move.

Definition at line 344 of file hashTable_tpl.h.

344 :
349 // for debugging purposes
350 table._size_ = 0;
352 }
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition hashTable.h:1478
std::vector< HashTableConstIteratorSafe< Key, Val > * > _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition hashTable.h:1503
std::vector< HashTableList< Key, Val > > _nodes_
The hash table is represented as a vector of chained lists.
Definition hashTable.h:1469
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition hashTable.h:1475

References HashTable(), _begin_index_, _hash_func_, _key_uniqueness_policy_, _nb_elements_, _nodes_, _resize_policy_, _safe_iterators_, and _size_.

Here is the call graph for this function:

◆ ~HashTable()

template<typename Key, typename Val>
INLINE gum::HashTable< Key, Val >::~HashTable ( )

Class destructor.

Definition at line 376 of file hashTable_tpl.h.

376 {
377 // for debugging purposes
379
380 // update all the registered iterators: they should now point to nullptr
381 // and their hashtable should be set to nullptr
383 }
void _clearIterators_()
Clear all the safe iterators.

References HashTable(), and _clearIterators_().

Here is the call graph for this function:

Member Function Documentation

◆ _clearIterators_()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::_clearIterators_ ( )
private

Clear all the safe iterators.

Definition at line 355 of file hashTable_tpl.h.

355 {
356 const Size len = _safe_iterators_.size();
357 for (Size i = Size(0); i < len; ++i)
359 }

References _safe_iterators_, and clear().

Referenced by ~HashTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _copy_()

template<typename Key, typename Val>
void gum::HashTable< Key, Val >::_copy_ ( const HashTable< Key, Val > & table)
private

A function used to perform copies of HashTables.

This code is shared by the copy constructor and the copy operator. The function ensures that when a memory allocation problem occurs:

  • no memory leak occurs
  • the hashtable returned is empty but in a coherent state
  • an exception is thrown

The function assumes that both this and table have arrays ' __nodes' of the same size.

Parameters
tableThe gum::HashTable to copy.

Definition at line 267 of file hashTable_tpl.h.

267 {
268 // in debug mode, check that this and table have ' __nodes' arrays of the
269 // same size
271
272 // try to fill the array of chained lists
273 for (Size i = 0; i < table._size_; ++i) {
274 try {
275 _nodes_[i] = table._nodes_[i];
276 } catch (...) {
277 // here we could allocate the _nodes_[j], j=0..i-1, so we should
278 // deallocate them
279 for (Size j = 0; j < _size_; ++j)
280 _nodes_[j].clear();
281
282 _nb_elements_ = Size(0);
283
284 // propagate the exception
285 throw;
286 }
287 }
288
290 }

References HashTable(), _nb_elements_, _nodes_, _size_, and clear().

Referenced by HashTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _create_()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::_create_ ( Size size)
private

Used by all default constructors (general and specialized).

Parameters
sizeThe size of the gum::HashTable to create.

Definition at line 293 of file hashTable_tpl.h.

293 {
294 // setup the _nodes_ vector (contains only empty lists)
295 _nodes_.resize(size);
296
297 // set up properly the hash function
298 _hash_func_.resize(size);
299 }

References _hash_func_, _nodes_, and size().

Referenced by HashTable(), HashTable(), and HashTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _erase_()

template<typename Key, typename Val>
void gum::HashTable< Key, Val >::_erase_ ( HashTableBucket< Key, Val > * bucket,
Size index )
private

Erases a given bucket.

Definition at line 732 of file hashTable_tpl.h.

732 {
733 if (bucket == nullptr) return;
734
735 // update the registered iterators pointing to this bucket
736 for (auto iter: _safe_iterators_) {
737 if (iter->_bucket_ == bucket) {
738 iter->operator++();
739 iter->_next_bucket_ = iter->_bucket_;
740 iter->_bucket_ = nullptr;
741 } else if (iter->_next_bucket_ == bucket) {
742 iter->_bucket_ = bucket;
743 iter->operator++();
744 iter->_next_bucket_ = iter->_bucket_;
745 iter->_bucket_ = nullptr;
746 }
747 }
748
749 // remove the element from the _nodes_ vector
750 _nodes_[index].erase(bucket);
751
753
754 if ((index == _begin_index_) && _nodes_[index].empty()) {
756 }
757 }
bool empty() const noexcept
Indicates whether the hash table is empty.

References _begin_index_, _nb_elements_, _nodes_, _safe_iterators_, and empty().

Referenced by erase(), erase(), and eraseByVal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _insert_()

template<typename Key, typename Val>
void gum::HashTable< Key, Val >::_insert_ ( Bucket * bucket)
private

Adds a new element (actually a copy of this element) in the hash table.

If there already exists an element with the same key in the list and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Parameters
bucketThe bucket inserted in the hash table.
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.

Definition at line 632 of file hashTable_tpl.h.

632 {
634
635 // check that there does not already exist an element with the same key
637 // remove the bucket from memory
638 Key k = bucket->key();
639 delete bucket;
641 "the hashtable contains an element with the same key (" << k << ")");
642 }
643
644 // check whether there is sufficient space to insert the new pair
645 // if not, resize the current hashtable
647 resize(_size_ << 1);
649 }
650
651 // add the new pair
652 _nodes_[hash_key].insert(bucket);
654
655 // recompute the index of the beginning of the hashtable if possible
656 // WARNING: if _begin_index_ = std::numeric_limits<Size>::max (), we CANNOT
657 // recompute the index because we cannot know whether the current index is
658 // equal to max because there was no element in the hashtable or whether a
659 // previous _erase_() has set the index to max.
661 }
void resize(Size new_size)
Changes the number of slots in the 'nodes' vector of the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const Key & key(const Key &key) const
Returns a reference on a given key.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _begin_index_, _hash_func_, _key_uniqueness_policy_, _nb_elements_, _nodes_, _resize_policy_, _size_, gum::HashTableConst::default_mean_val_by_slot, exists(), GUM_ERROR, gum::HashTableBucket< Key, Val >::key(), and resize().

Referenced by insert(), insert(), and insert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ begin() [1/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::iterator gum::HashTable< Key, Val >::begin ( )

Returns an unsafe iterator pointing to the beginning of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns an unsafe iterator pointing to the beginning of the hashtable.

Definition at line 464 of file hashTable_tpl.h.

464 {
465 // if the table is empty, make the begin and end point to the same element
466 if (_nb_elements_ == Size(0)) return iterator{end()};
467 else return iterator{*this};
468 }
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.

Referenced by gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::_findRetrogradeVariables_(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::_findRetrogradeVariables_(), keyByVal(), and gum::learning::SimpleMiic::orientationMiic_().

Here is the caller graph for this function:

◆ begin() [2/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::const_iterator gum::HashTable< Key, Val >::begin ( ) const

Returns an unsafe const_iterator pointing to the beginning of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns an unsafe const_iterator pointing to the beginning of the hashtable.

Definition at line 471 of file hashTable_tpl.h.

471 {
472 // if the table is empty, make the begin and end point to the same element
473 if (_nb_elements_ == Size(0)) return const_iterator{end()};
474 else return const_iterator{*this};
475 }

◆ beginSafe() [1/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::iterator_safe gum::HashTable< Key, Val >::beginSafe ( )

Returns the safe iterator pointing to the beginning of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe iterator pointing to the beginning of the hashtable.

Definition at line 503 of file hashTable_tpl.h.

503 {
504 // if the table is empty, make the begin and end point to the same element
505 if (_nb_elements_ == Size(0)) return iterator_safe{endSafe()};
506 else return iterator_safe{*this};
507 }
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.

References _nb_elements_, and endSafe().

Referenced by gum::DAGCycleDetector::hasCycleFromModifications(), gum::LeafAggregator::leavesMap(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ beginSafe() [2/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::const_iterator_safe gum::HashTable< Key, Val >::beginSafe ( ) const

Returns the safe const_iterator pointing to the beginning of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe const_iterator pointing to the beginning of the hashtable.

Definition at line 511 of file hashTable_tpl.h.

511 {
512 // if the table is empty, make the begin and end point to the same element
513 if (_nb_elements_ == Size(0)) return const_iterator_safe{endSafe()};
514 else return const_iterator_safe{*this};
515 }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition hashTable.h:653

References _nb_elements_, and endSafe().

Here is the call graph for this function:

◆ capacity()

template<typename Key, typename Val>
INLINE Size gum::HashTable< Key, Val >::capacity ( ) const
noexcept

Returns the number of slots in the 'nodes' vector of the hashtable.

The method runs in constant time.

Returns
Returns the number of slots in the 'nodes' vector of the hashtable.

Definition at line 541 of file hashTable_tpl.h.

541 {
542 return _size_;
543 }

References _size_.

Referenced by gum::ArcGraphPart::ArcGraphPart().

Here is the caller graph for this function:

◆ cbegin()

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::const_iterator gum::HashTable< Key, Val >::cbegin ( ) const

Returns an unsafe const_iterator pointing to the beginning of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns an unsafe const_iterator pointing to the beginning of the hashtable.

Definition at line 478 of file hashTable_tpl.h.

478 {
479 // if the table is empty, make the begin and end point to the same element
480 if (_nb_elements_ == Size(0)) return const_iterator{cend()};
481 else return const_iterator{*this};
482 }
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.

References _nb_elements_, and cend().

Referenced by gum::learning::StructuralConstraintSliceOrder::StructuralConstraintSliceOrder(), gum::DAGCycleDetector::_addWeightedSet_(), gum::BijectionImplementation< T1, T2, Gen >::_copy_(), gum::DAGCycleDetector::_delWeightedSet_(), gum::DAGCycleDetector::_restrictWeightedSet_(), gum::DAGCycleDetector::addArc(), gum::DAGCycleDetector::eraseArc(), eraseByVal(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::Set< Key >::operator*(), and gum::Set< Key >::operator+().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cbeginSafe()

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::const_iterator_safe gum::HashTable< Key, Val >::cbeginSafe ( ) const

Returns the safe const_iterator pointing to the beginning of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe const_iterator pointing to the beginning of the hashtable.

Definition at line 519 of file hashTable_tpl.h.

519 {
520 // if the table is empty, make the begin and end point to the same element
521 if (_nb_elements_ == Size(0)) return const_iterator_safe{cendSafe()};
522 else return const_iterator_safe{*this};
523 }
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.

References _nb_elements_, and cendSafe().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::_rebuildFunctionGraph_(), and gum::LeastSquareTestPolicy< GUM_SCALAR >::add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cend()

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::const_iterator & gum::HashTable< Key, Val >::cend ( ) const
noexcept

Returns the unsafe const_iterator pointing to the end of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns the unsafe const_iterator pointing to the end of the hashtable.

Definition at line 459 of file hashTable_tpl.h.

459 {
460 return *(reinterpret_cast< const const_iterator* >(_HashTable_cend_));
461 }

Referenced by gum::learning::StructuralConstraintSliceOrder::StructuralConstraintSliceOrder(), gum::DAGCycleDetector::_addWeightedSet_(), gum::BijectionImplementation< T1, T2, Gen >::_copy_(), gum::DAGCycleDetector::_delWeightedSet_(), gum::DAGCycleDetector::_restrictWeightedSet_(), gum::DAGCycleDetector::addArc(), cbegin(), gum::DAGCycleDetector::eraseArc(), eraseByVal(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::Set< Key >::operator*(), and gum::Set< Key >::operator+().

Here is the caller graph for this function:

◆ cendSafe()

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::const_iterator_safe & gum::HashTable< Key, Val >::cendSafe ( ) const
noexcept

Returns the safe const_iterator pointing to the end of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe const_iterator pointing to the end of the hashtable.

Definition at line 498 of file hashTable_tpl.h.

498 {
499 return *(reinterpret_cast< const const_iterator_safe* >(_HashTable_cend_safe_));
500 }

Referenced by gum::IMDDI< AttributeSelection, isScalar >::_rebuildFunctionGraph_(), gum::LeastSquareTestPolicy< GUM_SCALAR >::add(), cbeginSafe(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

Here is the caller graph for this function:

◆ clear()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::clear ( )

Removes all the elements in the hash table.

The function does not resize the nodes vector (even if the size of this one has been increased after the creation of the hash table) and it resets the iterators on the hash table to end. The method runs in linear time w.r.t. the number of iterators pointing to the hash table.

Definition at line 362 of file hashTable_tpl.h.

362 {
363 // update all the registered iterators: they should now point to nullptr
364 // and they are positioned to the end of the hashtable.
366
367 // remove the buckets
368 for (Size i = Size(0); i < _size_; ++i)
369 _nodes_[i].clear();
370
371 _nb_elements_ = Size(0);
373 }

Referenced by _clearIterators_(), _copy_(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::_findRetrogradeVariables_(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::_findRetrogradeVariables_(), and operator=().

Here is the caller graph for this function:

◆ emplace() [1/2]

template<typename Key, typename Val>
template<typename... Args>
value_type & gum::HashTable< Key, Val >::emplace ( Args &&... args)

Emplace a new element into the hashTable.

If there already exists an element with the same key in the list and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Returns
a reference to the pair (key,val) inserted in the hash table.
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.
Parameters
argsThe element to emplace.

◆ emplace() [2/2]

template<typename Key, typename Val>
template<typename... Args>
INLINE HashTable< Key, Val >::value_type & gum::HashTable< Key, Val >::emplace ( Args &&... args)

Definition at line 698 of file hashTable_tpl.h.

698 {
699 auto bucket
702 return bucket->elt();
703 }
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition hashTable.h:657

◆ empty()

template<typename Key, typename Val>
INLINE bool gum::HashTable< Key, Val >::empty ( ) const
noexcept

Indicates whether the hash table is empty.

Returns
Returns true if the gum::HashTable is empty.

Definition at line 820 of file hashTable_tpl.h.

820 {
821 return (_nb_elements_ == Size(0));
822 }

References _nb_elements_.

Referenced by _erase_(), gum::prm::PRMFactory< GUM_SCALAR >::addInstance(), gum::BayesBall::relevantTensors(), and gum::dSeparationAlgorithm::relevantTensors().

Here is the caller graph for this function:

◆ end() [1/2]

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::const_iterator & gum::HashTable< Key, Val >::end ( ) const
noexcept

Returns the unsafe const_iterator pointing to the end of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns the unsafe const_iterator pointing to the end of the hashtable.

Definition at line 453 of file hashTable_tpl.h.

453 {
454 return *(reinterpret_cast< const const_iterator* >(_HashTable_cend_));
455 }

◆ end() [2/2]

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::iterator & gum::HashTable< Key, Val >::end ( )
noexcept

Returns the unsafe iterator pointing to the end of the hashtable.

Unsafe iterators are slightly faster than safe iterators. However, BE CAREFUL when using them: they should ONLY be used when you have the guarantee that they will never point to a deleted element. If unsure, prefer using the safe iterators (those are only slightly slower).

Returns
Returns the unsafe iterator pointing to the end of the hashtable.

Definition at line 447 of file hashTable_tpl.h.

447 {
448 return *(reinterpret_cast< const iterator* >(_HashTable_end_));
449 }

Referenced by gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::_findRetrogradeVariables_(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::_findRetrogradeVariables_(), keyByVal(), and gum::learning::SimpleMiic::orientationMiic_().

Here is the caller graph for this function:

◆ endSafe() [1/2]

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::const_iterator_safe & gum::HashTable< Key, Val >::endSafe ( ) const
noexcept

Returns the safe const_iterator pointing to the end of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe const_iterator pointing to the end of the hashtable.

Definition at line 492 of file hashTable_tpl.h.

492 {
493 return *(reinterpret_cast< const const_iterator_safe* >(_HashTable_cend_safe_));
494 }

◆ endSafe() [2/2]

template<typename Key, typename Val>
INLINE const HashTable< Key, Val >::iterator_safe & gum::HashTable< Key, Val >::endSafe ( )
noexcept

Returns the safe iterator pointing to the end of the hashtable.

Safe iterators are slightly slower than unsafe ones but they guarantee that you will never get a segfault if they try to access to a deleted element or if they try a ++ operation from a deleted element.

Returns
Returns the safe iterator pointing to the end of the hashtable.

Definition at line 486 of file hashTable_tpl.h.

486 {
487 return *(reinterpret_cast< const iterator_safe* >(_HashTable_end_safe_));
488 }

Referenced by beginSafe(), beginSafe(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::LeafAggregator::leavesMap(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

Here is the caller graph for this function:

◆ erase() [1/3]

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::erase ( const const_iterator_safe & iter)

Removes a given element from the hash table.

This method updates all the safe iterators pointing to the deleted element, i.e., when trying to dereference those iterators, an exception will be raised because they will know that the element they point to no longer exists.

Parameters
iterAn iterator over the element to remove.

Definition at line 776 of file hashTable_tpl.h.

776 {
777 _erase_(iter._getBucket_(), iter._getIndex_());
778 }
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.

References _erase_(), gum::HashTableConstIteratorSafe< Key, Val >::_getBucket_(), and gum::HashTableConstIteratorSafe< Key, Val >::_getIndex_().

Here is the call graph for this function:

◆ erase() [2/3]

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::erase ( const iterator_safe & iter)

Removes a given element from the hash table.

This method updates all the safe iterators pointing to the deleted element, i.e., when trying to dereference those iterators, an exception will be raised because they will know that the element they point to no longer exists.

Parameters
iterAn iterator over the element to remove.

Definition at line 771 of file hashTable_tpl.h.

771 {
772 _erase_(iter._getBucket_(), iter._getIndex_());
773 }

References _erase_().

Here is the call graph for this function:

◆ erase() [3/3]

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::erase ( const Key & key)

Removes a given element from the hash table.

The element is the first one encountered in the list (from begin() to end()) having the specified key. If no such element can be found, nothing is done (in particular, it does not throw any exception). The function never resizes the nodes vector (even if the resizing policy would enable to decrease this size). The method runs in average in time linear to the number of iterators pointing to the table if the automatic resizing policy is set (else it is in linear time in the number of elements of the hash table plus the number of iterators).

Parameters
keyThe key of the element to remove.

Definition at line 760 of file hashTable_tpl.h.

760 {
761 // get the hashed key
763
764 // get the bucket containing the element to erase
766
768 }

Referenced by gum::DAGCycleDetector::_delWeightedSet_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fillMaps_(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::BayesBall::relevantTensors(), gum::dSeparationAlgorithm::relevantTensors(), and reset().

Here is the caller graph for this function:

◆ eraseAllVal()

template<typename Key, typename Val>
void gum::HashTable< Key, Val >::eraseAllVal ( const Val & val)

Removes all the elements having a certain value from the hash table.

If no such element can be found, nothing is done (in particular, it does not throw any exception). The function never resizes the nodes vector (even if the resizing policy would enable to decrease this size). Comparisons between Val instances are performed through == operators.

Parameters
valThe value to remove.

Definition at line 813 of file hashTable_tpl.h.

813 {
814 for (auto iterAll = cbeginSafe(); iterAll != cendSafe(); ++iterAll) {
815 if (iterAll._bucket_->val() == val) { _erase_(iterAll._bucket_, iterAll._index_); }
816 }
817 }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.

◆ eraseByVal()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::eraseByVal ( const Val & val)

Removes a given element from the hash table.

The element is the first one encountered in the list (from begin() to end()) having the specified value. If no such element can be found, nothing is done (in particular, it does not throw any exception). The function never resizes the nodes vector (even if the resizing policy would enable to decrease this size). Comparisons between Val instances are performed through == operators. Logically, this method should have been named "erase", however, this would have prevented creating hash tables where both keys and vals have the same type. Hence we chose to add "ByVal" after erase to make a difference between erasing by key and erasing by val.

Parameters
valThe value to remove.

Definition at line 781 of file hashTable_tpl.h.

781 {
782 for (auto iter = cbegin(); iter != cend(); ++iter)
783 if (iter._bucket_->val() == val) {
784 _erase_(iter._getBucket_(), iter._getIndex_());
785 return;
786 }
787 }
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.

References _erase_(), cbegin(), and cend().

Here is the call graph for this function:

◆ exists()

template<typename Key, typename Val>
INLINE bool gum::HashTable< Key, Val >::exists ( const Key & key) const

Checks whether there exists an element with a given key in the hashtable.

The method runs in average in constant time if the resizing policy is set.

Parameters
keyThe key to test for existence.
Returns
True if key is in this gum::HashTable.

Definition at line 546 of file hashTable_tpl.h.

546 {
547 return _nodes_[_hash_func_(key)].exists(key);
548 }

References _hash_func_, _nodes_, and key().

Referenced by gum::DAGCycleDetector::_addWeightedSet_(), gum::DAGCycleDetector::_delWeightedSet_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fromChild_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fromParent_(), _insert_(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::_label_(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::_label_(), gum::IMDDI< AttributeSelection, isScalar >::_rebuildFunctionGraph_(), gum::StructuredPlaner< GUM_SCALAR >::_recurArgMaxCopy_(), gum::StructuredPlaner< GUM_SCALAR >::_recurExtractOptPol_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_(), gum::ArcGraphPart::directedPath(), gum::ArcGraphPart::directedUnorientedPath(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::MixedGraph::hasMixedOrientedPath(), gum::StructuredPlaner< double >::initialize(), gum::LeafAggregator::leavesMap(), gum::MixedGraph::mixedOrientedPath(), gum::MixedGraph::mixedUnorientedPath(), gum::UndiGraph::nodes2ConnectedComponent(), gum::Set< Key >::operator*(), gum::Set< Key >::operator*=(), gum::Set< Key >::operator+(), gum::MultiDimFunctionGraphProjector< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::project(), gum::BayesBall::relevantTensors(), gum::dSeparationAlgorithm::relevantTensors(), gum::Tensor< GUM_SCALAR >::reorganize(), gum::BayesBall::requisiteNodes(), gum::EliminationSequenceStrategy::setGraph(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), gum::Set< const gum::Observation * >::toString(), and gum::EdgeGraphPart::undirectedPath().

Here is the call graph for this function:

◆ getWithDefault() [1/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::mapped_type & gum::HashTable< Key, Val >::getWithDefault ( const Key & key,
const Val & default_value )

Returns a reference on the element the key of which is passed in argument.

In case of multiple identical keys in the hash table, the first value encountered is returned. The method runs in constant time. In case of not found key, (key,default_value) is inserted in *this.

Parameters
keyThe key for wich we want the value.
default_valueThe default value to return if key does not match any value.
Returns
Returns a reference on the element the key of which is passed in argument.

Definition at line 707 of file hashTable_tpl.h.

707 {
708 Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
709
710 if (bucket == nullptr) return insert(key, default_value).second;
711 else return bucket->val();
712 }

References _hash_func_, _nodes_, insert(), key(), and gum::HashTableBucket< Key, Val >::val().

Here is the call graph for this function:

◆ getWithDefault() [2/2]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::mapped_type & gum::HashTable< Key, Val >::getWithDefault ( Key && key,
Val && default_value )

Returns a reference on the element the key of which is passed in argument.

In case of multiple identical keys in the hash table, the first value encountered is returned. The method runs in constant time. In case of not found key, (key,default_value) is inserted in *this.

Parameters
keyThe key for wich we want the value.
default_valueThe default value to return if key does not match any value.
Returns
Returns a reference on the element the key of which is passed in argument.

Definition at line 716 of file hashTable_tpl.h.

716 {
717 Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
718
719 if (bucket == nullptr) return insert(std::move(key), std::move(default_value)).second;
720 else return bucket->val();
721 }

References _hash_func_, _nodes_, insert(), key(), and gum::HashTableBucket< Key, Val >::val().

Here is the call graph for this function:

◆ insert() [1/4]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::value_type & gum::HashTable< Key, Val >::insert ( const Key & key,
const Val & val )

Adds a new element (actually a copy of this element) into the hash table.

If there already exists an element with the same key in the table and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Returns
As only a copy of val is inserted into the hashtable, the method returns a reference on a copy of the pair (key,val).
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.
Parameters
keyThe key to add.
valThe value to add.
Returns
The value added by copy to this gum::HashTable.

Definition at line 665 of file hashTable_tpl.h.

665 {
666 auto bucket = new Bucket(thekey, theval);
668 return bucket->elt();
669 }

References _insert_().

Referenced by gum::learning::IBNLearner::Database::Database(), gum::learning::IBNLearner::Database::Database(), HashTable(), gum::prm::gspan::Pattern::Pattern(), gum::DAGCycleDetector::_addWeightedSet_(), gum::MaxInducedWidthMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::_checkConditions_(), gum::DefaultJunctionTreeStrategy::_computeJunctionTree_(), gum::StaticTriangulation::_computeMaxPrimeJunctionTree_(), gum::StaticTriangulation::_computeRecursiveThinning_(), gum::prm::PRMFormAttribute< GUM_SCALAR >::_fillCpf_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fillMaps_(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::_findRetrogradeVariables_(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::_findRetrogradeVariables_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fromChild_(), gum::prm::StructuredBayesBall< GUM_SCALAR >::_fromParent_(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::_label_(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::_label_(), gum::IMDDI< AttributeSelection, isScalar >::_rebuildFunctionGraph_(), gum::StructuredPlaner< GUM_SCALAR >::_recurArgMaxCopy_(), gum::StructuredPlaner< GUM_SCALAR >::_recurExtractOptPol_(), gum::DAGCycleDetector::_restrictWeightedSet_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_(), gum::prm::GSpan< GUM_SCALAR >::_subgraph_mining_(), gum::DAGCycleDetector::addArc(), gum::prm::gspan::DFSTree< GUM_SCALAR >::addRoot(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::BarrenNodesFinder::barrenNodes(), gum::BarrenNodesFinder::barrenTensors(), gum::HashTable< const gum::DiscreteVariable *, Idx >::beginSafe(), gum::ExactBNdistance< GUM_SCALAR >::computeKL_(), gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), gum::ArcGraphPart::directedPath(), gum::ArcGraphPart::directedUnorientedPath(), gum::DAGCycleDetector::eraseArc(), gum::MultiDimFunctionGraphGenerator::generate(), gum::InfluenceDiagramGenerator< GUM_SCALAR >::generateID(), getWithDefault(), getWithDefault(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::Set< Key >::hashMap(), gum::MixedGraph::hasMixedOrientedPath(), gum::StructuredPlaner< double >::initialize(), gum::LeafAggregator::leavesMap(), gum::MixedGraph::mixedOrientedPath(), gum::MixedGraph::mixedUnorientedPath(), gum::UndiGraph::nodes2ConnectedComponent(), gum::Set< Key >::operator*(), gum::Set< Key >::operator+(), gum::HashTable< const gum::DiscreteVariable *, Idx >::operator=(), gum::learning::Miic::orientationMiic_(), gum::MultiDimFunctionGraphProjector< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::project(), gum::BayesBall::relevantTensors(), gum::dSeparationAlgorithm::relevantTensors(), gum::Tensor< GUM_SCALAR >::reorganize(), gum::BayesBall::requisiteNodes(), gum::prm::PRMClass< GUM_SCALAR >::scope(), set(), gum::DAGCycleDetector::setDAG(), gum::learning::IBNLearner::setSliceOrder(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), gum::EdgeGraphPart::undirectedPath(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

Here is the call graph for this function:

◆ insert() [2/4]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::value_type & gum::HashTable< Key, Val >::insert ( const std::pair< Key, Val > & elt)

Adds a new element (actually a copy of this element) into the hash table.

If there already exists an element with the same key in the table and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Returns
As only a copy of val is inserted into the hashtable, the method returns a reference on a copy of the pair (key,val).
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.
Parameters
eltThe pair of key value to add.
Returns
The value added by copy to this gum::HashTable.

Definition at line 681 of file hashTable_tpl.h.

681 {
682 auto bucket = new Bucket(reinterpret_cast< const value_type& >(elt));
684 return bucket->elt();
685 }
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:643

◆ insert() [3/4]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::value_type & gum::HashTable< Key, Val >::insert ( Key && key,
Val && val )

Moves a new element in the hash table.

If there already exists an element with the same key in the table and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Returns
a reference to the pair (key,val) in the hashtable.
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.
Parameters
keyThe key to move.
valThe value to move.
Returns
The value moved to this gum::HashTable.

Definition at line 672 of file hashTable_tpl.h.

673 {
676 return bucket->elt();
677 }

References _insert_().

Here is the call graph for this function:

◆ insert() [4/4]

template<typename Key, typename Val>
INLINE HashTable< Key, Val >::value_type & gum::HashTable< Key, Val >::insert ( std::pair< Key, Val > && elt)

Moves a new element in the hash table.

If there already exists an element with the same key in the table and the uniqueness policy prevents multiple identical keys to belong to the same hashtable, an exception DuplicateElement is thrown. If the uniqueness policy is not set, the method runs in the worst case in constant time, else if the automatic resizing policy is set, it runs in constant time in average linear in the number of elements by slot.

Returns
a reference to the pair (key,val) in the hashtable.
Exceptions
DuplicateElementis thrown when attempting to insert a pair (key,val) in a hash table containing already a pair with the same key and when the hash table's uniqueness policy is set.
Parameters
eltThe pair of key value to move in this gum::HashTable.
Returns
The value moved to this gum::HashTable.

Definition at line 689 of file hashTable_tpl.h.

689 {
690 auto bucket = new Bucket(std::move(reinterpret_cast< value_type& >(elt)));
692 return bucket->elt();
693 }

References _insert_().

Here is the call graph for this function:

◆ key()

template<typename Key, typename Val>
INLINE const Key & gum::HashTable< Key, Val >::key ( const Key & key) const

Returns a reference on a given key.

Some complex structures use pointers on keys of hashtables. These structures thus require that we do not only get a copy of a given key, but the key stored in the hashtable itself. This is the very purpose of this function.

Parameters
keyThe key to return.
Returns
Returns a reference on a given key.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 803 of file hashTable_tpl.h.

803 {
804 // get the bucket corresponding to the key
805 Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
806
807 if (bucket == nullptr) { GUM_ERROR(NotFound, "key does not belong to the hashtable") }
808
809 return bucket->key();
810 }

References _hash_func_, _nodes_, GUM_ERROR, key(), and gum::HashTableBucket< Key, Val >::key().

Referenced by exists(), getWithDefault(), getWithDefault(), key(), operator[](), operator[](), reset(), and set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ keyByVal()

template<typename Key, typename Val>
INLINE const Key & gum::HashTable< Key, Val >::keyByVal ( const Val & val) const

Returns a reference on the key given a value.

In case of multiple identical values in the hash table, the first key encountered is returned. The method runs in linear time.

Parameters
valThe value for which the key is returned.
Returns
Returns a reference on the key given a value.
Exceptions
NotFoundRaised if the element cannot be found.

Definition at line 795 of file hashTable_tpl.h.

795 {
796 for (auto iter = begin(); iter != end(); ++iter)
797 if (iter._bucket_->val() == val) return iter.key();
798
799 GUM_ERROR(NotFound, "not enough elements in the chained list")
800 }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.

References begin(), end(), and GUM_ERROR.

Here is the call graph for this function:

◆ keyUniquenessPolicy()

template<typename Key, typename Val>
INLINE bool gum::HashTable< Key, Val >::keyUniquenessPolicy ( ) const
noexcept

Returns the current checking policy.

Returns
Returns the current checking policy.

Definition at line 566 of file hashTable_tpl.h.

566 {
568 }

References _key_uniqueness_policy_.

◆ map() [1/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > INLINE gum::HashTable< Key, Val >::map ( const Mount & val,
Size size,
bool resize_pol,
bool key_uniqueness_pol ) const

Definition at line 895 of file hashTable_tpl.h.

898 {
899 // determine the proper size of the hashtable
900 // by default, the size of the table is set so that the table does not take
901 // too much space while allowing to add a few elements without needing to
902 // resize in autmatic resizing mode
903 if (size == Size(0)) size = std::max(Size(2), _nb_elements_ / 2);
904
905 // create a new table
907
908 // fill the new hash table
909 for (auto iter = begin(); iter != end(); ++iter) {
910 table.insert(iter.key(), val);
911 }
912
913 return table;
914 }

◆ map() [2/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > gum::HashTable< Key, Val >::map ( const Mount & val,
Size size = Size(0),
bool resize_pol = HashTableConst::default_resize_policy,
bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy ) const

Creates a hashtable of mounts with a given value from a hashtable of vals.

Warning
Although the resulting hashtable has the same number of elements as the original hashtable, by default, the size of the former may not be equal to that of the latter. Hence iterators on the original hashtable may not parse it in the same order as iterators on the resulting hashtable. To guarrantee that both hashtables have the same size (and thus have the elements in the same order), set the size argument to the size of the original hashtable.
Parameters
valThe value taken by all the elements of the resulting hashtable.
sizeThe size of the resulting hashtable. When equal to 0, a default size is computed that is a good trade-off between space consumption and efficiency of new elements insertions
resize_polthe resizing policy (automatic or manual resizing)
key_uniqueness_poluniqueness policy
Returns
Returns the gum::HashTable of mountains.

◆ map() [3/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > INLINE gum::HashTable< Key, Val >::map ( Mount(* )(const Val &),
Size size,
bool resize_pol,
bool key_uniqueness_pol ) const

Definition at line 872 of file hashTable_tpl.h.

875 {
876 // determine the proper size of the hashtable
877 // by default, the size of the table is set so that the table does not take
878 // too much space while allowing to add a few elements without needing to
879 // resize in autmatic resizing mode
880 if (size == Size(0)) size = std::max(Size(2), _nb_elements_ / 2);
881
882 // create a new table
884
885 // fill the new hash table
886 for (auto iter = begin(); iter != end(); ++iter) {
887 table.insert(iter.key(), f(iter.val()));
888 }
889
890 return table;
891 }

◆ map() [4/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > gum::HashTable< Key, Val >::map ( Mount(* )(const Val &),
Size size = Size(0),
bool resize_pol = HashTableConst::default_resize_policy,
bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy ) const

Transforms a hashtable of vals into a hashtable of mountains.

Warning
Although the resulting hashtable has the same number of elements as the original hashtable, by default, the size of the former may not be equal to that of the latter. Hence iterators on the original hashtable may not parse it in the same order as iterators on the resulting hashtable. To guarrantee that both hashtables have the same size (and thus have the elements in the same order), set the size argument to the size of the original hashtable.
Parameters
fA function that maps any Val element into a Mount.
sizeThe size of the resulting hashtable. When equal to 0, a default size is computed that is a good trade-off between space consumption and efficiency of new elements insertions
resize_polthe resizing policy (automatic or manual resizing)
key_uniqueness_poluniqueness policy
Returns
Returns the gum::HashTable of mountains.

◆ map() [5/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > INLINE gum::HashTable< Key, Val >::map ( Mount(* )(Val &),
Size size,
bool resize_pol,
bool key_uniqueness_pol ) const

Definition at line 849 of file hashTable_tpl.h.

852 {
853 // determine the proper size of the hashtable
854 // by default, the size of the table is set so that the table does not take
855 // too much space while allowing to add a few elements without needing to
856 // resize in autmatic resizing mode
857 if (size == Size(0)) size = std::max(Size(2), _nb_elements_ / 2);
858
859 // create a new table
861
862 // fill the new hash table
863 for (auto iter = begin(); iter != end(); ++iter) {
864 table.insert(iter.key(), f(const_cast< Val& >(iter.val())));
865 }
866
867 return table;
868 }

◆ map() [6/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > gum::HashTable< Key, Val >::map ( Mount(* )(Val &),
Size size = Size(0),
bool resize_pol = HashTableConst::default_resize_policy,
bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy ) const

Transforms a hashtable of vals into a hashtable of mountains.

Warning
Although the resulting hashtable has the same number of elements as the original hashtable, by default, the size of the former may not be equal to that of the latter. Hence iterators on the original hashtable may not parse it in the same order as iterators on the resulting hashtable. To guarrantee that both hashtables have the same size (and thus have the elements in the same order), set the size argument to the size of the original hashtable.
Parameters
fA function that maps any Val element into a Mount.
sizeThe size of the resulting hashtable. When equal to 0, a default size is computed that is a good trade-off between space consumption and efficiency of new elements insertions
resize_polthe resizing policy (automatic or manual resizing)
key_uniqueness_poluniqueness policy
Returns
Returns the gum::HashTable of mountains.

◆ map() [7/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > INLINE gum::HashTable< Key, Val >::map ( Mount(* )(Val),
Size size,
bool resize_pol,
bool key_uniqueness_pol ) const

Definition at line 826 of file hashTable_tpl.h.

829 {
830 // determine the proper size of the hashtable
831 // by default, the size of the table is set so that the table does not take
832 // too much space while allowing to add a few elements without needing to
833 // resize in autmatic resizing mode
834 if (size == 0) size = std::max(Size(2), _nb_elements_ / 2);
835
836 // create a new table
838
839 // fill the new hash table
840 for (auto iter = begin(); iter != end(); ++iter) {
841 table.insert(iter.key(), f(iter.val()));
842 }
843
844 return table;
845 }

◆ map() [8/8]

template<typename Key, typename Val>
template<typename Mount>
HashTable< Key, Mount > gum::HashTable< Key, Val >::map ( Mount(* )(Val),
Size size = Size(0),
bool resize_pol = HashTableConst::default_resize_policy,
bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy ) const

Transforms a hashtable of vals into a hashtable of mountains.

Warning
Although the resulting hashtable has the same number of elements as the original hashtable, by default, the size of the former may not be equal to that of the latter. Hence iterators on the original hashtable may not parse it in the same order as iterators on the resulting hashtable. To guarrantee that both hashtables have the same size (and thus have the elements in the same order), set the size argument to the size of the original hashtable.
Parameters
fA function that maps any Val element into a Mount.
sizeThe size of the resulting hashtable. When equal to 0, a default size is computed that is a good trade-off between space consumption and efficiency of new elements insertions
resize_polthe resizing policy (automatic or manual resizing)
key_uniqueness_poluniqueness policy
Returns
Returns the gum::HashTable of mountains.

◆ operator!=()

template<typename Key, typename Val>
INLINE bool gum::HashTable< Key, Val >::operator!= ( const HashTable< Key, Val > & from) const

Checks whether two hashtables contain different sets of elements.

Two hashtables are considered different if they contain different pairs (key,val). Two pairs are different if their keys have different hashed values, or if they are different in the sense of !=, or if their val's are different in the sense of !=.

Parameters
fromThe gum::HashTable to test for inequality.
Returns
True if this and from are not equal.

Definition at line 932 of file hashTable_tpl.h.

932 {
933 return !operator==(from);
934 }
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243

References HashTable(), and gum::operator==().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Key, typename Val>
HashTable< Key, Val > & gum::HashTable< Key, Val >::operator= ( const HashTable< Key, Val > & from)

Copy operator.

The copy operators ensures that whenever a memory allocation problem occurs, no memory leak occurs as well and it also guarantees that in this case the hashtable returned is in a coherent state (it is an empty hashtable). Note that the copy not only involves copying pairs (key,value) but also the copy of the resize and key uniqueness policies.

Parameters
fromThe gum::HashTable to copy.
Returns
Returns this gum::HashTable.

Definition at line 386 of file hashTable_tpl.h.

386 {
387 // avoid self assignment
388 if (this != &from) {
389 // for debugging purposes
391
392 // first remove the current content of the hashtable and make
393 // the iterators point to end
394 clear();
395
396 // if sizes of from's and this' _nodes_ vectors are not the same,
397 // we need to remove the current _nodes_' array and to create a
398 // new array with the correct size
399 if (_size_ != from._size_) {
400 _nodes_.resize(from._size_);
402
403 // update the hash function : this is important as the computation of
404 // the hash values heavily depends on the size of the hash table
405 _hash_func_.resize(_size_);
406 }
407
411
412 // perform the copy
413 _copy_(from);
414 }
415
416 return *this;
417 }

References HashTable().

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Key, typename Val>
HashTable< Key, Val > & gum::HashTable< Key, Val >::operator= ( HashTable< Key, Val > && from)

Move operator.

Parameters
fromThe gum::HashTable to move.
Returns
Returns this gum::HashTable.

Definition at line 420 of file hashTable_tpl.h.

420 {
421 // avoid self assignment
422 if (this != &table) {
423 // for debugging purposes
425
426 // first remove the current content of the hashtable and make
427 // the iterators point to end
428 clear();
429
438
439 table._size_ = 0; // necessary if we wish to perform moves iteratively,
440 // i.e. x = std::move ( y ); y = std::move ( z ); ...
441 }
442
443 return *this;
444 }

References HashTable(), and clear().

Here is the call graph for this function:

◆ operator==()

template<typename Key, typename Val>
bool gum::HashTable< Key, Val >::operator== ( const HashTable< Key, Val > & from) const

Checks whether two hashtables contain the same elements.

Two hashtables are considered equal if they contain the identical pairs (key,val). Two pairs are identical if their keys have the same hashed value, these two keys are equal in the sense of ==, and their val's are also equal in the sense of ==.

Parameters
fromThe gum::HashTable to test for equality.
Returns
True if this and from are equal.

Definition at line 917 of file hashTable_tpl.h.

917 {
918 // checks whether the two hashtables contain the same number of elements
919 if (from._nb_elements_ != _nb_elements_) return false;
920
921 // parse this and check that each element also belongs to from
922 for (auto iter = begin(); iter != end(); ++iter) {
923 try {
924 if (iter.val() != from[iter.key()]) return false;
925 } catch (NotFound const&) { return false; }
926 }
927
928 return true;
929 }

References HashTable().

Here is the call graph for this function:

◆ operator[]() [1/2]

template<typename Key, typename Val>
INLINE Val & gum::HashTable< Key, Val >::operator[] ( const Key & key)

Returns a reference on the value the key of which is passed in argument.

In case of multiple identical keys in the hash table, the first value encountered is returned. The method runs in constant time.

Parameters
keyThe key of the value to return.
Returns
Returns the value matching the given key.
Exceptions
NotFoundexception is thrown if the element cannot be found.

Definition at line 526 of file hashTable_tpl.h.

526 {
527 return _nodes_[_hash_func_(key)][key];
528 }

References _hash_func_, _nodes_, and key().

Here is the call graph for this function:

◆ operator[]() [2/2]

template<typename Key, typename Val>
INLINE const Val & gum::HashTable< Key, Val >::operator[] ( const Key & key) const

returns a reference on the value the key of which is passed in argument

In case of multiple identical keys in the hash table, the first value encountered is returned. The method runs in constant time.

Exceptions
NotFoundexception is thrown if the element cannot be found.

Definition at line 531 of file hashTable_tpl.h.

531 {
532 return _nodes_[_hash_func_(key)][key];
533 }

References _hash_func_, _nodes_, and key().

Here is the call graph for this function:

◆ reset()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::reset ( const Key & key)

Removes a property (i.e., remove an element).

Reset removes a property (i.e., a pair (key,val)) if it exists. This is an alias for erase but it is quite convenient when dealing with "dynamic property lists".

Parameters
keyThe property to remove.

Definition at line 790 of file hashTable_tpl.h.

790 {
791 erase(key);
792 }
void erase(const Key &key)
Removes a given element from the hash table.

References erase(), and key().

Here is the call graph for this function:

◆ resize()

template<typename Key, typename Val>
void gum::HashTable< Key, Val >::resize ( Size new_size)

Changes the number of slots in the 'nodes' vector of the hash table.

Usually, method resize enables the user to resize manually the hashtable. When in automatic resize mode, the function will actually resize the table only if resizing policy is compatible with the new size, i.e., the new size is not so small that there would be too many elements per slot in the table (this would lead to a significant loss in performance). However, the resizing policy may be changed by using method setResizePolicy. The method runs in linear time in the size of the hashtable. Upon memory allocation problem, the fuction guarantees that no data is lost and that the hash table and its iterators are in a coherent state. In such a case, a bad_alloc exception is thrown.

Parameters
new_sizeThe new number of slots in the gum::HashTable.

Definition at line 571 of file hashTable_tpl.h.

571 {
572 // new_size must be >= 2 else all the bits of the hash function are lost
574
575 // find the real size for allocation (the smallest power of 2 greater
576 // than or equal to new_size) and get its base-2 logarithm
578 new_size = Size(1) << log_size;
579
580 // check if the new size is different from the actual size
581 // if not, nothing else need be done
582
583 if (new_size != _size_) {
584 // under automatic resize policy, check if the new size leaves
585 // enough space for storing all the current elements
586 if (!_resize_policy_
588 // create a new array of _nodes_ to store the elements
590
591 // set the new hash function
592 _hash_func_.resize(new_size);
593
594 // put all the elements of the current _nodes_ array into the new one
595 Bucket* bucket;
597
598 for (Size i = Size(0); i < _size_; ++i) {
599 while ((bucket = _nodes_[i]._deb_list_) != nullptr) {
600 // compute the new hashed key
602
603 // remove the bucket from the list of buckets of the current
604 // node vector
605 _nodes_[i]._deb_list_ = bucket->next;
606
607 // put the bucket into the new _nodes_ vector
609 }
610 }
611
612 // update the size of the hash table
615
616 // substitute the current _nodes_ array by the new one
618
619 // update the iterators
620 for (auto iter: _safe_iterators_) {
621 if (iter->_bucket_) iter->_index_ = _hash_func_(iter->_bucket_->key());
622 else {
623 iter->_next_bucket_ = nullptr;
624 iter->_index_ = 0;
625 }
626 }
627 }
628 }
629 }

References _begin_index_, _hash_func_, gum::_hashTableLog2_(), _nb_elements_, _nodes_, _resize_policy_, _safe_iterators_, _size_, gum::HashTableConst::default_mean_val_by_slot, gum::HashTableBucket< Key, Val >::key(), and gum::HashTableBucket< Key, Val >::next.

Referenced by _insert_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resizePolicy()

template<typename Key, typename Val>
INLINE bool gum::HashTable< Key, Val >::resizePolicy ( ) const
noexcept

Returns the current resizing policy.

Returns
Returns the current resizing policy.

Definition at line 556 of file hashTable_tpl.h.

556 {
557 return _resize_policy_;
558 }

References _resize_policy_.

◆ set()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::set ( const Key & key,
const Val & default_value )

Add a new property or modify it if it already existed.

When used as a "dynamic property list", it may be convenient to use this function. Function set inserts a new pair (key,val) if the key does not already exists, or it changes the value associated with key if a pair (key,val) already exists in the hash table.

Parameters
keyThe key of the value to add or set.
default_valueThe value to set or add.

Definition at line 724 of file hashTable_tpl.h.

724 {
725 Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
726
727 if (bucket == nullptr) insert(key, value);
728 else bucket->val() = value;
729 }

References _hash_func_, _nodes_, insert(), key(), and gum::HashTableBucket< Key, Val >::val().

Here is the call graph for this function:

◆ setKeyUniquenessPolicy()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::setKeyUniquenessPolicy ( const bool new_policy)
noexcept

Enables the user to change dynamically the policy for checking whether there can exist several elements in the table with identical keys.

By default, we should always check that there does not exist duplicate keys. However, this test slows the insertion of elements in the table. So, when we know for sure that no duplicate key will be entered into the table, we may avoid uniqueness checks.

Warning
When setting the key policy to "uniqueness", the function does not check whether there are already different elements with identical keys in the table. It thus only ensures that elements inserted from now on will have unique keys.

Definition at line 561 of file hashTable_tpl.h.

561 {
563 }

References _key_uniqueness_policy_.

◆ setResizePolicy()

template<typename Key, typename Val>
INLINE void gum::HashTable< Key, Val >::setResizePolicy ( const bool new_policy)
noexcept

Enables the user to change dynamically the resizing policy.

In most cases, this should be useless. However, when available memory becomes rare, avoiding automatic resizing may speed-up new insertions in the table.

Warning
This function never resizes the hashtable by itself: even if you set the new policy to be an automatic resizing and the number of elements in the table is sufficiently high that we should resize the table, function setResizePolicy won't perform this resizing. The resizing will happen only if you insert a new element or if use method resize.
Parameters
new_policyThe new resizing policy, true implies automatic resizing.

Definition at line 551 of file hashTable_tpl.h.

551 {
553 }

References _resize_policy_.

◆ size()

template<typename Key, typename Val>
INLINE Size gum::HashTable< Key, Val >::size ( ) const
noexcept

Returns the number of elements stored into the hashtable.

The method runs in constant time.

Returns
Returns the number of elements stored into the hashtable.

Definition at line 536 of file hashTable_tpl.h.

536 {
537 return _nb_elements_;
538 }

References _nb_elements_.

Referenced by HashTable(), _create_(), and gum::Set< Key >::operator*().

Here is the caller graph for this function:

◆ Bijection

template<typename Key, typename Val>
template<typename T1, typename T2>
friend class Bijection
friend

For bijections to quickly access data.

Definition at line 1462 of file hashTable.h.

◆ HashTableConstIterator< Key, Val >

template<typename Key, typename Val>
friend class HashTableConstIterator< Key, Val >
friend

Friends to optimize the access to data, iterators must be friends.

Definition at line 1441 of file hashTable.h.

◆ HashTableConstIteratorSafe< Key, Val >

template<typename Key, typename Val>
friend class HashTableConstIteratorSafe< Key, Val >
friend

Friends to optimize the access to data, iterators must be friends.

Definition at line 1441 of file hashTable.h.

◆ HashTableIterator< Key, Val >

template<typename Key, typename Val>
friend class HashTableIterator< Key, Val >
friend

Friends to optimize the access to data, iterators must be friends.

Definition at line 1441 of file hashTable.h.

◆ HashTableIteratorSafe< Key, Val >

template<typename Key, typename Val>
friend class HashTableIteratorSafe< Key, Val >
friend

Friends to optimize the access to data, iterators must be friends.

Definition at line 1441 of file hashTable.h.

◆ operator<< [1/2]

template<typename Key, typename Val>
std::ostream & operator<< ( std::ostream & s,
const HashTable< Key *, Val > & table )
friend

Prints the content of a gum::HashTable with pointers key in the stream.

Definition at line 990 of file hashTable_tpl.h.

990 {
991 bool deja = false;
992 stream << "{";
993
994 for (Size i = Size(0); i < table._size_; ++i)
995 for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
996 if (deja) stream << " , ";
997
998 stream << ptr->key() << "=>" << ptr->val();
999
1000 deja = true;
1001 }
1002
1003 stream << "}";
1004
1005 return stream;
1006 }

◆ operator<< [2/2]

template<typename Key, typename Val>
std::ostream & operator<< ( std::ostream & s,
const HashTable< Key, Val > & table )
friend

Prints the content of a gum::HashTable in the stream.

Definition at line 971 of file hashTable_tpl.h.

971 {
972 bool deja = false;
973 stream << "{";
974
975 for (Size i = Size(0); i < table._size_; ++i)
976 for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
977 if (deja) stream << " , ";
978
979 stream << ptr->key() << "=>" << ptr->val();
980
981 deja = true;
982 }
983
984 stream << "}";
985
986 return stream;
987 }

Member Data Documentation

◆ _begin_index_

template<typename Key, typename Val>
Size gum::HashTable< Key, Val >::_begin_index_ {std::numeric_limits< Size >::max()}
mutableprivate

Returns where the begin index should be.

Beware: the beginning of a HashTable is the end of its nodes vector, i.e., the Bucket at the highest index in nodes. This enables a slightly faster parsing than if it were the lowest index.

Warning
std::numeric_limits<Size>::max() means that we do not know where the beginning of the table really is (this can mean either that there is not yet any element in the hash table or that an erase operation has been performed and that we lost track of the element that should correspond to the begin().
Returns
Returns where the begin index should be.

Definition at line 1500 of file hashTable.h.

Referenced by HashTable(), HashTable(), _erase_(), _insert_(), and resize().

◆ _hash_func_

template<typename Key, typename Val>
HashFunc< Key > gum::HashTable< Key, Val >::_hash_func_
private

The function used to hash keys (may change when the table is resized).

Definition at line 1478 of file hashTable.h.

Referenced by HashTable(), _create_(), _insert_(), exists(), getWithDefault(), getWithDefault(), key(), operator[](), operator[](), resize(), and set().

◆ _key_uniqueness_policy_

template<typename Key, typename Val>
bool gum::HashTable< Key, Val >::_key_uniqueness_policy_ {true}
private

Shall we check for key uniqueness in the table?

Definition at line 1484 of file hashTable.h.

1484{true};

Referenced by HashTable(), HashTable(), HashTable(), _insert_(), keyUniquenessPolicy(), and setKeyUniquenessPolicy().

◆ _nb_elements_

template<typename Key, typename Val>
Size gum::HashTable< Key, Val >::_nb_elements_ {Size(0)}
private

Number of elements of type Val stored in the hash table.

Definition at line 1475 of file hashTable.h.

1475{Size(0)};

Referenced by HashTable(), _copy_(), _erase_(), _insert_(), beginSafe(), beginSafe(), cbegin(), cbeginSafe(), empty(), resize(), and size().

◆ _nodes_

template<typename Key, typename Val>
std::vector< HashTableList< Key, Val > > gum::HashTable< Key, Val >::_nodes_
private

The hash table is represented as a vector of chained lists.

' __nodes' is this very vector.

Definition at line 1469 of file hashTable.h.

Referenced by HashTable(), _copy_(), _create_(), _erase_(), _insert_(), exists(), getWithDefault(), getWithDefault(), key(), gum::HashTableList< Key, Val >::operator<<, gum::HashTableList< Key, Val >::operator<<, operator[](), operator[](), resize(), and set().

◆ _resize_policy_

template<typename Key, typename Val>
bool gum::HashTable< Key, Val >::_resize_policy_ {true}
private

Is resizing performed automatically?

Definition at line 1481 of file hashTable.h.

1481{true};

Referenced by HashTable(), HashTable(), HashTable(), _insert_(), resize(), resizePolicy(), and setResizePolicy().

◆ _safe_iterators_

template<typename Key, typename Val>
std::vector< HashTableConstIteratorSafe< Key, Val >* > gum::HashTable< Key, Val >::_safe_iterators_
mutableprivate

The list of safe iterators pointing to the hash table.

Definition at line 1503 of file hashTable.h.

Referenced by HashTable(), _clearIterators_(), _erase_(), and resize().

◆ _size_

template<typename Key, typename Val>
Size gum::HashTable< Key, Val >::_size_
private

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