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

Unsafe Iterators for hashtables. More...

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

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

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

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

Protected Member Functions

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

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 by the iterator in the array of nodes of the hash table.
HashTable< Key, Val >::Bucket * _bucket_ {nullptr}
 The bucket in the chained list pointed to by the iterator.

Detailed Description

template<typename Key, typename Val>
class 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;
HashTable<int,string>::value_type& elt = *iter;
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 2428 of file hashTable.h.

Member Typedef Documentation

◆ const_pointer

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

types for STL compliance

Definition at line 2439 of file hashTable.h.

◆ const_reference

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

types for STL compliance

Definition at line 2437 of file hashTable.h.

◆ difference_type

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

types for STL compliance

Definition at line 2440 of file hashTable.h.

◆ iterator_category

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

types for STL compliance

Definition at line 2432 of file hashTable.h.

◆ key_type

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

types for STL compliance

Definition at line 2433 of file hashTable.h.

◆ mapped_type

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

types for STL compliance

Definition at line 2434 of file hashTable.h.

◆ pointer

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

types for STL compliance

Definition at line 2438 of file hashTable.h.

◆ reference

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

types for STL compliance

Definition at line 2436 of file hashTable.h.

◆ value_type

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

types for STL compliance

Definition at line 2435 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableIterator() [1/5]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 1843 of file hashTable_tpl.h.

1843 :
1846 }
Unsafe Iterators for hashtables.
Definition hashTable.h:2428
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

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

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

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

◆ HashTableIterator() [2/5]

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

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

Parameters
tabThe gum::HashTable to iterate over.

Definition at line 1849 of file hashTable_tpl.h.

References HashTableConstIterator< Key, Val >::HashTableConstIterator(), HashTableIterator(), and HashTableConstIterator< Key, Val >::HashTable< Key, Val >.

Here is the call graph for this function:

◆ HashTableIterator() [3/5]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val >::HashTableIterator ( 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 1855 of file hashTable_tpl.h.

References HashTableConstIterator< Key, Val >::HashTableConstIterator(), HashTableIterator(), and HashTableConstIterator< Key, Val >::HashTable< Key, Val >.

Here is the call graph for this function:

◆ HashTableIterator() [4/5]

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

Copy constructor.

Parameters
fromThe gum::HashTableIterator to copy.

Definition at line 1862 of file hashTable_tpl.h.

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

Here is the call graph for this function:

◆ HashTableIterator() [5/5]

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

Move constructor.

Parameters
fromThe gum::HashTableIterator to move.

Definition at line 1869 of file hashTable_tpl.h.

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

Here is the call graph for this function:

◆ ~HashTableIterator()

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

Class destructor.

Definition at line 1876 of file hashTable_tpl.h.

1876 {
1878 }

References HashTableIterator().

Here is the call graph for this function:

Member Function Documentation

◆ _getBucket_()

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

Returns the current iterator's bucket.

Returns
Returns the current iterator's bucket.

Definition at line 1829 of file hashTable_tpl.h.

1829 {
1830 return _bucket_;
1831 }
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition hashTable.h:2359

References _bucket_.

◆ _getIndex_()

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

1834 {
1835 return _index_;
1836 }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table.
Definition hashTable.h:2356

References _index_.

◆ clear()

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

1719 {
1720 _table_ = nullptr;
1721 _bucket_ = nullptr;
1722 _index_ = 0;
1723 }
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition hashTable.h:2350

References _table_.

◆ key()

template<typename Key, typename Val>
INLINE const HashTableConstIterator< Key, Val >::key_type & 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 1706 of file hashTable_tpl.h.

1706 {
1707 if (_bucket_) return _bucket_->pair.first;
1708 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1709 }
Unsafe Const Iterators for hashtables.
Definition hashTable.h:2146
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

◆ operator!=() [1/2]

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

1810 {
1811 return (_bucket_ != from._bucket_);
1812 }

References HashTableConstIterator(), and _bucket_.

Referenced by HashTableIterator< Key, Val >::operator!=().

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

◆ operator!=() [2/2]

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

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

References HashTableIterator(), and HashTableConstIterator< Key, Val >::operator!=().

Here is the call graph for this function:

◆ operator*() [1/2]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val >::value_type & 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 1935 of file hashTable_tpl.h.

1935 {
1937 }
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:2435

References HashTableConstIterator< Key, Val >::operator*().

Here is the call graph for this function:

◆ operator*() [2/2]

template<typename Key, typename Val>
INLINE const HashTableIterator< Key, Val >::value_type & 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 1941 of file hashTable_tpl.h.

1941 {
1943 }

References HashTableConstIterator< Key, Val >::operator*().

Here is the call graph for this function:

◆ operator+()

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

1915 {
1917 iter += nb;
1918 return iter;
1919 }

References HashTableIterator().

Here is the call graph for this function:

◆ operator++()

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val > & 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 1901 of file hashTable_tpl.h.

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

References HashTableIterator(), and HashTableConstIterator< Key, Val >::operator++().

Here is the call graph for this function:

◆ operator+=()

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val > & 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 1908 of file hashTable_tpl.h.

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

References HashTableConstIterator< Key, Val >::operator+=().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val > & 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 1888 of file hashTable_tpl.h.

1888 {
1890 return *this;
1891 }
HashTableConstIterator< Key, Val > & operator=(const HashTableConstIterator< Key, Val > &from) noexcept
Copy operator.

References HashTableIterator(), and HashTableConstIterator< Key, Val >::operator=().

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val > & 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 1895 of file hashTable_tpl.h.

1895 {
1897 return *this;
1898 }

References HashTableIterator(), and HashTableConstIterator< Key, Val >::operator=().

Here is the call graph for this function:

◆ operator==() [1/2]

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

1816 {
1817 return (_bucket_ == from._bucket_);
1818 }

References HashTableConstIterator(), and _bucket_.

Referenced by HashTableIterator< Key, Val >::operator==().

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

◆ operator==() [2/2]

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

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

References HashTableIterator(), and HashTableConstIterator< Key, Val >::operator==().

Here is the call graph for this function:

◆ val() [1/2]

template<typename Key, typename Val>
INLINE const HashTableConstIterator< Key, Val >::mapped_type & 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 1713 of file hashTable_tpl.h.

1713 {
1714 if (_bucket_) return _bucket_->val();
1715 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1716 }

References _bucket_, and GUM_ERROR.

◆ val() [2/2]

template<typename Key, typename Val>
INLINE HashTableIterator< Key, Val >::mapped_type & 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 1881 of file hashTable_tpl.h.

1881 {
1882 if (this->_bucket_) return this->_bucket_->val();
1883 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object") }
1884 }

References GUM_ERROR.

Member Data Documentation

◆ _bucket_

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

◆ _index_

template<typename Key, typename Val>
Size 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 2356 of file hashTable.h.

2356{Size(0)};

Referenced by _getIndex_(), gum::HashTableConstIteratorSafe< Key, Val >::operator!=(), operator++(), gum::HashTableConstIteratorSafe< Key, Val >::operator=(), operator=(), and gum::HashTableConstIteratorSafe< Key, Val >::operator==().

◆ _table_

template<typename Key, typename Val>
const HashTable< Key, Val >* HashTableConstIterator< Key, Val >::_table_ {nullptr}
protectedinherited

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