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

Unsafe Iterators for hashtablesHashTableIterator provides a fast but unsafe way to parse HashTables. More...

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

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

Public Member Functions

mapped_typeval ()
 Returns the mapped value pointed to by the iterator. More...
 
template<typename Alloc >
INLINE HashTableIterator (const HashTable< Key, Val, Alloc > &tab) noexcept
 
template<typename Alloc >
INLINE HashTableIterator (const HashTable< Key, Val, Alloc > &tab, Size ind_elt)
 
Constructors / Destructors
 HashTableIterator () noexcept
 Basic constructor: creates an iterator pointing to nothing. More...
 
template<typename Alloc >
 HashTableIterator (const HashTable< Key, Val, Alloc > &tab) noexcept
 Constructor for an iterator pointing to the first element of a hashtable. More...
 
template<typename Alloc >
 HashTableIterator (const HashTable< Key, Val, Alloc > &tab, Size ind_elt)
 Constructor for an iterator pointing to the nth element of a hashtable. More...
 
 HashTableIterator (const HashTableIterator< Key, Val > &from) noexcept
 Copy constructor. More...
 
 HashTableIterator (HashTableIterator< Key, Val > &&from) noexcept
 Move constructor. More...
 
 ~HashTableIterator () noexcept
 Class destructor. More...
 
Operators
HashTableIterator< Key, Val > & operator= (const HashTableIterator< Key, Val > &from) noexcept
 Copy operator. More...
 
HashTableIterator< Key, Val > & operator= (HashTableIterator< Key, Val > &&from) noexcept
 Move operator. More...
 
HashTableIterator< Key, Val > & operator++ () noexcept
 Makes the iterator point to the next element in the hash table. More...
 
HashTableIterator< Key, Val > & operator+= (Size i) noexcept
 Makes the iterator point to i elements further in the hashtable. More...
 
HashTableIterator< Key, Val > operator+ (Size i) const noexcept
 Returns a new iterator. More...
 
bool operator!= (const HashTableIterator< Key, Val > &from) const noexcept
 Checks whether two iterators are pointing toward different elements. More...
 
bool operator== (const HashTableIterator< Key, Val > &from) const noexcept
 Checks whether two iterators are pointing toward equal elements. More...
 
value_typeoperator* ()
 Returns the value pointed to by the iterator. More...
 
const value_typeoperator* () const
 Returns the value pointed to by the iterator. More...
 
Accessors / Modifiers
const key_typekey () const
 Returns the key corresponding to the element 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
bool operator!= (const HashTableConstIterator< Key, Val > &from) const noexcept
 Checks whether two iterators are pointing toward different elements. More...
 
bool operator== (const HashTableConstIterator< Key, Val > &from) const noexcept
 Checks whether two iterators are pointing toward equal elements. 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 by the iterator in the array of nodes of the hash table. More...
 
HashTable< Key, Val >::Bucket * __bucket {nullptr}
 The bucket in the chained list pointed to by the iterator. More...
 

Protected Member Functions

HashTable< Key, Val >::Bucket * __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...
 

Detailed Description

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

Unsafe Iterators for hashtables

HashTableIterator provides a fast but unsafe way to parse HashTables.

They should only be used when parsing hashtables in which no element is removed from the hashtable. Removing an element where the iterator points to will mess the iterator as it will most certainly point to an unallocated memory. So, this kind of iterator should only be used when parsing "(key) constant" hash tables, e.g., when we wish to display the content of a hash table or when we wish to update the mapped values of some elements of the hash table without ever modifying their keys.

Developers may consider using HashTable<x,y>::iterator instead of HashTableIterator<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>::iterator iter = table.begin ();
iter != table.end (); ++iter) {
// display the values
cerr << "at " << iter.key() << " value = " << iter.val () << endl;
std::pair<const int, string>& xelt = *iter;
}
// check whether two iterators point toward the same element
HashTable<int,string>::iterator iter1 = table1.begin();
HashTable<int,string>::iterator iter2 = table1.end();
if (iter1 != iter) {
cerr << "iter1 and iter2 point toward different elements";
}
// make iter1 point toward nothing
iter1.clear ();
Template Parameters
KeyThe gum::HashTable key.
ValThe gum::HashTable Value.

Definition at line 2750 of file hashTable.h.

Member Typedef Documentation

◆ const_pointer

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::const_pointer = const value_type*

types for STL compliance

Definition at line 2761 of file hashTable.h.

◆ const_reference

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::const_reference = const value_type&

types for STL compliance

Definition at line 2759 of file hashTable.h.

◆ difference_type

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::difference_type = std::ptrdiff_t

types for STL compliance

Definition at line 2762 of file hashTable.h.

◆ iterator_category

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::iterator_category = std::forward_iterator_tag

types for STL compliance

Definition at line 2754 of file hashTable.h.

◆ key_type

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::key_type = Key

types for STL compliance

Definition at line 2755 of file hashTable.h.

◆ mapped_type

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::mapped_type = Val

types for STL compliance

Definition at line 2756 of file hashTable.h.

◆ pointer

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::pointer = value_type*

types for STL compliance

Definition at line 2760 of file hashTable.h.

◆ reference

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::reference = value_type&

types for STL compliance

Definition at line 2758 of file hashTable.h.

◆ value_type

template<typename Key, typename Val>
using gum::HashTableIterator< Key, Val >::value_type = std::pair< const Key, Val >

types for STL compliance

Definition at line 2757 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableIterator() [1/7]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 2176 of file hashTable_tpl.h.

2176  :
2177  HashTableConstIterator< Key, Val >() {
2178  GUM_CONSTRUCTOR(HashTableIterator);
2179  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableIterator() [2/7]

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

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

Template Parameters
AllocThe gum::HashTable allocator.
Parameters
tabThe gum::HashTable to iterate over.

◆ HashTableIterator() [3/7]

template<typename Key, typename Val>
template<typename Alloc >
gum::HashTableIterator< Key, Val >::HashTableIterator ( 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.

◆ HashTableIterator() [4/7]

template<typename Key, typename Val>
INLINE gum::HashTableIterator< Key, Val >::HashTableIterator ( const HashTableIterator< Key, Val > &  from)
noexcept

Copy constructor.

Parameters
fromThe gum::HashTableIterator to copy.

Definition at line 2198 of file hashTable_tpl.h.

2199  :
2200  HashTableConstIterator< Key, Val >(from) {
2201  GUM_CONS_CPY(HashTableIterator);
2202  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableIterator() [5/7]

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

Move constructor.

Parameters
fromThe gum::HashTableIterator to move.

Definition at line 2205 of file hashTable_tpl.h.

2206  :
2207  HashTableConstIterator< Key, Val >(std::move(from)) {
2208  GUM_CONS_MOV(HashTableIterator);
2209  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ ~HashTableIterator()

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

Class destructor.

Definition at line 2212 of file hashTable_tpl.h.

2212  {
2213  GUM_DESTRUCTOR(HashTableIterator);
2214  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableIterator() [6/7]

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

Definition at line 2183 of file hashTable_tpl.h.

2184  :
2185  HashTableConstIterator< Key, Val >(tab) {
2186  GUM_CONSTRUCTOR(HashTableIterator);
2187  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableIterator() [7/7]

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

Definition at line 2191 of file hashTable_tpl.h.

2192  :
2193  HashTableConstIterator< Key, Val >(tab, ind_elt) {
2194  GUM_CONSTRUCTOR(HashTableIterator);
2195  }
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

Member Function Documentation

◆ __getBucket()

template<typename Key , typename Val >
INLINE HashTable< Key, Val >::Bucket * gum::HashTableConstIterator< Key, Val >::__getBucket ( ) const
protectednoexceptinherited

Returns the current iterator's bucket.

Returns
Returns the current iterator's bucket.

Definition at line 2162 of file hashTable_tpl.h.

2162  {
2163  return __bucket;
2164  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681

◆ __getIndex()

template<typename Key , typename Val >
INLINE Size gum::HashTableConstIterator< Key, Val >::__getIndex ( ) const
protectednoexceptinherited

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

2167  {
2168  return __index;
2169  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2678

◆ clear()

template<typename Key , typename Val >
INLINE void gum::HashTableConstIterator< Key, Val >::clear ( )
noexceptinherited

Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash table).

Definition at line 2044 of file hashTable_tpl.h.

Referenced by gum::SetIterator< Key >::clear().

2044  {
2045  __table = nullptr;
2046  __bucket = nullptr;
2047  __index = 0;
2048  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2678
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2672
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
+ Here is the caller graph for this function:

◆ key()

template<typename Key , typename Val >
INLINE const HashTableConstIterator< Key, Val >::key_type & gum::HashTableConstIterator< Key, Val >::key ( ) const
inherited

Returns the key corresponding to the element pointed to by the iterator.

Warning
Using this method on an iterator that points to an element that has been deleted will most certainly result in a segfault. If unsure, use a safe iterator instead of an unsafe one.
Returns
Returns the key corresponding to the element pointed to by the iterator.

Definition at line 2025 of file hashTable_tpl.h.

Referenced by gum::SetIterator< Key >::operator*(), and gum::SetIterator< Key >::operator->().

2025  {
2026  if (__bucket)
2027  return __bucket->pair.first;
2028  else {
2029  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2030  }
2031  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ operator!=() [1/2]

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

Checks whether two iterators are pointing toward different elements.

Parameters
fromThe gum::HashTableConstIterator to test for inequality.
Returns
Returns true if this and from are not equal.

Definition at line 2139 of file hashTable_tpl.h.

2140  {
2141  return (__bucket != from.__bucket);
2142  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681

◆ operator!=() [2/2]

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

Checks whether two iterators are pointing toward different elements.

Parameters
fromThe gum::HashTableIterator to test for inequality.
Returns
Returns true if this and from are not equal.

Definition at line 2263 of file hashTable_tpl.h.

2264  {
2266  }
bool operator!=(const HashTableConstIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward different elements.

◆ operator*() [1/2]

template<typename Key , typename Val >
INLINE HashTableIterator< Key, Val >::value_type & gum::HashTableIterator< Key, Val >::operator* ( )

Returns the value pointed to by the iterator.

Warning
using this method on an iterator that points to an element that has been deleted will most certainly result in a segfault. If unsure, use a safe iterator instead of an unsafe one.
Returns
Returns the value pointed to by the iterator.

Definition at line 2276 of file hashTable_tpl.h.

2276  {
2277  return const_cast< value_type& >(
2279  }
const value_type & operator*() const
Returns the value pointed to by the iterator.
std::pair< const Key, Val > value_type
types for STL compliance
Definition: hashTable.h:2757

◆ operator*() [2/2]

template<typename Key , typename Val >
INLINE const HashTableIterator< Key, Val >::value_type & gum::HashTableIterator< Key, Val >::operator* ( ) const

Returns the value pointed to by the iterator.

Warning
using this method on an iterator that points to an element that has been deleted will most certainly result in a segfault. If unsure, use a safe iterator instead of an unsafe one.
Returns
Returns the value pointed to by the iterator.

Definition at line 2283 of file hashTable_tpl.h.

2283  {
2285  }
const value_type & operator*() const
Returns the value pointed to by the iterator.

◆ operator+()

template<typename Key , typename Val >
INLINE HashTableIterator< Key, Val > gum::HashTableIterator< Key, Val >::operator+ ( Size  i) const
noexcept

Returns a new iterator.

Parameters
iThe number of increments.
Returns
Returns this gum::HashTableIterator.

Definition at line 2256 of file hashTable_tpl.h.

2256  {
2257  HashTableIterator< Key, Val > iter{*this};
2258  iter += nb;
2259  return iter;
2260  }

◆ operator++()

template<typename Key , typename Val >
INLINE HashTableIterator< Key, Val > & gum::HashTableIterator< Key, Val >::operator++ ( )
noexcept

Makes the iterator point to the next element in the hash table.

for (iter = begin(); iter != end(); ++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.

Warning
performing a ++ on an iterator that points to an element that has been deleted will most certainly result in a segfault.
Returns
Returns this gum::HashTableIterator.

Definition at line 2242 of file hashTable_tpl.h.

2242  {
2244  return *this;
2245  }
HashTableConstIterator< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.

◆ operator+=()

template<typename Key , typename Val >
INLINE HashTableIterator< Key, Val > & gum::HashTableIterator< Key, Val >::operator+= ( Size  i)
noexcept

Makes the iterator point to i elements further in the hashtable.

Parameters
iThe number of increments.
Returns
Returns this gum::HashTableIterator.

Definition at line 2249 of file hashTable_tpl.h.

2249  {
2251  return *this;
2252  }
HashTableConstIterator< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.

◆ operator=() [1/2]

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

Copy operator.

Parameters
fromThe gum::HashTableIterator to copy.
Returns
Returns this gum::HashTableIterator.

Definition at line 2227 of file hashTable_tpl.h.

2228  {
2230  return *this;
2231  }
HashTableConstIterator< Key, Val > & operator=(const HashTableConstIterator< Key, Val > &from) noexcept
Copy operator.

◆ operator=() [2/2]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val > & gum::HashTableIterator< Key, Val >::operator= ( HashTableIterator< Key, Val > &&  from)
noexcept

Move operator.

Parameters
fromThe gum::HashTableIterator to move.
Returns
Returns this gum::HashTableIterator.

Definition at line 2234 of file hashTable_tpl.h.

2235  {
2237  return *this;
2238  }
HashTableConstIterator< Key, Val > & operator=(const HashTableConstIterator< Key, Val > &from) noexcept
Copy operator.

◆ operator==() [1/2]

template<typename Key, typename Val>
INLINE bool gum::HashTableConstIterator< Key, Val >::operator== ( const HashTableConstIterator< Key, Val > &  from) const
noexceptinherited

Checks whether two iterators are pointing toward equal elements.

Parameters
fromThe gum::HashTableConstIterator to test for equality.
Returns
Returns true if this and from are equal.

Definition at line 2145 of file hashTable_tpl.h.

2146  {
2147  return (__bucket == from.__bucket);
2148  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681

◆ operator==() [2/2]

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

Checks whether two iterators are pointing toward equal elements.

Parameters
fromThe gum::HashTableIterator to test for equality.
Returns
Returns true if this and from are equal.

Definition at line 2269 of file hashTable_tpl.h.

2270  {
2272  }
bool operator==(const HashTableConstIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.

◆ val() [1/2]

template<typename Key , typename Val >
INLINE const HashTableConstIterator< Key, Val >::mapped_type & gum::HashTableConstIterator< Key, Val >::val ( ) const
inherited

Returns the mapped value pointed to by the iterator.

Warning
Using this method on an iterator that points to an element that has been deleted will most certainly result in a segfault. If unsure, use a safe iterator instead of an unsafe one.
Returns
Returns the mapped value pointed to by the iterator.

Definition at line 2035 of file hashTable_tpl.h.

Referenced by gum::HashTableConstIterator< Key, bool >::val().

2035  {
2036  if (__bucket)
2037  return __bucket->val();
2038  else {
2039  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2040  }
2041  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ val() [2/2]

template<typename Key , typename Val >
INLINE HashTableIterator< Key, Val >::mapped_type & gum::HashTableIterator< Key, Val >::val ( )

Returns the mapped value pointed to by the iterator.

Warning
using this method on an iterator that points to an element that has been deleted will most certainly result in a segfault. If unsure, use a safe iterator instead of an unsafe one.
Returns
Returns the mapped value pointed to by the iterator.

Definition at line 2218 of file hashTable_tpl.h.

Referenced by gum::HashTableIterator< int, int >::val().

2218  {
2219  if (this->__bucket)
2220  return this->__bucket->val();
2221  else {
2222  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2223  }
2224  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

Member Data Documentation

◆ __bucket

template<typename Key, typename Val>
HashTable< Key, Val >::Bucket* gum::HashTableConstIterator< Key, Val >::__bucket {nullptr}
protectedinherited

The bucket in the chained list pointed to by the iterator.

Definition at line 2681 of file hashTable.h.

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

◆ __index

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

The index of the chained list pointed by the iterator in the array of nodes of the hash table.

Definition at line 2678 of file hashTable.h.

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

◆ __table


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