aGrUM 3.1.1
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) noexcept
 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 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:640
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:646
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
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:656

Definition at line 1662 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 1673 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 1671 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 1674 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 1666 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 1667 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 1668 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 1672 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 1670 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 1669 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableConstIteratorSafe() [1/6]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 1136 of file hashTable_tpl.h.

1136 {
1137 // for debugging purposes
1139 }
Safe Const Iterators for hashtables.
Definition hashTable.h:1662
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.

References HashTableConstIteratorSafe().

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

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

◆ HashTableConstIteratorSafe() [2/6]

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

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

Parameters
tabA gum::HashTable to iterate over.

Definition at line 1142 of file hashTable_tpl.h.

1143 :
1144 _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1145 // for debugging purposes
1147
1148 // make the hashtable keep track of this iterator
1150
1151 if (_table_->_nb_elements_) {
1152 if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1153 _index_ = _table_->_begin_index_;
1154 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1155 } else {
1156 // find the element we shall point to from the start of the hashtable
1157 for (Size i = _table_->_size_ - Size(1);; --i) {
1158 // no test on i since
1159 // _nb_elements_ != 0
1160 if (_table_->_nodes_[i]._nb_elements_) {
1161 _index_ = i;
1162 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1163 _table_->_begin_index_ = _index_;
1164 break;
1165 }
1166 }
1167 }
1168 }
1169 }
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table.
Definition hashTable.h:1866
void _insertIntoSafeList_() const
Insert the iterator into the hashtable's list of safe iterators.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition hashTable.h:1860
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition hashTable.h:1869
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:1857

References HashTableConstIteratorSafe(), _bucket_, _index_, _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 1172 of file hashTable_tpl.h.

1174 : _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1175 Size i;
1176
1177 // check if we are looking for a begin() and we know for sure its index
1178 if ((ind_elt == Size(0)) && (_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1179 _index_ = _table_->_begin_index_;
1180 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1181 } else {
1182 // check if it is faster to find the ind_eltth element from the start or
1183 // from the end of the hashtable
1184 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1185 // find the element we shall point to from the start of the hashtable
1186 for (i = _table_->_size_ - 1;; --i) {
1187 // no test on i since
1188 // ind_elt < table_-> _nb_elements_
1189 if (_table_->_nodes_[i]._nb_elements_) {
1190 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1191 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1192 else {
1193 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1194 --ind_elt, _bucket_ = _bucket_->prev) {}
1195
1196 _index_ = i;
1197 break;
1198 }
1199 }
1200 }
1201 } else {
1202 // ind_elt = the index of the element we should point to
1203 // check if the index passed as parameter is valid
1204 if (ind_elt >= _table_->_nb_elements_) {
1205 GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the hashtable")
1206 }
1207
1208 // find the element we shall point to from the end of the hashtable
1209 for (i = 0, ind_elt = _table_->_nb_elements_ - ind_elt - 1;; ++i) {
1210 if (_table_->_nodes_[i]._nb_elements_) {
1211 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1212 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1213 else {
1214 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1215 --ind_elt, _bucket_ = _bucket_->next) {}
1216
1217 _index_ = i;
1218 break;
1219 }
1220 }
1221 }
1222 }
1223 }
1224
1225 // for debugging purposes
1227
1228 // make the hashtable keep track of this iterator
1230 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _table_, and HashTable< Key, Val >.

Here is the call graph for this function:

◆ HashTableConstIteratorSafe() [4/6]

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

Copy constructor.

Parameters
fromThe gum::HashTableConstIteratorSafe to copy.

Definition at line 1233 of file hashTable_tpl.h.

1234 :
1237 // make the hashtable keep track of this iterator
1238 if (_table_ != nullptr) { _insertIntoSafeList_(); }
1239
1240 // for debugging purposes
1242 }
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
Definition hashTable.h:1879

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>
gum::HashTableConstIteratorSafe< Key, Val >::HashTableConstIteratorSafe ( const HashTableConstIterator< Key, Val > & from)
explicit

Copy constructor.

Parameters
fromThe gum::HashTableConstIterator to copy.

Definition at line 1245 of file hashTable_tpl.h.

1246 :
1248 // make the hashtable keep track of this iterator
1249 if (_table_ != nullptr) { _insertIntoSafeList_(); }
1250
1251 // for debugging purposes
1253 }

◆ HashTableConstIteratorSafe() [6/6]

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

Move constructor.

Parameters
fromThe gum::HashTableConstIteratorSafe to move.

Definition at line 1256 of file hashTable_tpl.h.

1257 :
1261
1262 // find "from" in the hashtable's list of safe iterators and substitute
1263 // it by this
1264 if (_table_ != nullptr) {
1266
1267 for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1268 if (*ptr == &from) {
1269 *ptr = this;
1270 from._table_ = nullptr;
1271 break;
1272 }
1273 }
1274 }
1275 }

References HashTableConstIteratorSafe().

