aGrUM  0.20.3
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 1899 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 1910 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 1908 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 1911 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 1903 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 1904 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 1905 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 1909 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 1907 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 1906 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 1265 of file hashTable_tpl.h.

1265  {
1266  // for debugging purposes
1267  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1268  }
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 1302 of file hashTable_tpl.h.

1304  :
1305  _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1306  Size i;
1307 
1308  // check if we are looking for a begin() and we know for sure its index
1309  if ((ind_elt == Size(0)) && (_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1311  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1312  } else {
1313  // check if it is faster to find the ind_eltth element from the start or
1314  // from the end of the hashtable
1315  if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1316  // find the element we shall point to from the start of the hashtable
1317  for (i = _table_->_size_ - 1;; --i) { // no test on i since
1318  // ind_elt < table_-> _nb_elements_
1319  if (_table_->_nodes_[i]._nb_elements_) {
1320  if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1321  ind_elt -= _table_->_nodes_[i]._nb_elements_;
1322  else {
1323  for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1324  --ind_elt, _bucket_ = _bucket_->prev) {}
1325 
1326  _index_ = i;
1327  break;
1328  }
1329  }
1330  }
1331  } else {
1332  // ind_elt = the index of the element we should point to
1333  // check if the index passed as parameter is valid
1334  if (ind_elt >= _table_->_nb_elements_) {
1335  GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the hashtable")
1336  }
1337 
1338  // find the element we shall point to from the end of the hashtable
1339  for (i = 0, ind_elt = _table_->_nb_elements_ - ind_elt - 1;; ++i) {
1340  if (_table_->_nodes_[i]._nb_elements_) {
1341  if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1342  ind_elt -= _table_->_nodes_[i]._nb_elements_;
1343  else {
1344  for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1345  --ind_elt, _bucket_ = _bucket_->next) {}
1346 
1347  _index_ = i;
1348  break;
1349  }
1350  }
1351  }
1352  }
1353  }
1354 
1355  // for debugging purposes
1356  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1357 
1358  // make the hashtable keep track of this iterator
1360  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

