aGrUM  0.16.0
gum::HashTableConstIterator< Key, Val > Class Template Reference

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

#include <agrum/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 2465 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 2476 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 2474 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 2477 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 2469 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 2470 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 2471 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 2475 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 2473 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 2472 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 1876 of file hashTable_tpl.h.

1876  {
1877  GUM_CONSTRUCTOR(HashTableConstIterator);
1878  }
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 1909 of file hashTable_tpl.h.

1910  :
1911  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1912  Size i;
1913 
1914  // check if we are looking for a begin() and we know for sure its index
1915  if ((ind_elt == Size(0))
1916  && (__table->__begin_index != std::numeric_limits< Size >::max())) {
1918  __bucket = __table->__nodes[__index].__end_list;
1919  } else {
1920  // check if it is faster to find the ind_eltth element from the start or
1921  // from the end of the hashtable
1922  if (ind_elt < (__table->__nb_elements >> 1)) {
1923  // find the element we shall point to from the start of the hashtable
1924  for (i = __table->__size - 1;; --i) { // no test on i since
1925  // ind_elt < _table->__nb_elements
1926  if (__table->__nodes[i].__nb_elements) {
1927  if (ind_elt >= __table->__nodes[i].__nb_elements)
1928  ind_elt -= __table->__nodes[i].__nb_elements;
1929  else {
1930  for (__bucket = __table->__nodes[i].__end_list; ind_elt;
1931  --ind_elt, __bucket = __bucket->prev) {}
1932 
1933  __index = i;
1934  break;
1935  }
1936  }
1937  }
1938  } else {
1939  // ind_elt = the index of the element we should point to
1940  // check if the index passed as parameter is valid
1941  if (ind_elt >= __table->__nb_elements) {
1942  GUM_ERROR(UndefinedIteratorValue,
1943  "Not enough elements in the hashtable");
1944  }
1945 
1946  // find the element we shall point to from the end of the hashtable
1947  for (i = 0, ind_elt = __table->__nb_elements - ind_elt - 1;; ++i) {
1948  if (__table->__nodes[i].__nb_elements) {
1949  if (ind_elt >= __table->__nodes[i].__nb_elements)
1950  ind_elt -= __table->__nodes[i].__nb_elements;
1951  else {
1952  for (__bucket = __table->__nodes[i].__deb_list; ind_elt;
1953  --ind_elt, __bucket = __bucket->next) {}
1954 
1955  __index = i;
1956  break;
1957  }
1958  }
1959  }
1960  }
1961  }
1962 
1963  // for debugging purposes
1964  GUM_CONSTRUCTOR(HashTableConstIterator);
1965  }
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
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
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 1968 of file hashTable_tpl.h.

1969  :
1970  __table{from.__table},
1971  __index{from.__index}, __bucket{from.__bucket} {
1972  GUM_CONS_CPY(HashTableConstIterator);
1973  }
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
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 1976 of file hashTable_tpl.h.

1977  :
1978  __table{from.__table},
1979  __index{from.__index}, __bucket{from.__bucket} {
1980  GUM_CONS_MOV(HashTableConstIterator);
1981  }
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
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 1984 of file hashTable_tpl.h.

1984  {
1985  // for debugging purposes
1986  GUM_DESTRUCTOR(HashTableConstIterator);
1987  }
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 1882 of file hashTable_tpl.h.

1883  :
1884  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1885  // for debugging purposes
1886  GUM_CONSTRUCTOR(HashTableConstIterator);
1887 
1888  if (__table->__nb_elements) {
1889  if (__table->__begin_index != std::numeric_limits< Size >::max()) {
1891  __bucket = __table->__nodes[__index].__end_list;
1892  } else {
1893  // find the element we shall point to from the start of the hashtable
1894  for (Size i = __table->__size - Size(1);; --i) { // no test on i since
1895  // __nb_elements != 0
1896  if (__table->__nodes[i].__nb_elements) {
1897  __index = i;
1898  __bucket = __table->__nodes[__index].__end_list;
1900  break;
1901  }
1902  }
1903  }
1904  }
1905  }
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
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
Size __nb_elements
Number of elements of type Val stored in the hash table.
Definition: hashTable.h:1724
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
Size __begin_index
Returns where the begin index should be.
Definition: hashTable.h:1749
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
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 2154 of file hashTable_tpl.h.

2154  {
2155  return __bucket;
2156  }
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
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 2159 of file hashTable_tpl.h.

2159  {
2160  return __index;
2161  }
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 ( )
noexcept

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

