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

Safe Const Iterators for hashtables. More...

#include <agrum/core/hashTable.h>

+ Inheritance diagram for gum::HashTableConstIteratorSafe< Key, Val >:
+ Collaboration diagram for gum::HashTableConstIteratorSafe< Key, Val >:

Public Member Functions

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

Public Types

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

Protected Attributes

const HashTable< Key, Val > * __table {nullptr}
 The hash table the iterator is pointing to. More...
 
Size __index {Size(0)}
 the index of the chained list pointed to by the iterator in the array __nodes of the hash table. More...
 
HashTableBucket< Key, Val > * __bucket {nullptr}
 The bucket in the chained list pointed to by the iterator. More...
 
HashTableBucket< Key, Val > * __next_bucket {nullptr}
 the bucket we should start from when we decide to do a ++. More...
 

Protected Member Functions

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

Friends

template<typename K , typename V , typename A >
class HashTable
 Class HashTable must be a friend because it stores iterator end and this can be properly initialized only when the hashtable has been fully allocated. More...
 

Detailed Description

template<typename Key, typename Val>
class gum::HashTableConstIteratorSafe< Key, Val >

Safe Const Iterators for hashtables.

HashTableConstIteratorSafe provides a safe way to parse HashTables. They are safe because they are kept informed by the hashtable they belong to of the elements deleted by the user. Hence, even if the user removes an element pointed to by a HashTableConstIteratorSafe, using the latter to access this element will never crash the application. Instead it will properly throw a UndefinedIteratorValue exception.

Developers may consider using HashTable<x,y>::const_iterator_safe instead of HashTableConstIteratorSafe<x,y>.

Usage example:
// creation of a hash table with 10 elements
HashTable<int,string> table;
for (int i = 0; i< 10; ++i)
table.insert (i,"xxx" + string (i,'x'));
// parse the hash table
for (HashTable<int,string>::const_iterator_safe iter = table.cbeginSafe ();
iter != table.cendSafe (); ++iter) {
// display the values
cerr << "at " << iter.key () << " value = " << iter.val () << endl;
const std::pair<const int, string>& xelt = *iter;
}
// check whether two iterators point toward the same element
HashTable<int,string>::const_iterator_safe iter1 = table1.cbeginSafe ();
HashTable<int,string>::const_iterator_safe iter2 = table1.cendSafe ();
if (iter1 != iter) {
cerr << "iter1 and iter2 point toward different elements";
}
// make iter1 point toward nothing
iter1.clear ();

Definition at line 1915 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 1926 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 1924 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 1927 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 1919 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 1920 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 1921 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 1925 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 1923 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 1922 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 1308 of file hashTable_tpl.h.

1308  {
1309  // for debugging purposes
1310  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1311  }
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 1345 of file hashTable_tpl.h.

1346  :
1347  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1348  Size i;
1349 
1350  // check if we are looking for a begin() and we know for sure its index
1351  if ((ind_elt == Size(0))
1352  && (__table->__begin_index != std::numeric_limits< Size >::max())) {
1354  __bucket = __table->__nodes[__index].__end_list;
1355  } else {
1356  // check if it is faster to find the ind_eltth element from the start or
1357  // from the end of the hashtable
1358  if (ind_elt < (__table->__nb_elements >> 1)) {
1359  // find the element we shall point to from the start of the hashtable
1360  for (i = __table->__size - 1;; --i) { // no test on i since
1361  // ind_elt < _table->__nb_elements
1362  if (__table->__nodes[i].__nb_elements) {
1363  if (ind_elt >= __table->__nodes[i].__nb_elements)
1364  ind_elt -= __table->__nodes[i].__nb_elements;
1365  else {
1366  for (__bucket = __table->__nodes[i].__end_list; ind_elt;
1367  --ind_elt, __bucket = __bucket->prev) {}
1368 
1369  __index = i;
1370  break;
1371  }
1372  }
1373  }
1374  } else {
1375  // ind_elt = the index of the element we should point to
1376  // check if the index passed as parameter is valid
1377  if (ind_elt >= __table->__nb_elements) {
1378  GUM_ERROR(UndefinedIteratorValue,
1379  "Not enough elements in the hashtable");
1380  }
1381 
1382  // find the element we shall point to from the end of the hashtable
1383  for (i = 0, ind_elt = __table->__nb_elements - ind_elt - 1;; ++i) {
1384  if (__table->__nodes[i].__nb_elements) {
1385  if (ind_elt >= __table->__nodes[i].__nb_elements)
1386  ind_elt -= __table->__nodes[i].__nb_elements;
1387  else {
1388  for (__bucket = __table->__nodes[i].__deb_list; ind_elt;
1389  --ind_elt, __bucket = __bucket->next) {}
1390 
1391  __index = i;
1392  break;
1393  }
1394  }
1395  }
1396  }
1397  }
1398 
1399  // for debugging purposes
1400  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1401 
1402  // make the hashtable keep track of this iterator
1404  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

1408  :
1409  __table{from.__table},
1410  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1411  from.__next_bucket} {
1412  // make the hashtable keep track of this iterator
1413  if (__table != nullptr) { __insertIntoSafeList(); }
1414 
1415  // for debugging purposes
1416  GUM_CONS_CPY(HashTableConstIteratorSafe);
1417  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1421  :
1422  __table{from.__table},
1423  __index{from.__index}, __bucket{from.__bucket} {
1424  // make the hashtable keep track of this iterator
1425  if (__table != nullptr) { __insertIntoSafeList(); }
1426 
1427  // for debugging purposes
1428  GUM_CONS_CPY(HashTableConstIteratorSafe);
1429  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.

◆ HashTableConstIteratorSafe() [6/7]

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

Move constructor.

Parameters
fromThe gum::HashTableConstIteratorSafe to move.

Definition at line 1432 of file hashTable_tpl.h.

1433  :
1434  __table{from.__table},
1435  __index{from.__index}, __bucket{from.__bucket}, __next_bucket{
1436  from.__next_bucket} {
1437  GUM_CONS_MOV(HashTableConstIteratorSafe);
1438 
1439  // find "from" in the hashtable's list of safe iterators and substitute
1440  // it by this
1441  if (__table != nullptr) {
1442  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1444 
1445  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1446  if (*ptr == &from) {
1447  *ptr = this;
1448  from.__table = nullptr;
1449  break;
1450  }
1451  }
1452  }
1453  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

◆ ~HashTableConstIteratorSafe()

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

Destructor.

Definition at line 1458 of file hashTable_tpl.h.

1458  {
1459  // for debugging purposes
1460  GUM_DESTRUCTOR(HashTableConstIteratorSafe);
1461 
1462  // remove the iterator from the table's iterator list
1464  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.

◆ HashTableConstIteratorSafe() [7/7]

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

Definition at line 1315 of file hashTable_tpl.h.

1316  :
1317  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1318  // for debugging purposes
1319  GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1320 
1321  // make the hashtable keep track of this iterator
1323 
1324  if (__table->__nb_elements) {
1325  if (__table->__begin_index != std::numeric_limits< Size >::max()) {
1327  __bucket = __table->__nodes[__index].__end_list;
1328  } else {
1329  // find the element we shall point to from the start of the hashtable
1330  for (Size i = __table->__size - Size(1);; --i) { // no test on i since
1331  // __nb_elements != 0
1332  if (__table->__nodes[i].__nb_elements) {
1333  __index = i;
1334  __bucket = __table->__nodes[__index].__end_list;
1336  break;
1337  }
1338  }
1339  }
1340  }
1341  }
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1721
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1746
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

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

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

1732  {
1733  return __bucket;
1734  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
+ 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 1737 of file hashTable_tpl.h.

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

1737  {
1738  return __index;
1739  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
+ 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 1284 of file hashTable_tpl.h.

1284  {
1285  __table->__safe_iterators.push_back(
1286  const_cast< HashTableConstIteratorSafe< Key, Val >* >(this));
1287  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123

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

1291  {
1292  if (__table == nullptr) return;
1293 
1294  // find where the iterator is
1295  std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect =
1297 
1298  auto len = iter_vect.size();
1299  for (Size i = Size(0); i < len; ++i) {
1300  if (iter_vect[i] == this) {
1301  iter_vect.erase(iter_vect.begin() + i);
1302  break;
1303  }
1304  }
1305  }
std::vector< HashTableConstIteratorSafe< Key, Val > *> __safe_iterators
The list of safe iterators pointing to the hash table.
Definition: hashTable.h:1750
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

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

1580  {
1581  // remove the iterator from the table's iterator list
1583 
1584  // set its table as well as the element it points to to 0
1585  __table = nullptr;
1586  __bucket = nullptr;
1587  __next_bucket = nullptr;
1588  __index = Size(0);
1589  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

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

1561  {
1562  if (__bucket != nullptr)
1563  return __bucket->key();
1564  else {
1565  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1566  }
1567  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ operator!=()

template<typename Key, typename Val>
INLINE bool gum::HashTableConstIteratorSafe< Key, Val >::operator!= ( const HashTableConstIteratorSafe< Key, Val > &  from) const
noexcept

Checks whether two iterators are not equal.

Parameters
fromfrom The iterator to test for inequality.
Returns
Returns true if from and this iterator are inequal.

Definition at line 1708 of file hashTable_tpl.h.

1709  {
1710  return ((__bucket != from.__bucket) || (__index != from.__index));
1711  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132

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

1722  {
1723  if (__bucket)
1724  return __bucket->elt();
1725  else {
1726  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1727  }
1728  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

1702  {
1703  return HashTableConstIteratorSafe< Key, Val >{*this} += nb;
1704  }

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

1595  {
1596  // if __bucket != nullptr then use it, else use next_bucket
1597  if (__bucket == nullptr) {
1598  // note that this case only happens when the iterator pointed to an
1599  // element
1600  // that has just been erased. Fortunately, in this case, the Hashtable's
1601  // erase functions update appropriately the __next_bucket and __index
1602  // fields.
1604  __next_bucket = nullptr;
1605  } else {
1606  // ok, here we can use __bucket as a starting point
1607 
1608  // if we are not pointing on the first element of the chained list, just
1609  // point to the preceding bucket in this list
1610  if (__bucket->prev) {
1611  __bucket = __bucket->prev;
1612  // here, no need to update __next_bucket, which is compulsorily
1613  // equal to nullptr, nor __index which has not changed.
1614  } else {
1615  // ok, here we are on the beginning of a chained list,
1616  // so 2 cases can obtain:
1617  // 1/ index = 0 : then we have reached the end of the hashtable
1618  // 2/ index != 0 => we must search for a new slot containing elements
1619 
1620  // case 1:
1621  if (__index == Size(0)) {
1622  __bucket = nullptr;
1623  // we are thus at the end() of the hashTable
1624  }
1625  // case 2:
1626  else {
1627  // arrived here, we need to parse the hash table until we find a new
1628  // bucket because we are pointing on a chained list with no more
1629  // element
1630  // to the left of the current element
1631  if (__index > Size(0)) {
1632  for (Size i = __index - Size(1); i > Size(0); --i) {
1633  if (__table->__nodes[i].__nb_elements) {
1634  __index = i;
1635  __bucket = __table->__nodes[i].__end_list;
1636  return *this;
1637  }
1638  }
1639  }
1640 
1641  if (__table->__nodes[0].__nb_elements)
1642  __bucket = __table->__nodes[0].__end_list;
1643  else
1644  __bucket = nullptr;
1645 
1646  __index = 0;
1647  }
1648  }
1649  }
1650 
1651  return *this;
1652  }
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1656  {
1657  if ((nb == Size(0)) || (__table == nullptr)) return *this;
1658 
1659  // if __bucket != nullptr then use it, else use next_bucket
1660  if (__bucket == nullptr) {
1661  // note that this case only happens when the iterator pointed to an
1662  // element
1663  // that has just been erased. Fortunately, in this case, the Hashtable's
1664  // erase functions update appropriately the __next_bucket and __index
1665  // fields.
1667  __next_bucket = nullptr;
1668  --nb;
1669  }
1670 
1671  // ok, here we can use __bucket as a starting point: parse all the elements
1672  // of the current chained list
1673  for (; nb && __bucket != nullptr; --nb, __bucket = __bucket->prev) {}
1674 
1675  if (__bucket != nullptr) return *this;
1676 
1677  // here, we shall skip all the chained list that have not sufficiently
1678  // many elements
1679  --__index;
1680 
1681  for (; __index < __table->__size
1682  && nb >= __table->__nodes[__index].__nb_elements;
1683  nb -= __table->__nodes[__index].__nb_elements, --__index) {}
1684 
1685  // here: either __index >= __table->__size, which means that we did not find
1686  // the element we looked for, i.e., we are at the end of the hashtable, or
1687  // nb < __table->__nodes[__index].__nb_elements, and we should parse the
1688  // chained list to get the element (which, we know for sure, exists)
1689  if (__index >= __table->__size) {
1690  __index = Size(0);
1691  return *this;
1692  }
1693 
1694  for (__bucket = __table->__nodes[__index].__end_list; nb;
1695  --nb, __bucket = __bucket->prev) {}
1696 
1697  return *this;
1698  }
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1718
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1715
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1468  {
1469  // here, no need to avoid self assignment: this would slow down normal
1470  // assignments and, in any case, this would not result in an iterator in
1471  // an incoherent state
1472  // check if the current hashtable is different from that of "from". In such
1473  // a case, we shall remove the iterator from its current hashtable
1474  // iterator's
1475  // list and add it to the new hashtable iterator's list
1476  if (__table != from.__table) {
1477  // remove the iterator from its hashtable iterator's list'
1479 
1480  __table = from.__table;
1481 
1482  // add to the new table
1483  if (__table) { __insertIntoSafeList(); }
1484  }
1485 
1486  __index = from.__index;
1487  __bucket = from.__bucket;
1488  __next_bucket = from.__next_bucket;
1489 
1490  return *this;
1491  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1495  {
1496  // here, no need to avoid self assignment: this would slow down normal
1497  // assignments and, in any case, this would not result in an iterator in
1498  // an incoherent state
1499  // check if the current hashtable is different from that of "from". In such
1500  // a case, we shall remove the iterator from its current hashtable
1501  // iterator's
1502  // list and add it to the new hashtable iterator's list
1503  if (__table != from.__table) {
1504  // remove the iterator from its hashtable iterator's list'
1506 
1507  __table = from.__table;
1508 
1509  // add to the new table
1510  if (__table) { __insertIntoSafeList(); }
1511  }
1512 
1513  __index = from.__index;
1514  __bucket = from.__bucket;
1515  __next_bucket = nullptr;
1516 
1517  return *this;
1518  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
void __insertIntoSafeList() const
Insert the iterator into the hashtable&#39;s list of safe iterators.
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1523  {
1524  // here, no need to avoid self assignment: this would slow down normal
1525  // assignments and, in any case, this would not result in an iterator in
1526  // an incoherent state
1527  // check if the current hashtable is different from that of "from". In such
1528  // a case, we shall remove the iterator from its current hashtable
1529  // iterator's
1530  // list and add it to the new hashtable iterator's list
1531  if (__table != from.__table) {
1532  // remove the iterator from its hashtable iterator's list'
1534 
1535  if (from.__table != nullptr) {
1536  // substitute from by this in the list of safe iterators
1537  std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
1538  from.__table->__safe_iterators;
1539 
1540  for (auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1541  if (*ptr == &from) {
1542  *ptr = this;
1543  break;
1544  }
1545  }
1546  }
1547 
1548  __table = from.__table;
1549  from.__table = nullptr;
1550  }
1551 
1552  __index = from.__index;
1553  __bucket = from.__bucket;
1554  __next_bucket = from.__next_bucket;
1555 
1556  return *this;
1557  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
void __removeFromSafeList() const
Removes the iterator from its hashtable&#39; safe iterators list.
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2123
HashTableBucket< Key, Val > * __next_bucket
the bucket we should start from when we decide to do a ++.
Definition: hashTable.h:2142

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

1716  {
1717  return ((__bucket == from.__bucket) && (__index == from.__index));
1718  }
Size __index
the index of the chained list pointed to by the iterator in the array __nodes of the hash table...
Definition: hashTable.h:2129
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132

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

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

1571  {
1572  if (__bucket != nullptr)
1573  return __bucket->val();
1574  else {
1575  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
1576  }
1577  }
HashTableBucket< Key, Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2132
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ HashTable

template<typename Key, typename Val>
template<typename K , typename V , typename A >
friend class HashTable
friend

Class HashTable must be a friend because it stores iterator end and this can be properly initialized only when the hashtable has been fully allocated.

Thus, proper initialization can only take place within the constructor's code of the hashtable.

Definition at line 2120 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 2132 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __index

template<typename Key, typename Val>
Size gum::HashTableConstIteratorSafe< Key, Val >::__index {Size(0)}
protected

the index of the chained list pointed to by the iterator in the array __nodes of the hash table.

Definition at line 2129 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __next_bucket

template<typename Key, typename Val>
HashTableBucket< Key, Val >* gum::HashTableConstIteratorSafe< Key, Val >::__next_bucket {nullptr}
protected

the bucket we should start from when we decide to do a ++.

Usually it should be equal to nullptr. However, if the user has deleted the object pointed to by bucket, this will point to another bucket. When it is equal to nullptr, it means that the bucket reached after a ++ belongs to another slot of the hash table's '__node' vector.

Definition at line 2142 of file hashTable.h.

Referenced by gum::HashTableConstIteratorSafe< const gum::DiscreteVariable *, Idx >::operator=().

◆ __table

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

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