1364  :
1365  _table_{from._table_},
1366  _index_{from._index_}, _bucket_{from._bucket_}, _next_bucket_{from._next_bucket_} {
1367  // make the hashtable keep track of this iterator
1368  if (_table_ != nullptr) { _insertIntoSafeList_(); }
1369 
1370  // for debugging purposes
1371  GUM_CONS_CPY(HashTableConstIteratorSafe);
1372  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
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:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1376  :
1377  _table_{from._table_},
1378  _index_{from._index_}, _bucket_{from._bucket_} {
1379  // make the hashtable keep track of this iterator
1380  if (_table_ != nullptr) { _insertIntoSafeList_(); }
1381 
1382  // for debugging purposes
1383  GUM_CONS_CPY(HashTableConstIteratorSafe);
1384  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1388  :
1389  _table_{from._table_},
1390  _index_{from._index_}, _bucket_{from._bucket_}, _next_bucket_{from._next_bucket_} {
1391  GUM_CONS_MOV(HashTableConstIteratorSafe);
1392 
1393  // find "from" in the hashtable's list of safe iterators and substitute
1394  // it by this
1395  if (_table_ != nullptr) {
1396  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect = _table_->_safe_iterators_;
1397 
1398  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1399  if (*ptr == &from) {
1400  *ptr = this;
1401  from._table_ = nullptr;
1402  break;
1403  }
1404  }
1405  }
1406  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
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:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1409 of file hashTable_tpl.h.

1409  {
1410  // for debugging purposes
1411  GUM_DESTRUCTOR(HashTableConstIteratorSafe);
1412 
1413  // remove the iterator from the table's iterator list
1415  }
void _removeFromSafeList_() const
Removes the iterator from its hashtable&#39; safe iterators list.
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.

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

1273  :
1274  _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1275  // for debugging purposes
1276  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1277 
1278  // make the hashtable keep track of this iterator
1280 
1281  if (_table_->_nb_elements_) {
1282  if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1284  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1285  } else {
1286  // find the element we shall point to from the start of the hashtable
1287  for (Size i = _table_->_size_ - Size(1);; --i) { // no test on i since
1288  // _nb_elements_ != 0
1289  if (_table_->_nodes_[i]._nb_elements_) {
1290  _index_ = i;
1291  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1293  break;
1294  }
1295  }
1296  }
1297  }
1298  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1678  {
1679  return _bucket_;
1680  }
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1683  {
1684  return _index_;
1685  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109

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

1243  {
1244  _table_->_safe_iterators_.push_back(
1245  const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1246  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103

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

1249  {
1250  if (_table_ == nullptr) return;
1251 
1252  // find where the iterator is
1253  std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect = _table_->_safe_iterators_;
1254 
1255  auto len = iter_vect.size();
1256  for (Size i = Size(0); i < len; ++i) {
1257  if (iter_vect[i] == this) {
1258  iter_vect.erase(iter_vect.begin() + i);
1259  break;
1260  }
1261  }
1262  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> _safe_iterators_
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1734
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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

1530  {
1531  // remove the iterator from the table's iterator list
1533 
1534  // set its table as well as the element it points to to 0
1535  _table_ = nullptr;
1536  _bucket_ = nullptr;
1537  _next_bucket_ = nullptr;
1538  _index_ = Size(0);
1539  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
void _removeFromSafeList_() const
Removes the iterator from its hashtable&#39; safe iterators list.
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1511  {
1512  if (_bucket_ != nullptr)
1513  return _bucket_->key();
1514  else {
1515  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
1516  }
1517  }
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

1656  {
1657  return ((_bucket_ != from._bucket_) || (_index_ != from._index_));
1658  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1668  {
1669  if (_bucket_)
1670  return _bucket_->elt();
1671  else {
1672  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
1673  }
1674  }
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

1650  {
1651  return HashTableConstIteratorSafe< Key, Val >{*this} += nb;
1652  }

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

1545  {
1546  // if _bucket_ != nullptr then use it, else use next_bucket
1547  if (_bucket_ == nullptr) {
1548  // note that this case only happens when the iterator pointed to an
1549  // element
1550  // that has just been erased. Fortunately, in this case, the Hashtable's
1551  // erase functions update appropriately the _next_bucket_ and _index_
1552  // fields.
1554  _next_bucket_ = nullptr;
1555  } else {
1556  // ok, here we can use _bucket_ as a starting point
1557 
1558  // if we are not pointing on the first element of the chained list, just
1559  // point to the preceding bucket in this list
1560  if (_bucket_->prev) {
1561  _bucket_ = _bucket_->prev;
1562  // here, no need to update _next_bucket_, which is compulsorily
1563  // equal to nullptr, nor _index_ which has not changed.
1564  } else {
1565  // ok, here we are on the beginning of a chained list,
1566  // so 2 cases can obtain:
1567  // 1/ index = 0 : then we have reached the end of the hashtable
1568  // 2/ index != 0 => we must search for a new slot containing elements
1569 
1570  // case 1:
1571  if (_index_ == Size(0)) {
1572  _bucket_ = nullptr;
1573  // we are thus at the end() of the hashTable
1574  }
1575  // case 2:
1576  else {
1577  // arrived here, we need to parse the hash table until we find a new
1578  // bucket because we are pointing on a chained list with no more
1579  // element
1580  // to the left of the current element
1581  if (_index_ > Size(0)) {
1582  for (Size i = _index_ - Size(1); i > Size(0); --i) {
1583  if (_table_->_nodes_[i]._nb_elements_) {
1584  _index_ = i;
1585  _bucket_ = _table_->_nodes_[i]._end_list_;
1586  return *this;
1587  }
1588  }
1589  }
1590 
1591  if (_table_->_nodes_[0]._nb_elements_)
1592  _bucket_ = _table_->_nodes_[0]._end_list_;
1593  else
1594  _bucket_ = nullptr;
1595 
1596  _index_ = 0;
1597  }
1598  }
1599  }
1600 
1601  return *this;
1602  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1606  {
1607  if ((nb == Size(0)) || (_table_ == nullptr)) return *this;
1608 
1609  // if _bucket_ != nullptr then use it, else use next_bucket
1610  if (_bucket_ == nullptr) {
1611  // note that this case only happens when the iterator pointed to an
1612  // element
1613  // that has just been erased. Fortunately, in this case, the Hashtable's
1614  // erase functions update appropriately the _next_bucket_ and _index_
1615  // fields.
1617  _next_bucket_ = nullptr;
1618  --nb;
1619  }
1620 
1621  // ok, here we can use _bucket_ as a starting point: parse all the elements
1622  // of the current chained list
1623  for (; nb && _bucket_ != nullptr; --nb, _bucket_ = _bucket_->prev) {}
1624 
1625  if (_bucket_ != nullptr) return *this;
1626 
1627  // here, we shall skip all the chained list that have not sufficiently
1628  // many elements
1629  --_index_;
1630 
1631  for (; _index_ < _table_->_size_ && nb >= _table_->_nodes_[_index_]._nb_elements_;
1632  nb -= _table_->_nodes_[_index_]._nb_elements_, --_index_) {}
1633 
1634  // here: either _index_ >= _table_-> _size_, which means that we did not find
1635  // the element we looked for, i.e., we are at the end of the hashtable, or
1636  // nb < _table_-> _nodes_[ _index_]. _nb_elements_, and we should parse the
1637  // chained list to get the element (which, we know for sure, exists)
1638  if (_index_ >= _table_->_size_) {
1639  _index_ = Size(0);
1640  return *this;
1641  }
1642 
1643  for (_bucket_ = _table_->_nodes_[_index_]._end_list_; nb; --nb, _bucket_ = _bucket_->prev) {}
1644 
1645  return *this;
1646  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1419  {
1420  // here, no need to avoid self assignment: this would slow down normal
1421  // assignments and, in any case, this would not result in an iterator in
1422  // an incoherent state
1423  // check if the current hashtable is different from that of "from". In such
1424  // a case, we shall remove the iterator from its current hashtable
1425  // iterator's
1426  // list and add it to the new hashtable iterator's list
1427  if (_table_ != from._table_) {
1428  // remove the iterator from its hashtable iterator's list'
1430 
1431  _table_ = from._table_;
1432 
1433  // add to the new table
1434  if (_table_) { _insertIntoSafeList_(); }
1435  }
1436 
1437  _index_ = from._index_;
1438  _bucket_ = from._bucket_;
1439  _next_bucket_ = from._next_bucket_;
1440 
1441  return *this;
1442  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
void _removeFromSafeList_() const
Removes the iterator from its hashtable&#39; safe iterators list.
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1446  {
1447  // here, no need to avoid self assignment: this would slow down normal
1448  // assignments and, in any case, this would not result in an iterator in
1449  // an incoherent state
1450  // check if the current hashtable is different from that of "from". In such
1451  // a case, we shall remove the iterator from its current hashtable
1452  // iterator's
1453  // list and add it to the new hashtable iterator's list
1454  if (_table_ != from._table_) {
1455  // remove the iterator from its hashtable iterator's list'
1457 
1458  _table_ = from._table_;
1459 
1460  // add to the new table
1461  if (_table_) { _insertIntoSafeList_(); }
1462  }
1463 
1464  _index_ = from._index_;
1465  _bucket_ = from._bucket_;
1466  _next_bucket_ = nullptr;
1467 
1468  return *this;
1469  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
void _removeFromSafeList_() const
Removes the iterator from its hashtable&#39; safe iterators list.
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
void _insertIntoSafeList_() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1473  {
1474  // here, no need to avoid self assignment: this would slow down normal
1475  // assignments and, in any case, this would not result in an iterator in
1476  // an incoherent state
1477  // check if the current hashtable is different from that of "from". In such
1478  // a case, we shall remove the iterator from its current hashtable
1479  // iterator's
1480  // list and add it to the new hashtable iterator's list
1481  if (_table_ != from._table_) {
1482  // remove the iterator from its hashtable iterator's list'
1484 
1485  if (from._table_ != nullptr) {
1486  // substitute from by this in the list of safe iterators
1487  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect
1488  = from._table_->_safe_iterators_;
1489 
1490  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1491  if (*ptr == &from) {
1492  *ptr = this;
1493  break;
1494  }
1495  }
1496  }
1497 
1498  _table_ = from._table_;
1499  from._table_ = nullptr;
1500  }
1501 
1502  _index_ = from._index_;
1503  _bucket_ = from._bucket_;
1504  _next_bucket_ = from._next_bucket_;
1505 
1506  return *this;
1507  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
void _removeFromSafeList_() const
Removes the iterator from its hashtable&#39; safe iterators list.
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2122
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2103
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1662  {
1663  return ((_bucket_ == from._bucket_) && (_index_ == from._index_));
1664  }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table...
Definition: hashTable.h:2109
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112

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

1521  {
1522  if (_bucket_ != nullptr)
1523  return _bucket_->val();
1524  else {
1525  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
1526  }
1527  }
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2112
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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 2100 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 2112 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 2109 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 2122 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 2103 of file hashTable.h.


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