Definition at line 2036 of file hashTable_tpl.h.

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

2036  {
2037  __table = nullptr;
2038  __bucket = nullptr;
2039  __index = 0;
2040  }
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

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

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

2017  {
2018  if (__bucket)
2019  return __bucket->pair.first;
2020  else {
2021  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2022  }
2023  }
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!=()

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

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

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

2144  {
2145  if (__bucket)
2146  return __bucket->elt();
2147  else {
2148  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2149  }
2150  }
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

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

2126  {
2127  return HashTableConstIterator< Key, Val >{*this} += nb;
2128  }

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

2044  {
2045  // if __bucket == nullptr then we are at the end of the hashtable
2046  if (__bucket == nullptr) return *this;
2047 
2048  // if we are not pointing on the first element of the chained list, just
2049  // point to the next bucket in this list
2050  if (__bucket->prev) {
2051  __bucket = __bucket->prev;
2052  // here, no need to update __index which has not changed.
2053  } else {
2054  // ok, here we are on the end of a chained list,
2055  // so 2 cases can obtain:
2056  // 1/ index = 0 : then we have reached the end of the hashtable
2057  // 2/ index != 0 => we must search for a new slot containing elements
2058 
2059  // case 1:
2060  if (__index == Size(0)) {
2061  __bucket = nullptr;
2062  // we are thus at the end() of the hashTable
2063  }
2064 
2065  // case 2:
2066  else {
2067  // arrived here, we need to parse the hash table until we find a new
2068  // bucket because we are pointing on a chained list with no more element
2069  // to the right of the current element
2070  for (Size i = __index - Size(1); i; --i) {
2071  if (__table->__nodes[i].__nb_elements) {
2072  __index = i;
2073  __bucket = __table->__nodes[i].__end_list;
2074  return *this;
2075  }
2076  }
2077 
2078  if (__table->__nodes[0].__nb_elements)
2079  __bucket = __table->__nodes[0].__end_list;
2080  else
2081  __bucket = nullptr;
2082 
2083  __index = Size(0);
2084  }
2085  }
2086 
2087  return *this;
2088  }
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
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

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

2092  {
2093  if ((nb == 0) || (__table == nullptr) || (__bucket == nullptr)) return *this;
2094 
2095  // ok, here we can use __bucket as a starting point: parse all the elements
2096  // of the current chained list
2097  for (; nb && __bucket != nullptr; --nb, __bucket = __bucket->prev) {}
2098 
2099  if (__bucket != nullptr) return *this;
2100 
2101  // here, we shall skip all the chained list that have not sufficiently
2102  // many elements
2103  --__index;
2104 
2105  for (; __index < __table->__size
2106  && nb >= __table->__nodes[__index].__nb_elements;
2107  nb -= __table->__nodes[__index].__nb_elements, --__index) {}
2108 
2109  // here: either __index >= __table->__size, which means that we did not find
2110  // the element we looked for, i.e., we are at the end of the hashtable, or
2111  // nb < __table->__nodes[__index].__nb_elements, and we should parse the
2112  // chained list to get the element (which, we know for sure, exists)
2113  if (__index >= __table->__size) {
2114  __index = 0;
2115  return *this;
2116  }
2117 
2118  for (__bucket = __table->__nodes[__index].__end_list; nb;
2119  --nb, __bucket = __bucket->prev) {}
2120 
2121  return *this;
2122  }
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
Size __size
The number of nodes in vector &#39;__nodes&#39;.
Definition: hashTable.h:1721
std::vector< HashTableList< Key, Val, Alloc > > __nodes
The hash table is represented as a vector of chained lists.
Definition: hashTable.h:1718
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2681

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

1991  {
1992  // here, no need to avoid self assignment: this would slow down normal
1993  // assignments and, in any case, this would not result in an iterator in
1994  // an incoherent state
1995  __table = from.__table;
1996  __index = from.__index;
1997  __bucket = from.__bucket;
1998 
1999  return *this;
2000  }
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

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

2004  {
2005  // here, no need to avoid self assignment: this would slow down normal
2006  // assignments and, in any case, this would not result in an iterator in
2007  // an incoherent state
2008  __table = from.__table;
2009  __index = from.__index;
2010  __bucket = from.__bucket;
2011 
2012  return *this;
2013  }
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

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

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

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

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

2027  {
2028  if (__bucket)
2029  return __bucket->val();
2030  else {
2031  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2032  }
2033  }
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:

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 2666 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 2669 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 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)}
protected

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: