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

Safe Const Iterators for hashtables. More...

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

Public Types

using iterator_category = std::forward_iterator_tag
 Types for STL compliance.
using key_type = Key
 Types for STL compliance.
using mapped_type = Val
 Types for STL compliance.
using value_type = std::pair< const Key, Val >
 Types for STL compliance.
using reference = value_type&
 Types for STL compliance.
using const_reference = const value_type&
 Types for STL compliance.
using pointer = value_type*
 Types for STL compliance.
using const_pointer = const value_type*
 Types for STL compliance.
using difference_type = std::ptrdiff_t
 Types for STL compliance.

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

const HashTable< Key, Val > * _table_ {nullptr}
 The hash table the iterator is pointing to.
Size _index_ {Size(0)}
 the index of the chained list pointed to by the iterator in the array nodes of the hash table.
HashTableBucket< Key, Val > * _bucket_ {nullptr}
 The bucket in the chained list pointed to by the iterator.
HashTableBucket< Key, Val > * _next_bucket_ {nullptr}
 the bucket we should start from when we decide to do a ++.

Friends

class HashTable< Key, Val >
 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.

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
for (int i = 0; i< 10; ++i)
table.insert (i,"xxx" + string (i,'x'));
// parse the hash table
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 ();
void clear() noexcept
Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash...
The class for generic Hash Tables.
Definition hashTable.h:637
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:643
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
Definition hashTable.h:653
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.

Definition at line 1602 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 1613 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 1611 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 1614 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 1606 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 1607 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 1608 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 1612 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 1610 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 1609 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableConstIteratorSafe() [1/6]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 1035 of file hashTable_tpl.h.

1035 {
1036 // for debugging purposes
1038 }
Safe Const Iterators for hashtables.
Definition hashTable.h:1602
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), ~HashTableConstIteratorSafe(), operator!=(), operator+(), operator=(), operator=(), operator=(), and operator==().

Here is the caller graph for this function:

◆ HashTableConstIteratorSafe() [2/6]

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

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

Parameters
tabA gum::HashTable to iterate over.

Definition at line 1041 of file hashTable_tpl.h.

1042 :
1043 _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1044 // for debugging purposes
1046
1047 // make the hashtable keep track of this iterator
1049
1050 if (_table_->_nb_elements_) {
1051 if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1052 _index_ = _table_->_begin_index_;
1053 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1054 } else {
1055 // find the element we shall point to from the start of the hashtable
1056 for (Size i = _table_->_size_ - Size(1);; --i) { // no test on i since
1057 // _nb_elements_ != 0
1058 if (_table_->_nodes_[i]._nb_elements_) {
1059 _index_ = i;
1060 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1061 _table_->_begin_index_ = _index_;
1062 break;
1063 }
1064 }
1065 }
1066 }
1067 }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table.
Definition hashTable.h:1813
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition hashTable.h:1807
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition hashTable.h:1816
friend class HashTable< Key, Val >
Class HashTable must be a friend because it stores iterator end and this can be properly initialized ...
Definition hashTable.h:1804
void _insertIntoSafeList_() const
Insert the iterator into the hashtable's list of safe iterators.

References HashTableConstIteratorSafe(), _insertIntoSafeList_(), _table_, and HashTable< Key, Val >.

Here is the call graph for this function:

◆ HashTableConstIteratorSafe() [3/6]

template<typename Key, typename Val>
gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTable< Key, Val > & 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.

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

1072 : _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1073 Size i;
1074
1075 // check if we are looking for a begin() and we know for sure its index
1076 if ((ind_elt == Size(0)) && (_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1077 _index_ = _table_->_begin_index_;
1078 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1079 } else {
1080 // check if it is faster to find the ind_eltth element from the start or
1081 // from the end of the hashtable
1082 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1083 // find the element we shall point to from the start of the hashtable
1084 for (i = _table_->_size_ - 1;; --i) { // no test on i since
1085 // ind_elt < table_-> _nb_elements_
1086 if (_table_->_nodes_[i]._nb_elements_) {
1087 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1088 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1089 else {
1090 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1091 --ind_elt, _bucket_ = _bucket_->prev) {}
1092
1093 _index_ = i;
1094 break;
1095 }
1096 }
1097 }
1098 } else {
1099 // ind_elt = the index of the element we should point to
1100 // check if the index passed as parameter is valid
1101 if (ind_elt >= _table_->_nb_elements_) {
1102 GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the hashtable")
1103 }
1104
1105 // find the element we shall point to from the end of the hashtable
1106 for (i = 0, ind_elt = _table_->_nb_elements_ - ind_elt - 1;; ++i) {
1107 if (_table_->_nodes_[i]._nb_elements_) {
1108 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1109 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1110 else {
1111 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1112 --ind_elt, _bucket_ = _bucket_->next) {}
1113
1114 _index_ = i;
1115 break;
1116 }
1117 }
1118 }
1119 }
1120 }
1121
1122 // for debugging purposes
1124
1125 // make the hashtable keep track of this iterator
1127 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _table_, and HashTable< Key, Val >.

Here is the call graph for this function:

◆ HashTableConstIteratorSafe() [4/6]

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

1131 :
1134 // make the hashtable keep track of this iterator
1135 if (_table_ != nullptr) { _insertIntoSafeList_(); }
1136
1137 // for debugging purposes
1139 }
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition hashTable.h:1826

References HashTableConstIteratorSafe(), _bucket_, _index_, _insertIntoSafeList_(), _next_bucket_, and _table_.

Here is the call graph for this function:

◆ HashTableConstIteratorSafe() [5/6]

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

1143 :
1145 // make the hashtable keep track of this iterator
1146 if (_table_ != nullptr) { _insertIntoSafeList_(); }
1147
1148 // for debugging purposes
1150 }

References HashTableConstIteratorSafe(), _bucket_, _index_, _insertIntoSafeList_(), and _table_.

Here is the call graph for this function:

◆ HashTableConstIteratorSafe() [6/6]

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

1154 :
1158
1159 // find "from" in the hashtable's list of safe iterators and substitute
1160 // it by this
1161 if (_table_ != nullptr) {
1163
1164 for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1165 if (*ptr == &from) {
1166 *ptr = this;
1167 from._table_ = nullptr;
1168 break;
1169 }
1170 }
1171 }
1172 }

References HashTableConstIteratorSafe(), _bucket_, _index_, _next_bucket_, and _table_.

Here is the call graph for this function:

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1175 of file hashTable_tpl.h.

1175 {
1176 // for debugging purposes
1178
1179 // remove the iterator from the table's iterator list
1181 }
void _removeFromSafeList_() const
Removes the iterator from its hashtable' safe iterators list.

References HashTableConstIteratorSafe(), and _removeFromSafeList_().

Here is the call graph for this function:

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

1433 {
1434 return _bucket_;
1435 }

References _bucket_.

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

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

1438 {
1439 return _index_;
1440 }

References _index_.

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

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

1013 {
1014 _table_->_safe_iterators_.push_back(
1015 const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1016 }

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), and operator=().

Here is the caller graph for this function:

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

1019 {
1020 if (_table_ == nullptr) return;
1021
1022 // find where the iterator is
1024
1025 auto len = iter_vect.size();
1026 for (Size i = Size(0); i < len; ++i) {
1027 if (iter_vect[i] == this) {
1028 iter_vect.erase(iter_vect.begin() + i);
1029 break;
1030 }
1031 }
1032 }

References _table_.

Referenced by ~HashTableConstIteratorSafe(), operator=(), and operator=().

Here is the caller graph for this function:

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

1290 {
1291 // remove the iterator from the table's iterator list
1293
1294 // set its table as well as the element it points to to 0
1295 _table_ = nullptr;
1296 _bucket_ = nullptr;
1297 _next_bucket_ = nullptr;
1298 _index_ = Size(0);
1299 }

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

1277 {
1278 if (_bucket_ != nullptr) return _bucket_->key();
1279 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1280 }

References _bucket_.

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

1414 {
1415 return ((_bucket_ != from._bucket_) || (_index_ != from._index_));
1416 }

References HashTableConstIteratorSafe(), _bucket_, HashTableConstIterator< Key, Val >::_bucket_, _index_, and HashTableConstIterator< Key, Val >::_index_.

Here is the call graph for this function:

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

1426 {
1427 if (_bucket_) return _bucket_->elt();
1428 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1429 }

References _bucket_, and GUM_ERROR.

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

1408 {
1410 }

References HashTableConstIteratorSafe().

Here is the call graph for this function:

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

1305 {
1306 // if _bucket_ != nullptr then use it, else use next_bucket
1307 if (_bucket_ == nullptr) {
1308 // note that this case only happens when the iterator pointed to an
1309 // element
1310 // that has just been erased. Fortunately, in this case, the Hashtable's
1311 // erase functions update appropriately the _next_bucket_ and _index_
1312 // fields.
1314 _next_bucket_ = nullptr;
1315 } else {
1316 // ok, here we can use _bucket_ as a starting point
1317
1318 // if we are not pointing on the first element of the chained list, just
1319 // point to the preceding bucket in this list
1320 if (_bucket_->prev) {
1321 _bucket_ = _bucket_->prev;
1322 // here, no need to update _next_bucket_, which is compulsorily
1323 // equal to nullptr, nor _index_ which has not changed.
1324 } else {
1325 // ok, here we are on the beginning of a chained list,
1326 // so 2 cases can obtain:
1327 // 1/ index = 0 : then we have reached the end of the hashtable
1328 // 2/ index != 0 => we must search for a new slot containing elements
1329
1330 // case 1:
1331 if (_index_ == Size(0)) {
1332 _bucket_ = nullptr;
1333 // we are thus at the end() of the hashTable
1334 }
1335 // case 2:
1336 else {
1337 // arrived here, we need to parse the hash table until we find a new
1338 // bucket because we are pointing on a chained list with no more
1339 // element
1340 // to the left of the current element
1341 if (_index_ > Size(0)) {
1342 for (Size i = _index_ - Size(1); i > Size(0); --i) {
1343 if (_table_->_nodes_[i]._nb_elements_) {
1344 _index_ = i;
1345 _bucket_ = _table_->_nodes_[i]._end_list_;
1346 return *this;
1347 }
1348 }
1349 }
1350
1351 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1352 else _bucket_ = nullptr;
1353
1354 _index_ = 0;
1355 }
1356 }
1357 }
1358
1359 return *this;
1360 }

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

1364 {
1365 if ((nb == Size(0)) || (_table_ == nullptr)) return *this;
1366
1367 // if _bucket_ != nullptr then use it, else use next_bucket
1368 if (_bucket_ == nullptr) {
1369 // note that this case only happens when the iterator pointed to an
1370 // element
1371 // that has just been erased. Fortunately, in this case, the Hashtable's
1372 // erase functions update appropriately the _next_bucket_ and _index_
1373 // fields.
1375 _next_bucket_ = nullptr;
1376 --nb;
1377 }
1378
1379 // ok, here we can use _bucket_ as a starting point: parse all the elements
1380 // of the current chained list
1381 for (; nb && _bucket_ != nullptr; --nb, _bucket_ = _bucket_->prev) {}
1382
1383 if (_bucket_ != nullptr) return *this;
1384
1385 // here, we shall skip all the chained list that have not sufficiently
1386 // many elements
1387 --_index_;
1388
1389 for (; _index_ < _table_->_size_ && nb >= _table_->_nodes_[_index_]._nb_elements_;
1390 nb -= _table_->_nodes_[_index_]._nb_elements_, --_index_) {}
1391
1392 // here: either _index_ >= _table_-> _size_, which means that we did not find
1393 // the element we looked for, i.e., we are at the end of the hashtable, or
1394 // nb < _table_-> _nodes_[ _index_]. _nb_elements_, and we should parse the
1395 // chained list to get the element (which, we know for sure, exists)
1396 if (_index_ >= _table_->_size_) {
1397 _index_ = Size(0);
1398 return *this;
1399 }
1400
1401 for (_bucket_ = _table_->_nodes_[_index_]._end_list_; nb; --nb, _bucket_ = _bucket_->prev) {}
1402
1403 return *this;
1404 }

References _bucket_, _index_, _next_bucket_, and _table_.

Referenced by gum::HashTable< const gum::DiscreteVariable *, Idx >::_create_().

Here is the caller graph for this function:

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

1212 {
1213 // here, no need to avoid self assignment: this would slow down normal
1214 // assignments and, in any case, this would not result in an iterator in
1215 // an incoherent state
1216 // check if the current hashtable is different from that of "from". In such
1217 // a case, we shall remove the iterator from its current hashtable
1218 // iterator's
1219 // list and add it to the new hashtable iterator's list
1220 if (_table_ != from._table_) {
1221 // remove the iterator from its hashtable iterator's list'
1223
1225
1226 // add to the new table
1227 if (_table_) { _insertIntoSafeList_(); }
1228 }
1229
1232 _next_bucket_ = nullptr;
1233
1234 return *this;
1235 }

References HashTableConstIteratorSafe(), _table_, and HashTableConstIterator< Key, Val >::_table_.

Here is the call graph for this function:

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

1185 {
1186 // here, no need to avoid self assignment: this would slow down normal
1187 // assignments and, in any case, this would not result in an iterator in
1188 // an incoherent state
1189 // check if the current hashtable is different from that of "from". In such
1190 // a case, we shall remove the iterator from its current hashtable
1191 // iterator's
1192 // list and add it to the new hashtable iterator's list
1193 if (_table_ != from._table_) {
1194 // remove the iterator from its hashtable iterator's list'
1196
1198
1199 // add to the new table
1200 if (_table_) { _insertIntoSafeList_(); }
1201 }
1202
1206
1207 return *this;
1208 }

References HashTableConstIteratorSafe(), _bucket_, _index_, _insertIntoSafeList_(), _next_bucket_, _removeFromSafeList_(), and _table_.

Referenced by gum::HashTable< const gum::DiscreteVariable *, Idx >::_erase_().

Here is the call graph for this function:
Here is the caller graph for this function:

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

1239 {
1240 // here, no need to avoid self assignment: this would slow down normal
1241 // assignments and, in any case, this would not result in an iterator in
1242 // an incoherent state
1243 // check if the current hashtable is different from that of "from". In such
1244 // a case, we shall remove the iterator from its current hashtable
1245 // iterator's
1246 // list and add it to the new hashtable iterator's list
1247 if (_table_ != from._table_) {
1248 // remove the iterator from its hashtable iterator's list'
1250
1251 if (from._table_ != nullptr) {
1252 // substitute from by this in the list of safe iterators
1254 = from._table_->_safe_iterators_;
1255
1256 for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1257 if (*ptr == &from) {
1258 *ptr = this;
1259 break;
1260 }
1261 }
1262 }
1263
1265 from._table_ = nullptr;
1266 }
1267
1271
1272 return *this;
1273 }

References HashTableConstIteratorSafe(), _bucket_, HashTableConstIterator< Key, Val >::_bucket_, _index_, HashTableConstIterator< Key, Val >::_index_, _next_bucket_, _removeFromSafeList_(), _table_, and HashTableConstIterator< Key, Val >::_table_.

Here is the call 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 equal.

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

Definition at line 1419 of file hashTable_tpl.h.

1420 {
1421 return ((_bucket_ == from._bucket_) && (_index_ == from._index_));
1422 }

References HashTableConstIteratorSafe(), _bucket_, HashTableConstIterator< Key, Val >::_bucket_, _index_, and HashTableConstIterator< Key, Val >::_index_.

Here is the call graph for this function:

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

1284 {
1285 if (_bucket_ != nullptr) return _bucket_->val();
1286 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1287 }

References _bucket_, and GUM_ERROR.

◆ HashTable< Key, Val >

template<typename Key, typename Val>
friend class HashTable< Key, Val >
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 1793 of file hashTable.h.

Referenced by HashTableConstIteratorSafe(), and HashTableConstIteratorSafe().

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

1816{nullptr};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), _getBucket_(), key(), operator!=(), operator*(), operator+=(), operator=(), operator=(), operator==(), and val().

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

1813{Size(0)};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), _getIndex_(), operator!=(), operator+=(), operator=(), operator=(), and 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 1826 of file hashTable.h.

1826{nullptr};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), operator+=(), operator=(), and 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: