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

Safe Const Iterators for hashtables. More...

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

+ Inheritance diagram for gum::HashTableConstIteratorSafe< Key, Val >:
+ Collaboration diagram for gum::HashTableConstIteratorSafe< Key, Val >:

Public Member Functions

template<typename Alloc >
INLINE HashTableConstIteratorSafe (const HashTable< Key, Val, Alloc > &tab)
 
Constructors / Destructors
 HashTableConstIteratorSafe ()
 Basic constructor: creates an iterator pointing to nothing. More...
 
template<typename Alloc >
 HashTableConstIteratorSafe (const HashTable< Key, Val, Alloc > &tab)
 Constructor for an iterator pointing to the first element of a hashtable. More...
 
template<typename Alloc >
 HashTableConstIteratorSafe (const HashTable< Key, Val, Alloc > &tab, Size ind_elt)
 Constructor for an iterator pointing to the nth element of a hashtable. More...
 
 HashTableConstIteratorSafe (const HashTableConstIteratorSafe< Key, Val > &from)
 Copy constructor. More...
 
 HashTableConstIteratorSafe (const HashTableConstIterator< Key, Val > &from)
 Copy constructor. More...
 
 HashTableConstIteratorSafe (HashTableConstIteratorSafe< Key, Val > &&from)
 Move constructor. More...
 
 ~HashTableConstIteratorSafe () noexcept
 Destructor. More...
 
Accessors / Modifiers
const key_typekey () const
 Returns the key pointed to by the iterator. More...
 
const mapped_typeval () const
 Returns the mapped value pointed to by the iterator. More...
 
void clear () noexcept
 Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash table). More...
 
Operators
HashTableConstIteratorSafe< Key, Val > & operator= (const HashTableConstIteratorSafe< Key, Val > &from)
 Copy operator. More...
 
HashTableConstIteratorSafe< Key, Val > & operator= (const HashTableConstIterator< Key, Val > &from)
 Copy operator. More...
 
HashTableConstIteratorSafe< Key, Val > & operator= (HashTableConstIteratorSafe< Key, Val > &&from) noexcept
 Move operator. More...
 
HashTableConstIteratorSafe< Key, Val > & operator++ () noexcept
 Makes the iterator point to the next element in the hash table. More...
 
HashTableConstIteratorSafe< Key, Val > & operator+= (Size i) noexcept
 Makes the iterator point to i elements further in the hashtable. More...
 
HashTableConstIteratorSafe< Key, Val > operator+ (Size i) const
 Returns a new iterator poiting to i elements further in the hashtable. More...
 
bool operator!= (const HashTableConstIteratorSafe< Key, Val > &from) const noexcept
 Checks whether two iterators are not equal. More...
 
bool operator== (const HashTableConstIteratorSafe< Key, Val > &from) const noexcept
 Checks whether two iterators are equal. More...
 
const value_typeoperator* () const
 Returns the element pointed to by the iterator. More...
 

Public Types

using iterator_category = std::forward_iterator_tag
 Types for STL compliance. 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 difference_type = std::ptrdiff_t
 Types for STL compliance. More...
 

Protected Attributes

const HashTable< Key, Val > * __table {nullptr}
 The hash table the iterator is pointing to. More...
 
Size __index {Size(0)}
 the index of the chained list pointed to by the iterator in the array __nodes of the hash table. More...
 
HashTableBucket< Key, Val > * __bucket {nullptr}
 The bucket in the chained list pointed to by the iterator. More...
 
HashTableBucket< Key, Val > * __next_bucket {nullptr}
 the bucket we should start from when we decide to do a ++. More...
 

Protected Member Functions

HashTableBucket< Key, Val > * __getBucket () const noexcept
 Returns the current iterator's bucket. More...
 
Size __getIndex () const noexcept
 Returns the index in the hashtable's node vector pointed to by the iterator. More...
 
void __removeFromSafeList () const
 Removes the iterator from its hashtable' safe iterators list. More...
 
void __insertIntoSafeList () const
 Insert the iterator into the hashtable's list of safe iterators. More...
 

Friends

template<typename K , typename V , typename A >
class HashTable
 Class HashTable must be a friend because it stores iterator end and this can be properly initialized only when the hashtable has been fully allocated. More...
 

Detailed Description

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

Safe Const Iterators for hashtables.

HashTableConstIteratorSafe provides a safe way to parse HashTables. They are safe because they are kept informed by the hashtable they belong to of the elements deleted by the user. Hence, even if the user removes an element pointed to by a HashTableConstIteratorSafe, using the latter to access this element will never crash the application. Instead it will properly throw a UndefinedIteratorValue exception.

Developers may consider using HashTable<x,y>::const_iterator_safe instead of HashTableConstIteratorSafe<x,y>.

Usage example:
// creation of a hash table with 10 elements
HashTable<int,string> table;
for (int i = 0; i< 10; ++i)
table.insert (i,"xxx" + string (i,'x'));
// parse the hash table
for (HashTable<int,string>::const_iterator_safe iter = table.cbeginSafe ();
iter != table.cendSafe (); ++iter) {
// display the values
cerr << "at " << iter.key () << " value = " << iter.val () << endl;
const std::pair<const int, string>& xelt = *iter;
}
// check whether two iterators point toward the same element
HashTable<int,string>::const_iterator_safe iter1 = table1.cbeginSafe ();
HashTable<int,string>::const_iterator_safe iter2 = table1.cendSafe ();
if (iter1 != iter) {
cerr << "iter1 and iter2 point toward different elements";
}
// make iter1 point toward nothing
iter1.clear ();

Definition at line 1918 of file hashTable.h.

Member Typedef Documentation

◆ const_pointer

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

Types for STL compliance.

Definition at line 1929 of file hashTable.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 1927 of file hashTable.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 1930 of file hashTable.h.

◆ iterator_category

template<typename Key, typename Val>
using gum::HashTableConstIteratorSafe< Key, Val >::iterator_category = std::forward_iterator_tag

Types for STL compliance.

Definition at line 1922 of file hashTable.h.

◆ key_type

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

Types for STL compliance.

Definition at line 1923 of file hashTable.h.

◆ mapped_type

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

Types for STL compliance.

Definition at line 1924 of file hashTable.h.

◆ pointer

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

Types for STL compliance.

Definition at line 1928 of file hashTable.h.

◆ reference

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

Types for STL compliance.

Definition at line 1926 of file hashTable.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1925 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableConstIteratorSafe() [1/7]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 1314 of file hashTable_tpl.h.

1314  {
1315  // for debugging purposes
1316  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1317  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableConstIteratorSafe() [2/7]

template<typename Key, typename Val>
template<typename Alloc >
gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTable< Key, Val, Alloc > &  tab)

Constructor for an iterator pointing to the first element of a hashtable.

Template Parameters
AllocThe gum::HashTable allocator.
Parameters
tabA gum::HashTable to iterate over.

◆ HashTableConstIteratorSafe() [3/7]

template<typename Key, typename Val>
template<typename Alloc >
gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTable< Key, Val, Alloc > &  tab,
Size  ind_elt 
)

Constructor for an iterator pointing to the nth element of a hashtable.

The method runs in time linear to ind_elt.

Template Parameters
AllocThe gum::HashTable allocator.
Parameters
tabthe hash table to which the so-called element belongs
ind_eltthe position of the element in the hash table (0 means the first element).
Exceptions
UndefinedIteratorValueRaised if the element cannot be found.

Definition at line 1351 of file hashTable_tpl.h.

1352  :
1353  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1354  Size i;
1355 
1356  // check if we are looking for a begin() and we know for sure its index
1357  if ((ind_elt == Size(0))
1358  && (__table->__begin_index != std::numeric_limits< Size >::max())) {
1360  __bucket = __table->__nodes[__index].__end_list;
1361  } else {
1362  // check if it is faster to find the ind_eltth element from the start or
1363  // from the end of the hashtable
1364  if (ind_elt < (__table->__nb_elements >> 1)) {
1365  // find the element we shall point to from the start of the hashtable
1366  for (i = __table->__size - 1;; --i) { // no test on i since
1367  // ind_elt < _table->__nb_elements
1368  if (__table->__nodes[i].__nb_elements) {
1369  if (ind_elt >= __table->__nodes[i].__nb_elements)
1370  ind_elt -= __table->__nodes[i].__nb_elements;
1371  else {
1372  for (__bucket = __table->__nodes[i].__end_list; ind_elt;
1373  --ind_elt, __bucket = __bucket->prev) {}
1374 
1375  __index = i;
1376  break;
1377  }
1378  }
1379  }
1380  } else {
1381  // ind_elt = the index of the element we should point to
1382  // check if the index passed as parameter is valid
1383  if (ind_elt >= __table->__nb_elements) {
1384  GUM_ERROR(UndefinedIteratorValue,
1385  "Not enough elements in the hashtable");
1386  }
1387 
1388  // find the element we shall point to from the end of the hashtable
1389  for (i = 0, ind_elt = __table->__nb_elements - ind_elt - 1;; ++i) {
1390  if (__table->__nodes[i].__nb_elements) {
1391  if (ind_elt >= __table->__nodes[i].__nb_elements)
1392  ind_elt -= __table->__nodes[i].__nb_elements;
1393  else {
1394  for (__bucket = __table->__nodes[i].__deb_list; ind_elt;
1395  --ind_elt, __bucket = __bucket->next) {}
1396 
1397  __index = i;
1398  break;
1399  }
1400  }
1401  }
1402  }
1403  }
1404 
1405  // for debugging purposes
1406  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1407 
1408  // make the hashtable keep track of this iterator
1410  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ HashTableConstIteratorSafe() [4/7]

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

Copy constructor.

Parameters
fromThe gum::HashTableConstIteratorSafe to copy.

Definition at line 1413 of file hashTable_tpl.h.

1414  :
1415  __table{from.__table},
1416  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1417  from.__next_bucket} {
1418  // make the hashtable keep track of this iterator
1419  if (__table != nullptr) { __insertIntoSafeList(); }
1420 
1421  // for debugging purposes
1422  GUM_CONS_CPY(HashTableConstIteratorSafe);
1423  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ HashTableConstIteratorSafe() [5/7]

template<typename Key, typename Val>
INLINE gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTableConstIterator< Key, Val > &  from)
explicit

Copy constructor.

Parameters
fromThe gum::HashTableConstIterator to copy.

Definition at line 1426 of file hashTable_tpl.h.

1427  :
1428  __table{from.__table},
1429  __index{from.__index}, __bucket{from.__bucket} {
1430  // make the hashtable keep track of this iterator
1431  if (__table != nullptr) { __insertIntoSafeList(); }
1432 
1433  // for debugging purposes
1434  GUM_CONS_CPY(HashTableConstIteratorSafe);
1435  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

◆ HashTableConstIteratorSafe() [6/7]

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

Move constructor.

Parameters
fromThe gum::HashTableConstIteratorSafe to move.

Definition at line 1438 of file hashTable_tpl.h.

1439  :
1440  __table{from.__table},
1441  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1442  from.__next_bucket} {
1443  GUM_CONS_MOV(HashTableConstIteratorSafe);
1444 
1445  // find "from" in the hashtable's list of safe iterators and substitute
1446  // it by this
1447  if (__table != nullptr) {
1448  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1450 
1451  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1452  if (*ptr == &from) {
1453  *ptr = this;
1454  from.__table = nullptr;
1455  break;
1456  }
1457  }
1458  }
1459  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1753
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1464 of file hashTable_tpl.h.

1464  {
1465  // for debugging purposes
1466  GUM_DESTRUCTOR(HashTableConstIteratorSafe);
1467 
1468  // remove the iterator from the table's iterator list
1470  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.

◆ HashTableConstIteratorSafe() [7/7]

template<typename Key, typename Val>
template<typename Alloc >
INLINE gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTable< Key, Val, Alloc > &  tab)

Definition at line 1321 of file hashTable_tpl.h.

1322  :
1323  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1324  // for debugging purposes
1325  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1326 
1327  // make the hashtable keep track of this iterator
1329 
1330  if (__table->__nb_elements) {
1331  if (__table->__begin_index != std::numeric_limits< Size >::max()) {
1333  __bucket = __table->__nodes[__index].__end_list;
1334  } else {
1335  // find the element we shall point to from the start of the hashtable
1336  for (Size i = __table->__size - Size(1);; --i) { // no test on i since
1337  // __nb_elements != 0
1338  if (__table->__nodes[i].__nb_elements) {
1339  __index = i;
1340  __bucket = __table->__nodes[__index].__end_list;
1342  break;
1343  }
1344  }
1345  }
1346  }
1347  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

Member Function Documentation

◆ __getBucket()

template<typename Key , typename Val >
INLINE HashTableBucket< Key, Val > * gum::HashTableConstIteratorSafe< Key, Val >::__getBucket ( ) const
protectednoexcept

Returns the current iterator's bucket.

Definition at line 1738 of file hashTable_tpl.h.

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

1738  {
1739  return __bucket;
1740  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
+ Here is the caller graph for this function:

◆ __getIndex()

template<typename Key , typename Val >
INLINE Size gum::HashTableConstIteratorSafe< Key, Val >::__getIndex ( ) const
protectednoexcept

Returns the index in the hashtable's node vector pointed to by the iterator.

Returns
Returns the index in the hashtable's node vector pointed to by the iterator.

Definition at line 1743 of file hashTable_tpl.h.

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

1743  {
1744  return __index;
1745  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
+ Here is the caller graph for this function:

◆ __insertIntoSafeList()

template<typename Key , typename Val >
INLINE void gum::HashTableConstIteratorSafe< Key, Val >::__insertIntoSafeList ( ) const
protected

Insert the iterator into the hashtable's list of safe iterators.

Definition at line 1290 of file hashTable_tpl.h.

1290  {
1291  __table->__safe_iterators.push_back(
1292  const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1293  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1753
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126

◆ __removeFromSafeList()

template<typename Key , typename Val >
INLINE void gum::HashTableConstIteratorSafe< Key, Val >::__removeFromSafeList ( ) const
protected

Removes the iterator from its hashtable' safe iterators list.

Definition at line 1297 of file hashTable_tpl.h.

1297  {
1298  if (__table == nullptr) return;
1299 
1300  // find where the iterator is
1301  std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect =
1303 
1304  auto len = iter_vect.size();
1305  for (Size i = Size(0); i < len; ++i) {
1306  if (iter_vect[i] == this) {
1307  iter_vect.erase(iter_vect.begin() + i);
1308  break;
1309  }
1310  }
1311  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1753
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

◆ clear()

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

Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash table).

It is mainly used by the hashtable when the latter is deleted while the iterator is still alive.

Definition at line 1588 of file hashTable_tpl.h.

1588  {
1589  // remove the iterator from the table's iterator list
1591 
1592  // set its table as well as the element it points to to 0
1593  __table = nullptr;
1594  __bucket = nullptr;
1595  __next_bucket = nullptr;
1596  __index = Size(0);
1597  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ key()

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

Returns the key pointed to by the iterator.

Returns
Returns the key pointed to by the iterator.
Exceptions
UndefinedIteratorValueRaised when the iterator does not point to a valid hash table element

Definition at line 1569 of file hashTable_tpl.h.

Referenced by gum::SetTerminalNodePolicy< GUM_SCALAR >::id(), and gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::key().

1569  {
1570  if (__bucket != nullptr)
1571  return __bucket->key();
1572  else {
1573  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1574  }
1575  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ operator!=()

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

Checks whether two iterators are not equal.

Parameters
fromfrom The iterator to test for inequality.
Returns
Returns true if from and this iterator are inequal.

Definition at line 1715 of file hashTable_tpl.h.

1716  {
1717  return ((__bucket != from.__bucket) || (__index != from.__index));
1718  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135

◆ operator*()

template<typename Key , typename Val >
INLINE const HashTableConstIteratorSafe< Key, Val >::value_type & gum::HashTableConstIteratorSafe< Key, Val >::operator* ( ) const

Returns the element pointed to by the iterator.

Returns
Returns the element pointed to by the iterator.
Exceptions
UndefinedIteratorValueRaised when the iterator does not point to a valid hash table element.

Definition at line 1728 of file hashTable_tpl.h.

1728  {
1729  if (__bucket)
1730  return __bucket->elt();
1731  else {
1732  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1733  }
1734  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ operator+()

template<typename Key , typename Val >
INLINE HashTableConstIteratorSafe< Key, Val > gum::HashTableConstIteratorSafe< Key, Val >::operator+ ( Size  i) const

Returns a new iterator poiting to i elements further in the hashtable.

Parameters
iThe number of steps to increment the gum::HashTableConstIteratorSafe.
Returns
Returns a new gum::HashTableConstIteratorSafe.

Definition at line 1710 of file hashTable_tpl.h.

1710  {
1711  return HashTableConstIteratorSafe< Key, Val >{*this} += nb;
1712  }

◆ operator++()

template<typename Key , typename Val >
HashTableConstIteratorSafe< Key, Val > & gum::HashTableConstIteratorSafe< Key, Val >::operator++ ( )
noexcept

Makes the iterator point to the next element in the hash table.

for (iter = hash.beginSafe(); iter != hash.endSafe (); ++iter) { }

The above loop is guaranteed to parse the whole hash table as long as no element is added to or deleted from the hash table while being in the loop. Deleting elements during the loop is guaranteed to never produce a segmentation fault.

Returns
Returns this gum::HashTableConstIteratorSafe pointing to the next element in the gum::HashTable it's iterating over.

Definition at line 1603 of file hashTable_tpl.h.

1603  {
1604  // if __bucket != nullptr then use it, else use next_bucket
1605  if (__bucket == nullptr) {
1606  // note that this case only happens when the iterator pointed to an
1607  // element
1608  // that has just been erased. Fortunately, in this case, the Hashtable's
1609  // erase functions update appropriately the __next_bucket and __index
1610  // fields.
1612  __next_bucket = nullptr;
1613  } else {
1614  // ok, here we can use __bucket as a starting point
1615 
1616  // if we are not pointing on the first element of the chained list, just
1617  // point to the preceding bucket in this list
1618  if (__bucket->prev) {
1619  __bucket = __bucket->prev;
1620  // here, no need to update __next_bucket, which is compulsorily
1621  // equal to nullptr, nor __index which has not changed.
1622  } else {
1623  // ok, here we are on the beginning of a chained list,
1624  // so 2 cases can obtain:
1625  // 1/ index = 0 : then we have reached the end of the hashtable
1626  // 2/ index != 0 => we must search for a new slot containing elements
1627 
1628  // case 1:
1629  if (__index == Size(0)) {
1630  __bucket = nullptr;
1631  // we are thus at the end() of the hashTable
1632  }
1633  // case 2:
1634  else {
1635  // arrived here, we need to parse the hash table until we find a new
1636  // bucket because we are pointing on a chained list with no more
1637  // element
1638  // to the left of the current element
1639  if (__index > Size(0)) {
1640  for (Size i = __index - Size(1); i > Size(0); --i) {
1641  if (__table->__nodes[i].__nb_elements) {
1642  __index = i;
1643  __bucket = __table->__nodes[i].__end_list;
1644  return *this;
1645  }
1646  }
1647  }
1648 
1649  if (__table->__nodes[0].__nb_elements)
1650  __bucket = __table->__nodes[0].__end_list;
1651  else
1652  __bucket = nullptr;
1653 
1654  __index = 0;
1655  }
1656  }
1657  }
1658 
1659  return *this;
1660  }
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ operator+=()

template<typename Key , typename Val >
HashTableConstIteratorSafe< Key, Val > & gum::HashTableConstIteratorSafe< Key, Val >::operator+= ( Size  i)
noexcept

Makes the iterator point to i elements further in the hashtable.

Parameters
iThe number of steps to increment the gum::HashTableConstIteratorSafe.
Returns
Returns this gum::HashTableConstIteratorSafe.

Definition at line 1664 of file hashTable_tpl.h.

1664  {
1665  if ((nb == Size(0)) || (__table == nullptr)) return *this;
1666 
1667  // if __bucket != nullptr then use it, else use next_bucket
1668  if (__bucket == nullptr) {
1669  // note that this case only happens when the iterator pointed to an
1670  // element
1671  // that has just been erased. Fortunately, in this case, the Hashtable's
1672  // erase functions update appropriately the __next_bucket and __index
1673  // fields.
1675  __next_bucket = nullptr;
1676  --nb;
1677  }
1678 
1679  // ok, here we can use __bucket as a starting point: parse all the elements
1680  // of the current chained list
1681  for (; nb && __bucket != nullptr; --nb, __bucket = __bucket->prev) {}
1682 
1683  if (__bucket != nullptr) return *this;
1684 
1685  // here, we shall skip all the chained list that have not sufficiently
1686  // many elements
1687  --__index;
1688 
1689  for (; __index < __table->__size
1690  && nb >= __table->__nodes[__index].__nb_elements;
1691  nb -= __table->__nodes[__index].__nb_elements, --__index) {}
1692 
1693  // here: either __index >= __table->__size, which means that we did not find
1694  // the element we looked for, i.e., we are at the end of the hashtable, or
1695  // nb < __table->__nodes[__index].__nb_elements, and we should parse the
1696  // chained list to get the element (which, we know for sure, exists)
1697  if (__index >= __table->__size) {
1698  __index = Size(0);
1699  return *this;
1700  }
1701 
1702  for (__bucket = __table->__nodes[__index].__end_list; nb;
1703  --nb, __bucket = __bucket->prev) {}
1704 
1705  return *this;
1706  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ operator=() [1/3]

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

Copy operator.

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

Definition at line 1474 of file hashTable_tpl.h.

1475  {
1476  // here, no need to avoid self assignment: this would slow down normal
1477  // assignments and, in any case, this would not result in an iterator in
1478  // an incoherent state
1479  // check if the current hashtable is different from that of "from". In such
1480  // a case, we shall remove the iterator from its current hashtable
1481  // iterator's
1482  // list and add it to the new hashtable iterator's list
1483  if (__table != from.__table) {
1484  // remove the iterator from its hashtable iterator's list'
1486 
1487  __table = from.__table;
1488 
1489  // add to the new table
1490  if (__table) { __insertIntoSafeList(); }
1491  }
1492 
1493  __index = from.__index;
1494  __bucket = from.__bucket;
1495  __next_bucket = from.__next_bucket;
1496 
1497  return *this;
1498  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ operator=() [2/3]

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

Copy operator.

Parameters
fromThe gum::HashTableConstIterator to copy.
Returns
Returns this gum::HashTableConstIteratorSafe.

Definition at line 1502 of file hashTable_tpl.h.

1503  {
1504  // here, no need to avoid self assignment: this would slow down normal
1505  // assignments and, in any case, this would not result in an iterator in
1506  // an incoherent state
1507  // check if the current hashtable is different from that of "from". In such
1508  // a case, we shall remove the iterator from its current hashtable
1509  // iterator's
1510  // list and add it to the new hashtable iterator's list
1511  if (__table != from.__table) {
1512  // remove the iterator from its hashtable iterator's list'
1514 
1515  __table = from.__table;
1516 
1517  // add to the new table
1518  if (__table) { __insertIntoSafeList(); }
1519  }
1520 
1521  __index = from.__index;
1522  __bucket = from.__bucket;
1523  __next_bucket = nullptr;
1524 
1525  return *this;
1526  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ operator=() [3/3]

template<typename Key, typename Val>
INLINE HashTableConstIteratorSafe< Key, Val > & gum::HashTableConstIteratorSafe< Key, Val >::operator= ( HashTableConstIteratorSafe< Key, Val > &&  from)
noexcept

Move operator.

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

Definition at line 1530 of file hashTable_tpl.h.

1531  {
1532  // here, no need to avoid self assignment: this would slow down normal
1533  // assignments and, in any case, this would not result in an iterator in
1534  // an incoherent state
1535  // check if the current hashtable is different from that of "from". In such
1536  // a case, we shall remove the iterator from its current hashtable
1537  // iterator's
1538  // list and add it to the new hashtable iterator's list
1539  if (__table != from.__table) {
1540  // remove the iterator from its hashtable iterator's list'
1542 
1543  if (from.__table != nullptr) {
1544  // substitute from by this in the list of safe iterators
1545  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1546  from.__table->__safe_iterators;
1547 
1548  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1549  if (*ptr == &from) {
1550  *ptr = this;
1551  break;
1552  }
1553  }
1554  }
1555 
1556  __table = from.__table;
1557  from.__table = nullptr;
1558  }
1559 
1560  __index = from.__index;
1561  __bucket = from.__bucket;
1562  __next_bucket = from.__next_bucket;
1563 
1564  return *this;
1565  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2126
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2145

◆ operator==()

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

Checks whether two iterators are equal.

Parameters
fromfrom The iterator to test for equality.
Returns
Returns true if from and this iterator are equal.

Definition at line 1721 of file hashTable_tpl.h.

1722  {
1723  return ((__bucket == from.__bucket) && (__index == from.__index));
1724  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2132
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135

◆ val()

template<typename Key , typename Val >
INLINE const HashTableConstIteratorSafe< Key, Val >::mapped_type & gum::HashTableConstIteratorSafe< Key, Val >::val ( ) const

Returns the mapped value pointed to by the iterator.

Returns
Returns the mapped value pointed to by the iterator.
Exceptions
UndefinedIteratorValueRaised when the iterator does not point to a valid hash table element.

Definition at line 1579 of file hashTable_tpl.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::val(), and gum::SetTerminalNodePolicy< GUM_SCALAR >::value().

1579  {
1580  if (__bucket != nullptr)
1581  return __bucket->val();
1582  else {
1583  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1584  }
1585  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2135
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ HashTable

template<typename Key, typename Val>
template<typename K , typename V , typename A >
friend class HashTable
friend

Class HashTable must be a friend because it stores iterator end and this can be properly initialized only when the hashtable has been fully allocated.

Thus, proper initialization can only take place within the constructor's code of the hashtable.

Definition at line 2123 of file hashTable.h.

Member Data Documentation

◆ __bucket

template<typename Key, typename Val>
HashTableBucket< Key, Val >* gum::HashTableConstIteratorSafe< Key, Val >::__bucket {nullptr}
protected

The bucket in the chained list pointed to by the iterator.

Definition at line 2135 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __index

template<typename Key, typename Val>
Size gum::HashTableConstIteratorSafe< Key, Val >::__index {Size(0)}
protected

the index of the chained list pointed to by the iterator in the array __nodes of the hash table.

Definition at line 2132 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __next_bucket

template<typename Key, typename Val>
HashTableBucket< Key, Val >* gum::HashTableConstIteratorSafe< Key, Val >::__next_bucket {nullptr}
protected

the bucket we should start from when we decide to do a ++.

Usually it should be equal to nullptr. However, if the user has deleted the object pointed to by bucket, this will point to another bucket. When it is equal to nullptr, it means that the bucket reached after a ++ belongs to another slot of the hash table's '__node' vector.

Definition at line 2145 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __table

template<typename Key, typename Val>
const HashTable< Key, Val >* gum::HashTableConstIteratorSafe< Key, Val >::__table {nullptr}
protected

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