@@ -26,7 +26,11 @@ probing.
2626
2727#include < algorithm>
2828#include < cstddef>
29+ #include < iterator>
30+ #include < memory>
2931#include < stdexcept>
32+ #include < type_traits>
33+ #include < utility>
3034#include < vector>
3135
3236namespace rigtorp {
@@ -65,6 +69,9 @@ class HashMap {
6569 using allocator_type = Allocator;
6670 using reference = value_type &;
6771 using const_reference = const value_type &;
72+ using pointer = typename std::allocator_traits<allocator_type>::pointer;
73+ using const_pointer =
74+ typename std::allocator_traits<allocator_type>::const_pointer;
6875 using buckets = std::vector<value_type, allocator_type>;
6976
7077 template <typename ContT, typename IterVal> struct hm_iterator {
@@ -74,39 +81,52 @@ class HashMap {
7481 using reference = value_type &;
7582 using iterator_category = std::forward_iterator_tag;
7683
84+ hm_iterator () = default ;
85+
7786 bool operator ==(const hm_iterator &other) const {
7887 return other.hm_ == hm_ && other.idx_ == idx_;
7988 }
80- bool operator !=(const hm_iterator &other) const {
81- return !(other == *this );
82- }
8389
8490 hm_iterator &operator ++() {
8591 ++idx_;
8692 advance_past_empty ();
8793 return *this ;
8894 }
8995
96+ hm_iterator operator ++(int ) {
97+ auto tmp = *this ;
98+ ++(*this );
99+ return tmp;
100+ }
101+
90102 reference operator *() const { return hm_->buckets_ [idx_]; }
91103 pointer operator ->() const { return &hm_->buckets_ [idx_]; }
92104
93- private:
94- explicit hm_iterator (ContT *hm) : hm_(hm) { advance_past_empty (); }
95- explicit hm_iterator (ContT *hm, size_type idx) : hm_(hm), idx_(idx) {}
96105 template <typename OtherContT, typename OtherIterVal>
106+ requires std::is_convertible_v<OtherContT *, ContT *> &&
107+ std::is_convertible_v<OtherIterVal *, IterVal *>
97108 hm_iterator (const hm_iterator<OtherContT, OtherIterVal> &other)
98109 : hm_(other.hm_), idx_(other.idx_) {}
99110
111+ private:
112+ explicit hm_iterator (ContT *hm) : hm_(hm) { advance_past_empty (); }
113+ explicit hm_iterator (ContT *hm, size_type idx) : hm_(hm), idx_(idx) {}
114+
100115 void advance_past_empty () {
101116 while (idx_ < hm_->buckets_ .size () &&
102117 key_equal ()(hm_->buckets_ [idx_].first , hm_->empty_key_ )) {
103118 ++idx_;
104119 }
120+ if (idx_ == hm_->buckets_ .size ()) {
121+ hm_ = nullptr ;
122+ idx_ = 0 ;
123+ }
105124 }
106125
107126 ContT *hm_ = nullptr ;
108127 typename ContT::size_type idx_ = 0 ;
109128 friend ContT;
129+ template <typename , typename > friend struct hm_iterator ;
110130 };
111131
112132 using iterator = hm_iterator<HashMap, value_type>;
@@ -141,15 +161,11 @@ class HashMap {
141161
142162 const_iterator cbegin () const noexcept { return const_iterator (this ); }
143163
144- iterator end () noexcept { return iterator (this , buckets_. size () ); }
164+ iterator end () noexcept { return iterator (); }
145165
146- const_iterator end () const noexcept {
147- return const_iterator (this , buckets_.size ());
148- }
166+ const_iterator end () const noexcept { return const_iterator (); }
149167
150- const_iterator cend () const noexcept {
151- return const_iterator (this , buckets_.size ());
152- }
168+ const_iterator cend () const noexcept { return const_iterator (); }
153169
154170 // Capacity
155171 bool empty () const noexcept { return size () == 0 ; }
@@ -161,7 +177,7 @@ class HashMap {
161177 // Modifiers
162178 void clear () noexcept {
163179 for (auto &b : buckets_) {
164- if (b.first != empty_key_) {
180+ if (! key_equal ()( b.first , empty_key_) ) {
165181 b.first = empty_key_;
166182 }
167183 }
@@ -173,15 +189,17 @@ class HashMap {
173189 }
174190
175191 std::pair<iterator, bool > insert (value_type &&value) {
176- return emplace_impl (value.first , std::move (value.second ));
192+ return emplace_impl (std::move ( value.first ) , std::move (value.second ));
177193 }
178194
179195 template <typename ... Args>
180196 std::pair<iterator, bool > emplace (Args &&...args) {
181197 return emplace_impl (std::forward<Args>(args)...);
182198 }
183199
184- void erase (iterator it) { erase_impl (it); }
200+ void erase (iterator it) { erase_at (it); }
201+
202+ void erase (const_iterator it) { erase_at (it); }
185203
186204 size_type erase (const key_type &key) { return erase_impl (key); }
187205
@@ -208,6 +226,10 @@ class HashMap {
208226 return emplace_impl (key).first ->second ;
209227 }
210228
229+ mapped_type &operator [](key_type &&key) {
230+ return emplace_impl (std::move (key)).first ->second ;
231+ }
232+
211233 size_type count (const key_type &key) const { return count_impl (key); }
212234
213235 template <typename K> size_type count (const K &x) const {
@@ -249,15 +271,15 @@ class HashMap {
249271
250272private:
251273 template <typename K, typename ... Args>
252- std::pair<iterator, bool > emplace_impl (const K &key, Args &&...args) {
274+ std::pair<iterator, bool > emplace_impl (K & &key, Args &&...args) {
253275 if (key_equal ()(empty_key_, key)) [[unlikely]] {
254276 detail::throw_empty_key ();
255277 }
256278 reserve (size_ + 1 );
257279 for (size_t idx = key_to_idx (key);; idx = probe_next (idx)) {
258280 if (key_equal ()(buckets_[idx].first , empty_key_)) {
259281 buckets_[idx].second = mapped_type (std::forward<Args>(args)...);
260- buckets_[idx].first = key;
282+ buckets_[idx].first = std::forward<K>( key) ;
261283 size_++;
262284 return {iterator (this , idx), true };
263285 } else if (key_equal ()(buckets_[idx].first , key)) {
@@ -266,7 +288,7 @@ class HashMap {
266288 }
267289 }
268290
269- void erase_impl (iterator it) {
291+ void erase_at (const_iterator it) {
270292 size_t bucket = it.idx_ ;
271293 for (size_t idx = probe_next (bucket);; idx = probe_next (idx)) {
272294 if (key_equal ()(buckets_[idx].first , empty_key_)) {
@@ -286,7 +308,7 @@ class HashMap {
286308 template <typename K> size_type erase_impl (const K &key) {
287309 auto it = find_impl (key);
288310 if (it != end ()) {
289- erase_impl (it);
311+ erase_at (it);
290312 return 1 ;
291313 }
292314 return 0 ;
0 commit comments