aGrUM  0.14.2
gum::HashTable< Key, Val, Alloc > Class Template Reference

The class for generic Hash Tables. More...

#include <agrum/core/hashTable.h>

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

Public Member Functions

template<typename... Args>
INLINE HashTable< Key, Val, Alloc >::value_typeemplace (Args &&... args)
 
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE map (Mount(*f)(Val), Size size, bool resize_pol, bool key_uniqueness_pol) const
 
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE map (Mount(*f)(Val &), Size size, bool resize_pol, bool key_uniqueness_pol) const
 
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE map (Mount(*f)(const Val &), Size size, bool resize_pol, bool key_uniqueness_pol) const
 
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE map (const Mount &val, Size size, bool resize_pol, bool key_uniqueness_pol) const
 
template<typename OtherAlloc >
INLINE bool operator!= (const HashTable< Key, Val, OtherAlloc > &from) 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. More...
 
 HashTable (std::initializer_list< std::pair< Key, Val > > list)
 Initializer list constructor. More...
 
 HashTable (const HashTable< Key, Val, Alloc > &from)
 Copy constructor. More...
 
template<typename OtherAlloc >
 HashTable (const HashTable< Key, Val, OtherAlloc > &from)
 Generalized copy constructor. More...
 
 HashTable (HashTable< Key, Val, Alloc > &&from)
 Move constructor. More...
 
 ~HashTable ()
 Class destructor. More...
 
Operators
HashTable< Key, Val, Alloc > & operator= (const HashTable< Key, Val, Alloc > &from)
 Copy operator. More...
 
template<typename OtherAlloc >
HashTable< Key, Val, Alloc > & operator= (const HashTable< Key, Val, OtherAlloc > &from)
 Generalized copy operator. More...
 
HashTable< Key, Val, Alloc > & operator= (HashTable< Key, Val, Alloc > &&from)
 Move operator. More...
 
Val & operator[] (const Key &key)
 Returns a reference on the value the key of which is passed in argument. More...
 
const Val & operator[] (const Key &key) const
 returns a reference on the value the key of which is passed in argument More...
 
template<typename OtherAlloc >
bool operator== (const HashTable< Key, Val, OtherAlloc > &from) const
 Checks whether two hashtables contain the same elements. More...
 
template<typename OtherAlloc >
bool operator!= (const HashTable< Key, Val, OtherAlloc > &from) const
 Checks whether two hashtables contain different sets of elements. More...
 
Fine tuning
Size capacity () const noexcept
 Returns the number of slots in the 'nodes' vector of the hashtable. More...
 
void resize (Size new_size)
 Changes the number of slots in the 'nodes' vector of the hash table. More...
 
void setResizePolicy (const bool new_policy) noexcept
 Enables the user to change dynamically the resizing policy. More...
 
bool resizePolicy () const noexcept
 Returns the current resizing policy. More...
 
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. More...
 
bool keyUniquenessPolicy () const noexcept
 Returns the current checking policy. More...
 
Accessors / Modifiers
Size size () const noexcept
 Returns the number of elements stored into the hashtable. More...
 
bool exists (const Key &key) const
 Checks whether there exists an element with a given key in the hashtable. More...
 
value_typeinsert (const Key &key, const Val &val)
 Adds a new element (actually a copy of this element) into the hash table. More...
 
value_typeinsert (Key &&key, Val &&val)
 Moves a new element in the hash table. More...
 
value_typeinsert (const std::pair< Key, Val > &elt)
 Adds a new element (actually a copy of this element) into the hash table. More...
 
value_typeinsert (std::pair< Key, Val > &&elt)
 Moves a new element in the hash table. More...
 
template<typename... Args>
value_typeemplace (Args &&... args)
 Emplace a new element into the hashTable. More...
 
mapped_typegetWithDefault (const Key &key, const Val &default_value)
 Returns a reference on the element the key of which is passed in argument. More...
 
mapped_typegetWithDefault (Key &&key, Val &&default_value)
 Returns a reference on the element the key of which is passed in argument. More...
 
void set (const Key &key, const Val &default_value)
 Add a new property or modify it if it already existed. More...
 
void reset (const Key &key)
 Removes a property (i.e., remove an element). More...
 
void erase (const Key &key)
 Removes a given element from the hash table. More...
 
void erase (const iterator_safe &iter)
 Removes a given element from the hash table. More...
 
void erase (const const_iterator_safe &iter)
 Removes a given element from the hash table. More...
 
void eraseByVal (const Val &val)
 Removes a given element from the hash table. More...
 
const Key & keyByVal (const Val &val) const
 Returns a reference on the key given a value. More...
 
const Key & key (const Key &key) const
 Returns a reference on a given key. More...
 
void eraseAllVal (const Val &val)
 Removes all the elements having a certain value from the hash table. More...
 
void clear ()
 Removes all the elements in the hash table. More...
 
bool empty () const noexcept
 Indicates whether the hash table is empty. More...
 
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > 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. More...
 
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > 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. More...
 
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > 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. More...
 
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > 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. More...
 

Public Types

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

Friends

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

Iterators

const iteratorend () noexcept
 Returns the unsafe iterator pointing to the end of the hashtable. More...
 
const const_iteratorend () const noexcept
 Returns the unsafe const_iterator pointing to the end of the hashtable. More...
 
const const_iteratorcend () const noexcept
 Returns the unsafe const_iterator pointing to the end of the hashtable. More...
 
iterator begin ()
 Returns an unsafe iterator pointing to the beginning of the hashtable. More...
 
const_iterator begin () const
 Returns an unsafe const_iterator pointing to the beginning of the hashtable. More...
 
const_iterator cbegin () const
 Returns an unsafe const_iterator pointing to the beginning of the hashtable. More...
 
const iterator_safeendSafe () noexcept
 Returns the safe iterator pointing to the end of the hashtable. More...
 
const const_iterator_safeendSafe () const noexcept
 Returns the safe const_iterator pointing to the end of the hashtable. More...
 
const const_iterator_safecendSafe () const noexcept
 Returns the safe const_iterator pointing to the end of the hashtable. More...
 
iterator_safe beginSafe ()
 Returns the safe iterator pointing to the beginning of the hashtable. More...
 
const_iterator_safe beginSafe () const
 Returns the safe const_iterator pointing to the beginning of the hashtable. More...
 
const_iterator_safe cbeginSafe () const
 Returns the safe const_iterator pointing to the beginning of the hashtable. More...
 
static const iteratorend4Statics ()
 Returns the end iterator for other classes' statics (read the detailed description of this method). More...
 
static const const_iteratorconstEnd4Statics ()
 Returns the end iterator for other classes' statics (read the detailed description of this method). More...
 
static const iterator_safeendSafe4Statics ()
 Returns the end iterator for other classes' statics (read the detailed description of this method). More...
 
static const const_iterator_safeconstEndSafe4Statics ()
 Returns the end iterator for other classes' statics (read the detailed description of this method). More...
 

Detailed Description

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
class gum::HashTable< Key, Val, Alloc >

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
HashTable<int,string> table1;
// 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 wether 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 );
Template Parameters
KeyThe type for keys in a gum::HashTable.
ValThe type for values in a gum::HashTable.
AllocThe gum::HashTable allocator.

Definition at line 676 of file hashTable.h.

Member Typedef Documentation

◆ allocator_type

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::allocator_type = Alloc

Types for STL compliance.

Definition at line 689 of file hashTable.h.

◆ Bucket

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::Bucket = HashTableBucket< Key, Val >

The buckets where data are stored.

Definition at line 697 of file hashTable.h.

◆ BucketAllocator

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::BucketAllocator = typename Alloc::template rebind< Bucket >::other

The Bucket allocator.

Definition at line 700 of file hashTable.h.

◆ const_iterator

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::const_iterator = HashTableConstIterator< Key, Val >

Types for STL compliance.

Definition at line 691 of file hashTable.h.

◆ const_iterator_safe

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::const_iterator_safe = HashTableConstIteratorSafe< Key, Val >

Types for STL compliance.

Definition at line 693 of file hashTable.h.

◆ const_pointer

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

Types for STL compliance.

Definition at line 686 of file hashTable.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 684 of file hashTable.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 688 of file hashTable.h.

◆ iterator

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::iterator = HashTableIterator< Key, Val >

Types for STL compliance.

Definition at line 690 of file hashTable.h.

◆ iterator_safe

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::iterator_safe = HashTableIteratorSafe< Key, Val >

Types for STL compliance.

Definition at line 692 of file hashTable.h.

◆ key_type

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::key_type = Key

Types for STL compliance.

Definition at line 680 of file hashTable.h.

◆ mapped_type

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::mapped_type = Val

Types for STL compliance.

Definition at line 681 of file hashTable.h.

◆ pointer

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::pointer = value_type*

Types for STL compliance.

Definition at line 685 of file hashTable.h.

◆ reference

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::reference = value_type&

Types for STL compliance.

Definition at line 683 of file hashTable.h.

◆ size_type

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
using gum::HashTable< Key, Val, Alloc >::size_type = Size

Types for STL compliance.

Definition at line 687 of file hashTable.h.

◆ value_type

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

Types for STL compliance.

Definition at line 682 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTable() [1/5]

template<typename Key , typename Val , typename Alloc >
gum::HashTable< Key, Val, Alloc >::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 380 of file hashTable_tpl.h.

382  :
383  // size must be >= 2 else we lose all the bits of the hash function
384  __size{Size(1) << __hashTableLog2(std::max(Size(2), size_param))},
385  __resize_policy{resize_pol}, __key_uniqueness_policy{key_uniqueness_pol} {
386  // for debugging purposes
387  GUM_CONSTRUCTOR(HashTable);
388 
389  // finalize the creation
390  __create(__size);
391  }
unsigned int __hashTableLog2(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb...
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
void __create(Size size)
Used by all default constructors (general and specialized).
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ HashTable() [2/5]

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

Initializer list constructor.

Parameters
listThe initialized list.

Definition at line 394 of file hashTable_tpl.h.

395  :
396  // size must be >= 2 else we lose all the bits of the hash function
398  std::max< Size >(Size(2), Size(list.size()) / 2))} {
399  // for debugging purposes
400  GUM_CONSTRUCTOR(HashTable);
401 
402  // setup the __nodes vector (contains only empty lists)
403  __create(__size);
404 
405  // insert all the elements
406  for (const auto& elt : list) {
407  insert(elt);
408  }
409  }
unsigned int __hashTableLog2(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb...
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
void __create(Size size)
Used by all default constructors (general and specialized).
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ HashTable() [3/5]

template<typename Key, typename Val, typename Alloc>
gum::HashTable< Key, Val, Alloc >::HashTable ( const HashTable< Key, Val, Alloc > &  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 412 of file hashTable_tpl.h.

413  :
414  __size{table.__size},
415  __resize_policy{table.__resize_policy},
416  __key_uniqueness_policy{table.__key_uniqueness_policy},
417  __begin_index{table.__begin_index} {
418  // for debugging purposes
419  GUM_CONS_CPY(HashTable);
420 
421  // setup the __nodes vector (contains only empty lists)
422  __create(__size);
423 
424  // fill with the content of table
425  __copy(table);
426  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
void __create(Size size)
Used by all default constructors (general and specialized).
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void __copy(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.

◆ HashTable() [4/5]

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

Generalized 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 'table'

Parameters
fromThe gum::HashTable to copy.

Definition at line 430 of file hashTable_tpl.h.

431  :
432  __size{table.__size},
433  __resize_policy{table.__resize_policy},
434  __key_uniqueness_policy{table.__key_uniqueness_policy},
435  __begin_index{table.__begin_index} {
436  // for debugging purposes
437  GUM_CONS_CPY(HashTable);
438 
439  // setup the __nodes vector (contains only empty lists)
440  __create(__size);
441 
442  // fill with the content of table
443  __copy(table);
444  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
void __create(Size size)
Used by all default constructors (general and specialized).
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void __copy(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.

◆ HashTable() [5/5]

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

Move constructor.

Parameters
fromThe gum::HashTable to move.

Definition at line 447 of file hashTable_tpl.h.

447  :
448  __nodes(std::move(table.__nodes)), __size{table.__size},
449  __nb_elements{table.__nb_elements}, __hash_func{table.__hash_func},
450  __resize_policy{table.__resize_policy},
451  __key_uniqueness_policy{table.__key_uniqueness_policy},
452  __begin_index{table.__begin_index},
453  __safe_iterators(std::move(table.__safe_iterators)),
454  __alloc(std::move(table.__alloc)) {
455  // for debugging purposes
456  table.__size = 0;
457  GUM_CONS_MOV(HashTable);
458  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746

◆ ~HashTable()

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

Class destructor.

Definition at line 482 of file hashTable_tpl.h.

482  {
483  // for debugging purposes
484  GUM_DESTRUCTOR(HashTable);
485 
486  // update all the registered iterators: they should now point to nullptr
487  // and their hashtable should be set to nullptr
489  }
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
void __clearIterators()
Clear all the safe iterators.

Member Function Documentation

◆ __clearIterators()

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

Clear all the safe iterators.

Definition at line 461 of file hashTable_tpl.h.

461  {
462  const Size len = __safe_iterators.size();
463  for (Size i = Size(0); i < len; ++i)
464  __safe_iterators[i]->clear();
465  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ __copy()

template<typename Key, typename Val, typename Alloc >
template<typename OtherAlloc >
void gum::HashTable< Key, Val, Alloc >::__copy ( const HashTable< Key, Val, OtherAlloc > &  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.
Template Parameters
OtherAllocThe other gum::HashTable allocator.

Definition at line 308 of file hashTable_tpl.h.

309  {
310  // in debug mode, check that this and table have '__nodes' arrays of the
311  // same size
312  GUM_ASSERT(table.__size == __size);
313 
314  // try to fill the array of chained lists
315  for (Size i = 0; i < table.__size; ++i) {
316  try {
317  __nodes[i] = table.__nodes[i];
318  } catch (...) {
319  // here we could allocate the __nodes[j], j=0..i-1, so we should
320  // deallocate them
321  for (Size j = 0; j < __size; ++j)
322  __nodes[j].clear();
323 
324  __nb_elements = Size(0);
325 
326  // propagate the exception
327  throw;
328  }
329  }
330 
331  __nb_elements = table.__nb_elements;
332  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ __create()

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

Used by all default constructors (general and specialized).

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

Definition at line 363 of file hashTable_tpl.h.

363  {
364  // setup the __nodes vector (contains only empty lists)
365  __nodes.resize(size);
366 
367  for (auto& list : __nodes) {
368  list.setAllocator(__alloc);
369  }
370 
371  // set up properly the hash function
372  __hash_func.resize(size);
373 
374  // make sure the end() iterator is constructed properly
375  end4Statics();
376  endSafe4Statics();
377  }
static const iterator_safe & endSafe4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
Size size() const noexcept
Returns the number of elements stored into the hashtable.
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
static const iterator & end4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...

◆ __erase()

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

Erases a given bucket.

Definition at line 989 of file hashTable_tpl.h.

990  {
991  if (bucket == nullptr) return;
992 
993  // update the registered iterators pointing to this bucket
994  for (auto iter : __safe_iterators) {
995  if (iter->__bucket == bucket) {
996  iter->operator++();
997  iter->__next_bucket = iter->__bucket;
998  iter->__bucket = nullptr;
999  } else if (iter->__next_bucket == bucket) {
1000  iter->__bucket = bucket;
1001  iter->operator++();
1002  iter->__next_bucket = iter->__bucket;
1003  iter->__bucket = nullptr;
1004  }
1005  }
1006 
1007  // remove the element from the __nodes vector
1008  __nodes[index].erase(bucket);
1009 
1010  --__nb_elements;
1011 
1012  if ((index == __begin_index) && __nodes[index].empty()) {
1013  __begin_index = std::numeric_limits< Size >::max();
1014  }
1015  }
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
bool empty() const noexcept
Indicates whether the hash table is empty.

◆ __insert()

template<typename Key , typename Val , typename Alloc >
void gum::HashTable< Key, Val, Alloc >::__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 838 of file hashTable_tpl.h.

838  {
839  Size hash_key = __hash_func(bucket->key());
840 
841  // check that there does not already exist an element with the same key
842  if (__key_uniqueness_policy && __nodes[hash_key].exists(bucket->key())) {
843  // remove the bucket from memory
844  Key k = bucket->key();
845  __alloc.destroy(bucket);
846  __alloc.deallocate(bucket, 1);
847  GUM_ERROR(DuplicateElement,
848  "the hashtable contains an element with the same key (" << k
849  << ")");
850  }
851 
852  // check whether there is sufficient space to insert the new pair
853  // if not, resize the current hashtable
854  if (__resize_policy
856  resize(__size << 1);
857  hash_key = __hash_func(bucket->key());
858  }
859 
860  // add the new pair
861  __nodes[hash_key].insert(bucket);
862  ++__nb_elements;
863 
864  // recompute the index of the beginning of the hashtable if possible
865  // WARNING: if __begin_index = std::numeric_limits<Size>::max (), we CANNOT
866  // recompute the index because we cannot know whether the current index is
867  // equal to max because there was no element in the hashtable or whether a
868  // previous __erase() has set the index to max.
869  if (__begin_index < hash_key) { __begin_index = hash_key; }
870  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition: hashTable.h:84
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ begin() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::iterator gum::HashTable< Key, Val, Alloc >::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 633 of file hashTable_tpl.h.

Referenced by gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::learning::Miic::_orientation_3off2(), gum::learning::Miic::_orientation_miic(), gum::Estimator< GUM_SCALAR >::confidence(), and gum::SmallObjectAllocator::~SmallObjectAllocator().

633  {
634  // if the table is empty, make the begin and end point to the same element
635  if (__nb_elements == Size(0))
636  return iterator{end()};
637  else
638  return iterator{*this};
639  }
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:690
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ begin() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::const_iterator gum::HashTable< Key, Val, Alloc >::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 643 of file hashTable_tpl.h.

643  {
644  // if the table is empty, make the begin and end point to the same element
645  if (__nb_elements == Size(0))
646  return const_iterator{end()};
647  else
648  return const_iterator{*this};
649  }
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:691

◆ beginSafe() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::iterator_safe gum::HashTable< Key, Val, Alloc >::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 693 of file hashTable_tpl.h.

Referenced by gum::SetTerminalNodePolicy< GUM_SCALAR >::clearAllTerminalNodes(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::LeafAggregator::leavesMap(), gum::StructuredPlaner< double >::optimalPolicy2String(), gum::SetTerminalNodePolicy< GUM_SCALAR >::terminalNodeId(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::toDot(), gum::ITI< AttributeSelection, isScalar >::updateGraph(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::~IncrementalGraphLearner(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::~MultiDimFunctionGraphOperator(), and gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::~Regress().

693  {
694  // if the table is empty, make the begin and end point to the same element
695  if (__nb_elements == Size(0))
696  return iterator_safe{endSafe()};
697  else
698  return iterator_safe{*this};
699  }
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:692
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ beginSafe() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::const_iterator_safe gum::HashTable< Key, Val, Alloc >::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 703 of file hashTable_tpl.h.

703  {
704  // if the table is empty, make the begin and end point to the same element
705  if (__nb_elements == Size(0))
706  return const_iterator_safe{endSafe()};
707  else
708  return const_iterator_safe{*this};
709  }
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:693
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ capacity()

template<typename Key , typename Val , typename Alloc >
INLINE Size gum::HashTable< Key, Val, Alloc >::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 738 of file hashTable_tpl.h.

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

738  {
739  return __size;
740  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
+ Here is the caller graph for this function:

◆ cbegin()

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::const_iterator gum::HashTable< Key, Val, Alloc >::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 653 of file hashTable_tpl.h.

Referenced by gum::DAGCycleDetector::__addWeightedSet(), gum::BijectionImplementation< Idx, const std::string *, std::allocator< const std::string * >, std::is_scalar< Idx >::value &&std::is_scalar< const std::string * >::value >::__copy(), gum::DAGCycleDetector::__delWeightedSet(), gum::DAGCycleDetector::__restrictWeightedSet(), gum::credal::InferenceEngine< GUM_SCALAR >::_updateCredalSets(), gum::DAGCycleDetector::addArc(), gum::DAGCycleDetector::eraseArc(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator+(), and gum::learning::StructuralConstraintSliceOrder::StructuralConstraintSliceOrder().

653  {
654  // if the table is empty, make the begin and end point to the same element
655  if (__nb_elements == Size(0))
656  return const_iterator{cend()};
657  else
658  return const_iterator{*this};
659  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:691
+ Here is the caller graph for this function:

◆ cbeginSafe()

template<typename Key , typename Val , typename Alloc >
INLINE HashTable< Key, Val, Alloc >::const_iterator_safe gum::HashTable< Key, Val, Alloc >::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 713 of file hashTable_tpl.h.

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), gum::LeastSquareTestPolicy< GUM_SCALAR >::add(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrABeginSafe(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrBBeginSafe(), and gum::SetTerminalNodePolicy< GUM_SCALAR >::beginValues().

713  {
714  // if the table is empty, make the begin and end point to the same element
715  if (__nb_elements == Size(0))
716  return const_iterator_safe{cendSafe()};
717  else
718  return const_iterator_safe{*this};
719  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:693
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ cend()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator & gum::HashTable< Key, Val, Alloc >::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 623 of file hashTable_tpl.h.

Referenced by gum::DAGCycleDetector::__addWeightedSet(), gum::BijectionImplementation< Idx, const std::string *, std::allocator< const std::string * >, std::is_scalar< Idx >::value &&std::is_scalar< const std::string * >::value >::__copy(), gum::DAGCycleDetector::__delWeightedSet(), gum::DAGCycleDetector::__restrictWeightedSet(), gum::credal::InferenceEngine< GUM_SCALAR >::_updateCredalSets(), gum::DAGCycleDetector::addArc(), gum::DAGCycleDetector::eraseArc(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator+(), and gum::learning::StructuralConstraintSliceOrder::StructuralConstraintSliceOrder().

623  {
624  // note that, here, we know for sure that HashTableIterEnd has been properly
625  // initialized as it is initialized by end4Statics, which is called by
626  // all hashtables' constructors
627  return *(reinterpret_cast< const const_iterator* >(
629  }
static const HashTableIterator< int, int > * __HashTableIterEnd
The unsafe iterator used by everyone.
Definition: hashTable.h:1831
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:691
+ Here is the caller graph for this function:

◆ cendSafe()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator_safe & gum::HashTable< Key, Val, Alloc >::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 683 of file hashTable_tpl.h.

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), gum::LeastSquareTestPolicy< GUM_SCALAR >::add(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrAEndSafe(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrBEndSafe(), gum::SetTerminalNodePolicy< GUM_SCALAR >::hasValue(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

683  {
684  // note that, here, we know for sure that HashTableIterEnd has been properly
685  // initialized as it is initialized by end4Statics, which is called by
686  // all hashtables' constructors
687  return *(reinterpret_cast< const const_iterator_safe* >(
689  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:693
static const HashTableIteratorSafe< int, int > * __HashTableIterEndSafe
The safe iterator used by everyone.
Definition: hashTable.h:1834
+ Here is the caller graph for this function:

◆ clear()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 468 of file hashTable_tpl.h.

Referenced by gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::credal::InferenceEngine< GUM_SCALAR >::_initExpectations(), gum::credal::InferenceEngine< GUM_SCALAR >::_initMarginals(), gum::credal::InferenceEngine< GUM_SCALAR >::_initMarginalSets(), gum::credal::MultipleInferenceEngine< GUM_SCALAR, BNInferenceEngine >::_initThreadsData(), gum::credal::InferenceEngine< GUM_SCALAR >::_repetitiveInit(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::Estimator< GUM_SCALAR >::clear(), gum::SetTerminalNodePolicy< GUM_SCALAR >::clearAllTerminalNodes(), gum::credal::InferenceEngine< GUM_SCALAR >::eraseAllEvidence(), gum::MultiDimSparse< GUM_SCALAR >::fill(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidence(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidenceFile(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQuery(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQueryFile(), and gum::Chi2::setConfidenceProba().

468  {
469  // update all the registered iterators: they should now point to nullptr
470  // and they are positioned to the end of the hashtable.
472 
473  // remove the buckets
474  for (Size i = Size(0); i < __size; ++i)
475  __nodes[i].clear();
476 
477  __nb_elements = Size(0);
478  __begin_index = std::numeric_limits< Size >::max();
479  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
void __clearIterators()
Clear all the safe iterators.
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ constEnd4Statics()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator & gum::HashTable< Key, Val, Alloc >::constEnd4Statics ( )
static

Returns the end iterator for other classes' statics (read the detailed description of this method).

To reduce memory consumption of hash tables (which are heavily used in aGrUM) while allowing fast for(iter=begin(); iter!=end();++iter) loops, end iterators are created just once as a static member of a non-template hashtable. While this scheme is efficient and it works quite effectively when manipulating hashtables, it has a drawback: other classes with static members using the HashTable's end() iterator may fail to work due to the well known "static initialization order fiasco" (see Marshall Cline's C++ FAQ for more details about this C++ feature). OK, so what is the problem? Consider for instance class Set. A Set contains a hashtable that stores all its elements in a convenient way. To reduce memory consumption, Set::end iterator is a static member that is initialized with a HashTable's end iterator. If the compiler decides to initialize Set::end before initializing HashTable::end, then Set::end will be in an incoherent state. Unfortunately, we cannot know for sure in which order static members will be initialized (the order is a compiler's decision). Hence, we shall enforce the fact that HashTable::end is initialized before Set::end. Using method HashTable::end4Statics will ensure this fact: it uses the C++ "construct on first use" idiom (see the C++ FAQ) that ensures that the order fiasco is avoided. More precisely, end4Statics initializes a global variable that is the very end iterator used by all hashtables. Now, this induces a small overhead. So, we also provide a HashTable::end() method that returns the end iterator without this small overhead, but assuming that function end4Statics has already been called once (which is always the case) when a hashtable has been created.

So, to summarize: when initializing static members, use constEnd4Statics() rather than cend(). In all the other cases, use simply the usual method cend().

Returns
Returns the end iterator for other classes' statics (read the detailed description of this method).

Definition at line 343 of file hashTable_tpl.h.

343  {
344  return *(reinterpret_cast< const const_iterator* >(
346  }
static const HashTableConstIterator< int, int > * constEnd4Statics()
Creates (if needed) and returns the iterator __HashTableIterEnd.
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:691

◆ constEndSafe4Statics()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator_safe & gum::HashTable< Key, Val, Alloc >::constEndSafe4Statics ( )
static

Returns the end iterator for other classes' statics (read the detailed description of this method).

To reduce memory consumption of hash tables (which are heavily used in aGrUM) while allowing fast for(iter=begin(); iter!=end();++iter) loops, end iterators are created just once as a static member of a non-template hashtable. While this scheme is efficient and it works quite effectively when manipulating hashtables, it has a drawback: other classes with static members using the HashTable's end() iterator may fail to work due to the well known "static initialization order fiasco" (see Marshall Cline's C++ FAQ for more details about this C++ feature). OK, so what is the problem? Consider for instance class Set. A Set contains a hashtable that stores all its elements in a convenient way. To reduce memory consumption, Set::end iterator is a static member that is initialized with a HashTable's end iterator. If the compiler decides to initialize Set::end before initializing HashTable::end, then Set::end will be in an incoherent state. Unfortunately, we cannot know for sure in which order static members will be initialized (the order is a compiler's decision). Hence, we shall enforce the fact that HashTable::end is initialized before Set::end. Using method HashTable::end4Statics will ensure this fact: it uses the C++ "construct on first use" idiom (see the C++ FAQ) that ensures that the order fiasco is avoided. More precisely, end4Statics initializes a global variable that is the very end iterator used by all hashtables. Now, this induces a small overhead. So, we also provide a HashTable::end() method that returns the end iterator without this small overhead, but assuming that function end4Statics has already been called once (which is always the case) when a hashtable has been created.

So, to summarize: when initializing static members, use constEndSafe4Statics() rather than cendSafe(). In all the other cases, use simply the usual method cendSafe().

Returns
Returns the end iterator for other classes' statics (read the detailed description of this method).

Definition at line 357 of file hashTable_tpl.h.

357  {
358  return *(reinterpret_cast< const const_iterator_safe* >(
360  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:693
static const HashTableConstIteratorSafe< int, int > * constEndSafe4Statics()
Creates (if needed) and returns the iterator __HashTableIterEndSafe.

◆ emplace() [1/2]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename... Args>
INLINE HashTable< Key, Val, Alloc >::value_type& gum::HashTable< Key, Val, Alloc >::emplace ( Args &&...  args)

Definition at line 939 of file hashTable_tpl.h.

939  {
940  Bucket* bucket = __alloc.allocate(1);
941 
942  try {
943  __alloc.construct(bucket,
945  std::forward< Args >(args)...);
946  } catch (...) {
947  __alloc.deallocate(bucket, 1);
948  throw;
949  }
950 
951  __insert(bucket);
952  return bucket->elt();
953  }
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:697
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760

◆ emplace() [2/2]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename... Args>
value_type& gum::HashTable< Key, Val, Alloc >::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.

◆ empty()

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

Indicates whether the hash table is empty.

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

Definition at line 1083 of file hashTable_tpl.h.

Referenced by gum::credal::InferenceEngine< GUM_SCALAR >::_dynamicExpectations(), gum::prm::PRMFactory< GUM_SCALAR >::addInstance(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidence(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidenceFile(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQuery(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQueryFile(), gum::MultiDimFunctionGraphProjector< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::project(), gum::BayesBall::relevantPotentials(), and gum::credal::InferenceEngine< GUM_SCALAR >::toString().

1083  {
1084  return (__nb_elements == Size(0));
1085  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ end() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::iterator & gum::HashTable< Key, Val, Alloc >::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 603 of file hashTable_tpl.h.

Referenced by gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::learning::Miic::_orientation_3off2(), gum::learning::Miic::_orientation_miic(), gum::Estimator< GUM_SCALAR >::confidence(), and gum::SmallObjectAllocator::~SmallObjectAllocator().

603  {
604  // note that, here, we know for sure that HashTableIterEnd has been properly
605  // initialized as it is initialized by end4Statics, which is called by
606  // all hashtables' constructors
607  return *(reinterpret_cast< const iterator* >(
609  }
static const HashTableIterator< int, int > * __HashTableIterEnd
The unsafe iterator used by everyone.
Definition: hashTable.h:1831
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:690
+ Here is the caller graph for this function:

◆ end() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator & gum::HashTable< Key, Val, Alloc >::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 613 of file hashTable_tpl.h.

613  {
614  // note that, here, we know for sure that HashTableIterEnd has been properly
615  // initialized as it is initialized by end4Statics, which is called by
616  // all hashtables' constructors
617  return *(reinterpret_cast< const const_iterator* >(
619  }
static const HashTableIterator< int, int > * __HashTableIterEnd
The unsafe iterator used by everyone.
Definition: hashTable.h:1831
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:691

◆ end4Statics()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::iterator & gum::HashTable< Key, Val, Alloc >::end4Statics ( )
static

Returns the end iterator for other classes' statics (read the detailed description of this method).

To reduce memory consumption of hash tables (which are heavily used in aGrUM) while allowing fast for(iter=begin(); iter!=end();++iter) loops, end iterators are created just once as a static member of a non-template hashtable. While this scheme is efficient and it works quite effectively when manipulating hashtables, it has a drawback: other classes with static members using the HashTable's end() iterator may fail to work due to the well known "static initialization order fiasco" (see Marshall Cline's C++ FAQ for more details about this C++ feature). OK, so what is the problem? Consider for instance class Set. A Set contains a hashtable that stores all its elements in a convenient way. To reduce memory consumption, Set::end iterator is a static member that is initialized with a HashTable's end iterator. If the compiler decides to initialize Set::end before initializing HashTable::end, then Set::end will be in an incoherent state. Unfortunately, we cannot know for sure in which order static members will be initialized (the order is a compiler's decision). Hence, we shall enforce the fact that HashTable::end is initialized before Set::end. Using method HashTable::end4Statics will ensure this fact: it uses the C++ "construct on first use" idiom (see the C++ FAQ) that ensures that the order fiasco is avoided. More precisely, end4Statics initializes a global variable that is the very end iterator used by all hashtables. Now, this induces a small overhead. So, we also provide a HashTable::end() method that returns the end iterator without this small overhead, but assuming that function end4Statics has already been called once (which is always the case) when a hashtable has been created.

So, to summarize: when initializing static members, use end4Statics() rather than end(). In all the other cases, use simply the usual method end().

Returns
Returns the end iterator for other classes' statics (read the detailed description of this method).

Definition at line 336 of file hashTable_tpl.h.

336  {
337  return *(reinterpret_cast< const iterator* >(
339  }
static const HashTableIterator< int, int > * end4Statics()
Creates (if needed) and returns the iterator __HashTableIterEnd.
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:690

◆ endSafe() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::iterator_safe & gum::HashTable< Key, Val, Alloc >::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 663 of file hashTable_tpl.h.

Referenced by gum::SetTerminalNodePolicy< GUM_SCALAR >::clearAllTerminalNodes(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::LeafAggregator::leavesMap(), gum::StructuredPlaner< double >::optimalPolicy2String(), gum::SetTerminalNodePolicy< GUM_SCALAR >::terminalNodeId(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::toDot(), gum::ITI< AttributeSelection, isScalar >::updateGraph(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::~IncrementalGraphLearner(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::~MultiDimFunctionGraphOperator(), and gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::~Regress().

663  {
664  // note that, here, we know for sure that HashTableIterEnd has been properly
665  // initialized as it is initialized by end4Statics, which is called by
666  // all hashtables' constructors
667  return *(reinterpret_cast< const iterator_safe* >(
669  }
static const HashTableIteratorSafe< int, int > * __HashTableIterEndSafe
The safe iterator used by everyone.
Definition: hashTable.h:1834
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:692
+ Here is the caller graph for this function:

◆ endSafe() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::const_iterator_safe & gum::HashTable< Key, Val, Alloc >::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 673 of file hashTable_tpl.h.

673  {
674  // note that, here, we know for sure that HashTableIterEnd has been properly
675  // initialized as it is initialized by end4Statics, which is called by
676  // all hashtables' constructors
677  return *(reinterpret_cast< const const_iterator_safe* >(
679  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:693
static const HashTableIteratorSafe< int, int > * __HashTableIterEndSafe
The safe iterator used by everyone.
Definition: hashTable.h:1834

◆ endSafe4Statics()

template<typename Key , typename Val , typename Alloc >
INLINE const HashTable< Key, Val, Alloc >::iterator_safe & gum::HashTable< Key, Val, Alloc >::endSafe4Statics ( )
static

Returns the end iterator for other classes' statics (read the detailed description of this method).

To reduce memory consumption of hash tables (which are heavily used in aGrUM) while allowing fast for(iter=begin(); iter!=end();++iter) loops, end iterators are created just once as a static member of a non-template hashtable. While this scheme is efficient and it works quite effectively when manipulating hashtables, it has a drawback: other classes with static members using the HashTable's end() iterator may fail to work due to the well known "static initialization order fiasco" (see Marshall Cline's C++ FAQ for more details about this C++ feature). OK, so what is the problem? Consider for instance class Set. A Set contains a hashtable that stores all its elements in a convenient way. To reduce memory consumption, Set::end iterator is a static member that is initialized with a HashTable's end iterator. If the compiler decides to initialize Set::end before initializing HashTable::end, then Set::end will be in an incoherent state. Unfortunately, we cannot know for sure in which order static members will be initialized (the order is a compiler's decision). Hence, we shall enforce the fact that HashTable::end is initialized before Set::end. Using method HashTable::end4Statics will ensure this fact: it uses the C++ "construct on first use" idiom (see the C++ FAQ) that ensures that the order fiasco is avoided. More precisely, end4Statics initializes a global variable that is the very end iterator used by all hashtables. Now, this induces a small overhead. So, we also provide a HashTable::end() method that returns the end iterator without this small overhead, but assuming that function end4Statics has already been called once (which is always the case) when a hashtable has been created.

So, to summarize: when initializing static members, use endSafe4Statics() rather than endSafe(). In all the other cases, use simply the usual method endSafe().

Returns
Returns the end iterator for other classes' statics (read the detailed description of this method).

Definition at line 350 of file hashTable_tpl.h.

350  {
351  return *(reinterpret_cast< const iterator_safe* >(
353  }
static const HashTableIteratorSafe< int, int > * endSafe4Statics()
Creates (if needed) and returns the iterator __HashTableIterEndSafe.
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:692

◆ erase() [1/3]

template<typename Key, typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 1018 of file hashTable_tpl.h.

Referenced by gum::DAGCycleDetector::__delWeightedSet(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_convertNode2Leaf(), gum::ITI< AttributeSelection, isScalar >::_removeNode(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_removeNode(), gum::SetTerminalNodePolicy< GUM_SCALAR >::eraseTerminalNode(), gum::DAGCycleDetector::hasCycleFromModifications(), and gum::BayesBall::relevantPotentials().

1018  {
1019  // get the hashed key
1020  Size hash = __hash_func(key);
1021 
1022  // get the bucket containing the element to erase
1023  HashTableBucket< Key, Val >* bucket = __nodes[hash].bucket(key);
1024 
1025  __erase(bucket, hash);
1026  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
void __erase(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ erase() [2/3]

template<typename Key, typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 1029 of file hashTable_tpl.h.

1029  {
1030  __erase(iter.__getBucket(), iter.__getIndex());
1031  }
void __erase(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.

◆ erase() [3/3]

template<typename Key, typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 1035 of file hashTable_tpl.h.

1035  {
1036  __erase(iter.__getBucket(), iter.__getIndex());
1037  }
void __erase(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.

◆ eraseAllVal()

template<typename Key , typename Val, typename Alloc >
void gum::HashTable< Key, Val, Alloc >::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 1074 of file hashTable_tpl.h.

1074  {
1075  for (auto iterAll = cbeginSafe(); iterAll != cendSafe(); ++iterAll) {
1076  if (iterAll.__bucket->val() == val) {
1077  __erase(iterAll.__bucket, iterAll.__index);
1078  }
1079  }
1080  }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void __erase(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.

◆ eraseByVal()

template<typename Key , typename Val, typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 1040 of file hashTable_tpl.h.

1040  {
1041  for (auto iter = cbegin(); iter != cend(); ++iter)
1042  if (iter.__bucket->val() == val) {
1043  __erase(iter.__getBucket(), iter.__getIndex());
1044  return;
1045  }
1046  }
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
void __erase(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.

◆ exists()

template<typename Key, typename Val , typename Alloc >
INLINE bool gum::HashTable< Key, Val, Alloc >::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 743 of file hashTable_tpl.h.

Referenced by gum::DAGCycleDetector::__addWeightedSet(), gum::prm::o3prm::O3ClassFactory< GUM_SCALAR >::__checkImplementation(), gum::BayesNetFactory< GUM_SCALAR >::__checkVariableName(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__compute(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__compute(), gum::DAGCycleDetector::__delWeightedSet(), gum::learning::Miic::__existsDirectedPath(), gum::prm::StructuredBayesBall< GUM_SCALAR >::__fromChild(), gum::prm::StructuredBayesBall< GUM_SCALAR >::__fromParent(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::__label(), gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), gum::StructuredPlaner< double >::__recurArgMaxCopy(), gum::StructuredPlaner< double >::__recurExtractOptPol(), gum::prm::PRMFactory< GUM_SCALAR >::__retrieveCommonType(), gum::credal::CredalNet< GUM_SCALAR >::__sort_varType(), gum::learning::Miic::_orientation_3off2(), gum::learning::Miic::_propagatesHead(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_removeNode(), gum::prm::PRMInference< double >::addEvidence(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::addObservation(), gum::SetTerminalNodePolicy< GUM_SCALAR >::addTerminalNode(), gum::SmallObjectAllocator::allocate(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrAMarginal(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrBMarginal(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::copyAndMultiplyByScalar(), gum::ArcGraphPart::directedPath(), gum::ArcGraphPart::directedUnorientedPath(), gum::SetTerminalNodePolicy< GUM_SCALAR >::eraseTerminalNode(), gum::HashTable< Val, Size, IndexAllocator >::exists(), gum::SetTerminalNodePolicy< GUM_SCALAR >::existsTerminalNodeWithId(), gum::MultiDimSparse< GUM_SCALAR >::get(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::isTerminal(), gum::ContingencyTable< Idx, GUM_SCALAR >::joint(), gum::LeafAggregator::leavesMap(), gum::MixedGraph::mixedOrientedPath(), gum::MixedGraph::mixedUnorientedPath(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*=(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator+(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator-(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator==(), gum::StructuredPlaner< double >::optimalPolicy2String(), gum::Estimator< GUM_SCALAR >::posterior(), gum::BayesBall::relevantPotentials(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClass(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClassElement(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveInterface(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveSlotType(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveType(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::EliminationSequenceStrategy::setGraph(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::toDot(), gum::credal::InferenceEngine< GUM_SCALAR >::toString(), gum::EdgeGraphPart::undirectedPath(), gum::Estimator< GUM_SCALAR >::update(), and gum::BayesNetFactory< GUM_SCALAR >::variableName().

743  {
744  return __nodes[__hash_func(key)].exists(key);
745  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715

◆ getWithDefault() [1/2]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::mapped_type & gum::HashTable< Key, Val, Alloc >::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 957 of file hashTable_tpl.h.

958  {
959  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
960 
961  if (bucket == nullptr)
962  return insert(key, default_value).second;
963  else
964  return bucket->val();
965  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:697
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ getWithDefault() [2/2]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::mapped_type & gum::HashTable< Key, Val, Alloc >::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 969 of file hashTable_tpl.h.

969  {
970  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
971 
972  if (bucket == nullptr)
973  return insert(std::move(key), std::move(default_value)).second;
974  else
975  return bucket->val();
976  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:697
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.

◆ insert() [1/4]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::value_type & gum::HashTable< Key, Val, Alloc >::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 874 of file hashTable_tpl.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__addDelayedVariable(), gum::prm::o3prm::O3SystemFactory< GUM_SCALAR >::__addInstances(), gum::prm::o3prm::O3InterfaceFactory< GUM_SCALAR >::__addInterface2Dag(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::__addNode(), gum::prm::o3prm::O3TypeFactory< GUM_SCALAR >::__addTypes2Dag(), gum::DAGCycleDetector::__addWeightedSet(), gum::prm::o3prm::O3ClassFactory< GUM_SCALAR >::__checkAndAddNodesToDag(), gum::MaxInducedWidthMCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::__checkConditions(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__compute(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__compute(), gum::DefaultJunctionTreeStrategy::__computeJunctionTree(), gum::StaticTriangulation::__computeMaxPrimeJunctionTree(), gum::StaticTriangulation::__computeRecursiveThinning(), gum::learning::Miic::__existsDirectedPath(), gum::prm::PRMFormAttribute< GUM_SCALAR >::__fillCpf(), gum::prm::StructuredBayesBall< GUM_SCALAR >::__fillMaps(), gum::Regress< GUM_SCALAR, COMBINEOPERATOR, PROJECTOPERATOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::MultiDimFunctionGraphOperator< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::__findRetrogradeVariables(), gum::prm::StructuredBayesBall< GUM_SCALAR >::__fromChild(), gum::prm::StructuredBayesBall< GUM_SCALAR >::__fromParent(), gum::prm::gspan::InterfaceGraph< GUM_SCALAR >::__label(), gum::learning::genericBNLearner::__prepare_miic_3off2(), gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), gum::StructuredPlaner< double >::__recurArgMaxCopy(), gum::StructuredPlaner< double >::__recurExtractOptPol(), gum::DAGCycleDetector::__restrictWeightedSet(), gum::prm::PRMFactory< GUM_SCALAR >::__retrieveCommonType(), gum::credal::CredalNet< GUM_SCALAR >::__sort_varType(), gum::prm::GSpan< GUM_SCALAR >::__subgraph_mining(), gum::ExactBNdistance< GUM_SCALAR >::_computeKL(), gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::credal::InferenceEngine< GUM_SCALAR >::_initExpectations(), gum::credal::InferenceEngine< GUM_SCALAR >::_initMarginals(), gum::credal::InferenceEngine< GUM_SCALAR >::_initMarginalSets(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_insertInternalNode(), gum::ITI< AttributeSelection, isScalar >::_insertNode(), gum::credal::InferenceEngine< GUM_SCALAR >::_repetitiveInit(), gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_transpose(), gum::DAGCycleDetector::addArc(), gum::prm::PRMInference< double >::addEvidence(), gum::prm::PRMFactory< GUM_SCALAR >::addInstance(), gum::prm::gspan::DFSTree< GUM_SCALAR >::addRoot(), gum::SetTerminalNodePolicy< GUM_SCALAR >::addTerminalNode(), gum::BarrenNodesFinder::barrenNodes(), gum::BarrenNodesFinder::barrenPotentials(), gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::ClassDependencyGraph(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::copyAndMultiplyByScalar(), gum::Chi2::criticalValue(), gum::learning::genericBNLearner::Database::Database(), gum::ArcGraphPart::directedPath(), gum::ArcGraphPart::directedUnorientedPath(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::DAGCycleDetector::eraseArc(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::MultiDimFunctionGraphGenerator::generate(), gum::SimpleBayesNetGenerator< GUM_SCALAR, ICPTGenerator >::generateBN(), gum::DAGCycleDetector::hasCycleFromModifications(), gum::AdaptiveRMaxPlaner::initialize(), gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::insert(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidence(), gum::credal::InferenceEngine< GUM_SCALAR >::insertEvidenceFile(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQuery(), gum::credal::InferenceEngine< GUM_SCALAR >::insertQueryFile(), gum::ITI< AttributeSelection, isScalar >::ITI(), gum::LeafAggregator::leavesMap(), gum::HashTable< Val, Size, IndexAllocator >::map(), gum::MultiDimFunctionGraphManager< bool, ExactTerminalNodePolicy >::minimizeSize(), gum::MixedGraph::mixedOrientedPath(), gum::MixedGraph::mixedUnorientedPath(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator+(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator-(), gum::PriorityQueueImplementation< NodeId, double, std::greater< double >, std::allocator< NodeId >, std::is_scalar< NodeId >::value >::operator=(), gum::StructuredPlaner< double >::optimalPolicy2String(), gum::prm::gspan::Pattern::Pattern(), gum::BayesBall::relevantPotentials(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClass(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveClassElement(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveInterface(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveSlotType(), gum::prm::o3prm::O3NameSolver< GUM_SCALAR >::resolveType(), gum::prm::PRMClass< double >::scope(), gum::DAGCycleDetector::setDAG(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::Estimator< GUM_SCALAR >::setFromLBP(), gum::DAGmodel::setProperty(), gum::learning::genericBNLearner::setSliceOrder(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::TaxiSimulator::TaxiSimulator(), gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::toDot(), gum::EdgeGraphPart::undirectedPath(), and gum::ITI< AttributeSelection, isScalar >::updateGraph().

874  {
875  Bucket* bucket = __alloc.allocate(1);
876 
877  try {
878  __alloc.construct(bucket, thekey, theval);
879  } catch (...) {
880  __alloc.deallocate(bucket, 1);
881  throw;
882  }
883 
884  __insert(bucket);
885  return bucket->elt();
886  }
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:697
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760

◆ insert() [2/4]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::value_type & gum::HashTable< Key, Val, Alloc >::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 890 of file hashTable_tpl.h.

890  {
891  Bucket* bucket = __alloc.allocate(1);
892 
893  try {
894  __alloc.construct(bucket, std::move(thekey), std::move(theval));
895  } catch (...) {
896  __alloc.deallocate(bucket, 1);
897  throw;
898  }
899 
900  __insert(bucket);
901  return bucket->elt();
902  }
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:697
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760

◆ insert() [3/4]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::value_type & gum::HashTable< Key, Val, Alloc >::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 906 of file hashTable_tpl.h.

906  {
907  Bucket* bucket = __alloc.allocate(1);
908 
909  try {
910  __alloc.construct(bucket, reinterpret_cast< const value_type& >(elt));
911  } catch (...) {
912  __alloc.deallocate(bucket, 1);
913  throw;
914  }
915 
916  __insert(bucket);
917  return bucket->elt();
918  }
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:697
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760

◆ insert() [4/4]

template<typename Key, typename Val, typename Alloc >
INLINE HashTable< Key, Val, Alloc >::value_type & gum::HashTable< Key, Val, Alloc >::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 922 of file hashTable_tpl.h.

922  {
923  Bucket* bucket = __alloc.allocate(1);
924 
925  try {
926  __alloc.construct(bucket, std::move(reinterpret_cast< value_type& >(elt)));
927  } catch (...) {
928  __alloc.deallocate(bucket, 1);
929  throw;
930  }
931 
932  __insert(bucket);
933  return bucket->elt();
934  }
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:697
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760

◆ key()

template<typename Key, typename Val , typename Alloc >
INLINE const Key & gum::HashTable< Key, Val, Alloc >::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 1062 of file hashTable_tpl.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::keyByVal(), gum::StructuredPlaner< double >::optimalPolicy2String(), and gum::MultiDimFunctionGraph< bool, ExactTerminalNodePolicy >::toDot().

1062  {
1063  // get the bucket corresponding to the key
1064  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
1065 
1066  if (bucket == nullptr) {
1067  GUM_ERROR(NotFound, "key does not belong to the hashtable");
1068  }
1069 
1070  return bucket->key();
1071  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:697
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ keyByVal()

template<typename Key , typename Val, typename Alloc >
INLINE const Key & gum::HashTable< Key, Val, Alloc >::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 1054 of file hashTable_tpl.h.

1054  {
1055  for (auto iter = begin(); iter != end(); ++iter)
1056  if (iter.__bucket->val() == val) return iter.key();
1057 
1058  GUM_ERROR(NotFound, "not enough elements in the chained list");
1059  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ keyUniquenessPolicy()

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

Returns the current checking policy.

Returns
Returns the current checking policy.

Definition at line 765 of file hashTable_tpl.h.

765  {
767  }
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730

◆ map() [1/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(Val)  f,
Size  size,
bool  resize_pol,
bool  key_uniqueness_pol 
) const

Definition at line 1089 of file hashTable_tpl.h.

1090  {
1091  // determine the proper size of the hashtable
1092  // by default, the size of the table is set so that the table does not take
1093  // too much space while allowing to add a few elements without needing to
1094  // resize in autmatic resizing mode
1095  if (size == 0) size = std::max(Size(2), __nb_elements / 2);
1096 
1097  // create a new table
1098  HashTable< Key, Mount, OtherAlloc > table(
1099  size, resize_pol, key_uniqueness_pol);
1100 
1101  // fill the new hash table
1102  for (auto iter = begin(); iter != end(); ++iter) {
1103  table.insert(iter.key(), f(iter.val()));
1104  }
1105 
1106  return table;
1107  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ map() [2/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(Val &)  f,
Size  size,
bool  resize_pol,
bool  key_uniqueness_pol 
) const

Definition at line 1111 of file hashTable_tpl.h.

1112  {
1113  // determine the proper size of the hashtable
1114  // by default, the size of the table is set so that the table does not take
1115  // too much space while allowing to add a few elements without needing to
1116  // resize in autmatic resizing mode
1117  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1118 
1119  // create a new table
1120  HashTable< Key, Mount, OtherAlloc > table(
1121  size, resize_pol, key_uniqueness_pol);
1122 
1123  // fill the new hash table
1124  for (auto iter = begin(); iter != end(); ++iter) {
1125  table.insert(iter.key(), f(const_cast< Val& >(iter.val())));
1126  }
1127 
1128  return table;
1129  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ map() [3/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(const Val &)  f,
Size  size,
bool  resize_pol,
bool  key_uniqueness_pol 
) const

Definition at line 1134 of file hashTable_tpl.h.

1137  {
1138  // determine the proper size of the hashtable
1139  // by default, the size of the table is set so that the table does not take
1140  // too much space while allowing to add a few elements without needing to
1141  // resize in autmatic resizing mode
1142  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1143 
1144  // create a new table
1145  HashTable< Key, Mount, OtherAlloc > table(
1146  size, resize_pol, key_uniqueness_pol);
1147 
1148  // fill the new hash table
1149  for (auto iter = begin(); iter != end(); ++iter) {
1150  table.insert(iter.key(), f(iter.val()));
1151  }
1152 
1153  return table;
1154  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ map() [4/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc >
HashTable< Key, Mount, OtherAlloc > INLINE gum::HashTable< Key, Val, Alloc >::map ( const Mount &  val,
Size  size,
bool  resize_pol,
bool  key_uniqueness_pol 
) const

Definition at line 1158 of file hashTable_tpl.h.

1159  {
1160  // determine the proper size of the hashtable
1161  // by default, the size of the table is set so that the table does not take
1162  // too much space while allowing to add a few elements without needing to
1163  // resize in autmatic resizing mode
1164  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1165 
1166  // create a new table
1167  HashTable< Key, Mount, OtherAlloc > table(
1168  size, resize_pol, key_uniqueness_pol);
1169 
1170  // fill the new hash table
1171  for (auto iter = begin(); iter != end(); ++iter) {
1172  table.insert(iter.key(), val);
1173  }
1174 
1175  return table;
1176  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ map() [5/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(Val)  f,
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() [6/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(Val &)  f,
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, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > gum::HashTable< Key, Val, Alloc >::map ( Mount(*)(const Val &)  f,
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() [8/8]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename Mount , typename OtherAlloc = typename Alloc::template rebind< std::pair< Key, Mount > >::other>
HashTable< Key, Mount, OtherAlloc > gum::HashTable< Key, Val, Alloc >::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.

◆ operator!=() [1/2]

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename OtherAlloc >
bool gum::HashTable< Key, Val, Alloc >::operator!= ( const HashTable< Key, Val, OtherAlloc > &  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.

◆ operator!=() [2/2]

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

Definition at line 1198 of file hashTable_tpl.h.

1198  {
1199  return !operator==(from);
1200  }
bool operator==(const HashTable< Key, Val, OtherAlloc > &from) const
Checks whether two hashtables contain the same elements.

◆ operator=() [1/3]

template<typename Key, typename Val, typename Alloc>
HashTable< Key, Val, Alloc > & gum::HashTable< Key, Val, Alloc >::operator= ( const HashTable< Key, Val, Alloc > &  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 493 of file hashTable_tpl.h.

493  {
494  // avoid self assignment
495  if (this != &from) {
496  // for debugging purposes
497  GUM_OP_CPY(HashTable);
498 
499  // first remove the current content of the hashtable and make
500  // the iterators point to end
501  clear();
502 
503  // if sizes of from's and this' __nodes vectors are not the same,
504  // we need to remove the current __nodes' array and to create a
505  // new array with the correct size
506  if (__size != from.__size) {
507  __nodes.resize(from.__size);
508 
509  for (Size i = Size(0); i < from.__size; ++i) {
510  __nodes[i].setAllocator(__alloc);
511  }
512 
513  __size = from.__size;
514 
515  // update the hash function : this is important as the computation of
516  // the hash values heavily depends on the size of the hash table
517  __hash_func.resize(__size);
518  }
519 
520  __resize_policy = from.__resize_policy;
521  __key_uniqueness_policy = from.__key_uniqueness_policy;
522  __begin_index = from.__begin_index;
523 
524  // perform the copy
525  __copy(from);
526  }
527 
528  return *this;
529  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void __copy(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.

◆ operator=() [2/3]

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

Generalized 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 534 of file hashTable_tpl.h.

534  {
535  // avoid self assignment
536  if (this != reinterpret_cast< const HashTable< Key, Val, Alloc >* >(&from)) {
537  // for debugging purposes
538  GUM_OP_CPY(HashTable);
539 
540  // first remove the current content of the hashtable and make
541  // the iterators point to end
542  clear();
543 
544  // if sizes of from's and this' __nodes vectors are not the same,
545  // we need to remove the current __nodes' array and to create a
546  // new array with the correct size
547  if (__size != from.__size) {
548  __nodes.resize(from.__size);
549 
550  for (Size i = 0; i < from.__size; ++i) {
551  __nodes[i].setAllocator(__alloc);
552  }
553 
554  __size = from.__size;
555 
556  // update the hash function : this is important as the computation of
557  // the hash values heavily depends on the size of the hash table
558  __hash_func.resize(__size);
559  }
560 
561  __resize_policy = from.__resize_policy;
562  __key_uniqueness_policy = from.__key_uniqueness_policy;
563  __begin_index = from.__begin_index;
564 
565  // perform the copy
566  __copy(from);
567  }
568 
569  return *this;
570  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void __copy(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.

◆ operator=() [3/3]

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

Move operator.

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

Definition at line 574 of file hashTable_tpl.h.

574  {
575  // avoid self assignment
576  if (this != &table) {
577  // for debugging purposes
578  GUM_OP_MOV(HashTable);
579 
580  // first remove the current content of the hashtable and make
581  // the iterators point to end
582  clear();
583 
584  __nodes = std::move(table.__nodes);
585  __safe_iterators = std::move(table.__safe_iterators);
586  __alloc = std::move(table.__alloc);
587  __size = table.__size;
588  __nb_elements = table.__nb_elements;
589  __hash_func = table.__hash_func;
590  __resize_policy = table.__resize_policy;
591  __key_uniqueness_policy = table.__key_uniqueness_policy;
592  __begin_index = table.__begin_index;
593 
594  table.__size = 0; // necessary if we wish to perform moves iteratively,
595  // i.e. x = std::move ( y ); y = std::move ( z ); ...
596  }
597 
598  return *this;
599  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1695
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
void clear()
Removes all the elements in the hash table.

◆ operator==()

template<typename Key, typename Val, typename Alloc >
template<typename OtherAlloc >
bool gum::HashTable< Key, Val, Alloc >::operator== ( const HashTable< Key, Val, OtherAlloc > &  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 1181 of file hashTable_tpl.h.

1181  {
1182  // checks whether the two hashtables contain the same number of elements
1183  if (from.__nb_elements != __nb_elements) return false;
1184 
1185  // parse this and check that each element also belongs to from
1186  for (auto iter = begin(); iter != end(); ++iter) {
1187  try {
1188  if (iter.val() != from[iter.key()]) return false;
1189  } catch (NotFound&) { return false; }
1190  }
1191 
1192  return true;
1193  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721

◆ operator[]() [1/2]

template<typename Key, typename Val , typename Alloc >
INLINE Val & gum::HashTable< Key, Val, Alloc >::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 722 of file hashTable_tpl.h.

722  {
723  return __nodes[__hash_func(key)][key];
724  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715

◆ operator[]() [2/2]

template<typename Key, typename Val , typename Alloc >
INLINE const Val & gum::HashTable< Key, Val, Alloc >::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 728 of file hashTable_tpl.h.

728  {
729  return __nodes[__hash_func(key)][key];
730  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715

◆ reset()

template<typename Key, typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 1049 of file hashTable_tpl.h.

Referenced by gum::MultiDimSparse< GUM_SCALAR >::set().

1049  {
1050  erase(key);
1051  }
void erase(const Key &key)
Removes a given element from the hash table.
const Key & key(const Key &key) const
Returns a reference on a given key.
+ Here is the caller graph for this function:

◆ resize()

template<typename Key , typename Val , typename Alloc >
void gum::HashTable< Key, Val, Alloc >::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 770 of file hashTable_tpl.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::__create(), and gum::BayesBall::relevantPotentials().

770  {
771  // new_size must be >= 2 else all the bits of the hash function are lost
772  new_size = std::max(Size(2), new_size);
773 
774  // find the real size for allocation (the smallest power of 2 greater
775  // than or equal to new_size) and get its base-2 logarithm
776  int log_size = __hashTableLog2(new_size);
777  new_size = Size(1) << log_size;
778 
779  // check if the new size is different from the actual size
780  // if not, nothing else need be done
781 
782  if (new_size != __size) {
783  // under automatic resize policy, check if the new size leaves
784  // enough space for storing all the current elements
785  if (!__resize_policy
786  || (__nb_elements
788  // create a new array of __nodes to store the elements
789  std::vector< HashTableList< Key, Val, Alloc > > new_nodes(new_size);
790 
791  for (auto& list : new_nodes) {
792  list.setAllocator(__alloc);
793  }
794 
795  // set the new hash function
796  __hash_func.resize(new_size);
797 
798  // put all the elements of the current __nodes array into the new one
799  Bucket* bucket;
800  Size new_hashed_key;
801 
802  for (Size i = Size(0); i < __size; ++i) {
803  while ((bucket = __nodes[i].__deb_list) != nullptr) {
804  // compute the new hashed key
805  new_hashed_key = __hash_func(bucket->key());
806 
807  // remove the bucket from the list of buckets of the current
808  // node vector
809  __nodes[i].__deb_list = bucket->next;
810 
811  // put the bucket into the new __nodes vector
812  new_nodes[new_hashed_key].insert(bucket);
813  }
814  }
815 
816  // update the size of the hash table
817  __size = new_size;
818  __begin_index = std::numeric_limits< Size >::max();
819 
820  // substitute the current __nodes array by the new one
821  std::swap(__nodes, new_nodes);
822 
823  // update the iterators
824  for (auto iter : __safe_iterators) {
825  if (iter->__bucket)
826  iter->__index = __hash_func(iter->__bucket->key());
827  else {
828  iter->__next_bucket = nullptr;
829  iter->__index = 0;
830  }
831  }
832  }
833  }
834  }
unsigned int __hashTableLog2(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb...
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:697
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition: hashTable.h:84
BucketAllocator __alloc
The allocator for the buckets.
Definition: hashTable.h:1760
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ resizePolicy()

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

Returns the current resizing policy.

Returns
Returns the current resizing policy.

Definition at line 754 of file hashTable_tpl.h.

754  {
755  return __resize_policy;
756  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727

◆ set()

template<typename Key, typename Val, typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 979 of file hashTable_tpl.h.

Referenced by gum::SmallObjectAllocator::allocate(), and gum::MultiDimSparse< GUM_SCALAR >::set().

979  {
980  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
981 
982  if (bucket == nullptr)
983  insert(key, value);
984  else
985  bucket->val() = value;
986  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:697
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1724
const Key & key(const Key &key) const
Returns a reference on a given key.
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the caller graph for this function:

◆ setKeyUniquenessPolicy()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 759 of file hashTable_tpl.h.

Referenced by gum::SmallObjectAllocator::SmallObjectAllocator().

760  {
761  __key_uniqueness_policy = new_policy;
762  }
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1730
+ Here is the caller graph for this function:

◆ setResizePolicy()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTable< Key, Val, Alloc >::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 748 of file hashTable_tpl.h.

749  {
750  __resize_policy = new_policy;
751  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1727

◆ size()

template<typename Key , typename Val , typename Alloc >
INLINE Size gum::HashTable< Key, Val, Alloc >::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 733 of file hashTable_tpl.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::__clearIterators(), gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__insertEvidence(), gum::credal::CredalNet< GUM_SCALAR >::__sort_varType(), gum::credal::InferenceEngine< GUM_SCALAR >::_computeEpsilon(), gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrASize(), gum::ContingencyTable< Idx, GUM_SCALAR >::attrBSize(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator*(), gum::Set< gum::Potential< GUM_SCALAR > * >::operator==(), gum::MultiDimSparse< GUM_SCALAR >::realSize(), and gum::DAGCycleDetector::setDAG().

733  {
734  return __nb_elements;
735  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ HashTable

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename K , typename V , typename A >
friend class HashTable
friend

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

Definition at line 1695 of file hashTable.h.

◆ Bijection

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
template<typename T1 , typename T2 , typename A >
friend class Bijection
friend

For bijections to quickly access data.

Definition at line 1708 of file hashTable.h.

◆ HashTableConstIterator< Key, Val >

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
friend class HashTableConstIterator< Key, Val >
friend

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

Definition at line 1697 of file hashTable.h.

◆ HashTableConstIteratorSafe< Key, Val >

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
friend class HashTableConstIteratorSafe< Key, Val >
friend

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

Definition at line 1699 of file hashTable.h.

◆ HashTableIterator< Key, Val >

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
friend class HashTableIterator< Key, Val >
friend

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

Definition at line 1696 of file hashTable.h.

◆ HashTableIteratorSafe< Key, Val >

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
friend class HashTableIteratorSafe< Key, Val >
friend

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

Definition at line 1698 of file hashTable.h.

◆ operator<< [1/2]

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

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

Definition at line 1239 of file hashTable_tpl.h.

1240  {
1241  bool deja = false;
1242  stream << "{";
1243 
1244  for (Size i = Size(0); i < table.__size; ++i)
1245  for (auto ptr = table.__nodes[i].__deb_list; ptr; ptr = ptr->next) {
1246  if (deja) stream << " , ";
1247 
1248  stream << ptr->key() << "=>" << ptr->val();
1249 
1250  deja = true;
1251  }
1252 
1253  stream << "}";
1254 
1255  return stream;
1256  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ operator<< [2/2]

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

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

Definition at line 1259 of file hashTable_tpl.h.

1260  {
1261  bool deja = false;
1262  stream << "{";
1263 
1264  for (Size i = Size(0); i < table.__size; ++i)
1265  for (auto ptr = table.__nodes[i].__deb_list; ptr; ptr = ptr->next) {
1266  if (deja) stream << " , ";
1267 
1268  stream << ptr->key() << "=>" << ptr->val();
1269 
1270  deja = true;
1271  }
1272 
1273  stream << "}";
1274 
1275  return stream;
1276  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

Member Data Documentation

◆ __alloc

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
BucketAllocator gum::HashTable< Key, Val, Alloc >::__alloc
private

The allocator for the buckets.

Warning
the allocator field should compulsorily be the last of field of the class. As such, for K and V fixed, all hashTable<K,V,A> are the same (up to the allocator) for all allocators A. This feature proves useful to avoid passing the allocator as a template parameter to iterators.

Definition at line 1760 of file hashTable.h.

◆ __begin_index

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
Size gum::HashTable< Key, Val, Alloc >::__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 1746 of file hashTable.h.

Referenced by gum::HashTableConstIterator< Key, bool >::HashTableConstIterator(), gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::HashTableConstIteratorSafe(), and gum::HashTable< Val, Size, IndexAllocator >::operator=().

◆ __hash_func

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
HashFunc< Key > gum::HashTable< Key, Val, Alloc >::__hash_func
private

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

Definition at line 1724 of file hashTable.h.

◆ __key_uniqueness_policy

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
bool gum::HashTable< Key, Val, Alloc >::__key_uniqueness_policy {true}
private

Shall we check for key uniqueness in the table?

Definition at line 1730 of file hashTable.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::operator=().

◆ __nb_elements

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
Size gum::HashTable< Key, Val, Alloc >::__nb_elements {Size(0)}
private

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

Definition at line 1721 of file hashTable.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::__copy(), and gum::HashTable< Val, Size, IndexAllocator >::operator==().

◆ __nodes

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
std::vector< HashTableList< Key, Val, Alloc > > gum::HashTable< Key, Val, Alloc >::__nodes
private

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

'__nodes' is this very vector.

Definition at line 1715 of file hashTable.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::__copy(), and gum::operator<<().

◆ __resize_policy

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
bool gum::HashTable< Key, Val, Alloc >::__resize_policy {true}
private

Is resizing performed automatically?

Definition at line 1727 of file hashTable.h.

Referenced by gum::HashTable< Val, Size, IndexAllocator >::operator=().

◆ __safe_iterators

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
std::vector< HashTableConstIteratorSafe< Key, Val >* > gum::HashTable< Key, Val, Alloc >::__safe_iterators
mutableprivate

The list of safe iterators pointing to the hash table.

Definition at line 1750 of file hashTable.h.

◆ __size

template<typename Key, typename Val, typename Alloc = std::allocator< std::pair< Key, Val > >>
Size gum::HashTable< Key, Val, Alloc >::__size
private

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