aGrUM  0.16.0
gum::HashTableConstIteratorSafe< Key, Val > Class Template Reference

Safe Const Iterators for hashtables. More...

#include <agrum/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 1311 of file hashTable_tpl.h.

1311  {
1312  // for debugging purposes
1313  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1314  }
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 1348 of file hashTable_tpl.h.

1349  :
1350  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1351  Size i;
1352 
1353  // check if we are looking for a begin() and we know for sure its index
1354  if ((ind_elt == Size(0))
1355  && (__table->__begin_index != std::numeric_limits< Size >::max())) {
1357  __bucket = __table->__nodes[__index].__end_list;
1358  } else {
1359  // check if it is faster to find the ind_eltth element from the start or
1360  // from the end of the hashtable
1361  if (ind_elt < (__table->__nb_elements >> 1)) {
1362  // find the element we shall point to from the start of the hashtable
1363  for (i = __table->__size - 1;; --i) { // no test on i since
1364  // ind_elt < _table->__nb_elements
1365  if (__table->__nodes[i].__nb_elements) {
1366  if (ind_elt >= __table->__nodes[i].__nb_elements)
1367  ind_elt -= __table->__nodes[i].__nb_elements;
1368  else {
1369  for (__bucket = __table->__nodes[i].__end_list; ind_elt;
1370  --ind_elt, __bucket = __bucket->prev) {}
1371 
1372  __index = i;
1373  break;
1374  }
1375  }
1376  }
1377  } else {
1378  // ind_elt = the index of the element we should point to
1379  // check if the index passed as parameter is valid
1380  if (ind_elt >= __table->__nb_elements) {
1381  GUM_ERROR(UndefinedIteratorValue,
1382  "Not enough elements in the hashtable");
1383  }
1384 
1385  // find the element we shall point to from the end of the hashtable
1386  for (i = 0, ind_elt = __table->__nb_elements - ind_elt - 1;; ++i) {
1387  if (__table->__nodes[i].__nb_elements) {
1388  if (ind_elt >= __table->__nodes[i].__nb_elements)
1389  ind_elt -= __table->__nodes[i].__nb_elements;
1390  else {
1391  for (__bucket = __table->__nodes[i].__deb_list; ind_elt;
1392  --ind_elt, __bucket = __bucket->next) {}
1393 
1394  __index = i;
1395  break;
1396  }
1397  }
1398  }
1399  }
1400  }
1401 
1402  // for debugging purposes
1403  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1404 
1405  // make the hashtable keep track of this iterator
1407  }
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 1410 of file hashTable_tpl.h.

1411  :
1412  __table{from.__table},
1413  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1414  from.__next_bucket} {
1415  // make the hashtable keep track of this iterator
1416  if (__table != nullptr) { __insertIntoSafeList(); }
1417 
1418  // for debugging purposes
1419  GUM_CONS_CPY(HashTableConstIteratorSafe);
1420  }
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 1423 of file hashTable_tpl.h.

1424  :
1425  __table{from.__table},
1426  __index{from.__index}, __bucket{from.__bucket} {
1427  // make the hashtable keep track of this iterator
1428  if (__table != nullptr) { __insertIntoSafeList(); }
1429 
1430  // for debugging purposes
1431  GUM_CONS_CPY(HashTableConstIteratorSafe);
1432  }
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 1435 of file hashTable_tpl.h.

1436  :
1437  __table{from.__table},
1438  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1439  from.__next_bucket} {
1440  GUM_CONS_MOV(HashTableConstIteratorSafe);
1441 
1442  // find "from" in the hashtable's list of safe iterators and substitute
1443  // it by this
1444  if (__table != nullptr) {
1445  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1447 
1448  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1449  if (*ptr == &from) {
1450  *ptr = this;
1451  from.__table = nullptr;
1452  break;
1453  }
1454  }
1455  }
1456  }
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 1461 of file hashTable_tpl.h.

1461  {
1462  // for debugging purposes
1463  GUM_DESTRUCTOR(HashTableConstIteratorSafe);
1464 
1465  // remove the iterator from the table's iterator list
1467  }
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 1318 of file hashTable_tpl.h.

1319  :
1320  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1321  // for debugging purposes
1322  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1323 
1324  // make the hashtable keep track of this iterator
1326 
1327  if (__table->__nb_elements) {
1328  if (__table->__begin_index != std::numeric_limits< Size >::max()) {
1330  __bucket = __table->__nodes[__index].__end_list;
1331  } else {
1332  // find the element we shall point to from the start of the hashtable
1333  for (Size i = __table->__size - Size(1);; --i) { // no test on i since
1334  // __nb_elements != 0
1335  if (__table->__nodes[i].__nb_elements) {
1336  __index = i;
1337  __bucket = __table->__nodes[__index].__end_list;
1339  break;
1340  }
1341  }
1342  }
1343  }
1344  }
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 1735 of file hashTable_tpl.h.

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

1735  {
1736  return __bucket;
1737  }
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 1740 of file hashTable_tpl.h.

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

1740  {
1741  return __index;
1742  }
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 1287 of file hashTable_tpl.h.

1287  {
1288  __table->__safe_iterators.push_back(
1289  const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1290  }
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 1294 of file hashTable_tpl.h.

1294  {
1295  if (__table == nullptr) return;
1296 
1297  // find where the iterator is
1298  std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect =
1300 
1301  auto len = iter_vect.size();
1302  for (Size i = Size(0); i < len; ++i) {
1303  if (iter_vect[i] == this) {
1304  iter_vect.erase(iter_vect.begin() + i);
1305  break;
1306  }
1307  }
1308  }
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 1583 of file hashTable_tpl.h.

1583  {
1584  // remove the iterator from the table's iterator list
1586 
1587  // set its table as well as the element it points to to 0
1588  __table = nullptr;
1589  __bucket = nullptr;
1590  __next_bucket = nullptr;
1591  __index = Size(0);
1592  }
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 1564 of file hashTable_tpl.h.

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

1564  {
1565  if (__bucket != nullptr)
1566  return __bucket->key();
1567  else {
1568  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1569  }
1570  }
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 1711 of file hashTable_tpl.h.

1712  {
1713  return ((__bucket != from.__bucket) || (__index != from.__index));
1714  }
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 1725 of file hashTable_tpl.h.

1725  {
1726  if (__bucket)
1727  return __bucket->elt();
1728  else {
1729  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1730  }
1731  }
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 1705 of file hashTable_tpl.h.

1705  {
1706  return HashTableConstIteratorSafe< Key, Val >{*this} += nb;
1707  }

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

1598  {
1599  // if __bucket != nullptr then use it, else use next_bucket
1600  if (__bucket == nullptr) {
1601  // note that this case only happens when the iterator pointed to an
1602  // element
1603  // that has just been erased. Fortunately, in this case, the Hashtable's
1604  // erase functions update appropriately the __next_bucket and __index
1605  // fields.
1607  __next_bucket = nullptr;
1608  } else {
1609  // ok, here we can use __bucket as a starting point
1610 
1611  // if we are not pointing on the first element of the chained list, just
1612  // point to the preceding bucket in this list
1613  if (__bucket->prev) {
1614  __bucket = __bucket->prev;
1615  // here, no need to update __next_bucket, which is compulsorily
1616  // equal to nullptr, nor __index which has not changed.
1617  } else {
1618  // ok, here we are on the beginning of a chained list,
1619  // so 2 cases can obtain:
1620  // 1/ index = 0 : then we have reached the end of the hashtable
1621  // 2/ index != 0 => we must search for a new slot containing elements
1622 
1623  // case 1:
1624  if (__index == Size(0)) {
1625  __bucket = nullptr;
1626  // we are thus at the end() of the hashTable
1627  }
1628  // case 2:
1629  else {
1630  // arrived here, we need to parse the hash table until we find a new
1631  // bucket because we are pointing on a chained list with no more
1632  // element
1633  // to the left of the current element
1634  if (__index > Size(0)) {
1635  for (Size i = __index - Size(1); i > Size(0); --i) {
1636  if (__table->__nodes[i].__nb_elements) {
1637  __index = i;
1638  __bucket = __table->__nodes[i].__end_list;
1639  return *this;
1640  }
1641  }
1642  }
1643 
1644  if (__table->__nodes[0].__nb_elements)
1645  __bucket = __table->__nodes[0].__end_list;
1646  else
1647  __bucket = nullptr;
1648 
1649  __index = 0;
1650  }
1651  }
1652  }
1653 
1654  return *this;
1655  }
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 1659 of file hashTable_tpl.h.

1659  {
1660  if ((nb == Size(0)) || (__table == nullptr)) return *this;
1661 
1662  // if __bucket != nullptr then use it, else use next_bucket
1663  if (__bucket == nullptr) {
1664  // note that this case only happens when the iterator pointed to an
1665  // element
1666  // that has just been erased. Fortunately, in this case, the Hashtable's
1667  // erase functions update appropriately the __next_bucket and __index
1668  // fields.
1670  __next_bucket = nullptr;
1671  --nb;
1672  }
1673 
1674  // ok, here we can use __bucket as a starting point: parse all the elements
1675  // of the current chained list
1676  for (; nb && __bucket != nullptr; --nb, __bucket = __bucket->prev) {}
1677 
1678  if (__bucket != nullptr) return *this;
1679 
1680  // here, we shall skip all the chained list that have not sufficiently
1681  // many elements
1682  --__index;
1683 
1684  for (; __index < __table->__size
1685  && nb >= __table->__nodes[__index].__nb_elements;
1686  nb -= __table->__nodes[__index].__nb_elements, --__index) {}
1687 
1688  // here: either __index >= __table->__size, which means that we did not find
1689  // the element we looked for, i.e., we are at the end of the hashtable, or
1690  // nb < __table->__nodes[__index].__nb_elements, and we should parse the
1691  // chained list to get the element (which, we know for sure, exists)
1692  if (__index >= __table->__size) {
1693  __index = Size(0);
1694  return *this;
1695  }
1696 
1697  for (__bucket = __table->__nodes[__index].__end_list; nb;
1698  --nb, __bucket = __bucket->prev) {}
1699 
1700  return *this;
1701  }
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 1471 of file hashTable_tpl.h.

1471  {
1472  // here, no need to avoid self assignment: this would slow down normal
1473  // assignments and, in any case, this would not result in an iterator in
1474  // an incoherent state
1475  // check if the current hashtable is different from that of "from". In such
1476  // a case, we shall remove the iterator from its current hashtable
1477  // iterator's
1478  // list and add it to the new hashtable iterator's list
1479  if (__table != from.__table) {
1480  // remove the iterator from its hashtable iterator's list'
1482 
1483  __table = from.__table;
1484 
1485  // add to the new table
1486  if (__table) { __insertIntoSafeList(); }
1487  }
1488 
1489  __index = from.__index;
1490  __bucket = from.__bucket;
1491  __next_bucket = from.__next_bucket;
1492 
1493  return *this;
1494  }
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 1498 of file hashTable_tpl.h.

1498  {
1499  // here, no need to avoid self assignment: this would slow down normal
1500  // assignments and, in any case, this would not result in an iterator in
1501  // an incoherent state
1502  // check if the current hashtable is different from that of "from". In such
1503  // a case, we shall remove the iterator from its current hashtable
1504  // iterator's
1505  // list and add it to the new hashtable iterator's list
1506  if (__table != from.__table) {
1507  // remove the iterator from its hashtable iterator's list'
1509 
1510  __table = from.__table;
1511 
1512  // add to the new table
1513  if (__table) { __insertIntoSafeList(); }
1514  }
1515 
1516  __index = from.__index;
1517  __bucket = from.__bucket;
1518  __next_bucket = nullptr;
1519 
1520  return *this;
1521  }
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 1526 of file hashTable_tpl.h.

1526  {
1527  // here, no need to avoid self assignment: this would slow down normal
1528  // assignments and, in any case, this would not result in an iterator in
1529  // an incoherent state
1530  // check if the current hashtable is different from that of "from". In such
1531  // a case, we shall remove the iterator from its current hashtable
1532  // iterator's
1533  // list and add it to the new hashtable iterator's list
1534  if (__table != from.__table) {
1535  // remove the iterator from its hashtable iterator's list'
1537 
1538  if (from.__table != nullptr) {
1539  // substitute from by this in the list of safe iterators
1540  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1541  from.__table->__safe_iterators;
1542 
1543  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1544  if (*ptr == &from) {
1545  *ptr = this;
1546  break;
1547  }
1548  }
1549  }
1550 
1551  __table = from.__table;
1552  from.__table = nullptr;
1553  }
1554 
1555  __index = from.__index;
1556  __bucket = from.__bucket;
1557  __next_bucket = from.__next_bucket;
1558 
1559  return *this;
1560  }
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 1718 of file hashTable_tpl.h.

1719  {
1720  return ((__bucket == from.__bucket) && (__index == from.__index));
1721  }
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 1574 of file hashTable_tpl.h.

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

1574  {
1575  if (__bucket != nullptr)
1576  return __bucket->val();
1577  else {
1578  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1579  }
1580  }
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: