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

A chained list used by gum::HashTable. More...

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

Public Member Functions

template<typename Key, typename Val, typename Alloc>
INLINE HashTableList (typename HashTableList< Key, Val, Alloc >::BucketAllocator *allocator) noexcept
 
template<typename OtherAlloc >
INLINE HashTableList< Key, Val, Alloc > & operator= (const HashTableList< Key, Val, OtherAlloc > &from)
 
Constructors / Destructors
 HashTableList (BucketAllocator *allocator=nullptr) noexcept
 Basic constructor that creates an empty list. More...
 
 HashTableList (const HashTableList< Key, Val, Alloc > &from)
 Copy constructor. More...
 
 HashTableList (HashTableList< Key, Val, Alloc > &&from) noexcept
 Move constructor. More...
 
 ~HashTableList ()
 Class destructor. More...
 
Operators
HashTableList< Key, Val, Alloc > & operator= (const HashTableList< Key, Val, Alloc > &from)
 Assignment operator. More...
 
template<typename OtherAlloc >
HashTableList< Key, Val, Alloc > & operator= (const HashTableList< Key, Val, OtherAlloc > &from)
 Generalized assignment operator. More...
 
HashTableList< Key, Val, Alloc > & operator= (HashTableList< Key, Val, Alloc > &&from) noexcept
 Move operator. More...
 
Accessors / Modifiers
value_typeat (Size i)
 Function at returns the ith element in the current chained list. More...
 
const value_typeat (Size i) const
 Function at returns the ith element in the current chained list. More...
 
mapped_typeoperator[] (const key_type &key)
 Returns the value corresponding to a given key. More...
 
const mapped_typeoperator[] (const key_type &key) const
 Returns the value corresponding to a given key. More...
 
bool exists (const key_type &key) const
 Returns true if a value with the given key exists. More...
 
void insert (Bucket *new_elt) noexcept
 Inserts a new element in the chained list. More...
 
void erase (Bucket *ptr)
 Removes an element from this chained list. More...
 
void clear ()
 Removes all the elements of this chained list. More...
 
bool empty () const noexcept
 Returns true if this chained list is empty. More...
 
Bucketbucket (const Key &key) const
 A method to get the bucket corresponding to a given key. More...
 
void setAllocator (BucketAllocator &alloc)
 Sets a new allocator. More...
 

Public Types

using BucketAllocator = typename Alloc::template rebind< Bucket >::other
 The Bucket allocator. 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 size_type = Size
 types for STL compliance More...
 
using allocator_type = Alloc
 types for STL compliance More...
 
using Bucket = HashTableBucket< Key, Val >
 types for STL compliance More...
 

Friends

template<typename K , typename V , typename A >
class HashTableList
 Friend for faster access. More...
 
class HashTable< Key, Val, Alloc >
 Friend for faster access. More...
 
class HashTableIterator< Key, Val >
 Friend for faster access. More...
 
class HashTableConstIterator< Key, Val >
 Friend for faster access. More...
 
class HashTableIteratorSafe< Key, Val >
 Friend for faster access. More...
 
class HashTableConstIteratorSafe< Key, Val >
 Friend for faster access. More...
 
std::ostream & operator<< (std::ostream &, const HashTableList< Key, Val, Alloc > &)
 Prints the content of a gum::HashTableList in the stream. More...
 
std::ostream & operator<< (std::ostream &, const HashTableList< Key *, Val, Alloc > &)
 Prints the content of a gum::HashTableList with pointers key in the stream. More...
 
std::ostream & operator<< (std::ostream &, const HashTable< Key, Val, Alloc > &)
 Prints the content of a gum::HashTable in the stream. More...
 
std::ostream & operator<< (std::ostream &, const HashTable< Key *, Val, Alloc > &)
 Prints the content of a gum::HashTable with pointers key in the stream. More...
 

Detailed Description

template<typename Key, typename Val, typename Alloc>
class gum::HashTableList< Key, Val, Alloc >

A chained list used by gum::HashTable.

Template Parameters
KeyThe type for keys in a gum::HashTable.
ValThe type for values in a gum::HashTable.
AllocThe gum::HashTable allocator.

Definition at line 301 of file hashTable.h.

Member Typedef Documentation

◆ allocator_type

template<typename Key, typename Val, typename Alloc>
using gum::HashTableList< Key, Val, Alloc >::allocator_type = Alloc

types for STL compliance

Definition at line 313 of file hashTable.h.

◆ Bucket

template<typename Key, typename Val, typename Alloc>
using gum::HashTableList< Key, Val, Alloc >::Bucket = HashTableBucket< Key, Val >

types for STL compliance

Definition at line 314 of file hashTable.h.

◆ BucketAllocator

template<typename Key, typename Val, typename Alloc>
using gum::HashTableList< Key, Val, Alloc >::BucketAllocator = typename Alloc::template rebind< Bucket >::other

The Bucket allocator.

Definition at line 318 of file hashTable.h.

◆ const_pointer

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

types for STL compliance

Definition at line 311 of file hashTable.h.

◆ const_reference

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

types for STL compliance

Definition at line 309 of file hashTable.h.

◆ key_type

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

types for STL compliance

Definition at line 305 of file hashTable.h.

◆ mapped_type

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

types for STL compliance

Definition at line 306 of file hashTable.h.

◆ pointer

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

types for STL compliance

Definition at line 310 of file hashTable.h.

◆ reference

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

types for STL compliance

Definition at line 308 of file hashTable.h.

◆ size_type

template<typename Key, typename Val, typename Alloc>
using gum::HashTableList< Key, Val, Alloc >::size_type = Size

types for STL compliance

Definition at line 312 of file hashTable.h.

◆ value_type

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

types for STL compliance

Definition at line 307 of file hashTable.h.

Constructor & Destructor Documentation

◆ HashTableList() [1/4]

template<typename Key, typename Val, typename Alloc>
gum::HashTableList< Key, Val, Alloc >::HashTableList ( BucketAllocator allocator = nullptr)
noexcept

Basic constructor that creates an empty list.

This is what is used basically by gum::HashTable.

Warning
If the allocator is not passed in argument, do not forget to use method setAllocator after the creation.
Parameters
allocatorThe gum::HashTableBucket allocator.

◆ HashTableList() [2/4]

template<typename Key, typename Val, typename Alloc>
INLINE gum::HashTableList< Key, Val, Alloc >::HashTableList ( const HashTableList< Key, Val, Alloc > &  from)

Copy constructor.

The new list and that which is copied do not share elements: the new list contains new instances of the keys and values stored in the copied list. Of course, if these values are pointers, the new values point toward the same elements.

Warning
Both from and this will share the same allocator.
Parameters
fromThe gum::HashTableList to copy.

Definition at line 136 of file hashTable_tpl.h.

136  :
137  _alloc_bucket_{from._alloc_bucket_} {
138  _copy_(from);
139  }
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
void _copy_(const HashTableList< Key, Val, OtherAlloc > &from)
A function used to perform copies of HashTableLists.
Definition: hashTable_tpl.h:44

◆ HashTableList() [3/4]

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

Move constructor.

Parameters
fromThe gum::HashTableList to move.

Definition at line 142 of file hashTable_tpl.h.

143  :
144  _deb_list_{from._deb_list_},
145  _end_list_{from._end_list_}, _nb_elements_{from._nb_elements_}, _alloc_bucket_{
146  from._alloc_bucket_} {
147  from._deb_list_ = nullptr;
148  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526

◆ ~HashTableList()

template<typename Key , typename Val , typename Alloc >
INLINE gum::HashTableList< Key, Val, Alloc >::~HashTableList ( )

Class destructor.

Definition at line 151 of file hashTable_tpl.h.

151  {
152  for (Bucket *next_ptr, *ptr = _deb_list_; ptr != nullptr; ptr = next_ptr) {
153  next_ptr = ptr->next;
154  _alloc_bucket_->destroy(ptr);
155  _alloc_bucket_->deallocate(ptr, 1);
156  }
157  }
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ HashTableList() [4/4]

template<typename Key, typename Val, typename Alloc>
template<typename Key, typename Val, typename Alloc>
INLINE gum::HashTableList< Key, Val, Alloc >::HashTableList ( typename HashTableList< Key, Val, Alloc >::BucketAllocator allocator)
noexcept

Definition at line 130 of file hashTable_tpl.h.

131  :
132  _alloc_bucket_{allocator} {}
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532

Member Function Documentation

◆ _copy_()

template<typename Key, typename Val, typename Alloc >
template<typename OtherAlloc >
void gum::HashTableList< Key, Val, Alloc >::_copy_ ( const HashTableList< Key, Val, OtherAlloc > &  from)
private

A function used to perform copies of HashTableLists.

This code is shared by the copy constructor and the copy operator. If it cannot perform the necessary allocations, no memory leak occurs and the list is set to the empty list.

Parameters
fromThe gum::HashTableList to copy.
Template Parameters
OtherAllocThe other gum::HashTableList allocator.

Definition at line 44 of file hashTable_tpl.h.

44  {
45  Bucket *ptr, *old_ptr{nullptr}, *new_elt{nullptr};
46  // set the defaults
47  _deb_list_ = nullptr;
48 
49  // copy from's list
50  try {
51  for (ptr = from._deb_list_; ptr != nullptr; ptr = ptr->next) {
52  // copy the current from's bucket (may throw an exception either because
53  // new cannot allocate the bucket or because the copy constructor of Val
54  // throws an exception)
55  new_elt = _alloc_bucket_->allocate(1);
56 
57  try {
58  _alloc_bucket_->construct(new_elt, *ptr);
59  } catch (...) {
60  _alloc_bucket_->deallocate(new_elt, 1);
61  throw;
62  }
63 
64  // rechain properly the new list
65  new_elt->prev = old_ptr;
66 
67  if (old_ptr != nullptr)
68  old_ptr->next = new_elt;
69  else
70  _deb_list_ = new_elt;
71 
72  old_ptr = new_elt;
73  }
74 
75  if (old_ptr != nullptr) old_ptr->next = nullptr;
76 
77  // update the number of elements stored into the list and the end of the
78  // list
79  _nb_elements_ = from._nb_elements_;
80  _end_list_ = new_elt;
81  } catch (...) {
82  // problem: we could not allocate an element in the list => we delete
83  // the elements created so far and we throw an exception
84  for (; _deb_list_ != nullptr; _deb_list_ = new_elt) {
85  new_elt = _deb_list_->next;
86  _alloc_bucket_->destroy(_deb_list_);
87  _alloc_bucket_->deallocate(_deb_list_, 1);
88  }
89 
90  _nb_elements_ = 0;
91  _end_list_ = nullptr;
92 
93  throw;
94  }
95  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ at() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTableList< Key, Val, Alloc >::value_type & gum::HashTableList< Key, Val, Alloc >::at ( Size  i)

Function at returns the ith element in the current chained list.

The first element has index 0.

Parameters
iThe index to look up.
Returns
Returns the value at index i.
Exceptions
NotFoundRaised if the list has fewer than i elements.

Definition at line 219 of file hashTable_tpl.h.

219  {
220  if (i >= _nb_elements_) { GUM_ERROR(NotFound, "not enough elements in the chained list") }
221 
222  Bucket* ptr;
223 
224  for (ptr = _deb_list_; i; --i, ptr = ptr->next) {}
225 
226  return ptr->elt();
227  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ at() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTableList< Key, Val, Alloc >::value_type & gum::HashTableList< Key, Val, Alloc >::at ( Size  i) const

Function at returns the ith element in the current chained list.

The first element has index 0.

Parameters
iThe index to look up.
Returns
Returns the value at index i.
Exceptions
NotFoundRaised if the list has fewer than i elements.

Definition at line 231 of file hashTable_tpl.h.

231  {
232  if (i >= _nb_elements_) { GUM_ERROR(NotFound, "not enough elements in the chained list") }
233 
234  Bucket* ptr;
235 
236  for (ptr = _deb_list_; i; --i, ptr = ptr->next) {}
237 
238  return ptr->elt();
239  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ bucket()

template<typename Key, typename Val , typename Alloc >
INLINE HashTableBucket< Key, Val > * gum::HashTableList< Key, Val, Alloc >::bucket ( const Key &  key) const

A method to get the bucket corresponding to a given key.

This enables efficient removals of buckets.

Parameters
keyThe key of the bucket to return.
Returns
Returns the buckket matching key.

Definition at line 99 of file hashTable_tpl.h.

99  {
100  for (Bucket* ptr = _deb_list_; ptr != nullptr; ptr = ptr->next)
101  if (ptr->key() == key) return ptr;
102 
103  return nullptr;
104  }
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ clear()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTableList< Key, Val, Alloc >::clear ( )

Removes all the elements of this chained list.

Definition at line 160 of file hashTable_tpl.h.

160  {
161  for (Bucket *next_ptr, *ptr = _deb_list_; ptr != nullptr; ptr = next_ptr) {
162  next_ptr = ptr->next;
163  _alloc_bucket_->destroy(ptr);
164  _alloc_bucket_->deallocate(ptr, 1);
165  }
166 
167  _nb_elements_ = Size(0);
168  _deb_list_ = nullptr;
169  _end_list_ = nullptr;
170  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ empty()

template<typename Key , typename Val , typename Alloc >
INLINE bool gum::HashTableList< Key, Val, Alloc >::empty ( ) const
noexcept

Returns true if this chained list is empty.

Returns
Returns true if this chained list is empty.

Definition at line 269 of file hashTable_tpl.h.

269  {
270  return (_nb_elements_ == Size(0));
271  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ erase()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTableList< Key, Val, Alloc >::erase ( Bucket ptr)

Removes an element from this chained list.

Parameters
ptrThe element to remove.

Definition at line 107 of file hashTable_tpl.h.

107  {
108  // check that the pointer is not nullptr
109  if (ptr == nullptr) { GUM_ERROR(NullElement, "trying to erase a nullptr bucket") }
110 
111  // relink properly the doubly chained list
112  if (ptr->prev != nullptr)
113  ptr->prev->next = ptr->next;
114  else
115  _deb_list_ = ptr->next;
116 
117  if (ptr->next != nullptr)
118  ptr->next->prev = ptr->prev;
119  else
120  _end_list_ = ptr->prev;
121 
122  // remove the current element from the list
123  _alloc_bucket_->destroy(ptr);
124  _alloc_bucket_->deallocate(ptr, 1);
125 
126  --_nb_elements_;
127  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ exists()

template<typename Key , typename Val , typename Alloc >
INLINE bool gum::HashTableList< Key, Val, Alloc >::exists ( const key_type key) const

Returns true if a value with the given key exists.

Checks whether there exists an element with a given key in the list.

Parameters
keyThe key to test for existence.
Returns
Returns true if a value with the given key exists.

Definition at line 260 of file hashTable_tpl.h.

260  {
261  for (Bucket* ptr = _deb_list_; ptr != nullptr; ptr = ptr->next) {
262  if (ptr->key() == key) { return true; }
263  }
264 
265  return false;
266  }
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ insert()

template<typename Key , typename Val , typename Alloc >
INLINE void gum::HashTableList< Key, Val, Alloc >::insert ( Bucket new_elt)
noexcept

Inserts a new element in the chained list.

The element is inserted at the beginning of the list.

Parameters
new_eltThe element to add in the gum::HashTableList.

Definition at line 275 of file hashTable_tpl.h.

275  {
276  // place the bucket at the beginning of the list
277  new_elt->prev = nullptr;
278  new_elt->next = _deb_list_;
279 
280  if (_deb_list_ != nullptr)
281  _deb_list_->prev = new_elt;
282  else
283  _end_list_ = new_elt;
284 
285  _deb_list_ = new_elt;
286 
287  ++_nb_elements_;
288  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526

◆ operator=() [1/4]

template<typename Key, typename Val, typename Alloc>
template<typename OtherAlloc >
INLINE HashTableList< Key, Val, Alloc >& gum::HashTableList< Key, Val, Alloc >::operator= ( const HashTableList< Key, Val, OtherAlloc > &  from)

Definition at line 186 of file hashTable_tpl.h.

187  {
188  // avoid self assignment
189  if (this != reinterpret_cast< const HashTableList< Key, Val, Alloc >* >(&from)) {
190  clear();
191  _copy_(from);
192  }
193 
194  return *this;
195  }
void clear()
Removes all the elements of this chained list.
void _copy_(const HashTableList< Key, Val, OtherAlloc > &from)
A function used to perform copies of HashTableLists.
Definition: hashTable_tpl.h:44

◆ operator=() [2/4]

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

Assignment operator.

The new list and that which is copied do not share elements: the new list contains new instances of the keys and values stored in the copied list. Of course, if these values are pointers, the new values point toward the same elements.

If some allocation problem occurs or if copying the Val elements cannot be performed properly, exceptions may be raised. In this case, the function guarantees that no memory leak occurs and that the list is kept in a coherent state (that of an empty list).

Warning
operator= does not change the current allocator of *this
Parameters
fromThe gum::HashTableList to copy.
Returns
Returns this gum::HashTableList.

Definition at line 174 of file hashTable_tpl.h.

174  {
175  // avoid self assignment
176  if (this != &from) {
177  clear();
178  _copy_(from);
179  }
180 
181  return *this;
182  }
void clear()
Removes all the elements of this chained list.
void _copy_(const HashTableList< Key, Val, OtherAlloc > &from)
A function used to perform copies of HashTableLists.
Definition: hashTable_tpl.h:44

◆ operator=() [3/4]

template<typename Key, typename Val, typename Alloc>
template<typename OtherAlloc >
HashTableList< Key, Val, Alloc >& gum::HashTableList< Key, Val, Alloc >::operator= ( const HashTableList< Key, Val, OtherAlloc > &  from)

Generalized assignment operator.

The new list and that which is copied do not share elements: the new list contains new instances of the keys and values stored in the copied list. Of course, if these values are pointers, the new values point toward the same elements.

If some allocation problem occurs or if copying the Val elements cannot be performed properly, exceptions may be raised. In this case, the function guarantees that no memory leak occurs and that the list is kept in a coherent state (that of an empty list).

Warning
operator= does not change the current allocator of *this
Parameters
fromThe gum::HashTableList to copy.
Returns
Returns this gum::HashTableList.

◆ operator=() [4/4]

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

Move operator.

Warning
operator= does not change the current allocator of *this
Parameters
fromThe gum::HashTableList to copy.
Returns
Returns this gum::HashTableList.

Definition at line 199 of file hashTable_tpl.h.

199  {
200  // avoid self assignment
201  if (this != &from) {
202  _deb_list_ = from._deb_list_;
203  _end_list_ = from._end_list_;
204  _nb_elements_ = from._nb_elements_;
205  from._deb_list_ = nullptr;
206  }
207 
208  return *this;
209  }
Size _nb_elements_
The number of elements in the chained list.
Definition: hashTable.h:529
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition: hashTable.h:526

◆ operator[]() [1/2]

template<typename Key , typename Val , typename Alloc >
INLINE HashTableList< Key, Val, Alloc >::mapped_type & gum::HashTableList< Key, Val, Alloc >::operator[] ( const key_type key)

Returns the value corresponding to a given key.

Parameters
keyThe key for which a value is returned.
Returns
Returns the value corresponding to a given key.
Exceptions
NotFoundis raised if the element cannot be found

Definition at line 252 of file hashTable_tpl.h.

252  {
253  for (Bucket* ptr = _deb_list_; ptr != nullptr; ptr = ptr->next)
254  if (ptr->key() == key) return ptr->val();
255 
256  GUM_ERROR(NotFound, "No element with the key <" << key << ">")
257  }
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ operator[]() [2/2]

template<typename Key , typename Val , typename Alloc >
INLINE const HashTableList< Key, Val, Alloc >::mapped_type & gum::HashTableList< Key, Val, Alloc >::operator[] ( const key_type key) const

Returns the value corresponding to a given key.

Parameters
keyThe key for which a value is returned.
Returns
Returns the value corresponding to a given key.
Exceptions
NotFoundis raised if the element cannot be found

Definition at line 243 of file hashTable_tpl.h.

243  {
244  for (Bucket* ptr = _deb_list_; ptr != nullptr; ptr = ptr->next)
245  if (ptr->key() == key) return ptr->val();
246 
247  GUM_ERROR(NotFound, "No element with the key <" << key << ">")
248  }
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition: hashTable.h:523
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition: hashTable.h:314

◆ setAllocator()

template<typename Key, typename Val, typename Alloc>
INLINE void gum::HashTableList< Key, Val, Alloc >::setAllocator ( BucketAllocator alloc)

Sets a new allocator.

Parameters
allocThe new allocator.

Definition at line 212 of file hashTable_tpl.h.

213  {
214  _alloc_bucket_ = &alloc;
215  }
BucketAllocator * _alloc_bucket_
The allocator of the containing hashTable.
Definition: hashTable.h:532

Friends And Related Function Documentation

◆ HashTableList

template<typename Key, typename Val, typename Alloc>
template<typename K , typename V , typename A >
friend class HashTableList
friend

Friend for faster access.

Definition at line 510 of file hashTable.h.

◆ HashTable< Key, Val, Alloc >

template<typename Key, typename Val, typename Alloc>
friend class HashTable< Key, Val, Alloc >
friend

Friend for faster access.

Definition at line 511 of file hashTable.h.

◆ HashTableConstIterator< Key, Val >

template<typename Key, typename Val, typename Alloc>
friend class HashTableConstIterator< Key, Val >
friend

Friend for faster access.

Definition at line 513 of file hashTable.h.

◆ HashTableConstIteratorSafe< Key, Val >

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

Friend for faster access.

Definition at line 515 of file hashTable.h.

◆ HashTableIterator< Key, Val >

template<typename Key, typename Val, typename Alloc>
friend class HashTableIterator< Key, Val >
friend

Friend for faster access.

Definition at line 512 of file hashTable.h.

◆ HashTableIteratorSafe< Key, Val >

template<typename Key, typename Val, typename Alloc>
friend class HashTableIteratorSafe< Key, Val >
friend

Friend for faster access.

Definition at line 514 of file hashTable.h.

◆ operator<< [1/4]

template<typename Key, typename Val, typename Alloc>
std::ostream& operator<< ( std::ostream &  s,
const HashTableList< Key, Val, Alloc > &  list 
)
friend

Prints the content of a gum::HashTableList in the stream.

Definition at line 1167 of file hashTable_tpl.h.

1167  {
1168  bool deja = false;
1169  stream << "[";
1170 
1171  for (HashTableBucket< Key, Val >* ptr = list._deb_list_; ptr;
1172  ptr = ptr->list.next, deja = true) {
1173  if (deja) stream << " , ";
1174 
1175  stream << ptr->key() << "=>" << ptr->val();
1176  }
1177 
1178  stream << "]";
1179 
1180  return stream;
1181  }

◆ operator<< [2/4]

template<typename Key, typename Val, typename Alloc>
std::ostream& operator<< ( std::ostream &  s,
const HashTableList< Key *, Val, Alloc > &  list 
)
friend

Prints the content of a gum::HashTableList with pointers key in the stream.

Definition at line 1184 of file hashTable_tpl.h.

1184  {
1185  bool deja = false;
1186  stream << "[";
1187 
1188  for (HashTableBucket< Key, Val >* ptr = list._deb_list_; ptr;
1189  ptr = ptr->list.next, deja = true) {
1190  if (deja) stream << " , ";
1191 
1192  stream << ptr->key() << "=>" << ptr->val();
1193  }
1194 
1195  stream << "]";
1196 
1197  return stream;
1198  }

◆ operator<< [3/4]

template<typename Key, typename Val, typename Alloc>
std::ostream& operator<< ( std::ostream &  s,
const HashTable< Key, Val, Alloc > &  table 
)
friend

Prints the content of a gum::HashTable in the stream.

Definition at line 1201 of file hashTable_tpl.h.

1201  {
1202  bool deja = false;
1203  stream << "{";
1204 
1205  for (Size i = Size(0); i < table._size_; ++i)
1206  for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1207  if (deja) stream << " , ";
1208 
1209  stream << ptr->key() << "=>" << ptr->val();
1210 
1211  deja = true;
1212  }
1213 
1214  stream << "}";
1215 
1216  return stream;
1217  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ operator<< [4/4]

template<typename Key, typename Val, typename Alloc>
std::ostream& operator<< ( std::ostream &  s,
const HashTable< Key *, Val, Alloc > &  table 
)
friend

Prints the content of a gum::HashTable with pointers key in the stream.

Definition at line 1220 of file hashTable_tpl.h.

1220  {
1221  bool deja = false;
1222  stream << "{";
1223 
1224  for (Size i = Size(0); i < table._size_; ++i)
1225  for (auto ptr = table._nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1226  if (deja) stream << " , ";
1227 
1228  stream << ptr->key() << "=>" << ptr->val();
1229 
1230  deja = true;
1231  }
1232 
1233  stream << "}";
1234 
1235  return stream;
1236  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

Member Data Documentation

◆ _alloc_bucket_

template<typename Key, typename Val, typename Alloc>
BucketAllocator* gum::HashTableList< Key, Val, Alloc >::_alloc_bucket_
mutableprivate

The allocator of the containing hashTable.

Definition at line 532 of file hashTable.h.

◆ _deb_list_

template<typename Key, typename Val, typename Alloc>
HashTableBucket< Key, Val >* gum::HashTableList< Key, Val, Alloc >::_deb_list_ {nullptr}
private

A pointer on the first element of the chained list.

Definition at line 523 of file hashTable.h.

◆ _end_list_

template<typename Key, typename Val, typename Alloc>
HashTableBucket< Key, Val >* gum::HashTableList< Key, Val, Alloc >::_end_list_ {nullptr}
private

A pointer on the last element of the chained list.

Definition at line 526 of file hashTable.h.

◆ _nb_elements_

template<typename Key, typename Val, typename Alloc>
Size gum::HashTableList< Key, Val, Alloc >::_nb_elements_ {Size(0)}
private

The number of elements in the chained list.

Definition at line 529 of file hashTable.h.


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