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

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

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

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

Public Member Functions

template<typename Alloc >
INLINE HashTableConstIterator (const HashTable< Key, Val, Alloc > &tab) noexcept
 
Constructors / Destructors
 HashTableConstIterator () noexcept
 Basic constructor: creates an iterator pointing to nothing. More...
 
template<typename Alloc >
 HashTableConstIterator (const HashTable< Key, Val, Alloc > &tab) noexcept
 Constructor for an iterator pointing to the first element of a hashtable. More...
 
template<typename Alloc >
 HashTableConstIterator (const HashTable< Key, Val, Alloc > &tab, Size ind_elt)
 Constructor for an iterator pointing to the nth element of a hashtable. More...
 
 HashTableConstIterator (const HashTableConstIterator< Key, Val > &from) noexcept
 Copy constructor. More...
 
 HashTableConstIterator (HashTableConstIterator< Key, Val > &&from) noexcept
 Move constructor. More...
 
 ~HashTableConstIterator () noexcept
 Class destructor. 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
HashTableConstIterator< Key, Val > & operator= (const HashTableConstIterator< Key, Val > &from) noexcept
 Copy operator. More...
 
HashTableConstIterator< Key, Val > & operator= (HashTableConstIterator< Key, Val > &&from) noexcept
 Move operator. More...
 
HashTableConstIterator< Key, Val > & operator++ () noexcept
 Makes the iterator point to the next element in the hash table. More...
 
HashTableConstIterator< Key, Val > & operator+= (Size i) noexcept
 Makes the iterator point to i elements further in the hashtable. More...
 
HashTableConstIterator< Key, Val > operator+ (Size i) const noexcept
 Returns a new iterator pointing to i elements further in the hashtable. More...
 
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...
 
const value_typeoperator* () const
 Returns the value 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 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...
 

Friends

template<typename K , typename V , typename A >
class HashTable
 Class HashTable must be a friend because it stores iterator end and this one can be properly initialized only when the hashtable has been fully allocated. More...
 
class HashTableConstIteratorSafe< Key, Val >
 For the safe copy constructor and operator. More...
 

Detailed Description

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

Unsafe Const Iterators for hashtables

HashTableConstIterator 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>::const_iterator instead of HashTableConstIterator<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 iter = table.cbegin ();
iter != table.cend (); ++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 iter1 = table1.cbegin();
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 2439 of file hashTable.h.

Member Typedef Documentation

◆ const_pointer

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

Types for STL compliance.

Definition at line 2450 of file hashTable.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 2448 of file hashTable.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 2451 of file hashTable.h.

◆ iterator_category

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

Types for STL compliance.

Definition at line 2443 of file hashTable.h.

◆ key_type

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

Types for STL compliance.

Definition at line 2444 of file hashTable.h.

◆ mapped_type

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

Types for STL compliance.

Definition at line 2445 of file hashTable.h.

◆ pointer

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

Types for STL compliance.

Definition at line 2449 of file hashTable.h.

◆ reference

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

Types for STL compliance.

Definition at line 2447 of file hashTable.h.

◆ value_type

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

Types for STL compliance.

Definition at line 2446 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableConstIterator() [1/6]

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

Basic constructor: creates an iterator pointing to nothing.

Definition at line 1820 of file hashTable_tpl.h.

1820  {
1821  GUM_CONSTRUCTOR(HashTableConstIterator);
1822  }
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableConstIterator() [2/6]

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

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

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

◆ HashTableConstIterator() [3/6]

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

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

1855  :
1856  _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1857  Size i;
1858 
1859  // check if we are looking for a begin() and we know for sure its index
1860  if ((ind_elt == Size(0)) && (_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1862  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1863  } else {
1864  // check if it is faster to find the ind_eltth element from the start or
1865  // from the end of the hashtable
1866  if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1867  // find the element we shall point to from the start of the hashtable
1868  for (i = _table_->_size_ - 1;; --i) { // no test on i since
1869  // ind_elt < table_-> _nb_elements_
1870  if (_table_->_nodes_[i]._nb_elements_) {
1871  if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1872  ind_elt -= _table_->_nodes_[i]._nb_elements_;
1873  else {
1874  for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1875  --ind_elt, _bucket_ = _bucket_->prev) {}
1876 
1877  _index_ = i;
1878  break;
1879  }
1880  }
1881  }
1882  } else {
1883  // ind_elt = the index of the element we should point to
1884  // check if the index passed as parameter is valid
1885  if (ind_elt >= _table_->_nb_elements_) {
1886  GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the hashtable")
1887  }
1888 
1889  // find the element we shall point to from the end of the hashtable
1890  for (i = 0, ind_elt = _table_->_nb_elements_ - ind_elt - 1;; ++i) {
1891  if (_table_->_nodes_[i]._nb_elements_) {
1892  if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1893  ind_elt -= _table_->_nodes_[i]._nb_elements_;
1894  else {
1895  for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1896  --ind_elt, _bucket_ = _bucket_->next) {}
1897 
1898  _index_ = i;
1899  break;
1900  }
1901  }
1902  }
1903  }
1904  }
1905 
1906  // for debugging purposes
1907  GUM_CONSTRUCTOR(HashTableConstIterator);
1908  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableConstIterator() [4/6]

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

Copy constructor.

Parameters
fromThe gum::HashTableConstIterator to copy.

Definition at line 1911 of file hashTable_tpl.h.

1912  :
1913  _table_{from._table_},
1914  _index_{from._index_}, _bucket_{from._bucket_} {
1915  GUM_CONS_CPY(HashTableConstIterator);
1916  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableConstIterator() [5/6]

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

Move constructor.

Parameters
fromThe gum::HashTableConstIterator to move.

Definition at line 1919 of file hashTable_tpl.h.

1920  :
1921  _table_{from._table_},
1922  _index_{from._index_}, _bucket_{from._bucket_} {
1923  GUM_CONS_MOV(HashTableConstIterator);
1924  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ ~HashTableConstIterator()

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

Class destructor.

Definition at line 1927 of file hashTable_tpl.h.

1927  {
1928  // for debugging purposes
1929  GUM_DESTRUCTOR(HashTableConstIterator);
1930  }
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

◆ HashTableConstIterator() [6/6]

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

Definition at line 1826 of file hashTable_tpl.h.

1827  :
1828  _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1829  // for debugging purposes
1830  GUM_CONSTRUCTOR(HashTableConstIterator);
1831 
1832  if (_table_->_nb_elements_) {
1833  if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1835  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1836  } else {
1837  // find the element we shall point to from the start of the hashtable
1838  for (Size i = _table_->_size_ - Size(1);; --i) { // no test on i since
1839  // _nb_elements_ != 0
1840  if (_table_->_nodes_[i]._nb_elements_) {
1841  _index_ = i;
1842  _bucket_ = _table_->_nodes_[_index_]._end_list_;
1844  break;
1845  }
1846  }
1847  }
1848  }
1849  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
Size _begin_index_
Returns where the begin index should be.
Definition: hashTable.h:1731
Size _nb_elements_
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1706
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654
HashTableConstIterator() 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
protectednoexcept

Returns the current iterator's bucket.

Returns
Returns the current iterator's bucket.

Definition at line 2094 of file hashTable_tpl.h.

2094  {
2095  return _bucket_;
2096  }
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ _getIndex_()

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

2099  {
2100  return _index_;
2101  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651

◆ clear()

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

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

Definition at line 1979 of file hashTable_tpl.h.

1979  {
1980  _table_ = nullptr;
1981  _bucket_ = nullptr;
1982  _index_ = 0;
1983  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ key()

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

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

1960  {
1961  if (_bucket_)
1962  return _bucket_->pair.first;
1963  else {
1964  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
1965  }
1966  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator!=()

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

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

2072  {
2073  return (_bucket_ != from._bucket_);
2074  }
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator*()

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

2084  {
2085  if (_bucket_)
2086  return _bucket_->elt();
2087  else {
2088  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
2089  }
2090  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator+()

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

Returns a new iterator pointing to i elements further in the hashtable.

Parameters
iThe number of increments.
Returns
Returns a new iterator pointing to i elements further in the hashtable.

Definition at line 2066 of file hashTable_tpl.h.

2066  {
2067  return HashTableConstIterator< Key, Val >{*this} += nb;
2068  }

◆ operator++()

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

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

for (iter = cbegin (); iter != cend(); ++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::HashTableConstIterator.

Definition at line 1986 of file hashTable_tpl.h.

1986  {
1987  // if _bucket_ == nullptr then we are at the end of the hashtable
1988  if (_bucket_ == nullptr) return *this;
1989 
1990  // if we are not pointing on the first element of the chained list, just
1991  // point to the next bucket in this list
1992  if (_bucket_->prev) {
1993  _bucket_ = _bucket_->prev;
1994  // here, no need to update _index_ which has not changed.
1995  } else {
1996  // ok, here we are on the end of a chained list,
1997  // so 2 cases can obtain:
1998  // 1/ index = 0 : then we have reached the end of the hashtable
1999  // 2/ index != 0 => we must search for a new slot containing elements
2000 
2001  // case 1:
2002  if (_index_ == Size(0)) {
2003  _bucket_ = nullptr;
2004  // we are thus at the end() of the hashTable
2005  }
2006 
2007  // case 2:
2008  else {
2009  // arrived here, we need to parse the hash table until we find a new
2010  // bucket because we are pointing on a chained list with no more element
2011  // to the right of the current element
2012  for (Size i = _index_ - Size(1); i; --i) {
2013  if (_table_->_nodes_[i]._nb_elements_) {
2014  _index_ = i;
2015  _bucket_ = _table_->_nodes_[i]._end_list_;
2016  return *this;
2017  }
2018  }
2019 
2020  if (_table_->_nodes_[0]._nb_elements_)
2021  _bucket_ = _table_->_nodes_[0]._end_list_;
2022  else
2023  _bucket_ = nullptr;
2024 
2025  _index_ = Size(0);
2026  }
2027  }
2028 
2029  return *this;
2030  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator+=()

template<typename Key , typename Val >
HashTableConstIterator< Key, Val > & gum::HashTableConstIterator< 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::HashTableConstIterator.

Definition at line 2034 of file hashTable_tpl.h.

2034  {
2035  if ((nb == 0) || (_table_ == nullptr) || (_bucket_ == nullptr)) return *this;
2036 
2037  // ok, here we can use _bucket_ as a starting point: parse all the elements
2038  // of the current chained list
2039  for (; nb && _bucket_ != nullptr; --nb, _bucket_ = _bucket_->prev) {}
2040 
2041  if (_bucket_ != nullptr) return *this;
2042 
2043  // here, we shall skip all the chained list that have not sufficiently
2044  // many elements
2045  --_index_;
2046 
2047  for (; _index_ < _table_->_size_ && nb >= _table_->_nodes_[_index_]._nb_elements_;
2048  nb -= _table_->_nodes_[_index_]._nb_elements_, --_index_) {}
2049 
2050  // here: either _index_ >= _table_-> _size_, which means that we did not find
2051  // the element we looked for, i.e., we are at the end of the hashtable, or
2052  // nb < _table_-> _nodes_[ _index_]. _nb_elements_, and we should parse the
2053  // chained list to get the element (which, we know for sure, exists)
2054  if (_index_ >= _table_->_size_) {
2055  _index_ = 0;
2056  return *this;
2057  }
2058 
2059  for (_bucket_ = _table_->_nodes_[_index_]._end_list_; nb; --nb, _bucket_ = _bucket_->prev) {}
2060 
2061  return *this;
2062  }
Size _size_
The number of nodes in vector &#39; __nodes&#39;.
Definition: hashTable.h:1703
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
std::vector< HashTableList< Key, Val, Alloc > > _nodes_
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1700
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator=() [1/2]

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

Copy operator.

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

Definition at line 1933 of file hashTable_tpl.h.

1934  {
1935  // here, no need to avoid self assignment: this would slow down normal
1936  // assignments and, in any case, this would not result in an iterator in
1937  // an incoherent state
1938  _table_ = from._table_;
1939  _index_ = from._index_;
1940  _bucket_ = from._bucket_;
1941 
1942  return *this;
1943  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator=() [2/2]

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

Move operator.

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

Definition at line 1946 of file hashTable_tpl.h.

1947  {
1948  // here, no need to avoid self assignment: this would slow down normal
1949  // assignments and, in any case, this would not result in an iterator in
1950  // an incoherent state
1951  _table_ = from._table_;
1952  _index_ = from._index_;
1953  _bucket_ = from._bucket_;
1954 
1955  return *this;
1956  }
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2651
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition: hashTable.h:2645
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ operator==()

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

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

2078  {
2079  return (_bucket_ == from._bucket_);
2080  }
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

◆ val()

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

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

1970  {
1971  if (_bucket_)
1972  return _bucket_->val();
1973  else {
1974  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object")
1975  }
1976  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2654

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 one 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.

Template Parameters
KThe gum::HashTable keys type.
VThe gum::HashTable values type.
AThe gum::HashTable allocator.

Definition at line 2639 of file hashTable.h.

◆ HashTableConstIteratorSafe< Key, Val >

template<typename Key, typename Val>
friend class HashTableConstIteratorSafe< Key, Val >
friend

For the safe copy constructor and operator.

Definition at line 2642 of file hashTable.h.

Member Data Documentation

◆ _bucket_

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

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

Definition at line 2654 of file hashTable.h.

◆ _index_

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

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

Definition at line 2651 of file hashTable.h.

◆ _table_

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

The hash table the iterator is pointing to.

Definition at line 2645 of file hashTable.h.


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