aGrUM  0.14.2
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 2462 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 2473 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 2471 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 2474 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 2466 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 2467 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 2468 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 2472 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 2470 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 2469 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 1873 of file hashTable_tpl.h.

1873  {
1874  GUM_CONSTRUCTOR(HashTableConstIterator);
1875  }
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 1906 of file hashTable_tpl.h.

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

1966  :
1967  __table{from.__table},
1968  __index{from.__index}, __bucket{from.__bucket} {
1969  GUM_CONS_CPY(HashTableConstIterator);
1970  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2669
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
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 1973 of file hashTable_tpl.h.

1974  :
1975  __table{from.__table},
1976  __index{from.__index}, __bucket{from.__bucket} {
1977  GUM_CONS_MOV(HashTableConstIterator);
1978  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2669
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
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 1981 of file hashTable_tpl.h.

1981  {
1982  // for debugging purposes
1983  GUM_DESTRUCTOR(HashTableConstIterator);
1984  }
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 1879 of file hashTable_tpl.h.

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

2151  {
2152  return __bucket;
2153  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678

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

2156  {
2157  return __index;
2158  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675

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

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

2033  {
2034  __table = nullptr;
2035  __bucket = nullptr;
2036  __index = 0;
2037  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2669
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
+ 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 2014 of file hashTable_tpl.h.

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

2014  {
2015  if (__bucket)
2016  return __bucket->pair.first;
2017  else {
2018  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2019  }
2020  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 2129 of file hashTable_tpl.h.

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

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

2141  {
2142  if (__bucket)
2143  return __bucket->elt();
2144  else {
2145  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2146  }
2147  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

2123  {
2124  return HashTableConstIterator< Key, Val >{*this} += nb;
2125  }

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

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

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

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

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

1988  {
1989  // here, no need to avoid self assignment: this would slow down normal
1990  // assignments and, in any case, this would not result in an iterator in
1991  // an incoherent state
1992  __table = from.__table;
1993  __index = from.__index;
1994  __bucket = from.__bucket;
1995 
1996  return *this;
1997  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2669
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678

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

2001  {
2002  // here, no need to avoid self assignment: this would slow down normal
2003  // assignments and, in any case, this would not result in an iterator in
2004  // an incoherent state
2005  __table = from.__table;
2006  __index = from.__index;
2007  __bucket = from.__bucket;
2008 
2009  return *this;
2010  }
Size __index
The index of the chained list pointed by the iterator in the array of nodes of the hash table...
Definition: hashTable.h:2675
const HashTable< Key, Val > * __table
The hash table the iterator is pointing to.
Definition: hashTable.h:2669
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678

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

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

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

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

2024  {
2025  if (__bucket)
2026  return __bucket->val();
2027  else {
2028  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2029  }
2030  }
HashTable< Key, Val >::Bucket * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: hashTable.h:2678
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 2663 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 2666 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 2678 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 2675 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: