63 template <
typename Key,
typename Val >
65 Bucket *ptr, *old_ptr{
nullptr}, *new_elt{
nullptr};
71 for (ptr = from._deb_list_; ptr !=
nullptr; ptr = ptr->
next) {
75 new_elt =
new Bucket(*ptr);
78 new_elt->prev = old_ptr;
80 if (old_ptr !=
nullptr) old_ptr->
next = new_elt;
86 if (old_ptr !=
nullptr) old_ptr->
next =
nullptr;
107 template <
typename Key,
typename Val >
110 if (ptr->key() == key)
return ptr;
115 template <
typename Key,
typename Val >
133 template <
typename Key,
typename Val >
136 template <
typename Key,
typename Val >
141 template <
typename Key,
typename Val >
143 _deb_list_{from._deb_list_}, _end_list_{from._end_list_}, _nb_elements_{from._nb_elements_} {
144 from._deb_list_ =
nullptr;
147 template <
typename Key,
typename Val >
150 next_ptr = ptr->
next;
155 template <
typename Key,
typename Val >
158 next_ptr = ptr->
next;
167 template <
typename Key,
typename Val >
168 INLINE HashTableList< Key, Val >&
179 template <
typename Key,
typename Val >
180 INLINE HashTableList< Key, Val >&
187 from._deb_list_ =
nullptr;
193 template <
typename Key,
typename Val >
204 template <
typename Key,
typename Val >
216 template <
typename Key,
typename Val >
220 if (ptr->key() == key)
return ptr->val();
225 template <
typename Key,
typename Val >
229 if (ptr->key() == key)
return ptr->val();
234 template <
typename Key,
typename Val >
237 if (ptr->key() == key) {
return true; }
243 template <
typename Key,
typename Val >
248 template <
typename Key,
typename Val >
251 new_elt->prev =
nullptr;
266 template <
typename Key,
typename Val >
292 template <
typename Key,
typename Val >
301 template <
typename Key,
typename Val >
313 template <
typename Key,
typename Val >
324 for (
const auto& elt: list) {
329 template <
typename Key,
typename Val >
343 template <
typename Key,
typename Val >
354 template <
typename Key,
typename Val >
357 for (
Size i =
Size(0); i < len; ++i)
361 template <
typename Key,
typename Val >
368 for (
Size i =
Size(0); i < _size_; ++i)
372 _begin_index_ = std::numeric_limits< Size >::max();
375 template <
typename Key,
typename Val >
385 template <
typename Key,
typename Val >
399 if (_size_ != from.
_size_) {
400 _nodes_.resize(from.
_size_);
405 _hash_func_.resize(_size_);
419 template <
typename Key,
typename Val >
422 if (
this != &table) {
430 _nodes_ = std::move(table._nodes_);
431 _safe_iterators_ = std::move(table._safe_iterators_);
432 _size_ = table._size_;
434 _hash_func_ = table._hash_func_;
435 _resize_policy_ = table._resize_policy_;
436 _key_uniqueness_policy_ = table._key_uniqueness_policy_;
437 _begin_index_ = table._begin_index_;
446 template <
typename Key,
typename Val >
448 return *(
reinterpret_cast< const iterator*
>(_HashTable_end_));
451 template <
typename Key,
typename Val >
454 return *(
reinterpret_cast< const const_iterator*
>(_HashTable_cend_));
457 template <
typename Key,
typename Val >
460 return *(
reinterpret_cast< const const_iterator*
>(_HashTable_cend_));
463 template <
typename Key,
typename Val >
467 else return iterator{*
this};
470 template <
typename Key,
typename Val >
474 else return const_iterator{*
this};
477 template <
typename Key,
typename Val >
481 else return const_iterator{*
this};
484 template <
typename Key,
typename Val >
487 return *(
reinterpret_cast< const iterator_safe*
>(_HashTable_end_safe_));
490 template <
typename Key,
typename Val >
493 return *(
reinterpret_cast< const const_iterator_safe*
>(_HashTable_cend_safe_));
496 template <
typename Key,
typename Val >
502 template <
typename Key,
typename Val >
509 template <
typename Key,
typename Val >
517 template <
typename Key,
typename Val >
525 template <
typename Key,
typename Val >
530 template <
typename Key,
typename Val >
535 template <
typename Key,
typename Val >
540 template <
typename Key,
typename Val >
545 template <
typename Key,
typename Val >
550 template <
typename Key,
typename Val >
555 template <
typename Key,
typename Val >
560 template <
typename Key,
typename Val >
565 template <
typename Key,
typename Val >
570 template <
typename Key,
typename Val >
573 new_size = std::max(
Size(2), new_size);
578 new_size =
Size(1) << log_size;
589 std::vector< HashTableList< Key, Val > > new_nodes(new_size);
599 while ((bucket =
_nodes_[i]._deb_list_) !=
nullptr) {
608 new_nodes[new_hashed_key].insert(bucket);
621 if (iter->_bucket_) iter->_index_ =
_hash_func_(iter->_bucket_->key());
623 iter->_next_bucket_ =
nullptr;
631 template <
typename Key,
typename Val >
638 Key k = bucket->
key();
641 "the hashtable contains an element with the same key (" << k <<
")");
652 _nodes_[hash_key].insert(bucket);
663 template <
typename Key,
typename Val >
666 auto bucket =
new Bucket(thekey, theval);
668 return bucket->elt();
671 template <
typename Key,
typename Val >
674 auto bucket =
new Bucket(std::move(thekey), std::move(theval));
676 return bucket->elt();
679 template <
typename Key,
typename Val >
684 return bucket->elt();
687 template <
typename Key,
typename Val >
692 return bucket->elt();
695 template <
typename Key,
typename Val >
696 template <
typename... Args >
702 return bucket->elt();
705 template <
typename Key,
typename Val >
710 if (bucket ==
nullptr)
return insert(
key, default_value).second;
711 else return bucket->
val();
714 template <
typename Key,
typename Val >
719 if (bucket ==
nullptr)
return insert(std::move(
key), std::move(default_value)).second;
720 else return bucket->
val();
723 template <
typename Key,
typename Val >
727 if (bucket ==
nullptr)
insert(
key, value);
728 else bucket->
val() = value;
731 template <
typename Key,
typename Val >
733 if (bucket ==
nullptr)
return;
737 if (iter->_bucket_ == bucket) {
739 iter->_next_bucket_ = iter->_bucket_;
740 iter->_bucket_ =
nullptr;
741 }
else if (iter->_next_bucket_ == bucket) {
742 iter->_bucket_ = bucket;
744 iter->_next_bucket_ = iter->_bucket_;
745 iter->_bucket_ =
nullptr;
759 template <
typename Key,
typename Val >
770 template <
typename Key,
typename Val >
772 _erase_(iter._getBucket_(), iter._getIndex_());
775 template <
typename Key,
typename Val >
780 template <
typename Key,
typename Val >
782 for (
auto iter =
cbegin(); iter !=
cend(); ++iter)
783 if (iter._bucket_->val() == val) {
784 _erase_(iter._getBucket_(), iter._getIndex_());
789 template <
typename Key,
typename Val >
794 template <
typename Key,
typename Val >
796 for (
auto iter =
begin(); iter !=
end(); ++iter)
797 if (iter._bucket_->val() == val)
return iter.key();
802 template <
typename Key,
typename Val >
807 if (bucket ==
nullptr) {
GUM_ERROR(
NotFound,
"key does not belong to the hashtable") }
809 return bucket->
key();
812 template <
typename Key,
typename Val >
815 if (iterAll._bucket_->val() == val) {
_erase_(iterAll._bucket_, iterAll._index_); }
819 template <
typename Key,
typename Val >
824 template <
typename Key,
typename Val >
825 template <
typename Mount >
829 bool key_uniqueness_pol)
const {
840 for (
auto iter =
begin(); iter !=
end(); ++iter) {
841 table.
insert(iter.key(), f(iter.val()));
847 template <
typename Key,
typename Val >
848 template <
typename Mount >
852 bool key_uniqueness_pol)
const {
863 for (
auto iter =
begin(); iter !=
end(); ++iter) {
864 table.
insert(iter.key(), f(
const_cast< Val&
>(iter.val())));
870 template <
typename Key,
typename Val >
871 template <
typename Mount >
875 bool key_uniqueness_pol)
const {
886 for (
auto iter =
begin(); iter !=
end(); ++iter) {
887 table.
insert(iter.key(), f(iter.val()));
893 template <
typename Key,
typename Val >
894 template <
typename Mount >
898 bool key_uniqueness_pol)
const {
909 for (
auto iter =
begin(); iter !=
end(); ++iter) {
910 table.
insert(iter.key(), val);
916 template <
typename Key,
typename Val >
922 for (
auto iter =
begin(); iter !=
end(); ++iter) {
924 if (iter.val() != from[iter.key()])
return false;
925 }
catch (
NotFound const&) {
return false; }
931 template <
typename Key,
typename Val >
936 template <
typename Key,
typename Val >
937 std::ostream&
operator<<(std::ostream& stream,
const HashTableList< Key, Val >& list) {
942 ptr = ptr->list.next, deja =
true) {
943 if (deja) stream <<
" , ";
945 stream << ptr->key() <<
"=>" << ptr->val();
953 template <
typename Key,
typename Val >
959 ptr = ptr->list.next, deja =
true) {
960 if (deja) stream <<
" , ";
962 stream << ptr->key() <<
"=>" << ptr->val();
970 template <
typename Key,
typename Val >
976 for (
auto ptr = table.
_nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
977 if (deja) stream <<
" , ";
979 stream << ptr->key() <<
"=>" << ptr->val();
989 template <
typename Key,
typename Val >
995 for (
auto ptr = table.
_nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
996 if (deja) stream <<
" , ";
998 stream << ptr->key() <<
"=>" << ptr->val();
1012 template <
typename Key,
typename Val >
1014 _table_->_safe_iterators_.push_back(
1018 template <
typename Key,
typename Val >
1020 if (
_table_ ==
nullptr)
return;
1023 std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect =
_table_->_safe_iterators_;
1025 auto len = iter_vect.size();
1026 for (
Size i =
Size(0); i < len; ++i) {
1027 if (iter_vect[i] ==
this) {
1028 iter_vect.erase(iter_vect.begin() + i);
1034 template <
typename Key,
typename Val >
1040 template <
typename Key,
typename Val >
1051 if (
_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1058 if (
_table_->_nodes_[i]._nb_elements_) {
1069 template <
typename Key,
typename Val >
1076 if ((ind_elt ==
Size(0)) && (
_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1077 _index_ = _table_->_begin_index_;
1078 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1082 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1084 for (i = _table_->_size_ - 1;; --i) {
1086 if (_table_->_nodes_[i]._nb_elements_) {
1087 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1088 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1090 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1091 --ind_elt, _bucket_ = _bucket_->prev) {}
1101 if (ind_elt >= _table_->_nb_elements_) {
1102 GUM_ERROR(UndefinedIteratorValue,
"Not enough elements in the hashtable")
1106 for (i = 0, ind_elt =
_table_->_nb_elements_ - ind_elt - 1;; ++i) {
1107 if (_table_->_nodes_[i]._nb_elements_) {
1108 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1109 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1111 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1112 --ind_elt, _bucket_ = _bucket_->next) {}
1123 GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1126 _insertIntoSafeList_();
1129 template <
typename Key,
typename Val >
1141 template <
typename Key,
typename Val >
1152 template <
typename Key,
typename Val >
1162 std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect =
_table_->_safe_iterators_;
1164 for (
auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1165 if (*ptr == &from) {
1167 from._table_ =
nullptr;
1174 template <
typename Key,
typename Val >
1183 template <
typename Key,
typename Val >
1210 template <
typename Key,
typename Val >
1222 _removeFromSafeList_();
1227 if (_table_) { _insertIntoSafeList_(); }
1232 _next_bucket_ =
nullptr;
1237 template <
typename Key,
typename Val >
1251 if (from.
_table_ !=
nullptr) {
1253 std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect
1254 = from.
_table_->_safe_iterators_;
1256 for (
auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1257 if (*ptr == &from) {
1275 template <
typename Key,
typename Val >
1282 template <
typename Key,
typename Val >
1289 template <
typename Key,
typename Val >
1292 _removeFromSafeList_();
1297 _next_bucket_ =
nullptr;
1303 template <
typename Key,
typename Val >
1307 if (_bucket_ ==
nullptr) {
1313 _bucket_ = _next_bucket_;
1314 _next_bucket_ =
nullptr;
1320 if (_bucket_->prev) {
1321 _bucket_ = _bucket_->prev;
1331 if (_index_ ==
Size(0)) {
1341 if (_index_ >
Size(0)) {
1343 if (_table_->_nodes_[i]._nb_elements_) {
1345 _bucket_ = _table_->_nodes_[i]._end_list_;
1351 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1352 else _bucket_ =
nullptr;
1362 template <
typename Key,
typename Val >
1365 if ((nb ==
Size(0)) || (
_table_ ==
nullptr))
return *
this;
1383 if (
_bucket_ !=
nullptr)
return *
this;
1406 template <
typename Key,
typename Val >
1412 template <
typename Key,
typename Val >
1418 template <
typename Key,
typename Val >
1424 template <
typename Key,
typename Val >
1431 template <
typename Key,
typename Val >
1437 template <
typename Key,
typename Val >
1446 template <
typename Key,
typename Val >
1452 template <
typename Key,
typename Val >
1455 HashTableConstIteratorSafe< Key, Val >(tab) {
1459 template <
typename Key,
typename Val >
1466 template <
typename Key,
typename Val >
1469 HashTableConstIteratorSafe< Key, Val >(from) {
1473 template <
typename Key,
typename Val >
1474 INLINE HashTableIteratorSafe< Key, Val >::HashTableIteratorSafe(
1476 GUM_CONS_CPY(HashTableIteratorSafe);
1479 template <
typename Key,
typename Val >
1480 INLINE HashTableIteratorSafe< Key, Val >::HashTableIteratorSafe(
1481 HashTableIteratorSafe< Key, Val >&& from) noexcept :
1483 GUM_CONS_MOV(HashTableIteratorSafe);
1486 template <
typename Key,
typename Val >
1487 INLINE HashTableIteratorSafe< Key, Val >::~HashTableIteratorSafe() noexcept {
1488 GUM_DESTRUCTOR(HashTableIteratorSafe);
1491 template <
typename Key,
typename Val >
1492 INLINE
typename HashTableIteratorSafe< Key, Val >::mapped_type&
1493 HashTableIteratorSafe< Key, Val >::val() {
1497 template <
typename Key,
typename Val >
1498 INLINE HashTableIteratorSafe< Key, Val >&
1499 HashTableIteratorSafe< Key, Val >::operator=(
const HashTableIteratorSafe< Key, Val >& from) {
1500 GUM_OP_CPY(HashTableIteratorSafe);
1505 template <
typename Key,
typename Val >
1506 INLINE HashTableIteratorSafe< Key, Val >&
1513 template <
typename Key,
typename Val >
1520 template <
typename Key,
typename Val >
1527 template <
typename Key,
typename Val >
1534 template <
typename Key,
typename Val >
1542 template <
typename Key,
typename Val >
1548 template <
typename Key,
typename Val >
1554 template <
typename Key,
typename Val >
1560 template <
typename Key,
typename Val >
1570 template <
typename Key,
typename Val >
1575 template <
typename Key,
typename Val >
1582 if (_table_->_nb_elements_) {
1583 if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1584 _index_ = _table_->_begin_index_;
1585 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1588 for (
Size i = _table_->_size_ -
Size(1);; --i) {
1590 if (_table_->_nodes_[i]._nb_elements_) {
1592 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1593 _table_->_begin_index_ = _index_;
1601 template <
typename Key,
typename Val >
1604 _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1608 if ((ind_elt == Size(0)) && (
_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1609 _index_ = _table_->_begin_index_;
1610 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1614 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1616 for (i = _table_->_size_ - 1;; --i) {
1618 if (_table_->_nodes_[i]._nb_elements_) {
1619 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1620 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1622 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1623 --ind_elt, _bucket_ = _bucket_->prev) {}
1633 if (ind_elt >= _table_->_nb_elements_) {
1634 GUM_ERROR(UndefinedIteratorValue,
"Not enough elements in the hashtable")
1638 for (i = 0, ind_elt =
_table_->_nb_elements_ - ind_elt - 1;; ++i) {
1639 if (_table_->_nodes_[i]._nb_elements_) {
1640 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1641 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1643 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1644 --ind_elt, _bucket_ = _bucket_->next) {}
1658 template <
typename Key,
typename Val >
1665 template <
typename Key,
typename Val >
1672 template <
typename Key,
typename Val >
1678 template <
typename Key,
typename Val >
1691 template <
typename Key,
typename Val >
1704 template <
typename Key,
typename Val >
1711 template <
typename Key,
typename Val >
1718 template <
typename Key,
typename Val >
1725 template <
typename Key,
typename Val >
1728 if (
_bucket_ ==
nullptr)
return *
this;
1752 for (Size i =
_index_ - Size(1); i; --i) {
1753 if (
_table_->_nodes_[i]._nb_elements_) {
1760 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1761 else _bucket_ =
nullptr;
1770 template <
typename Key,
typename Val >
1773 if ((nb == 0) || (
_table_ ==
nullptr) || (
_bucket_ ==
nullptr))
return *
this;
1779 if (
_bucket_ !=
nullptr)
return *
this;
1792 if (_index_ >= _table_->_size_) {
1797 for (_bucket_ = _table_->_nodes_[_index_]._end_list_; nb; --nb, _bucket_ = _bucket_->prev) {}
1802 template <
typename Key,
typename Val >
1808 template <
typename Key,
typename Val >
1814 template <
typename Key,
typename Val >
1820 template <
typename Key,
typename Val >
1827 template <
typename Key,
typename Val >
1828 INLINE
typename HashTable< Key, Val >::Bucket*
1833 template <
typename Key,
typename Val >
1842 template <
typename Key,
typename Val >
1848 template <
typename Key,
typename Val >
1854 template <
typename Key,
typename Val >
1861 template <
typename Key,
typename Val >
1868 template <
typename Key,
typename Val >
1875 template <
typename Key,
typename Val >
1880 template <
typename Key,
typename Val >
1882 if (this->_bucket_)
return this->_bucket_->val();
1886 template <
typename Key,
typename Val >
1893 template <
typename Key,
typename Val >
1900 template <
typename Key,
typename Val >
1906 template <
typename Key,
typename Val >
1913 template <
typename Key,
typename Val >
1921 template <
typename Key,
typename Val >
1927 template <
typename Key,
typename Val >
1933 template <
typename Key,
typename Val >
1939 template <
typename Key,
typename Val >
Unsafe Const Iterators for hashtables.
~HashTableConstIterator() noexcept
Class destructor.
Size _getIndex_() const noexcept
Returns the index in the hashtable's node vector pointed to by the iterator.
HashTableConstIterator< Key, Val > operator+(Size i) const noexcept
Returns a new iterator pointing to i elements further in the hashtable.
HashTableConstIterator< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
HashTableConstIterator< Key, Val > & operator=(const HashTableConstIterator< Key, Val > &from) noexcept
Copy operator.
Val mapped_type
Types for STL compliance.
HashTableConstIterator< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
const value_type & operator*() const
Returns the value pointed to by the iterator.
bool operator==(const HashTableConstIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
std::pair< const Key, Val > value_type
Types for STL compliance.
HashTable< Key, Val >::Bucket * _getBucket_() const noexcept
Returns the current iterator's bucket.
bool operator!=(const HashTableConstIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward different elements.
const mapped_type & val() const
Returns the mapped value pointed to by the iterator.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Key key_type
Types for STL compliance.
const key_type & key() const
Returns the key corresponding to the element pointed to by the iterator.
void clear() noexcept
Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash...
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table.
friend class HashTable< Key, Val >
Class HashTable must be a friend because it stores iterator end and this one can be properly initiali...
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
Safe Iterators for hashtables.
Unsafe Iterators for hashtables.
value_type & operator*()
Returns the value pointed to by the iterator.
Val mapped_type
types for STL compliance
HashTableIterator< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
HashTableIterator< Key, Val > operator+(Size i) const noexcept
Returns a new iterator.
~HashTableIterator() noexcept
Class destructor.
mapped_type & val()
Returns the mapped value pointed to by the iterator.
HashTableIterator< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
bool operator==(const HashTableIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
HashTableIterator< Key, Val > & operator=(const HashTableIterator< Key, Val > &from) noexcept
Copy operator.
std::pair< const Key, Val > value_type
types for STL compliance
bool operator!=(const HashTableIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward different elements.
Exception : a similar element already exists.
Safe Const Iterators for hashtables.
HashTableConstIteratorSafe< Key, Val > operator+(Size i) const
Returns a new iterator poiting to i elements further in the hashtable.
const key_type & key() const
Returns the key pointed to by the iterator.
Size _index_
the index of the chained list pointed to by the iterator in the array nodes of the hash table.
const value_type & operator*() const
Returns the element pointed to by the iterator.
Key key_type
Types for STL compliance.
Size _getIndex_() const noexcept
Returns the index in the hashtable's node vector pointed to by the iterator.
const mapped_type & val() const
Returns the mapped value pointed to by the iterator.
HashTableBucket< Key, Val > * _next_bucket_
the bucket we should start from when we decide to do a ++.
HashTableConstIteratorSafe< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
HashTableBucket< Key, Val > * _getBucket_() const noexcept
Returns the current iterator's bucket.
bool operator==(const HashTableConstIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are equal.
std::pair< const Key, Val > value_type
Types for STL compliance.
HashTableConstIteratorSafe< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
bool operator!=(const HashTableConstIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are not equal.
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
void clear() noexcept
Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash...
void _removeFromSafeList_() const
Removes the iterator from its hashtable' safe iterators list.
~HashTableConstIteratorSafe() noexcept
Destructor.
Val mapped_type
Types for STL compliance.
HashTableConstIteratorSafe< Key, Val > & operator=(const HashTableConstIteratorSafe< Key, Val > &from)
Copy operator.
friend class HashTable< Key, Val >
Class HashTable must be a friend because it stores iterator end and this can be properly initialized ...
void _insertIntoSafeList_() const
Insert the iterator into the hashtable's list of safe iterators.
Val mapped_type
types for STL compliance
~HashTableList()
Class destructor.
mapped_type & operator[](const key_type &key)
Returns the value corresponding to a given key.
bool exists(const key_type &key) const
Returns true if a value with the given key exists.
Bucket * bucket(const Key &key) const
A method to get the bucket corresponding to a given key.
HashTableBucket< Key, Val > Bucket
types for STL compliance
void erase(Bucket *ptr)
Removes an element from this chained list.
bool empty() const noexcept
Returns true if this chained list is empty.
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
void clear()
Removes all the elements of this chained list.
value_type & at(Size i)
Function at returns the ith element in the current chained list.
void _copy_(const HashTableList< Key, Val > &from)
A function used to perform copies of HashTableLists.
HashTableList< Key, Val > & operator=(const HashTableList< Key, Val > &from)
Assignment operator.
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
HashTableList() noexcept
Basic constructor that creates an empty list.
std::pair< const Key, Val > value_type
types for STL compliance
friend class HashTable< Key, Val >
Friend for faster access.
void insert(Bucket *new_elt) noexcept
Inserts a new element in the chained list.
Size _nb_elements_
The number of elements in the chained list.
The class for generic Hash Tables.
bool _resize_policy_
Is resizing performed automatically?
void eraseByVal(const Val &val)
Removes a given element from the hash table.
HashTableIterator< Key, Val > iterator
Types for STL compliance.
void eraseAllVal(const Val &val)
Removes all the elements having a certain value from the hash table.
void _create_(Size size)
Used by all default constructors (general and specialized).
HashFunc< Key > _hash_func_
The function used to hash keys (may change when the table is resized).
void resize(Size new_size)
Changes the number of slots in the 'nodes' vector of the hash table.
Size _begin_index_
Returns where the begin index should be.
friend class HashTableIteratorSafe< Key, Val >
std::vector< HashTableConstIteratorSafe< Key, Val > * > _safe_iterators_
The list of safe iterators pointing to the hash table.
void _copy_(const HashTable< Key, Val > &table)
A function used to perform copies of HashTables.
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
std::pair< const Key, Val > value_type
Types for STL compliance.
void _clearIterators_()
Clear all the safe iterators.
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
friend class HashTableConstIteratorSafe< Key, Val >
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
~HashTable()
Class destructor.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
Val & operator[](const Key &key)
Returns a reference on the value the key of which is passed in argument.
const Key & key(const Key &key) const
Returns a reference on a given key.
bool empty() const noexcept
Indicates whether the hash table is empty.
bool resizePolicy() const noexcept
Returns the current resizing policy.
HashTable< Key, Val > & operator=(const HashTable< Key, Val > &from)
Copy operator.
HashTable< Key, Mount > map(Mount(*f)(Val), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
Transforms a hashtable of vals into a hashtable of mountains.
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
std::vector< HashTableList< Key, Val > > _nodes_
The hash table is represented as a vector of chained lists.
bool keyUniquenessPolicy() const noexcept
Returns the current checking policy.
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Val mapped_type
Types for STL compliance.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
void clear()
Removes all the elements in the hash table.
bool operator==(const HashTable< Key, Val > &from) const
Checks whether two hashtables contain the same elements.
Size capacity() const noexcept
Returns the number of slots in the 'nodes' vector of the hashtable.
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
bool operator!=(const HashTable< Key, Val > &from) const
Checks whether two hashtables contain different sets of elements.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
void reset(const Key &key)
Removes a property (i.e., remove an element).
void setResizePolicy(const bool new_policy) noexcept
Enables the user to change dynamically the resizing policy.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
Size _size_
The number of nodes in vector ' __nodes'.
const Key & keyByVal(const Val &val) const
Returns a reference on the key given a value.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
HashTable(Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
Default constructor.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
friend class HashTableIterator< Key, Val >
value_type & emplace(Args &&... args)
Emplace a new element into the hashTable.
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Exception : the element we looked for cannot be found.
Exception : a pointer or a reference on a nullptr (0) object.
Exception : generic error on iterator.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
unsigned int _hashTableLog2_(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb.
Class hash tables iterators.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
bool operator==(const HashTableIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
A recipient for a pair of key value in a gum::HashTableList.
HashTableBucket< Key, Val > * prev
A pointer toward the previous bucket in the gum::HashTableList.
Key & key()
Returns the key part of the pair.
std::pair< const Key, Val > & elt()
Returns the pair stored in this bucket.
HashTableBucket< Key, Val > * next
A pointer toward the next bucket in the gum::HashTableList.
Val & val()
Returns the value part of the pair.
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.