| 253 | /// \brief Iterator representing the current run during iteration over a |
| 254 | /// run-end encoded array |
| 255 | class Iterator { |
| 256 | public: |
| 257 | Iterator(PrivateTag, const RunEndEncodedArraySpan& span, int64_t logical_pos, |
| 258 | int64_t physical_pos) |
| 259 | : span(span), logical_pos_(logical_pos), physical_pos_(physical_pos) {} |
| 260 | |
| 261 | /// \brief Return the physical index of the run |
| 262 | /// |
| 263 | /// The values array can be addressed with this index to get the value |
| 264 | /// that makes up the run. |
| 265 | /// |
| 266 | /// NOTE: if this Iterator is equal to RunEndEncodedArraySpan::end(), |
| 267 | /// the value returned is undefined. |
| 268 | int64_t index_into_array() const { return physical_pos_; } |
| 269 | |
| 270 | /// \brief Return the initial logical position of the run |
| 271 | /// |
| 272 | /// If this Iterator is equal to RunEndEncodedArraySpan::end(), this is |
| 273 | /// the same as RunEndEncodedArraySpan::length(). |
| 274 | int64_t logical_position() const { return logical_pos_; } |
| 275 | |
| 276 | /// \brief Return the logical position immediately after the run. |
| 277 | /// |
| 278 | /// Pre-condition: *this != RunEndEncodedArraySpan::end() |
| 279 | int64_t run_end() const { return span.run_end(physical_pos_); } |
| 280 | |
| 281 | /// \brief Returns the logical length of the run. |
| 282 | /// |
| 283 | /// Pre-condition: *this != RunEndEncodedArraySpan::end() |
| 284 | int64_t run_length() const { return run_end() - logical_pos_; } |
| 285 | |
| 286 | /// \brief Check if the iterator is at the end of the array. |
| 287 | /// |
| 288 | /// This can be used to avoid paying the cost of a call to |
| 289 | /// RunEndEncodedArraySpan::end(). |
| 290 | /// |
| 291 | /// \return true if the iterator is at the end of the array |
| 292 | bool is_end(const RunEndEncodedArraySpan& span) const { |
| 293 | return logical_pos_ >= span.length(); |
| 294 | } |
| 295 | |
| 296 | Iterator& operator++() { |
| 297 | logical_pos_ = span.run_end(physical_pos_); |
| 298 | physical_pos_ += 1; |
| 299 | return *this; |
| 300 | } |
| 301 | |
| 302 | Iterator operator++(int) { |
| 303 | const Iterator prev = *this; |
| 304 | ++(*this); |
| 305 | return prev; |
| 306 | } |
| 307 | |
| 308 | Iterator& operator--() { |
| 309 | physical_pos_ -= 1; |
| 310 | logical_pos_ = (physical_pos_ > 0) ? span.run_end(physical_pos_ - 1) : 0; |
| 311 | return *this; |
| 312 | } |