62 template <
typename Key,
typename Val >
64 Bucket *ptr, *old_ptr{
nullptr}, *new_elt{
nullptr};
70 for (ptr = from._deb_list_; ptr !=
nullptr; ptr = ptr->
next) {
74 new_elt =
new Bucket(*ptr);
77 new_elt->prev = old_ptr;
79 if (old_ptr !=
nullptr) old_ptr->
next = new_elt;
85 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 >
117 requires std::same_as< Key, std::string >
120 if (ptr->key() == key)
return ptr;
125 template <
typename Key,
typename Val >
143 template <
typename Key,
typename Val >
146 template < typename Key, typename Val >
151 template <
typename Key,
typename Val >
153 _deb_list_{from._deb_list_}, _end_list_{from._end_list_}, _nb_elements_{from._nb_elements_} {
154 from._deb_list_ =
nullptr;
155 from._end_list_ =
nullptr;
156 from._nb_elements_ = 0;
159 template <
typename Key,
typename Val >
162 next_ptr = ptr->
next;
167 template <
typename Key,
typename Val >
170 next_ptr = ptr->
next;
179 template <
typename Key,
typename Val >
180 HashTableList< Key, Val >&
191 template <
typename Key,
typename Val >
192 HashTableList< Key, Val >&
199 from._deb_list_ =
nullptr;
205 template <
typename Key,
typename Val >
216 template <
typename Key,
typename Val >
228 template <
typename Key,
typename Val >
232 if (ptr->key() == key)
return ptr->val();
237 template <
typename Key,
typename Val >
241 if (ptr->key() == key)
return ptr->val();
246 template <
typename Key,
typename Val >
249 if (ptr->key() == key) {
return true; }
255 template <
typename Key,
typename Val >
260 template <
typename Key,
typename Val >
263 new_elt->prev =
nullptr;
278 template <
typename Key,
typename Val >
291 for (
Size j = 0; j < _size_; ++j)
294 _nb_elements_ =
Size(0);
304 template <
typename Key,
typename Val >
313 template <
typename Key,
typename Val >
325 template <
typename Key,
typename Val >
336 for (
const auto& elt: list) {
341 template <
typename Key,
typename Val >
355 template <
typename Key,
typename Val >
371 template <
typename Key,
typename Val >
374 for (
Size i =
Size(0); i < len; ++i)
378 template <
typename Key,
typename Val >
389 _begin_index_ = std::numeric_limits< Size >::max();
392 template <
typename Key,
typename Val >
402 template <
typename Key,
typename Val >
416 if (_size_ != from.
_size_) {
417 _nodes_.resize(from.
_size_);
422 _hash_func_.resize(_size_);
436 template <
typename Key,
typename Val >
439 if (
this != &table) {
457 _nodes_ = std::move(table.
_nodes_);
472 template <
typename Key,
typename Val >
474 return *(
reinterpret_cast< const iterator*
>(_HashTable_end_));
477 template <
typename Key,
typename Val >
480 return *(
reinterpret_cast< const const_iterator*
>(_HashTable_cend_));
483 template <
typename Key,
typename Val >
486 return *(
reinterpret_cast< const const_iterator*
>(_HashTable_cend_));
489 template <
typename Key,
typename Val >
496 template <
typename Key,
typename Val >
503 template <
typename Key,
typename Val >
510 template <
typename Key,
typename Val >
512 return *(
reinterpret_cast< const iterator_safe*
>(_HashTable_end_safe_));
515 template <
typename Key,
typename Val >
521 template <
typename Key,
typename Val >
527 template <
typename Key,
typename Val >
534 template <
typename Key,
typename Val >
541 template <
typename Key,
typename Val >
548 template <
typename Key,
typename Val >
553 template <
typename Key,
typename Val >
558 template <
typename Key,
typename Val >
559 template <
typename K >
560 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
561 && !std::same_as< std::decay_t< K >, std::string >)
563 std::string_view sv{
key};
565 if (b ==
nullptr) {
GUM_ERROR(
NotFound,
"No element with the key <" << sv <<
">") }
569 template <
typename Key,
typename Val >
570 template <
typename K >
571 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
572 && !std::same_as< std::decay_t< K >, std::string >)
574 std::string_view sv{
key};
576 if (b ==
nullptr) {
GUM_ERROR(
NotFound,
"No element with the key <" << sv <<
">") }
577 return b->
pair.second;
580 template <
typename Key,
typename Val >
585 template <
typename Key,
typename Val >
590 template <
typename Key,
typename Val >
595 template <
typename Key,
typename Val >
596 template <
typename K >
597 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
598 && !std::same_as< std::decay_t< K >, std::string >)
600 std::string_view sv{
key};
604 template <
typename Key,
typename Val >
607 if (bucket ==
nullptr)
return {};
608 return bucket->
val();
611 template <
typename Key,
typename Val >
614 if (bucket ==
nullptr)
return {};
615 return bucket->
pair.second;
618 template <
typename Key,
typename Val >
619 template <
typename K >
620 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
621 && !std::same_as< std::decay_t< K >, std::string >)
623 std::string_view sv{
key};
625 if (b ==
nullptr)
return {};
629 template <
typename Key,
typename Val >
630 template <
typename K >
631 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
632 && !std::same_as< std::decay_t< K >, std::string >)
634 std::string_view sv{
key};
636 if (b ==
nullptr)
return {};
640 template <
typename Key,
typename Val >
645 template <
typename Key,
typename Val >
650 template <
typename Key,
typename Val >
655 template <
typename Key,
typename Val >
660 template <
typename Key,
typename Val >
663 new_size = std::max(
Size(2), new_size);
668 new_size =
Size(1) << log_size;
679 std::vector< HashTableList< Key, Val > > new_nodes(new_size);
689 while ((bucket =
_nodes_[i]._deb_list_) !=
nullptr) {
698 new_nodes[new_hashed_key].insert(bucket);
704 _begin_index_ = std::numeric_limits< Size >::max();
707 std::swap(_nodes_, new_nodes);
710 for (
auto iter: _safe_iterators_) {
711 if (iter->_bucket_) iter->_index_ = _hash_func_(iter->_bucket_->key());
713 iter->_next_bucket_ =
nullptr;
721 template <
typename Key,
typename Val >
728 Key k = bucket->
key();
731 "the hashtable contains an element with the same key (" << k <<
")");
742 _nodes_[hash_key].insert(bucket);
750 if (_begin_index_ < hash_key) { _begin_index_ = hash_key; }
753 template <
typename Key,
typename Val >
756 auto bucket =
new Bucket(thekey, theval);
758 return bucket->
elt();
761 template <
typename Key,
typename Val >
764 auto bucket =
new Bucket(std::move(thekey), std::move(theval));
766 return bucket->
elt();
769 template <
typename Key,
typename Val >
774 return bucket->
elt();
777 template <
typename Key,
typename Val >
782 return bucket->
elt();
785 template <
typename Key,
typename Val >
786 template <
typename... Args >
791 return bucket->
elt();
794 template <
typename Key,
typename Val >
799 if (bucket ==
nullptr)
return insert(
key, default_value).second;
800 else return bucket->
val();
803 template <
typename Key,
typename Val >
808 if (bucket ==
nullptr)
return insert(std::move(
key), std::move(default_value)).second;
809 else return bucket->
val();
812 template <
typename Key,
typename Val >
817 else bucket->
val() = value;
820 template <
typename Key,
typename Val >
822 if (bucket ==
nullptr)
return;
826 if (iter->_bucket_ == bucket) {
828 iter->_next_bucket_ = iter->_bucket_;
829 iter->_bucket_ =
nullptr;
830 }
else if (iter->_next_bucket_ == bucket) {
831 iter->_bucket_ = bucket;
833 iter->_next_bucket_ = iter->_bucket_;
834 iter->_bucket_ =
nullptr;
848 template <
typename Key,
typename Val >
859 template <
typename Key,
typename Val >
860 template <
typename K >
861 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
862 && !std::same_as< std::decay_t< K >, std::string >)
864 std::string_view sv{
key};
870 template <
typename Key,
typename Val >
872 _erase_(iter._getBucket_(), iter._getIndex_());
875 template <
typename Key,
typename Val >
880 template <
typename Key,
typename Val >
882 for (
auto iter =
cbegin(); iter !=
cend(); ++iter)
883 if (iter._bucket_->val() == val) {
884 _erase_(iter._getBucket_(), iter._getIndex_());
889 template <
typename Key,
typename Val >
894 template <
typename Key,
typename Val >
896 for (
auto iter =
begin(); iter !=
end(); ++iter)
897 if (iter._bucket_->val() == val)
return iter.key();
902 template <
typename Key,
typename Val >
907 if (bucket ==
nullptr) {
GUM_ERROR(
NotFound,
"key does not belong to the hashtable") }
909 return bucket->
key();
912 template <
typename Key,
typename Val >
920 template <
typename Key,
typename Val >
923 if (iterAll._bucket_->val() == val) {
_erase_(iterAll._bucket_, iterAll._index_); }
927 template <
typename Key,
typename Val >
932 template <
typename Key,
typename Val >
933 template <
typename Mount >
937 bool key_uniqueness_pol)
const {
948 for (
auto iter =
begin(); iter !=
end(); ++iter) {
949 table.
insert(iter.key(), f(iter.val()));
955 template <
typename Key,
typename Val >
956 template <
typename Mount >
960 bool key_uniqueness_pol)
const {
971 for (
auto iter =
begin(); iter !=
end(); ++iter) {
972 table.
insert(iter.key(), f(
const_cast< Val&
>(iter.val())));
978 template <
typename Key,
typename Val >
979 template <
typename Mount >
983 bool key_uniqueness_pol)
const {
994 for (
auto iter =
begin(); iter !=
end(); ++iter) {
995 table.
insert(iter.key(), f(iter.val()));
1001 template <
typename Key,
typename Val >
1002 template <
typename Mount >
1006 bool key_uniqueness_pol)
const {
1018 table.
insert(iter.key(), val);
1024 template <
typename Key,
typename Val >
1030 for (
auto iter =
begin(); iter !=
end(); ++iter) {
1031 if (
auto p = from.
tryGet(iter.key()); !p || iter.val() != *p)
return false;
1037 template <
typename Key,
typename Val >
1038 std::ostream&
operator<<(std::ostream& stream,
const HashTableList< Key, Val >& list) {
1043 ptr = ptr->list.next, deja =
true) {
1044 if (deja) stream <<
" , ";
1046 stream << ptr->key() <<
"=>" << ptr->val();
1054 template <
typename Key,
typename Val >
1060 ptr = ptr->list.next, deja =
true) {
1061 if (deja) stream <<
" , ";
1063 stream << ptr->key() <<
"=>" << ptr->val();
1071 template <
typename Key,
typename Val >
1077 for (
auto ptr = table.
_nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1078 if (deja) stream <<
" , ";
1080 stream << ptr->key() <<
"=>" << ptr->val();
1090 template <
typename Key,
typename Val >
1096 for (
auto ptr = table.
_nodes_[i]._deb_list_; ptr; ptr = ptr->next) {
1097 if (deja) stream <<
" , ";
1099 stream << ptr->key() <<
"=>" << ptr->val();
1113 template <
typename Key,
typename Val >
1115 _table_->_safe_iterators_.push_back(
1119 template <
typename Key,
typename Val >
1121 if (
_table_ ==
nullptr)
return;
1124 std::vector< HashTableConstIteratorSafe< Key, Val >* >& iter_vect = _table_->_safe_iterators_;
1126 auto len = iter_vect.size();
1127 for (
Size i =
Size(0); i < len; ++i) {
1128 if (iter_vect[i] ==
this) {
1129 iter_vect.erase(iter_vect.begin() + i);
1135 template <
typename Key,
typename Val >
1141 template <
typename Key,
typename Val >
1152 if (
_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1160 if (
_table_->_nodes_[i]._nb_elements_) {
1171 template <
typename Key,
typename Val >
1178 if ((ind_elt ==
Size(0)) && (
_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1179 _index_ = _table_->_begin_index_;
1180 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1184 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1186 for (i = _table_->_size_ - 1;; --i) {
1189 if (_table_->_nodes_[i]._nb_elements_) {
1190 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1191 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1193 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1194 --ind_elt, _bucket_ = _bucket_->prev) {}
1204 if (ind_elt >= _table_->_nb_elements_) {
1205 GUM_ERROR(UndefinedIteratorValue,
"Not enough elements in the hashtable")
1209 for (i = 0, ind_elt =
_table_->_nb_elements_ - ind_elt - 1;; ++i) {
1210 if (_table_->_nodes_[i]._nb_elements_) {
1211 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1212 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1214 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1215 --ind_elt, _bucket_ = _bucket_->next) {}
1226 GUM_CONSTRUCTOR(HashTableConstIteratorSafe);
1229 _insertIntoSafeList_();
1232 template <
typename Key,
typename Val >
1244 template <
typename Key,
typename Val >
1247 _table_{from._table_}, _index_{from._index_}, _bucket_{from._bucket_} {
1249 if (_table_ !=
nullptr) { _insertIntoSafeList_(); }
1255 template <
typename Key,
typename Val >
1258 _table_{from._table_}, _index_{from._index_}, _bucket_{from._bucket_},
1259 _next_bucket_{from._next_bucket_} {
1264 if (_table_ !=
nullptr) {
1265 std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect = _table_->_safe_iterators_;
1267 for (
auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1268 if (*ptr == &from) {
1270 from._table_ =
nullptr;
1277 template <
typename Key,
typename Val >
1286 template <
typename Key,
typename Val >
1313 template <
typename Key,
typename Val >
1325 _removeFromSafeList_();
1330 if (_table_) { _insertIntoSafeList_(); }
1335 _next_bucket_ =
nullptr;
1340 template <
typename Key,
typename Val >
1350 if (_table_ != from.
_table_) {
1352 _removeFromSafeList_();
1354 if (from.
_table_ !=
nullptr) {
1356 std::vector< HashTableConstIteratorSafe< Key, Val >* >& vect
1357 = from.
_table_->_safe_iterators_;
1359 for (
auto ptr = vect.rbegin(); ptr != vect.rend(); ++ptr) {
1360 if (*ptr == &from) {
1373 _next_bucket_ = from._next_bucket_;
1378 template <
typename Key,
typename Val >
1385 template <
typename Key,
typename Val >
1392 template <
typename Key,
typename Val >
1395 _removeFromSafeList_();
1400 _next_bucket_ =
nullptr;
1406 template <
typename Key,
typename Val >
1422 if (_bucket_->prev) {
1423 _bucket_ = _bucket_->prev;
1433 if (_index_ ==
Size(0)) {
1443 if (_index_ >
Size(0)) {
1444 for (Size i = _index_ -
Size(1); i >
Size(0); --i) {
1445 if (_table_->_nodes_[i]._nb_elements_) {
1447 _bucket_ = _table_->_nodes_[i]._end_list_;
1453 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1454 else _bucket_ =
nullptr;
1464 template <
typename Key,
typename Val >
1467 if ((nb ==
Size(0)) || (
_table_ ==
nullptr))
return *
this;
1485 if (
_bucket_ !=
nullptr)
return *
this;
1508 template <
typename Key,
typename Val >
1514 template <
typename Key,
typename Val >
1520 template <
typename Key,
typename Val >
1527 template <
typename Key,
typename Val >
1533 template <
typename Key,
typename Val >
1542 template <
typename Key,
typename Val >
1548 template <
typename Key,
typename Val >
1550 HashTableConstIteratorSafe< Key, Val >(tab) {
1554 template <
typename Key,
typename Val >
1558 GUM_CONSTRUCTOR(HashTableIteratorSafe);
1561 template <
typename Key,
typename Val >
1562 HashTableIteratorSafe< Key, Val >::HashTableIteratorSafe(
1563 const HashTableIteratorSafe< Key, Val >& from) :
1565 GUM_CONS_CPY(HashTableIteratorSafe);
1568 template <
typename Key,
typename Val >
1574 template <
typename Key,
typename Val >
1581 template <
typename Key,
typename Val >
1586 template <
typename Key,
typename Val >
1592 template <
typename Key,
typename Val >
1600 template <
typename Key,
typename Val >
1608 template <
typename Key,
typename Val >
1615 template <
typename Key,
typename Val >
1621 template <
typename Key,
typename Val >
1628 template <
typename Key,
typename Val >
1629 HashTableIteratorSafe< Key, Val > HashTableIteratorSafe< Key, Val >::operator+(
Size nb)
const {
1630 HashTableIteratorSafe< Key, Val > iter{*
this};
1635 template <
typename Key,
typename Val >
1636 bool HashTableIteratorSafe< Key, Val >::operator==(
1637 const HashTableIteratorSafe< Key, Val >& from)
const noexcept {
1641 template <
typename Key,
typename Val >
1642 typename HashTableIteratorSafe< Key, Val >::value_type&
1643 HashTableIteratorSafe< Key, Val >::operator*() {
1647 template <
typename Key,
typename Val >
1648 const typename HashTableIteratorSafe< Key, Val >::value_type&
1649 HashTableIteratorSafe< Key, Val >::operator*()
const {
1657 template <
typename Key,
typename Val >
1662 template <
typename Key,
typename Val >
1669 if (_table_->_nb_elements_) {
1670 if (_table_->_begin_index_ != std::numeric_limits< Size >::max()) {
1671 _index_ = _table_->_begin_index_;
1672 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1675 for (
Size i = _table_->_size_ -
Size(1);; --i) {
1678 if (_table_->_nodes_[i]._nb_elements_) {
1680 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1681 _table_->_begin_index_ = _index_;
1689 template <
typename Key,
typename Val >
1692 _table_{reinterpret_cast< const HashTable< Key, Val >* >(&tab)} {
1696 if ((ind_elt == Size(0)) && (
_table_->_begin_index_ != std::numeric_limits< Size >::max())) {
1697 _index_ = _table_->_begin_index_;
1698 _bucket_ = _table_->_nodes_[_index_]._end_list_;
1702 if (ind_elt < (_table_->_nb_elements_ >> 1)) {
1704 for (i = _table_->_size_ - 1;; --i) {
1707 if (_table_->_nodes_[i]._nb_elements_) {
1708 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1709 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1711 for (_bucket_ = _table_->_nodes_[i]._end_list_; ind_elt;
1712 --ind_elt, _bucket_ = _bucket_->prev) {}
1722 if (ind_elt >= _table_->_nb_elements_) {
1723 GUM_ERROR(UndefinedIteratorValue,
"Not enough elements in the hashtable")
1727 for (i = 0, ind_elt =
_table_->_nb_elements_ - ind_elt - 1;; ++i) {
1728 if (_table_->_nodes_[i]._nb_elements_) {
1729 if (ind_elt >= _table_->_nodes_[i]._nb_elements_)
1730 ind_elt -= _table_->_nodes_[i]._nb_elements_;
1732 for (_bucket_ = _table_->_nodes_[i]._deb_list_; ind_elt;
1733 --ind_elt, _bucket_ = _bucket_->next) {}
1747 template <
typename Key,
typename Val >
1754 template <
typename Key,
typename Val >
1761 template <
typename Key,
typename Val >
1767 template <
typename Key,
typename Val >
1771 template <
typename Key,
typename Val >
1784 template <
typename Key,
typename Val >
1791 template <
typename Key,
typename Val >
1798 template <
typename Key,
typename Val >
1805 template <
typename Key,
typename Val >
1808 if (
_bucket_ ==
nullptr)
return *
this;
1833 if (
_table_->_nodes_[i]._nb_elements_) {
1840 if (_table_->_nodes_[0]._nb_elements_) _bucket_ = _table_->_nodes_[0]._end_list_;
1841 else _bucket_ =
nullptr;
1850 template <
typename Key,
typename Val >
1853 if ((nb == 0) || (
_table_ ==
nullptr) || (
_bucket_ ==
nullptr))
return *
this;
1859 if (
_bucket_ !=
nullptr)
return *
this;
1882 template <
typename Key,
typename Val >
1888 template <
typename Key,
typename Val >
1894 template <
typename Key,
typename Val >
1901 template <
typename Key,
typename Val >
1907 template <
typename Key,
typename Val >
1916 template <
typename Key,
typename Val >
1922 template <
typename Key,
typename Val >
1928 template <
typename Key,
typename Val >
1934 template <
typename Key,
typename Val >
1941 template <
typename Key,
typename Val >
1947 template <
typename Key,
typename Val >
1952 template <
typename Key,
typename Val >
1958 template <
typename Key,
typename Val >
1965 template <
typename Key,
typename Val >
1972 template <
typename Key,
typename Val >
1978 template <
typename Key,
typename Val >
1984 template <
typename Key,
typename Val >
1991 template <
typename Key,
typename Val >
1997 template <
typename Key,
typename Val >
2002 template <
typename Key,
typename Val >
2012 template <
typename Key,
typename Val >
2015 template <
typename Key,
typename Val >
2019 template <
typename Key,
typename Val >
2020 template <
typename... Args >
2023 pair(
std::forward< Args >(args)...) {}
2025 template <
typename Key,
typename Val >
2030 template <
typename Key,
typename Val >
2032 return const_cast< Key&
>(
pair.first);
2035 template <
typename Key,
typename Val >
Unsafe Const Iterators for hashtables.
HashTableConstIterator< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
Val mapped_type
Types for STL compliance.
const value_type & operator*() const
Returns the value pointed to by the iterator.
HashTableConstIterator< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
const mapped_type & val() const
Returns the mapped value pointed to by the iterator.
HashTableConstIterator< Key, Val > & operator=(const HashTableConstIterator< Key, Val > &from) noexcept
Copy operator.
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.
std::pair< const Key, Val > value_type
Types for STL compliance.
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.
void clear() noexcept
Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash...
~HashTableConstIterator() noexcept
Class destructor.
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...
const key_type & key() const
Returns the key corresponding to the element pointed to by the iterator.
bool operator==(const HashTableConstIterator< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
HashTable< Key, Val >::Bucket * _getBucket_() const noexcept
Returns the current iterator's bucket.
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
Safe Iterators for hashtables.
Unsafe Iterators for hashtables.
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() noexcept
Basic constructor: creates an iterator pointing to nothing.
~HashTableIterator() noexcept
Class destructor.
HashTableIterator< Key, Val > operator+(Size i) const noexcept
Returns a new iterator.
HashTableIterator< Key, Val > & operator=(const HashTableIterator< Key, Val > &from) noexcept
Copy operator.
mapped_type & val()
Returns the mapped value pointed to by the iterator.
value_type & operator*()
Returns the value pointed to by the iterator.
HashTableIterator< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
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 equal elements.
Exception : a similar element already exists.
Safe Const Iterators for hashtables.
const mapped_type & val() const
Returns the mapped value pointed to by the iterator.
HashTableBucket< Key, Val > * _getBucket_() const noexcept
Returns the current iterator's bucket.
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 to by the iterator in the array nodes of the hash table.
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
void _insertIntoSafeList_() const
Insert the iterator into the hashtable's list of safe iterators.
Key key_type
Types for STL compliance.
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.
bool operator==(const HashTableConstIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are equal.
~HashTableConstIteratorSafe() noexcept
Destructor.
std::pair< const Key, Val > value_type
Types for STL compliance.
const value_type & operator*() const
Returns the element pointed to by the iterator.
HashTableConstIteratorSafe< Key, Val > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the hashtable.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
void _removeFromSafeList_() const
Removes the iterator from its hashtable' safe iterators list.
HashTableBucket< Key, Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Size _getIndex_() const noexcept
Returns the index in the hashtable's node vector pointed to by the iterator.
HashTableConstIteratorSafe< Key, Val > operator+(Size i) const
Returns a new iterator poiting to i elements further in the hashtable.
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 ...
const key_type & key() const
Returns the key pointed to by the iterator.
Val mapped_type
types for STL compliance
mapped_type & operator[](const key_type &key)
Returns the value corresponding to a given key.
void clear()
Removes all the elements of this chained list.
bool empty() const noexcept
Returns true if this chained list is empty.
HashTableBucket< Key, Val > Bucket
types for STL compliance
value_type & at(Size i)
Function at returns the ith element in the current chained list.
HashTableList< Key, Val > & operator=(const HashTableList< Key, Val > &from)
Assignment operator.
~HashTableList()
Class destructor.
bool exists(const key_type &key) const
Returns true if a value with the given key exists.
void insert(Bucket *new_elt) noexcept
Inserts a new element in the chained list.
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
void _copy_(const HashTableList< Key, Val > &from)
A function used to perform copies of HashTableLists.
HashTableList() noexcept
Basic constructor that creates an empty list.
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
void erase(Bucket *ptr)
Removes an element from this chained list.
std::pair< const Key, Val > value_type
types for STL compliance
friend class HashTable< Key, Val >
Friend for faster access.
Bucket * bucket(const Key &key) const
A method to get the bucket corresponding to a given key.
Size _nb_elements_
The number of elements in the chained list.
The class for generic Hash Tables.
bool resizePolicy() const noexcept
Returns the current resizing policy.
void _create_(Size size)
Used by all default constructors (general and specialized).
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
const Key & keyByVal(const Val &val) const
Returns a reference on the key given a value.
bool _resize_policy_
Is resizing performed automatically?
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.
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.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size _begin_index_
Returns where the begin index should be.
friend class HashTableIteratorSafe< Key, Val >
Size capacity() const noexcept
Returns the number of slots in the 'nodes' vector of the hashtable.
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.
bool empty() const noexcept
void clear()
Removes all the elements in the hash table.
const Key & key(const Key &key) const
Returns a reference on a given key.
value_type & emplace(Args &&... args)
Emplace a new element into the hashTable.
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 set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
void erase(const Key &key)
Removes a given element from the hash table.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
~HashTable()
Class destructor.
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
void reset(const Key &key)
Removes a property (i.e., remove an element).
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
void eraseByVal(const Val &val)
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< Key, Val > & operator=(const HashTable< Key, Val > &from)
Copy operator.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
void setResizePolicy(const bool new_policy) noexcept
Enables the user to change dynamically the resizing policy.
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.
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
std::vector< HashTableList< Key, Val > > _nodes_
The hash table is represented as a vector of chained lists.
HashTableConstIteratorSafe< Key, Val > const_iterator_safe
Types for STL compliance.
HashTableConstIterator< Key, Val > const_iterator
Types for STL compliance.
Val mapped_type
Types for STL compliance.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
bool operator==(const HashTable< Key, Val > &from) const
Checks whether two hashtables contain the same elements.
HashTableBucket< Key, Val > Bucket
The buckets where data are stored.
void _clearIterators_()
Clear all the safe iterators.
bool _key_uniqueness_policy_
Shall we check for key uniqueness in the table?
Size _size_
The number of nodes in vector ' __nodes'.
Val & operator[](const Key &key)
Returns a reference on the value the key of which is passed in argument.
Size _nb_elements_
Number of elements of type Val stored in the hash table.
optional_ref< const Key > tryGetKey(const Key &key) const
Returns an optional reference to a given key, or nullptr if the hash table does not contain it.
HashTable(Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
Default constructor.
friend class HashTableIterator< Key, Val >
bool keyUniquenessPolicy() const noexcept
Returns the current checking policy.
optional_ref< Val > tryGet(const Key &key)
Returns a pointer to the value associated with a given key, or nullptr if the key does not exist.
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.
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
#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
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.
Emplace
A dummy type for the emplace constructor.
std::pair< const Key, Val > pair
The pair stored in this bucket.
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.
HashTableBucket()=default
Class constructor.
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.