aGrUM  0.17.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 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 1882 of file hashTable_tpl.h.

1882  {
1883  GUM_CONSTRUCTOR(HashTableConstIterator);
1884  }
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 1915 of file hashTable_tpl.h.

1916  :
1917  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1918  Size i;
1919 
1920  // check if we are looking for a begin() and we know for sure its index
1921  if ((ind_elt == Size(0))
1922  && (__table->__begin_index != std::numeric_limits< Size >::max())) {
1924  __bucket = __table->__nodes[__index].__end_list;
1925  } else {
1926  // check if it is faster to find the ind_eltth element from the start or
1927  // from the end of the hashtable
1928  if (ind_elt < (__table->__nb_elements >> 1)) {
1929  // find the element we shall point to from the start of the hashtable
1930  for (i = __table->__size - 1;; --i) { // no test on i since
1931  // ind_elt < _table->__nb_elements
1932  if (__table->__nodes[i].__nb_elements) {
1933  if (ind_elt >= __table->__nodes[i].__nb_elements)
1934  ind_elt -= __table->__nodes[i].__nb_elements;
1935  else {
1936  for (__bucket = __table->__nodes[i].__end_list; ind_elt;
1937  --ind_elt, __bucket = __bucket->prev) {}
1938 
1939  __index = i;
1940  break;
1941  }
1942  }
1943  }
1944  } else {
1945  // ind_elt = the index of the element we should point to
1946  // check if the index passed as parameter is valid
1947  if (ind_elt >= __table->__nb_elements) {
1948  GUM_ERROR(UndefinedIteratorValue,
1949  "Not enough elements in the hashtable");
1950  }
1951 
1952  // find the element we shall point to from the end of the hashtable
1953  for (i = 0, ind_elt = __table->__nb_elements - ind_elt - 1;; ++i) {
1954  if (__table->__nodes[i].__nb_elements) {
1955  if (ind_elt >= __table->__nodes[i].__nb_elements)
1956  ind_elt -= __table->__nodes[i].__nb_elements;
1957  else {
1958  for (__bucket = __table->__nodes[i].__deb_list; ind_elt;
1959  --ind_elt, __bucket = __bucket->next) {}
1960 
1961  __index = i;
1962  break;
1963  }
1964  }
1965  }
1966  }
1967  }
1968 
1969  // for debugging purposes
1970  GUM_CONSTRUCTOR(HashTableConstIterator);
1971  }
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 1974 of file hashTable_tpl.h.

1975  :
1976  __table{from.__table},
1977  __index{from.__index}, __bucket{from.__bucket} {
1978  GUM_CONS_CPY(HashTableConstIterator);
1979  }
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 1982 of file hashTable_tpl.h.

1983  :
1984  __table{from.__table},
1985  __index{from.__index}, __bucket{from.__bucket} {
1986  GUM_CONS_MOV(HashTableConstIterator);
1987  }
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 1990 of file hashTable_tpl.h.

1990  {
1991  // for debugging purposes
1992  GUM_DESTRUCTOR(HashTableConstIterator);
1993  }
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 1888 of file hashTable_tpl.h.

1889  :
1890  __table{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1891  // for debugging purposes
1892  GUM_CONSTRUCTOR(HashTableConstIterator);
1893 
1894  if (__table->__nb_elements) {
1895  if (__table->__begin_index != std::numeric_limits< Size >::max()) {
1897  __bucket = __table->__nodes[__index].__end_list;
1898  } else {
1899  // find the element we shall point to from the start of the hashtable
1900  for (Size i = __table->__size - Size(1);; --i) { // no test on i since
1901  // __nb_elements != 0
1902  if (__table->__nodes[i].__nb_elements) {
1903  __index = i;
1904  __bucket = __table->__nodes[__index].__end_list;
1906  break;
1907  }
1908  }
1909  }
1910  }
1911  }
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 2162 of file hashTable_tpl.h.

2162  {
2163  return __bucket;
2164  }
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 2167 of file hashTable_tpl.h.

2167  {
2168  return __index;
2169  }
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 2044 of file hashTable_tpl.h.

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

2044  {
2045  __table = nullptr;
2046  __bucket = nullptr;
2047  __index = 0;
2048  }
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 2025 of file hashTable_tpl.h.

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

2025  {
2026  if (__bucket)
2027  return __bucket->pair.first;
2028  else {
2029  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2030  }
2031  }
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 2139 of file hashTable_tpl.h.

2140  {
2141  return (__bucket != from.__bucket);
2142  }
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 2152 of file hashTable_tpl.h.

2152  {
2153  if (__bucket)
2154  return __bucket->elt();
2155  else {
2156  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2157  }
2158  }
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 2134 of file hashTable_tpl.h.

2134  {
2135  return HashTableConstIterator< Key, Val >{*this} += nb;
2136  }

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

2052  {
2053  // if __bucket == nullptr then we are at the end of the hashtable
2054  if (__bucket == nullptr) return *this;
2055 
2056  // if we are not pointing on the first element of the chained list, just
2057  // point to the next bucket in this list
2058  if (__bucket->prev) {
2059  __bucket = __bucket->prev;
2060  // here, no need to update __index which has not changed.
2061  } else {
2062  // ok, here we are on the end of a chained list,
2063  // so 2 cases can obtain:
2064  // 1/ index = 0 : then we have reached the end of the hashtable
2065  // 2/ index != 0 => we must search for a new slot containing elements
2066 
2067  // case 1:
2068  if (__index == Size(0)) {
2069  __bucket = nullptr;
2070  // we are thus at the end() of the hashTable
2071  }
2072 
2073  // case 2:
2074  else {
2075  // arrived here, we need to parse the hash table until we find a new
2076  // bucket because we are pointing on a chained list with no more element
2077  // to the right of the current element
2078  for (Size i = __index - Size(1); i; --i) {
2079  if (__table->__nodes[i].__nb_elements) {
2080  __index = i;
2081  __bucket = __table->__nodes[i].__end_list;
2082  return *this;
2083  }
2084  }
2085 
2086  if (__table->__nodes[0].__nb_elements)
2087  __bucket = __table->__nodes[0].__end_list;
2088  else
2089  __bucket = nullptr;
2090 
2091  __index = Size(0);
2092  }
2093  }
2094 
2095  return *this;
2096  }
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 2100 of file hashTable_tpl.h.

2100  {
2101  if ((nb == 0) || (__table == nullptr) || (__bucket == nullptr)) return *this;
2102 
2103  // ok, here we can use __bucket as a starting point: parse all the elements
2104  // of the current chained list
2105  for (; nb && __bucket != nullptr; --nb, __bucket = __bucket->prev) {}
2106 
2107  if (__bucket != nullptr) return *this;
2108 
2109  // here, we shall skip all the chained list that have not sufficiently
2110  // many elements
2111  --__index;
2112 
2113  for (; __index < __table->__size
2114  && nb >= __table->__nodes[__index].__nb_elements;
2115  nb -= __table->__nodes[__index].__nb_elements, --__index) {}
2116 
2117  // here: either __index >= __table->__size, which means that we did not find
2118  // the element we looked for, i.e., we are at the end of the hashtable, or
2119  // nb < __table->__nodes[__index].__nb_elements, and we should parse the
2120  // chained list to get the element (which, we know for sure, exists)
2121  if (__index >= __table->__size) {
2122  __index = 0;
2123  return *this;
2124  }
2125 
2126  for (__bucket = __table->__nodes[__index].__end_list; nb;
2127  --nb, __bucket = __bucket->prev) {}
2128 
2129  return *this;
2130  }
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 1997 of file hashTable_tpl.h.

1998  {
1999  // here, no need to avoid self assignment: this would slow down normal
2000  // assignments and, in any case, this would not result in an iterator in
2001  // an incoherent state
2002  __table = from.__table;
2003  __index = from.__index;
2004  __bucket = from.__bucket;
2005 
2006  return *this;
2007  }
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 2011 of file hashTable_tpl.h.

2012  {
2013  // here, no need to avoid self assignment: this would slow down normal
2014  // assignments and, in any case, this would not result in an iterator in
2015  // an incoherent state
2016  __table = from.__table;
2017  __index = from.__index;
2018  __bucket = from.__bucket;
2019 
2020  return *this;
2021  }
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 2145 of file hashTable_tpl.h.

2146  {
2147  return (__bucket == from.__bucket);
2148  }
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 2035 of file hashTable_tpl.h.

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

2035  {
2036  if (__bucket)
2037  return __bucket->val();
2038  else {
2039  GUM_ERROR(UndefinedIteratorValue, "Accessing a nullptr object");
2040  }
2041  }
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: