Iterator support.
| 185 | |
| 186 | // Iterator support. |
| 187 | struct iterator { |
| 188 | // Standard iterator typedefs |
| 189 | // using difference_type = std::ptrdiff_t; |
| 190 | using value_type = pair<const Key, T>; // Keep const Key as per standard |
| 191 | using pointer = value_type *; |
| 192 | using reference = value_type &; |
| 193 | using iterator_category = fl::forward_iterator_tag; |
| 194 | |
| 195 | iterator() FL_NOEXCEPT : _map(nullptr), _idx(0) {} |
| 196 | iterator(unordered_map *m, fl::size idx) : _map(m), _idx(idx) { |
| 197 | advance_to_occupied(); |
| 198 | } |
| 199 | |
| 200 | reference operator*() const { |
| 201 | auto &e = _map->_buckets[_idx]; |
| 202 | // Entry and pair<const Key, T> have the same memory layout |
| 203 | // Safe to reinterpret since we only add const to Key |
| 204 | return *fl::bit_cast<value_type*>(&e); |
| 205 | } |
| 206 | |
| 207 | pointer operator->() const { |
| 208 | return &(operator*()); |
| 209 | } |
| 210 | |
| 211 | iterator &operator++() { |
| 212 | ++_idx; |
| 213 | advance_to_occupied(); |
| 214 | return *this; |
| 215 | } |
| 216 | |
| 217 | iterator operator++(int) { |
| 218 | iterator tmp = *this; |
| 219 | ++(*this); |
| 220 | return tmp; |
| 221 | } |
| 222 | |
| 223 | bool operator==(const iterator &o) const { |
| 224 | return _map == o._map && _idx == o._idx; |
| 225 | } |
| 226 | bool operator!=(const iterator &o) const { return !(*this == o); } |
| 227 | |
| 228 | void advance_to_occupied() { |
| 229 | if (!_map) |
| 230 | return; |
| 231 | fl::size cap = _map->_buckets.size(); |
| 232 | while (_idx < cap && !_map->is_occupied(_idx)) |
| 233 | ++_idx; |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | unordered_map *_map; |
| 238 | fl::size _idx; |
| 239 | friend class unordered_map; |
| 240 | }; |
| 241 | |
| 242 | struct const_iterator { |
| 243 | // Standard iterator typedefs |
no outgoing calls
no test coverage detected