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

The class for generic Hash Tables. More...

#include <agrum/tools/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 whether hashtables contain the same elements
if ((table1 == table2) && (table1 != table3))
cerr << "check for equality/inequality" << endl;
// parse the content of a hashtable using an unsafe iterator
for (HashTable<int,string>::const_iterator iter = table1.cbegin();
iter != table1.cend(); ++iter)
cerr << *iter;
HashTable<int,string>::iterator iter = table1.begin();
iter += 2;
cerr << iter.key () << " " << iter.val ();
// use an iterator to point the element we wish to erase
HashTable<int,string>::iterator_safe iterS = table1.beginSafe ();
table1.erase ( table1.beginSafe () + 4 );
table1.erase ( iterS ); // this is safe because the iterator is safe
// check for iterator's safety
for (HashTable<int,string>::iterator_safe iter = table1.beginSafe ();
iter != table1.endSafe (); ++iter )
table1.eraseByVal ( *iter );
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 666 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 679 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 687 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 690 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 681 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 683 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 676 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 674 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 678 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 680 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 682 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 670 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 671 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 675 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 673 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 677 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 672 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 366 of file hashTable_tpl.h.

368  :
369  // size must be >= 2 else we lose all the bits of the hash function
370  _size_{Size(1) << _hashTableLog2_(std::max(Size(2), size_param))},
371  _resize_policy_{resize_pol}, _key_uniqueness_policy_{key_uniqueness_pol} {
372  // for debugging purposes
373  GUM_CONSTRUCTOR(HashTable);
374 
375  // finalize the creation
376  _create_(_size_);
377  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
void _create_(Size size)
Used by all default constructors (general and specialized).
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...
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

380  :
381  // size must be >= 2 else we lose all the bits of the hash function
382  _size_{Size(1) << _hashTableLog2_(std::max< Size >(Size(2), Size(list.size()) / 2))} {
383  // for debugging purposes
384  GUM_CONSTRUCTOR(HashTable);
385 
386  // setup the _nodes_ vector (contains only empty lists)
387  _create_(_size_);
388 
389  // insert all the elements
390  for (const auto& elt: list) {
391  insert(elt);
392  }
393  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
void _create_(Size size)
Used by all default constructors (general and specialized).
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...
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
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 396 of file hashTable_tpl.h.

396  :
397  _size_{table._size_}, _resize_policy_{table._resize_policy_},
398  _key_uniqueness_policy_{table._key_uniqueness_policy_}, _begin_index_{table._begin_index_} {
399  // for debugging purposes
400  GUM_CONS_CPY(HashTable);
401 
402  // setup the _nodes_ vector (contains only empty lists)
403  _create_(_size_);
404 
405  // fill with the content of table
406  _copy_(table);
407  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
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:1682
void _copy_(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731

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

411  :
412  _size_{table._size_}, _resize_policy_{table._resize_policy_},
413  _key_uniqueness_policy_{table._key_uniqueness_policy_}, _begin_index_{table._begin_index_} {
414  // for debugging purposes
415  GUM_CONS_CPY(HashTable);
416 
417  // setup the _nodes_ vector (contains only empty lists)
418  _create_(_size_);
419 
420  // fill with the content of table
421  _copy_(table);
422  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
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:1682
void _copy_(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731

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

425  :
426  _nodes_(std::move(table._nodes_)), _size_{table._size_}, _nb_elements_{table._nb_elements_},
427  _hash_func_{table._hash_func_}, _resize_policy_{table._resize_policy_},
428  _key_uniqueness_policy_{table._key_uniqueness_policy_}, _begin_index_{table._begin_index_},
429  _safe_iterators_(std::move(table._safe_iterators_)), _alloc_(std::move(table._alloc_)) {
430  // for debugging purposes
431  table._size_ = 0;
432  GUM_CONS_MOV(HashTable);
433  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

◆ ~HashTable()

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

Class destructor.

Definition at line 457 of file hashTable_tpl.h.

457  {
458  // for debugging purposes
459  GUM_DESTRUCTOR(HashTable);
460 
461  // update all the registered iterators: they should now point to nullptr
462  // and their hashtable should be set to nullptr
464  }
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
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 436 of file hashTable_tpl.h.

436  {
437  const Size len = _safe_iterators_.size();
438  for (Size i = Size(0); i < len; ++i)
439  _safe_iterators_[i]->clear();
440  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
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:47

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

296  {
297  // in debug mode, check that this and table have ' __nodes' arrays of the
298  // same size
299  GUM_ASSERT(table._size_ == _size_);
300 
301  // try to fill the array of chained lists
302  for (Size i = 0; i < table._size_; ++i) {
303  try {
304  _nodes_[i] = table._nodes_[i];
305  } catch (...) {
306  // here we could allocate the _nodes_[j], j=0..i-1, so we should
307  // deallocate them
308  for (Size j = 0; j < _size_; ++j)
309  _nodes_[j].clear();
310 
311  _nb_elements_ = Size(0);
312 
313  // propagate the exception
314  throw;
315  }
316  }
317 
318  _nb_elements_ = table._nb_elements_;
319  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
void clear()
Removes all the elements in the hash table.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

349  {
350  // setup the _nodes_ vector (contains only empty lists)
351  _nodes_.resize(size);
352 
353  for (auto& list: _nodes_) {
354  list.setAllocator(_alloc_);
355  }
356 
357  // set up properly the hash function
358  _hash_func_.resize(size);
359 
360  // make sure the end() iterator is constructed properly
361  end4Statics();
362  endSafe4Statics();
363  }
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.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
static const iterator & end4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

954  {
955  if (bucket == nullptr) return;
956 
957  // update the registered iterators pointing to this bucket
958  for (auto iter: _safe_iterators_) {
959  if (iter->_bucket_ == bucket) {
960  iter->operator++();
961  iter->_next_bucket_ = iter->_bucket_;
962  iter->_bucket_ = nullptr;
963  } else if (iter->_next_bucket_ == bucket) {
964  iter->_bucket_ = bucket;
965  iter->operator++();
966  iter->_next_bucket_ = iter->_bucket_;
967  iter->_bucket_ = nullptr;
968  }
969  }
970 
971  // remove the element from the _nodes_ vector
972  _nodes_[index].erase(bucket);
973 
974  --_nb_elements_;
975 
976  if ((index == _begin_index_) && _nodes_[index].empty()) {
977  _begin_index_ = std::numeric_limits< Size >::max();
978  }
979  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
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 806 of file hashTable_tpl.h.

806  {
807  Size hash_key = _hash_func_(bucket->key());
808 
809  // check that there does not already exist an element with the same key
810  if (_key_uniqueness_policy_ && _nodes_[hash_key].exists(bucket->key())) {
811  // remove the bucket from memory
812  Key k = bucket->key();
813  _alloc_.destroy(bucket);
814  _alloc_.deallocate(bucket, 1);
815  GUM_ERROR(DuplicateElement,
816  "the hashtable contains an element with the same key (" << k << ")");
817  }
818 
819  // check whether there is sufficient space to insert the new pair
820  // if not, resize the current hashtable
822  resize(_size_ << 1);
823  hash_key = _hash_func_(bucket->key());
824  }
825 
826  // add the new pair
827  _nodes_[hash_key].insert(bucket);
828  ++_nb_elements_;
829 
830  // recompute the index of the beginning of the hashtable if possible
831  // WARNING: if _begin_index_ = std::numeric_limits<Size>::max (), we CANNOT
832  // recompute the index because we cannot know whether the current index is
833  // equal to max because there was no element in the hashtable or whether a
834  // previous _erase_() has set the index to max.
835  if (_begin_index_ < hash_key) { _begin_index_ = hash_key; }
836  }
void resize(Size new_size)
Changes the number of slots in the &#39;nodes&#39; vector of the hash table.
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition: hashTable.h:86
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

606  {
607  // if the table is empty, make the begin and end point to the same element
608  if (_nb_elements_ == Size(0))
609  return iterator{end()};
610  else
611  return iterator{*this};
612  }
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:680
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

616  {
617  // if the table is empty, make the begin and end point to the same element
618  if (_nb_elements_ == Size(0))
619  return const_iterator{end()};
620  else
621  return const_iterator{*this};
622  }
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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:681

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

666  {
667  // if the table is empty, make the begin and end point to the same element
668  if (_nb_elements_ == Size(0))
669  return iterator_safe{endSafe()};
670  else
671  return iterator_safe{*this};
672  }
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:682
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

676  {
677  // if the table is empty, make the begin and end point to the same element
678  if (_nb_elements_ == Size(0))
679  return const_iterator_safe{endSafe()};
680  else
681  return const_iterator_safe{*this};
682  }
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:683
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

710  {
711  return _size_;
712  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703

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

626  {
627  // if the table is empty, make the begin and end point to the same element
628  if (_nb_elements_ == Size(0))
629  return const_iterator{cend()};
630  else
631  return const_iterator{*this};
632  }
const const_iterator & cend() const noexcept
Returns the unsafe const_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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:681

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

686  {
687  // if the table is empty, make the begin and end point to the same element
688  if (_nb_elements_ == Size(0))
689  return const_iterator_safe{cendSafe()};
690  else
691  return const_iterator_safe{*this};
692  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:683
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

597  {
598  // note that, here, we know for sure that HashTableIterEnd has been properly
599  // initialized as it is initialized by end4Statics, which is called by
600  // all hashtables' constructors
601  return *(
603  }
static const HashTableIterator< int, int > * _HashTableIterEnd_
The unsafe iterator used by everyone.
Definition: hashTable.h:1815
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:681

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

656  {
657  // note that, here, we know for sure that HashTableIterEnd has been properly
658  // initialized as it is initialized by end4Statics, which is called by
659  // all hashtables' constructors
660  return *(reinterpret_cast< const const_iterator_safe* >(
662  }
static const HashTableIteratorSafe< int, int > * _HashTableIterEndSafe_
The safe iterator used by everyone.
Definition: hashTable.h:1818
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:683

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

443  {
444  // update all the registered iterators: they should now point to nullptr
445  // and they are positioned to the end of the hashtable.
447 
448  // remove the buckets
449  for (Size i = Size(0); i < _size_; ++i)
450  _nodes_[i].clear();
451 
452  _nb_elements_ = Size(0);
453  _begin_index_ = std::numeric_limits< Size >::max();
454  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
void clear()
Removes all the elements in the hash table.
void _clearIterators_()
Clear all the safe iterators.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

329  {
330  return *(
331  reinterpret_cast< const const_iterator* >(HashTableIteratorStaticEnd::constEnd4Statics()));
332  }
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:681

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

343  {
344  return *(reinterpret_cast< const const_iterator_safe* >(
346  }
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:683
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 905 of file hashTable_tpl.h.

905  {
906  Bucket* bucket = _alloc_.allocate(1);
907 
908  try {
909  _alloc_.construct(bucket,
911  std::forward< Args >(args)...);
912  } catch (...) {
913  _alloc_.deallocate(bucket, 1);
914  throw;
915  }
916 
917  _insert_(bucket);
918  return bucket->elt();
919  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

1042  {
1043  return (_nb_elements_ == Size(0));
1044  }
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

578  {
579  // note that, here, we know for sure that HashTableIterEnd has been properly
580  // initialized as it is initialized by end4Statics, which is called by
581  // all hashtables' constructors
582  return *(reinterpret_cast< const iterator* >(HashTableIteratorStaticEnd::_HashTableIterEnd_));
583  }
static const HashTableIterator< int, int > * _HashTableIterEnd_
The unsafe iterator used by everyone.
Definition: hashTable.h:1815
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:680

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

587  {
588  // note that, here, we know for sure that HashTableIterEnd has been properly
589  // initialized as it is initialized by end4Statics, which is called by
590  // all hashtables' constructors
591  return *(
593  }
static const HashTableIterator< int, int > * _HashTableIterEnd_
The unsafe iterator used by everyone.
Definition: hashTable.h:1815
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Definition: hashTable.h:681

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

323  {
324  return *(reinterpret_cast< const iterator* >(HashTableIteratorStaticEnd::end4Statics()));
325  }
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:680

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

636  {
637  // note that, here, we know for sure that HashTableIterEnd has been properly
638  // initialized as it is initialized by end4Statics, which is called by
639  // all hashtables' constructors
640  return *(reinterpret_cast< const iterator_safe* >(
642  }
static const HashTableIteratorSafe< int, int > * _HashTableIterEndSafe_
The safe iterator used by everyone.
Definition: hashTable.h:1818
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:682

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

646  {
647  // note that, here, we know for sure that HashTableIterEnd has been properly
648  // initialized as it is initialized by end4Statics, which is called by
649  // all hashtables' constructors
650  return *(reinterpret_cast< const const_iterator_safe* >(
652  }
static const HashTableIteratorSafe< int, int > * _HashTableIterEndSafe_
The safe iterator used by everyone.
Definition: hashTable.h:1818
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition: hashTable.h:683

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

336  {
337  return *(
338  reinterpret_cast< const iterator_safe* >(HashTableIteratorStaticEnd::endSafe4Statics()));
339  }
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:682

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

982  {
983  // get the hashed key
984  Size hash = _hash_func_(key);
985 
986  // get the bucket containing the element to erase
987  HashTableBucket< Key, Val >* bucket = _nodes_[hash].bucket(key);
988 
989  _erase_(bucket, hash);
990  }
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

993  {
994  _erase_(iter._getBucket_(), iter._getIndex_());
995  }
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 998 of file hashTable_tpl.h.

998  {
999  _erase_(iter._getBucket_(), iter._getIndex_());
1000  }
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 1035 of file hashTable_tpl.h.

1035  {
1036  for (auto iterAll = cbeginSafe(); iterAll != cendSafe(); ++iterAll) {
1037  if (iterAll._bucket_->val() == val) { _erase_(iterAll._bucket_, iterAll._index_); }
1038  }
1039  }
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.

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

1003  {
1004  for (auto iter = cbegin(); iter != cend(); ++iter)
1005  if (iter._bucket_->val() == val) {
1006  _erase_(iter._getBucket_(), iter._getIndex_());
1007  return;
1008  }
1009  }
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
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.

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

715  {
716  return _nodes_[_hash_func_(key)].exists(key);
717  }
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

923  {
924  Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
925 
926  if (bucket == nullptr)
927  return insert(key, default_value).second;
928  else
929  return bucket->val();
930  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

934  {
935  Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
936 
937  if (bucket == nullptr)
938  return insert(std::move(key), std::move(default_value)).second;
939  else
940  return bucket->val();
941  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

840  {
841  Bucket* bucket = _alloc_.allocate(1);
842 
843  try {
844  _alloc_.construct(bucket, thekey, theval);
845  } catch (...) {
846  _alloc_.deallocate(bucket, 1);
847  throw;
848  }
849 
850  _insert_(bucket);
851  return bucket->elt();
852  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

856  {
857  Bucket* bucket = _alloc_.allocate(1);
858 
859  try {
860  _alloc_.construct(bucket, std::move(thekey), std::move(theval));
861  } catch (...) {
862  _alloc_.deallocate(bucket, 1);
863  throw;
864  }
865 
866  _insert_(bucket);
867  return bucket->elt();
868  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

872  {
873  Bucket* bucket = _alloc_.allocate(1);
874 
875  try {
876  _alloc_.construct(bucket, reinterpret_cast< const value_type& >(elt));
877  } catch (...) {
878  _alloc_.deallocate(bucket, 1);
879  throw;
880  }
881 
882  _insert_(bucket);
883  return bucket->elt();
884  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

888  {
889  Bucket* bucket = _alloc_.allocate(1);
890 
891  try {
892  _alloc_.construct(bucket, std::move(reinterpret_cast< value_type& >(elt)));
893  } catch (...) {
894  _alloc_.deallocate(bucket, 1);
895  throw;
896  }
897 
898  _insert_(bucket);
899  return bucket->elt();
900  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

1025  {
1026  // get the bucket corresponding to the key
1027  Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
1028 
1029  if (bucket == nullptr) { GUM_ERROR(NotFound, "key does not belong to the hashtable") }
1030 
1031  return bucket->key();
1032  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

1017  {
1018  for (auto iter = begin(); iter != end(); ++iter)
1019  if (iter._bucket_->val() == val) return iter.key();
1020 
1021  GUM_ERROR(NotFound, "not enough elements in the chained list")
1022  }
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:51

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

735  {
737  }
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715

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

1052  {
1053  // determine the proper size of the hashtable
1054  // by default, the size of the table is set so that the table does not take
1055  // too much space while allowing to add a few elements without needing to
1056  // resize in autmatic resizing mode
1057  if (size == 0) size = std::max(Size(2), _nb_elements_ / 2);
1058 
1059  // create a new table
1060  HashTable< Key, Mount, OtherAlloc > table(size, resize_pol, key_uniqueness_pol);
1061 
1062  // fill the new hash table
1063  for (auto iter = begin(); iter != end(); ++iter) {
1064  table.insert(iter.key(), f(iter.val()));
1065  }
1066 
1067  return table;
1068  }
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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

1076  {
1077  // determine the proper size of the hashtable
1078  // by default, the size of the table is set so that the table does not take
1079  // too much space while allowing to add a few elements without needing to
1080  // resize in autmatic resizing mode
1081  if (size == Size(0)) size = std::max(Size(2), _nb_elements_ / 2);
1082 
1083  // create a new table
1084  HashTable< Key, Mount, OtherAlloc > table(size, resize_pol, key_uniqueness_pol);
1085 
1086  // fill the new hash table
1087  for (auto iter = begin(); iter != end(); ++iter) {
1088  table.insert(iter.key(), f(const_cast< Val& >(iter.val())));
1089  }
1090 
1091  return table;
1092  }
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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

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

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

1124  {
1125  // determine the proper size of the hashtable
1126  // by default, the size of the table is set so that the table does not take
1127  // too much space while allowing to add a few elements without needing to
1128  // resize in autmatic resizing mode
1129  if (size == Size(0)) size = std::max(Size(2), _nb_elements_ / 2);
1130 
1131  // create a new table
1132  HashTable< Key, Mount, OtherAlloc > table(size, resize_pol, key_uniqueness_pol);
1133 
1134  // fill the new hash table
1135  for (auto iter = begin(); iter != end(); ++iter) {
1136  table.insert(iter.key(), val);
1137  }
1138 
1139  return table;
1140  }
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:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ 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 >
INLINE bool gum::HashTable< Key, Val, Alloc >::operator!= ( const HashTable< Key, Val, OtherAlloc > &  from) const

Definition at line 1162 of file hashTable_tpl.h.

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

◆ operator!=() [2/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=() [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 468 of file hashTable_tpl.h.

468  {
469  // avoid self assignment
470  if (this != &from) {
471  // for debugging purposes
472  GUM_OP_CPY(HashTable);
473 
474  // first remove the current content of the hashtable and make
475  // the iterators point to end
476  clear();
477 
478  // if sizes of from's and this' _nodes_ vectors are not the same,
479  // we need to remove the current _nodes_' array and to create a
480  // new array with the correct size
481  if (_size_ != from._size_) {
482  _nodes_.resize(from._size_);
483 
484  for (Size i = Size(0); i < from._size_; ++i) {
485  _nodes_[i].setAllocator(_alloc_);
486  }
487 
488  _size_ = from._size_;
489 
490  // update the hash function : this is important as the computation of
491  // the hash values heavily depends on the size of the hash table
492  _hash_func_.resize(_size_);
493  }
494 
495  _resize_policy_ = from._resize_policy_;
496  _key_uniqueness_policy_ = from._key_uniqueness_policy_;
497  _begin_index_ = from._begin_index_;
498 
499  // perform the copy
500  _copy_(from);
501  }
502 
503  return *this;
504  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
void _copy_(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
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:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

509  {
510  // avoid self assignment
511  if (this != reinterpret_cast< const HashTable< Key, Val, Alloc >* >(&from)) {
512  // for debugging purposes
513  GUM_OP_CPY(HashTable);
514 
515  // first remove the current content of the hashtable and make
516  // the iterators point to end
517  clear();
518 
519  // if sizes of from's and this' _nodes_ vectors are not the same,
520  // we need to remove the current _nodes_' array and to create a
521  // new array with the correct size
522  if (_size_ != from._size_) {
523  _nodes_.resize(from._size_);
524 
525  for (Size i = 0; i < from._size_; ++i) {
526  _nodes_[i].setAllocator(_alloc_);
527  }
528 
529  _size_ = from._size_;
530 
531  // update the hash function : this is important as the computation of
532  // the hash values heavily depends on the size of the hash table
533  _hash_func_.resize(_size_);
534  }
535 
536  _resize_policy_ = from._resize_policy_;
537  _key_uniqueness_policy_ = from._key_uniqueness_policy_;
538  _begin_index_ = from._begin_index_;
539 
540  // perform the copy
541  _copy_(from);
542  }
543 
544  return *this;
545  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
void _copy_(const HashTable< Key, Val, OtherAlloc > &table)
A function used to perform copies of HashTables.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
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:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

549  {
550  // avoid self assignment
551  if (this != &table) {
552  // for debugging purposes
553  GUM_OP_MOV(HashTable);
554 
555  // first remove the current content of the hashtable and make
556  // the iterators point to end
557  clear();
558 
559  _nodes_ = std::move(table._nodes_);
560  _safe_iterators_ = std::move(table._safe_iterators_);
561  _alloc_ = std::move(table._alloc_);
562  _size_ = table._size_;
563  _nb_elements_ = table._nb_elements_;
564  _hash_func_ = table._hash_func_;
565  _resize_policy_ = table._resize_policy_;
566  _key_uniqueness_policy_ = table._key_uniqueness_policy_;
567  _begin_index_ = table._begin_index_;
568 
569  table._size_ = 0; // necessary if we wish to perform moves iteratively,
570  // i.e. x = std::move ( y ); y = std::move ( z ); ...
571  }
572 
573  return *this;
574  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
friend class HashTable
Friends to optimize the access to data, iterators must be friends.
Definition: hashTable.h:1682
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
void clear()
Removes all the elements in the hash table.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

1145  {
1146  // checks whether the two hashtables contain the same number of elements
1147  if (from._nb_elements_ != _nb_elements_) return false;
1148 
1149  // parse this and check that each element also belongs to from
1150  for (auto iter = begin(); iter != end(); ++iter) {
1151  try {
1152  if (iter.val() != from[iter.key()]) return false;
1153  } catch (NotFound&) { return false; }
1154  }
1155 
1156  return true;
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 _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706

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

695  {
696  return _nodes_[_hash_func_(key)][key];
697  }
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

700  {
701  return _nodes_[_hash_func_(key)][key];
702  }
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

1012  {
1013  erase(key);
1014  }
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.

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

740  {
741  // new_size must be >= 2 else all the bits of the hash function are lost
742  new_size = std::max(Size(2), new_size);
743 
744  // find the real size for allocation (the smallest power of 2 greater
745  // than or equal to new_size) and get its base-2 logarithm
746  int log_size = _hashTableLog2_(new_size);
747  new_size = Size(1) << log_size;
748 
749  // check if the new size is different from the actual size
750  // if not, nothing else need be done
751 
752  if (new_size != _size_) {
753  // under automatic resize policy, check if the new size leaves
754  // enough space for storing all the current elements
755  if (!_resize_policy_
757  // create a new array of _nodes_ to store the elements
758  std::vector< HashTableList< Key, Val, Alloc > > new_nodes(new_size);
759 
760  for (auto& list: new_nodes) {
761  list.setAllocator(_alloc_);
762  }
763 
764  // set the new hash function
765  _hash_func_.resize(new_size);
766 
767  // put all the elements of the current _nodes_ array into the new one
768  Bucket* bucket;
769  Size new_hashed_key;
770 
771  for (Size i = Size(0); i < _size_; ++i) {
772  while ((bucket = _nodes_[i]._deb_list_) != nullptr) {
773  // compute the new hashed key
774  new_hashed_key = _hash_func_(bucket->key());
775 
776  // remove the bucket from the list of buckets of the current
777  // node vector
778  _nodes_[i]._deb_list_ = bucket->next;
779 
780  // put the bucket into the new _nodes_ vector
781  new_nodes[new_hashed_key].insert(bucket);
782  }
783  }
784 
785  // update the size of the hash table
786  _size_ = new_size;
787  _begin_index_ = std::numeric_limits< Size >::max();
788 
789  // substitute the current _nodes_ array by the new one
790  std::swap(_nodes_, new_nodes);
791 
792  // update the iterators
793  for (auto iter: _safe_iterators_) {
794  if (iter->_bucket_)
795  iter->_index_ = _hash_func_(iter->_bucket_->key());
796  else {
797  iter->_next_bucket_ = nullptr;
798  iter->_index_ = 0;
799  }
800  }
801  }
802  }
803  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition: hashTable.h:86
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
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 _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
BucketAllocator _alloc_
The allocator for the buckets.
Definition: hashTable.h:1744

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

725  {
726  return _resize_policy_;
727  }
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712

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

944  {
945  Bucket* bucket = _nodes_[_hash_func_(key)].bucket(key);
946 
947  if (bucket == nullptr)
948  insert(key, value);
949  else
950  bucket->val() = value;
951  }
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
Definition: hashTable.h:687
const Key & key(const Key &key) const
Returns a reference on a given key.
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
Definition: hashTable.h:1709
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700

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

730  {
731  _key_uniqueness_policy_ = new_policy;
732  }
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Definition: hashTable.h:1715

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

720  {
721  _resize_policy_ = new_policy;
722  }
bool _resize_policy_
Is resizing performed automatically?
Definition: hashTable.h:1712

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

705  {
706  return _nb_elements_;
707  }
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706

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 1682 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 1693 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 1684 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 1686 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 1683 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 1685 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 1201 of file hashTable_tpl.h.

1201  {
1202  bool deja = false;
1203  stream << "{";
1204 
1205  for (Size i = Size(0); i < table._size_; ++i)
1206  for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1207  if (deja) stream << " , ";
1208 
1209  stream << ptr->key() << "=>" << ptr->val();
1210 
1211  deja = true;
1212  }
1213 
1214  stream << "}";
1215 
1216  return stream;
1217  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

1220  {
1221  bool deja = false;
1222  stream << "{";
1223 
1224  for (Size i = Size(0); i < table._size_; ++i)
1225  for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1226  if (deja) stream << " , ";
1227 
1228  stream << ptr->key() << "=>" << ptr->val();
1229 
1230  deja = true;
1231  }
1232 
1233  stream << "}";
1234 
1235  return stream;
1236  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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 1744 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 1731 of file hashTable.h.

◆ _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 1709 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 1715 of file hashTable.h.

◆ _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 1706 of file hashTable.h.

◆ _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 1700 of file hashTable.h.

◆ _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 1712 of file hashTable.h.

◆ _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 1734 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 number of nodes in vector ' __nodes'.

Definition at line 1703 of file hashTable.h.


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