aGrUM  0.20.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 1922 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 1933 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 1931 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 1934 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 1926 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 1927 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 1928 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 1932 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 1930 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 1929 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 1322 of file hashTable_tpl.h.

1322  {
1323  // for debugging purposes
1324  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1325  }
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 1359 of file hashTable_tpl.h.

1361  :
1362  table__{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1363  Size i;
1364 
1365  // check if we are looking for a begin() and we know for sure its index
1366  if ((ind_elt == Size(0))
1367  && (table__->begin_index__ != std::numeric_limits< Size >::max())) {
1369  bucket__ = table__->nodes__[index__].end_list__;
1370  } else {
1371  // check if it is faster to find the ind_eltth element from the start or
1372  // from the end of the hashtable
1373  if (ind_elt < (table__->nb_elements__ >> 1)) {
1374  // find the element we shall point to from the start of the hashtable
1375  for (i = table__->size__ - 1;; --i) { // no test on i since
1376  // ind_elt < table_->nb_elements__
1377  if (table__->nodes__[i].nb_elements__) {
1378  if (ind_elt >= table__->nodes__[i].nb_elements__)
1379  ind_elt -= table__->nodes__[i].nb_elements__;
1380  else {
1381  for (bucket__ = table__->nodes__[i].end_list__; ind_elt;
1382  --ind_elt, bucket__ = bucket__->prev) {}
1383 
1384  index__ = i;
1385  break;
1386  }
1387  }
1388  }
1389  } else {
1390  // ind_elt = the index of the element we should point to
1391  // check if the index passed as parameter is valid
1392  if (ind_elt >= table__->nb_elements__) {
1393  GUM_ERROR(UndefinedIteratorValue,
1394  "Not enough elements in the hashtable");
1395  }
1396 
1397  // find the element we shall point to from the end of the hashtable
1398  for (i = 0, ind_elt = table__->nb_elements__ - ind_elt - 1;; ++i) {
1399  if (table__->nodes__[i].nb_elements__) {
1400  if (ind_elt >= table__->nodes__[i].nb_elements__)
1401  ind_elt -= table__->nodes__[i].nb_elements__;
1402  else {
1403  for (bucket__ = table__->nodes__[i].deb_list__; ind_elt;
1404  --ind_elt, bucket__ = bucket__->next) {}
1405 
1406  index__ = i;
1407  break;
1408  }
1409  }
1410  }
1411  }
1412  }
1413 
1414  // for debugging purposes
1415  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1416 
1417  // make the hashtable keep track of this iterator
1419  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
Size begin_index__
Returns where the begin index should be.
Definition: hashTable.h:1753
Size nb_elements__
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1728
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130
void insertIntoSafeList__() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

1423  :
1424  table__{from.table__},
1425  index__{from.index__}, bucket__{from.bucket__}, next_bucket__{
1426  from.next_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  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130
void insertIntoSafeList__() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

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

1436  :
1437  table__{from.table__},
1438  index__{from.index__}, bucket__{from.bucket__} {
1439  // make the hashtable keep track of this iterator
1440  if (table__ != nullptr) { insertIntoSafeList__(); }
1441 
1442  // for debugging purposes
1443  GUM_CONS_CPY(HashTableConstIteratorSafe);
1444  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
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:2136
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130
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 1447 of file hashTable_tpl.h.

1448  :
1449  table__{from.table__},
1450  index__{from.index__}, bucket__{from.bucket__}, next_bucket__{
1451  from.next_bucket__} {
1452  GUM_CONS_MOV(HashTableConstIteratorSafe);
1453 
1454  // find "from" in the hashtable's list of safe iterators and substitute
1455  // it by this
1456  if (table__ != nullptr) {
1457  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect
1459 
1460  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1461  if (*ptr == &from) {
1462  *ptr = this;
1463  from.table__ = nullptr;
1464  break;
1465  }
1466  }
1467  }
1468  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
std::vector< HashTableConstIteratorSafe< Key, Val > *> safe_iterators__
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1757
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1473 of file hashTable_tpl.h.

1473  {
1474  // for debugging purposes
1475  GUM_DESTRUCTOR(HashTableConstIteratorSafe);
1476 
1477  // remove the iterator from the table's iterator list
1479  }
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 1329 of file hashTable_tpl.h.

1330  :
1331  table__{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1332  // for debugging purposes
1333  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1334 
1335  // make the hashtable keep track of this iterator
1337 
1338  if (table__->nb_elements__) {
1339  if (table__->begin_index__ != std::numeric_limits< Size >::max()) {
1341  bucket__ = table__->nodes__[index__].end_list__;
1342  } else {
1343  // find the element we shall point to from the start of the hashtable
1344  for (Size i = table__->size__ - Size(1);; --i) { // no test on i since
1345  // nb_elements__ != 0
1346  if (table__->nodes__[i].nb_elements__) {
1347  index__ = i;
1348  bucket__ = table__->nodes__[index__].end_list__;
1350  break;
1351  }
1352  }
1353  }
1354  }
1355  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
Size begin_index__
Returns where the begin index should be.
Definition: hashTable.h:1753
Size nb_elements__
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1728
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130
void insertIntoSafeList__() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

Member Function Documentation

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

1597  {
1598  // remove the iterator from the table's iterator list
1600 
1601  // set its table as well as the element it points to to 0
1602  table__ = nullptr;
1603  bucket__ = nullptr;
1604  next_bucket__ = nullptr;
1605  index__ = Size(0);
1606  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
void removeFromSafeList__() const
Removes the iterator from its hashtable&#39; safe iterators list.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130

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

1747  {
1748  return bucket__;
1749  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139

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

1752  {
1753  return index__;
1754  }
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136

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

1298  {
1299  table__->safe_iterators__.push_back(
1300  const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1301  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> safe_iterators__
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1757
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130

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

1578  {
1579  if (bucket__ != nullptr)
1580  return bucket__->key();
1581  else {
1582  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1583  }
1584  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

1725  {
1726  return ((bucket__ != from.bucket__) || (index__ != from.index__));
1727  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136

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

1737  {
1738  if (bucket__)
1739  return bucket__->elt();
1740  else {
1741  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1742  }
1743  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

1719  {
1720  return HashTableConstIteratorSafe< Key, Val >{*this} += nb;
1721  }

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

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

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

1673  {
1674  if ((nb == Size(0)) || (table__ == nullptr)) return *this;
1675 
1676  // if bucket__ != nullptr then use it, else use next_bucket
1677  if (bucket__ == nullptr) {
1678  // note that this case only happens when the iterator pointed to an
1679  // element
1680  // that has just been erased. Fortunately, in this case, the Hashtable's
1681  // erase functions update appropriately the next_bucket__ and index__
1682  // fields.
1684  next_bucket__ = nullptr;
1685  --nb;
1686  }
1687 
1688  // ok, here we can use bucket__ as a starting point: parse all the elements
1689  // of the current chained list
1690  for (; nb && bucket__ != nullptr; --nb, bucket__ = bucket__->prev) {}
1691 
1692  if (bucket__ != nullptr) return *this;
1693 
1694  // here, we shall skip all the chained list that have not sufficiently
1695  // many elements
1696  --index__;
1697 
1698  for (; index__ < table__->size__
1699  && nb >= table__->nodes__[index__].nb_elements__;
1700  nb -= table__->nodes__[index__].nb_elements__, --index__) {}
1701 
1702  // here: either index__ >= table__->size__, which means that we did not find
1703  // the element we looked for, i.e., we are at the end of the hashtable, or
1704  // nb < table__->nodes__[index__].nb_elements__, and we should parse the
1705  // chained list to get the element (which, we know for sure, exists)
1706  if (index__ >= table__->size__) {
1707  index__ = Size(0);
1708  return *this;
1709  }
1710 
1711  for (bucket__ = table__->nodes__[index__].end_list__; nb;
1712  --nb, bucket__ = bucket__->prev) {}
1713 
1714  return *this;
1715  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130

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

1484  {
1485  // here, no need to avoid self assignment: this would slow down normal
1486  // assignments and, in any case, this would not result in an iterator in
1487  // an incoherent state
1488  // check if the current hashtable is different from that of "from". In such
1489  // a case, we shall remove the iterator from its current hashtable
1490  // iterator's
1491  // list and add it to the new hashtable iterator's list
1492  if (table__ != from.table__) {
1493  // remove the iterator from its hashtable iterator's list'
1495 
1496  table__ = from.table__;
1497 
1498  // add to the new table
1499  if (table__) { insertIntoSafeList__(); }
1500  }
1501 
1502  index__ = from.index__;
1503  bucket__ = from.bucket__;
1504  next_bucket__ = from.next_bucket__;
1505 
1506  return *this;
1507  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
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:2130
void insertIntoSafeList__() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

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

1512  {
1513  // here, no need to avoid self assignment: this would slow down normal
1514  // assignments and, in any case, this would not result in an iterator in
1515  // an incoherent state
1516  // check if the current hashtable is different from that of "from". In such
1517  // a case, we shall remove the iterator from its current hashtable
1518  // iterator's
1519  // list and add it to the new hashtable iterator's list
1520  if (table__ != from.table__) {
1521  // remove the iterator from its hashtable iterator's list'
1523 
1524  table__ = from.table__;
1525 
1526  // add to the new table
1527  if (table__) { insertIntoSafeList__(); }
1528  }
1529 
1530  index__ = from.index__;
1531  bucket__ = from.bucket__;
1532  next_bucket__ = nullptr;
1533 
1534  return *this;
1535  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
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:2130
void insertIntoSafeList__() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

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

1540  {
1541  // here, no need to avoid self assignment: this would slow down normal
1542  // assignments and, in any case, this would not result in an iterator in
1543  // an incoherent state
1544  // check if the current hashtable is different from that of "from". In such
1545  // a case, we shall remove the iterator from its current hashtable
1546  // iterator's
1547  // list and add it to the new hashtable iterator's list
1548  if (table__ != from.table__) {
1549  // remove the iterator from its hashtable iterator's list'
1551 
1552  if (from.table__ != nullptr) {
1553  // substitute from by this in the list of safe iterators
1554  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect
1555  = from.table__->safe_iterators__;
1556 
1557  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1558  if (*ptr == &from) {
1559  *ptr = this;
1560  break;
1561  }
1562  }
1563  }
1564 
1565  table__ = from.table__;
1566  from.table__ = nullptr;
1567  }
1568 
1569  index__ = from.index__;
1570  bucket__ = from.bucket__;
1571  next_bucket__ = from.next_bucket__;
1572 
1573  return *this;
1574  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
HashTableBucket< Key, Val > * next_bucket__
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2149
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136
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:2130

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

1731  {
1732  return ((bucket__ == from.bucket__) && (index__ == from.index__));
1733  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
Size index__
the index of the chained list pointed to by the iterator in the array nodes__ of the hash table...
Definition: hashTable.h:2136

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

1305  {
1306  if (table__ == nullptr) return;
1307 
1308  // find where the iterator is
1309  std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect
1311 
1312  auto len = iter_vect.size();
1313  for (Size i = Size(0); i < len; ++i) {
1314  if (iter_vect[i] == this) {
1315  iter_vect.erase(iter_vect.begin() + i);
1316  break;
1317  }
1318  }
1319  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> safe_iterators__
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1757
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2130

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

1588  {
1589  if (bucket__ != nullptr)
1590  return bucket__->val();
1591  else {
1592  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1593  }
1594  }
HashTableBucket< Key, Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2139
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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 2127 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 2139 of file hashTable.h.

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

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

◆ table__

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

The hash table the iterator is pointing to.

Definition at line 2130 of file hashTable.h.


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