Here is the call graph for this function:

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1278 of file hashTable_tpl.h.

1278 {
1279 // for debugging purposes
1281
1282 // remove the iterator from the table's iterator list
1284 }
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>
HashTableBucket< Key, Val > * gum::HashTableConstIteratorSafe< Key, Val >::_getBucket_ ( ) const
nodiscardprotectednoexcept

Returns the current iterator's bucket.

Definition at line 1529 of file hashTable_tpl.h.

1529 {
1530 return _bucket_;
1531 }

References _bucket_.

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

Here is the caller graph for this function:

◆ _getIndex_()

template<typename Key, typename Val>
Size gum::HashTableConstIteratorSafe< Key, Val >::_getIndex_ ( ) const
nodiscardprotectednoexcept

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

1534 {
1535 return _index_;
1536 }

References _index_.

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

Here is the caller graph for this function:

◆ _insertIntoSafeList_()

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

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

Definition at line 1114 of file hashTable_tpl.h.

1114 {
1115 _table_->_safe_iterators_.push_back(
1116 const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1117 }

References HashTableConstIteratorSafe(), and _table_.

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

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

◆ _removeFromSafeList_()

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

Removes the iterator from its hashtable' safe iterators list.

Definition at line 1120 of file hashTable_tpl.h.

1120 {
1121 if (_table_ == nullptr) return;
1122
1123 // find where the iterator is
1125
1126 auto len = iter_vect.size();
1127 for (Size i = Size(0); i < len; ++i) {
1128 if (iter_vect[i] == this) {
1129 iter_vect.erase(iter_vect.begin() + i);
1130 break;
1131 }
1132 }
1133 }

References _table_.

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

Here is the caller graph for this function:

◆ clear()

template<typename Key, typename Val>
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 1393 of file hashTable_tpl.h.

1393 {
1394 // remove the iterator from the table's iterator list
1396
1397 // set its table as well as the element it points to to 0
1398 _table_ = nullptr;
1399 _bucket_ = nullptr;
1400 _next_bucket_ = nullptr;
1401 _index_ = Size(0);
1402 }

◆ key()

template<typename Key, typename Val>
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 1380 of file hashTable_tpl.h.

1380 {
1381 if (_bucket_ != nullptr) return _bucket_->key();
1382 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1383 }

References _bucket_, and GUM_ERROR.

◆ operator*()

template<typename Key, typename Val>
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 1522 of file hashTable_tpl.h.

1522 {
1523 if (_bucket_) return _bucket_->elt();
1524 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1525 }

References _bucket_, and GUM_ERROR.

◆ operator+()

template<typename Key, typename Val>
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 1510 of file hashTable_tpl.h.

1510 {
1512 }

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

1408 {
1409 // if _bucket_ != nullptr then use it, else use next_bucket
1410 if (_bucket_ == nullptr) {
1411 // note that this case only happens when the iterator pointed to an
1412 // element that has just been erased. Fortunately, in this case, the
1413 // Hashtable's erase functions update appropriately the _next_bucket_
1414 // and _index_ fields.
1416 _next_bucket_ = nullptr;
1417 } else {
1418 // ok, here we can use _bucket_ as a starting point
1419
1420 // if we are not pointing on the first element of the chained list, just
1421 // point to the preceding bucket in this list
1422 if (_bucket_->prev) {
1423 _bucket_ = _bucket_->prev;
1424 // here, no need to update _next_bucket_, which is compulsorily
1425 // equal to nullptr, nor _index_ which has not changed.
1426 } else {
1427 // ok, here we are on the beginning of a chained list,
1428 // so 2 cases can obtain:
1429 // 1/ index = 0 : then we have reached the end of the hashtable
1430 // 2/ index != 0 => we must search for a new slot containing elements
1431
1432 // case 1:
1433 if (_index_ == Size(0)) {
1434 _bucket_ = nullptr;
1435 // we are thus at the end() of the hashTable
1436 }
1437 // case 2:
1438 else {
1439 // arrived here, we need to parse the hash table until we find a new
1440 // bucket because we are pointing on a chained list with no more
1441 // element
1442 // to the left of the current element
1443 if (_index_ > Size(0)) {
1444 for (Size i = _index_ - Size(1); i > Size(0); --i) {
1445 if (_table_->_nodes_[i]._nb_elements_) {
1446 _index_ = i;
1447 _bucket_ = _table_->_nodes_[i]._end_list_;
1448 return *this;
1449 }
1450 }
1451 }
1452
1453 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1454 else _bucket_ = nullptr;
1455
1456 _index_ = 0;
1457 }
1458 }
1459 }
1460
1461 return *this;
1462 }

References _bucket_, and _next_bucket_.

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

1466 {
1467 if ((nb == Size(0)) || (_table_ == nullptr)) return *this;
1468
1469 // if _bucket_ != nullptr then use it, else use next_bucket
1470 if (_bucket_ == nullptr) {
1471 // note that this case only happens when the iterator pointed to an
1472 // element
1473 // that has just been erased. Fortunately, in this case, the Hashtable's
1474 // erase functions update appropriately the _next_bucket_ and _index_
1475 // fields.
1477 _next_bucket_ = nullptr;
1478 --nb;
1479 }
1480
1481 // ok, here we can use _bucket_ as a starting point: parse all the elements
1482 // of the current chained list
1483 for (; nb && _bucket_ != nullptr; --nb, _bucket_ = _bucket_->prev) {}
1484
1485 if (_bucket_ != nullptr) return *this;
1486
1487 // here, we shall skip all the chained list that have not sufficiently
1488 // many elements
1489 --_index_;
1490
1491 for (; _index_ < _table_->_size_ && nb >= _table_->_nodes_[_index_]._nb_elements_;
1492 nb -= _table_->_nodes_[_index_]._nb_elements_, --_index_) {}
1493
1494 // here: either _index_ >= _table_-> _size_, which means that we did not find
1495 // the element we looked for, i.e., we are at the end of the hashtable, or
1496 // nb < _table_-> _nodes_[ _index_]. _nb_elements_, and we should parse the
1497 // chained list to get the element (which, we know for sure, exists)
1498 if (_index_ >= _table_->_size_) {
1499 _index_ = Size(0);
1500 return *this;
1501 }
1502
1503 for (_bucket_ = _table_->_nodes_[_index_]._end_list_; nb; --nb, _bucket_ = _bucket_->prev) {}
1504
1505 return *this;
1506 }

References _bucket_, _index_, _next_bucket_, and _table_.

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

1315 {
1316 // here, no need to avoid self assignment: this would slow down normal
1317 // assignments and, in any case, this would not result in an iterator in
1318 // an incoherent state
1319 // check if the current hashtable is different from that of "from". In such
1320 // a case, we shall remove the iterator from its current hashtable
1321 // iterator's
1322 // list and add it to the new hashtable iterator's list
1323 if (_table_ != from._table_) {
1324 // remove the iterator from its hashtable iterator's list'
1326
1328
1329 // add to the new table
1330 if (_table_) { _insertIntoSafeList_(); }
1331 }
1332
1335 _next_bucket_ = nullptr;
1336
1337 return *this;
1338 }

References HashTableConstIteratorSafe().

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

1288 {
1289 // here, no need to avoid self assignment: this would slow down normal
1290 // assignments and, in any case, this would not result in an iterator in
1291 // an incoherent state
1292 // check if the current hashtable is different from that of "from". In such
1293 // a case, we shall remove the iterator from its current hashtable
1294 // iterator's
1295 // list and add it to the new hashtable iterator's list
1296 if (_table_ != from._table_) {
1297 // remove the iterator from its hashtable iterator's list'
1299
1301
1302 // add to the new table
1303 if (_table_) { _insertIntoSafeList_(); }
1304 }
1305
1309
1310 return *this;
1311 }

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

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

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

◆ operator=() [3/3]

template<typename Key, typename Val>
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 1341 of file hashTable_tpl.h.

1342 {
1343 // here, no need to avoid self assignment: this would slow down normal
1344 // assignments and, in any case, this would not result in an iterator in
1345 // an incoherent state
1346 // check if the current hashtable is different from that of "from". In such
1347 // a case, we shall remove the iterator from its current hashtable
1348 // iterator's
1349 // list and add it to the new hashtable iterator's list
1350 if (_table_ != from._table_) {
1351 // remove the iterator from its hashtable iterator's list'
1353
1354 if (from._table_ != nullptr) {
1355 // substitute from by this in the list of safe iterators
1357 = from._table_->_safe_iterators_;
1358
1359 for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1360 if (*ptr == &from) {
1361 *ptr = this;
1362 break;
1363 }
1364 }
1365 }
1366
1368 from._table_ = nullptr;
1369 }
1370
1374
1375 return *this;
1376 }

References HashTableConstIteratorSafe().

Here is the call graph for this function:

◆ operator==()

template<typename Key, typename Val>
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 1515 of file hashTable_tpl.h.

1516 {
1517 return ((_bucket_ == from._bucket_) && (_index_ == from._index_));
1518 }

References HashTableConstIteratorSafe().

Here is the call graph for this function:

◆ val()

template<typename Key, typename Val>
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 1387 of file hashTable_tpl.h.

1387 {
1388 if (_bucket_ != nullptr) return _bucket_->val();
1389 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1390 }

References _bucket_.

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

1869{nullptr};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), _getBucket_(), key(), 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 1866 of file hashTable.h.

1866{Size(0)};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), _getIndex_(), 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 1879 of file hashTable.h.

1879{nullptr};

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

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

1860{nullptr};

Referenced by HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), HashTableConstIteratorSafe(), _insertIntoSafeList_(), _removeFromSafeList_(), operator+=(), and operator=().


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