aGrUM  0.15.1
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 679 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 692 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 700 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 703 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 694 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 696 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 689 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 687 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 691 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 693 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 695 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 683 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 684 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 688 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 686 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 690 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 685 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 383 of file hashTable_tpl.h.

385  :
386  // size must be >= 2 else we lose all the bits of the hash function
387  __size{Size(1) << __hashTableLog2(std::max(Size(2), size_param))},
388  __resize_policy{resize_pol}, __key_uniqueness_policy{key_uniqueness_pol} {
389  // for debugging purposes
390  GUM_CONSTRUCTOR(HashTable);
391 
392  // finalize the creation
393  __create(__size);
394  }
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:1730
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
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:1698
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1733
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 397 of file hashTable_tpl.h.

398  :
399  // size must be >= 2 else we lose all the bits of the hash function
401  std::max< Size >(Size(2), Size(list.size()) / 2))} {
402  // for debugging purposes
403  GUM_CONSTRUCTOR(HashTable);
404 
405  // setup the __nodes vector (contains only empty lists)
406  __create(__size);
407 
408  // insert all the elements
409  for (const auto& elt : list) {
410  insert(elt);
411  }
412  }
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:1721
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:1698
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
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 415 of file hashTable_tpl.h.

416  :
417  __size{table.__size},
418  __resize_policy{table.__resize_policy},
419  __key_uniqueness_policy{table.__key_uniqueness_policy},
420  __begin_index{table.__begin_index} {
421  // for debugging purposes
422  GUM_CONS_CPY(HashTable);
423 
424  // setup the __nodes vector (contains only empty lists)
425  __create(__size);
426 
427  // fill with the content of table
428  __copy(table);
429  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1730
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
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:1698
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1733
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
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 433 of file hashTable_tpl.h.

434  :
435  __size{table.__size},
436  __resize_policy{table.__resize_policy},
437  __key_uniqueness_policy{table.__key_uniqueness_policy},
438  __begin_index{table.__begin_index} {
439  // for debugging purposes
440  GUM_CONS_CPY(HashTable);
441 
442  // setup the __nodes vector (contains only empty lists)
443  __create(__size);
444 
445  // fill with the content of table
446  __copy(table);
447  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1730
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
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:1698
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1733
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
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 450 of file hashTable_tpl.h.

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

◆ ~HashTable()

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

Class destructor.

Definition at line 485 of file hashTable_tpl.h.

485  {
486  // for debugging purposes
487  GUM_DESTRUCTOR(HashTable);
488 
489  // update all the registered iterators: they should now point to nullptr
490  // and their hashtable should be set to nullptr
492  }
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1698
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 464 of file hashTable_tpl.h.

464  {
465  const Size len = __safe_iterators.size();
466  for (Size i = Size(0); i < len; ++i)
467  __safe_iterators[i]->clear();
468  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1753
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:48

◆ __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 311 of file hashTable_tpl.h.

312  {
313  // in debug mode, check that this and table have '__nodes' arrays of the
314  // same size
315  GUM_ASSERT(table.__size == __size);
316 
317  // try to fill the array of chained lists
318  for (Size i = 0; i < table.__size; ++i) {
319  try {
320  __nodes[i] = table.__nodes[i];
321  } catch (...) {
322  // here we could allocate the __nodes[j], j=0..i-1, so we should
323  // deallocate them
324  for (Size j = 0; j < __size; ++j)
325  __nodes[j].clear();
326 
327  __nb_elements = Size(0);
328 
329  // propagate the exception
330  throw;
331  }
332  }
333 
334  __nb_elements = table.__nb_elements;
335  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
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:48

◆ __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 366 of file hashTable_tpl.h.

366  {
367  // setup the __nodes vector (contains only empty lists)
368  __nodes.resize(size);
369 
370  for (auto& list : __nodes) {
371  list.setAllocator(__alloc);
372  }
373 
374  // set up properly the hash function
375  __hash_func.resize(size);
376 
377  // make sure the end() iterator is constructed properly
378  end4Statics();
379  endSafe4Statics();
380  }
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:1763
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
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 992 of file hashTable_tpl.h.

993  {
994  if (bucket == nullptr) return;
995 
996  // update the registered iterators pointing to this bucket
997  for (auto iter : __safe_iterators) {
998  if (iter->__bucket == bucket) {
999  iter->operator++();
1000  iter->__next_bucket = iter->__bucket;
1001  iter->__bucket = nullptr;
1002  } else if (iter->__next_bucket == bucket) {
1003  iter->__bucket = bucket;
1004  iter->operator++();
1005  iter->__next_bucket = iter->__bucket;
1006  iter->__bucket = nullptr;
1007  }
1008  }
1009 
1010  // remove the element from the __nodes vector
1011  __nodes[index].erase(bucket);
1012 
1013  --__nb_elements;
1014 
1015  if ((index == __begin_index) && __nodes[index].empty()) {
1016  __begin_index = std::numeric_limits< Size >::max();
1017  }
1018  }
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1753
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
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 841 of file hashTable_tpl.h.

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

◆ 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 636 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().

636  {
637  // if the table is empty, make the begin and end point to the same element
638  if (__nb_elements == Size(0))
639  return iterator{end()};
640  else
641  return iterator{*this};
642  }
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:1724
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:693
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ 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 646 of file hashTable_tpl.h.

646  {
647  // if the table is empty, make the begin and end point to the same element
648  if (__nb_elements == Size(0))
649  return const_iterator{end()};
650  else
651  return const_iterator{*this};
652  }
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:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:694

◆ 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 696 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().

696  {
697  // if the table is empty, make the begin and end point to the same element
698  if (__nb_elements == Size(0))
699  return iterator_safe{endSafe()};
700  else
701  return iterator_safe{*this};
702  }
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:1724
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:695
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ 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 706 of file hashTable_tpl.h.

706  {
707  // if the table is empty, make the begin and end point to the same element
708  if (__nb_elements == Size(0))
709  return const_iterator_safe{endSafe()};
710  else
711  return const_iterator_safe{*this};
712  }
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:696
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 741 of file hashTable_tpl.h.

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

741  {
742  return __size;
743  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
+ 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 656 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().

656  {
657  // if the table is empty, make the begin and end point to the same element
658  if (__nb_elements == Size(0))
659  return const_iterator{cend()};
660  else
661  return const_iterator{*this};
662  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
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:48
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:694
+ 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 716 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().

716  {
717  // if the table is empty, make the begin and end point to the same element
718  if (__nb_elements == Size(0))
719  return const_iterator_safe{cendSafe()};
720  else
721  return const_iterator_safe{*this};
722  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:696
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
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:48
+ 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 626 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().

626  {
627  // note that, here, we know for sure that HashTableIterEnd has been properly
628  // initialized as it is initialized by end4Statics, which is called by
629  // all hashtables' constructors
630  return *(reinterpret_cast< const const_iterator* >(
632  }
static const HashTableIterator< int, int > * __HashTableIterEnd
The unsafe iterator used by everyone.
Definition: hashTable.h:1834
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:694
+ 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 686 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().

686  {
687  // note that, here, we know for sure that HashTableIterEnd has been properly
688  // initialized as it is initialized by end4Statics, which is called by
689  // all hashtables' constructors
690  return *(reinterpret_cast< const const_iterator_safe* >(
692  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:696
static const HashTableIteratorSafe< int, int > * __HashTableIterEndSafe
The safe iterator used by everyone.
Definition: hashTable.h:1837
+ 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 471 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().

471  {
472  // update all the registered iterators: they should now point to nullptr
473  // and they are positioned to the end of the hashtable.
475 
476  // remove the buckets
477  for (Size i = Size(0); i < __size; ++i)
478  __nodes[i].clear();
479 
480  __nb_elements = Size(0);
481  __begin_index = std::numeric_limits< Size >::max();
482  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
void __clearIterators()
Clear all the safe iterators.
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
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:48
+ 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 346 of file hashTable_tpl.h.

346  {
347  return *(reinterpret_cast< const const_iterator* >(
349  }
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:694

◆ 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 360 of file hashTable_tpl.h.

360  {
361  return *(reinterpret_cast< const const_iterator_safe* >(
363  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:696
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 942 of file hashTable_tpl.h.

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

◆ 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 1086 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().

1086  {
1087  return (__nb_elements == Size(0));
1088  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ 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 606 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().

606  {
607  // note that, here, we know for sure that HashTableIterEnd has been properly
608  // initialized as it is initialized by end4Statics, which is called by
609  // all hashtables' constructors
610  return *(reinterpret_cast< const iterator* >(
612  }
static const HashTableIterator< int, int > * __HashTableIterEnd
The unsafe iterator used by everyone.
Definition: hashTable.h:1834
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:693
+ 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 616 of file hashTable_tpl.h.

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

◆ 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 339 of file hashTable_tpl.h.

339  {
340  return *(reinterpret_cast< const iterator* >(
342  }
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:693

◆ 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 666 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().

666  {
667  // note that, here, we know for sure that HashTableIterEnd has been properly
668  // initialized as it is initialized by end4Statics, which is called by
669  // all hashtables' constructors
670  return *(reinterpret_cast< const iterator_safe* >(
672  }
static const HashTableIteratorSafe< int, int > * __HashTableIterEndSafe
The safe iterator used by everyone.
Definition: hashTable.h:1837
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:695
+ 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 676 of file hashTable_tpl.h.

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

◆ 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 353 of file hashTable_tpl.h.

353  {
354  return *(reinterpret_cast< const iterator_safe* >(
356  }
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:695

◆ 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 1021 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().

1021  {
1022  // get the hashed key
1023  Size hash = __hash_func(key);
1024 
1025  // get the bucket containing the element to erase
1026  HashTableBucket< Key, Val >* bucket = __nodes[hash].bucket(key);
1027 
1028  __erase(bucket, hash);
1029  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718
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:48
+ 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 1032 of file hashTable_tpl.h.

1032  {
1033  __erase(iter.__getBucket(), iter.__getIndex());
1034  }
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 1038 of file hashTable_tpl.h.

1038  {
1039  __erase(iter.__getBucket(), iter.__getIndex());
1040  }
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 1077 of file hashTable_tpl.h.

1077  {
1078  for (auto iterAll = cbeginSafe(); iterAll != cendSafe(); ++iterAll) {
1079  if (iterAll.__bucket->val() == val) {
1080  __erase(iterAll.__bucket, iterAll.__index);
1081  }
1082  }
1083  }
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 1043 of file hashTable_tpl.h.

1043  {
1044  for (auto iter = cbegin(); iter != cend(); ++iter)
1045  if (iter.__bucket->val() == val) {
1046  __erase(iter.__getBucket(), iter.__getIndex());
1047  return;
1048  }
1049  }
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 746 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().

746  {
747  return __nodes[__hash_func(key)].exists(key);
748  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718

◆ 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 960 of file hashTable_tpl.h.

961  {
962  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
963 
964  if (bucket == nullptr)
965  return insert(key, default_value).second;
966  else
967  return bucket->val();
968  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:700
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718
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 972 of file hashTable_tpl.h.

972  {
973  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
974 
975  if (bucket == nullptr)
976  return insert(std::move(key), std::move(default_value)).second;
977  else
978  return bucket->val();
979  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:700
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718
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 877 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().

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

◆ 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 893 of file hashTable_tpl.h.

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

◆ 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 909 of file hashTable_tpl.h.

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

◆ 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 925 of file hashTable_tpl.h.

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

◆ 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 1065 of file hashTable_tpl.h.

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

1065  {
1066  // get the bucket corresponding to the key
1067  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
1068 
1069  if (bucket == nullptr) {
1070  GUM_ERROR(NotFound, "key does not belong to the hashtable");
1071  }
1072 
1073  return bucket->key();
1074  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:700
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 1057 of file hashTable_tpl.h.

1057  {
1058  for (auto iter = begin(); iter != end(); ++iter)
1059  if (iter.__bucket->val() == val) return iter.key();
1060 
1061  GUM_ERROR(NotFound, "not enough elements in the chained list");
1062  }
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:55

◆ 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 768 of file hashTable_tpl.h.

768  {
770  }
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1733

◆ 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 1092 of file hashTable_tpl.h.

1093  {
1094  // determine the proper size of the hashtable
1095  // by default, the size of the table is set so that the table does not take
1096  // too much space while allowing to add a few elements without needing to
1097  // resize in autmatic resizing mode
1098  if (size == 0) size = std::max(Size(2), __nb_elements / 2);
1099 
1100  // create a new table
1101  HashTable< Key, Mount, OtherAlloc > table(
1102  size, resize_pol, key_uniqueness_pol);
1103 
1104  // fill the new hash table
1105  for (auto iter = begin(); iter != end(); ++iter) {
1106  table.insert(iter.key(), f(iter.val()));
1107  }
1108 
1109  return table;
1110  }
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:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 1114 of file hashTable_tpl.h.

1115  {
1116  // determine the proper size of the hashtable
1117  // by default, the size of the table is set so that the table does not take
1118  // too much space while allowing to add a few elements without needing to
1119  // resize in autmatic resizing mode
1120  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1121 
1122  // create a new table
1123  HashTable< Key, Mount, OtherAlloc > table(
1124  size, resize_pol, key_uniqueness_pol);
1125 
1126  // fill the new hash table
1127  for (auto iter = begin(); iter != end(); ++iter) {
1128  table.insert(iter.key(), f(const_cast< Val& >(iter.val())));
1129  }
1130 
1131  return table;
1132  }
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:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 1137 of file hashTable_tpl.h.

1140  {
1141  // determine the proper size of the hashtable
1142  // by default, the size of the table is set so that the table does not take
1143  // too much space while allowing to add a few elements without needing to
1144  // resize in autmatic resizing mode
1145  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1146 
1147  // create a new table
1148  HashTable< Key, Mount, OtherAlloc > table(
1149  size, resize_pol, key_uniqueness_pol);
1150 
1151  // fill the new hash table
1152  for (auto iter = begin(); iter != end(); ++iter) {
1153  table.insert(iter.key(), f(iter.val()));
1154  }
1155 
1156  return table;
1157  }
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:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 1161 of file hashTable_tpl.h.

1162  {
1163  // determine the proper size of the hashtable
1164  // by default, the size of the table is set so that the table does not take
1165  // too much space while allowing to add a few elements without needing to
1166  // resize in autmatic resizing mode
1167  if (size == Size(0)) size = std::max(Size(2), __nb_elements / 2);
1168 
1169  // create a new table
1170  HashTable< Key, Mount, OtherAlloc > table(
1171  size, resize_pol, key_uniqueness_pol);
1172 
1173  // fill the new hash table
1174  for (auto iter = begin(); iter != end(); ++iter) {
1175  table.insert(iter.key(), val);
1176  }
1177 
1178  return table;
1179  }
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:1724
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ 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 1201 of file hashTable_tpl.h.

1201  {
1202  return !operator==(from);
1203  }
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 496 of file hashTable_tpl.h.

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

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

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

1184  {
1185  // checks whether the two hashtables contain the same number of elements
1186  if (from.__nb_elements != __nb_elements) return false;
1187 
1188  // parse this and check that each element also belongs to from
1189  for (auto iter = begin(); iter != end(); ++iter) {
1190  try {
1191  if (iter.val() != from[iter.key()]) return false;
1192  } catch (NotFound&) { return false; }
1193  }
1194 
1195  return true;
1196  }
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:1724

◆ 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 725 of file hashTable_tpl.h.

725  {
726  return __nodes[__hash_func(key)][key];
727  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718

◆ 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 731 of file hashTable_tpl.h.

731  {
732  return __nodes[__hash_func(key)][key];
733  }
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718

◆ 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 1052 of file hashTable_tpl.h.

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

1052  {
1053  erase(key);
1054  }
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 773 of file hashTable_tpl.h.

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

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

757  {
758  return __resize_policy;
759  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1730

◆ 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 982 of file hashTable_tpl.h.

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

982  {
983  Bucket* bucket = __nodes[__hash_func(key)].bucket(key);
984 
985  if (bucket == nullptr)
986  insert(key, value);
987  else
988  bucket->val() = value;
989  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:700
HashFunc< Key > __hash_func
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1727
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:1718
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 762 of file hashTable_tpl.h.

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

763  {
764  __key_uniqueness_policy = new_policy;
765  }
bool __key_uniqueness_policy
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1733
+ 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 751 of file hashTable_tpl.h.

752  {
753  __resize_policy = new_policy;
754  }
bool __resize_policy
Is resizing performed automatically?
Definition: hashTable.h:1730

◆ 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 736 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().

736  {
737  return __nb_elements;
738  }
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
+ 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 1698 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 1711 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 1700 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 1702 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 1699 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 1701 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 1242 of file hashTable_tpl.h.

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

◆ 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 1262 of file hashTable_tpl.h.

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

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 1763 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 1749 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 1727 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 1733 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 1724 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 1718 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 1730 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 1753 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: