aGrUM  0.20.2
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 2469 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 2480 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 2478 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 2481 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 2473 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 2474 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 2475 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 2479 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 2477 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 2476 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 1892 of file hashTable_tpl.h.

1892  {
1893  GUM_CONSTRUCTOR(HashTableConstIterator);
1894  }
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 1925 of file hashTable_tpl.h.

1927  :
1928  table__{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1929  Size i;
1930 
1931  // check if we are looking for a begin() and we know for sure its index
1932  if ((ind_elt == Size(0))
1933  && (table__->begin_index__ != std::numeric_limits< Size >::max())) {
1935  bucket__ = table__->nodes__[index__].end_list__;
1936  } else {
1937  // check if it is faster to find the ind_eltth element from the start or
1938  // from the end of the hashtable
1939  if (ind_elt < (table__->nb_elements__ >> 1)) {
1940  // find the element we shall point to from the start of the hashtable
1941  for (i = table__->size__ - 1;; --i) { // no test on i since
1942  // ind_elt < table_->nb_elements__
1943  if (table__->nodes__[i].nb_elements__) {
1944  if (ind_elt >= table__->nodes__[i].nb_elements__)
1945  ind_elt -= table__->nodes__[i].nb_elements__;
1946  else {
1947  for (bucket__ = table__->nodes__[i].end_list__; ind_elt;
1948  --ind_elt, bucket__ = bucket__->prev) {}
1949 
1950  index__ = i;
1951  break;
1952  }
1953  }
1954  }
1955  } else {
1956  // ind_elt = the index of the element we should point to
1957  // check if the index passed as parameter is valid
1958  if (ind_elt >= table__->nb_elements__) {
1959  GUM_ERROR(UndefinedIteratorValue,
1960  "Not enough elements in the hashtable");
1961  }
1962 
1963  // find the element we shall point to from the end of the hashtable
1964  for (i = 0, ind_elt = table__->nb_elements__ - ind_elt - 1;; ++i) {
1965  if (table__->nodes__[i].nb_elements__) {
1966  if (ind_elt >= table__->nodes__[i].nb_elements__)
1967  ind_elt -= table__->nodes__[i].nb_elements__;
1968  else {
1969  for (bucket__ = table__->nodes__[i].deb_list__; ind_elt;
1970  --ind_elt, bucket__ = bucket__->next) {}
1971 
1972  index__ = i;
1973  break;
1974  }
1975  }
1976  }
1977  }
1978  }
1979 
1980  // for debugging purposes
1981  GUM_CONSTRUCTOR(HashTableConstIterator);
1982  }
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
Size begin_index__
Returns where the begin index should be.
Definition: hashTable.h:1753
Size nb_elements__
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1728
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
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 1985 of file hashTable_tpl.h.

1986  :
1987  table__{from.table__},
1988  index__{from.index__}, bucket__{from.bucket__} {
1989  GUM_CONS_CPY(HashTableConstIterator);
1990  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682
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 1993 of file hashTable_tpl.h.

1994  :
1995  table__{from.table__},
1996  index__{from.index__}, bucket__{from.bucket__} {
1997  GUM_CONS_MOV(HashTableConstIterator);
1998  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682
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 2001 of file hashTable_tpl.h.

2001  {
2002  // for debugging purposes
2003  GUM_DESTRUCTOR(HashTableConstIterator);
2004  }
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 1898 of file hashTable_tpl.h.

1899  :
1900  table__{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1901  // for debugging purposes
1902  GUM_CONSTRUCTOR(HashTableConstIterator);
1903 
1904  if (table__->nb_elements__) {
1905  if (table__->begin_index__ != std::numeric_limits< Size >::max()) {
1907  bucket__ = table__->nodes__[index__].end_list__;
1908  } else {
1909  // find the element we shall point to from the start of the hashtable
1910  for (Size i = table__->size__ - Size(1);; --i) { // no test on i since
1911  // nb_elements__ != 0
1912  if (table__->nodes__[i].nb_elements__) {
1913  index__ = i;
1914  bucket__ = table__->nodes__[index__].end_list__;
1916  break;
1917  }
1918  }
1919  }
1920  }
1921  }
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
Size begin_index__
Returns where the begin index should be.
Definition: hashTable.h:1753
Size nb_elements__
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1728
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.

Member Function Documentation

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

2055  {
2056  table__ = nullptr;
2057  bucket__ = nullptr;
2058  index__ = 0;
2059  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2173  {
2174  return bucket__;
2175  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685

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

2178  {
2179  return index__;
2180  }
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2036  {
2037  if (bucket__)
2038  return bucket__->pair.first;
2039  else {
2040  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2041  }
2042  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

2151  {
2152  return (bucket__ != from.bucket__);
2153  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685

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

2163  {
2164  if (bucket__)
2165  return bucket__->elt();
2166  else {
2167  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2168  }
2169  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

2145  {
2146  return HashTableConstIterator< Key, Val >{*this} += nb;
2147  }

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

2063  {
2064  // if bucket__ == nullptr then we are at the end of the hashtable
2065  if (bucket__ == nullptr) return *this;
2066 
2067  // if we are not pointing on the first element of the chained list, just
2068  // point to the next bucket in this list
2069  if (bucket__->prev) {
2070  bucket__ = bucket__->prev;
2071  // here, no need to update index__ which has not changed.
2072  } else {
2073  // ok, here we are on the end of a chained list,
2074  // so 2 cases can obtain:
2075  // 1/ index = 0 : then we have reached the end of the hashtable
2076  // 2/ index != 0 => we must search for a new slot containing elements
2077 
2078  // case 1:
2079  if (index__ == Size(0)) {
2080  bucket__ = nullptr;
2081  // we are thus at the end() of the hashTable
2082  }
2083 
2084  // case 2:
2085  else {
2086  // arrived here, we need to parse the hash table until we find a new
2087  // bucket because we are pointing on a chained list with no more element
2088  // to the right of the current element
2089  for (Size i = index__ - Size(1); i; --i) {
2090  if (table__->nodes__[i].nb_elements__) {
2091  index__ = i;
2092  bucket__ = table__->nodes__[i].end_list__;
2093  return *this;
2094  }
2095  }
2096 
2097  if (table__->nodes__[0].nb_elements__)
2098  bucket__ = table__->nodes__[0].end_list__;
2099  else
2100  bucket__ = nullptr;
2101 
2102  index__ = Size(0);
2103  }
2104  }
2105 
2106  return *this;
2107  }
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2111  {
2112  if ((nb == 0) || (table__ == nullptr) || (bucket__ == nullptr)) return *this;
2113 
2114  // ok, here we can use bucket__ as a starting point: parse all the elements
2115  // of the current chained list
2116  for (; nb && bucket__ != nullptr; --nb, bucket__ = bucket__->prev) {}
2117 
2118  if (bucket__ != nullptr) return *this;
2119 
2120  // here, we shall skip all the chained list that have not sufficiently
2121  // many elements
2122  --index__;
2123 
2124  for (; index__ < table__->size__
2125  && nb >= table__->nodes__[index__].nb_elements__;
2126  nb -= table__->nodes__[index__].nb_elements__, --index__) {}
2127 
2128  // here: either index__ >= table__->size__, which means that we did not find
2129  // the element we looked for, i.e., we are at the end of the hashtable, or
2130  // nb < table__->nodes__[index__].nb_elements__, and we should parse the
2131  // chained list to get the element (which, we know for sure, exists)
2132  if (index__ >= table__->size__) {
2133  index__ = 0;
2134  return *this;
2135  }
2136 
2137  for (bucket__ = table__->nodes__[index__].end_list__; nb;
2138  --nb, bucket__ = bucket__->prev) {}
2139 
2140  return *this;
2141  }
std::vector< HashTableList< Key, Val, Alloc > > nodes__
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1722
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size size__
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1725
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2009  {
2010  // here, no need to avoid self assignment: this would slow down normal
2011  // assignments and, in any case, this would not result in an iterator in
2012  // an incoherent state
2013  table__ = from.table__;
2014  index__ = from.index__;
2015  bucket__ = from.bucket__;
2016 
2017  return *this;
2018  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2023  {
2024  // here, no need to avoid self assignment: this would slow down normal
2025  // assignments and, in any case, this would not result in an iterator in
2026  // an incoherent state
2027  table__ = from.table__;
2028  index__ = from.index__;
2029  bucket__ = from.bucket__;
2030 
2031  return *this;
2032  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
const HashTable< Key, Val > * table__
The hash table the iterator is pointing to.
Definition: hashTable.h:2676
Size index__
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2682

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

2157  {
2158  return (bucket__ == from.bucket__);
2159  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685

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

2046  {
2047  if (bucket__)
2048  return bucket__->val();
2049  else {
2050  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2051  }
2052  }
HashTable< Key, Val >::Bucket * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2685
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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 2670 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 2673 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 2685 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 2682 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 2676 of file hashTable.h.


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