| 48 | template <typename ArrayType, |
| 49 | typename ValueAccessor = detail::DefaultValueAccessor<ArrayType>> |
| 50 | class ArrayIterator { |
| 51 | public: |
| 52 | using value_type = std::optional<typename ValueAccessor::ValueType>; |
| 53 | using difference_type = int64_t; |
| 54 | using pointer = value_type*; |
| 55 | using reference = value_type&; |
| 56 | using iterator_category = std::random_access_iterator_tag; |
| 57 | |
| 58 | // Some algorithms need to default-construct an iterator |
| 59 | ArrayIterator() : array_(NULLPTR), index_(0) {} |
| 60 | |
| 61 | explicit ArrayIterator(const ArrayType& array, int64_t index = 0) |
| 62 | : array_(&array), index_(index) {} |
| 63 | |
| 64 | // Value access |
| 65 | value_type operator*() const { |
| 66 | assert(array_); |
| 67 | return array_->IsNull(index_) ? value_type{} : array_->GetView(index_); |
| 68 | } |
| 69 | |
| 70 | value_type operator[](difference_type n) const { |
| 71 | assert(array_); |
| 72 | return array_->IsNull(index_ + n) ? value_type{} : array_->GetView(index_ + n); |
| 73 | } |
| 74 | |
| 75 | int64_t index() const { return index_; } |
| 76 | |
| 77 | // Forward / backward |
| 78 | ArrayIterator& operator++() { |
| 79 | ++index_; |
| 80 | return *this; |
| 81 | } |
| 82 | ArrayIterator& operator--() { |
| 83 | --index_; |
| 84 | return *this; |
| 85 | } |
| 86 | ArrayIterator operator++(int) { |
| 87 | ArrayIterator tmp(*this); |
| 88 | ++index_; |
| 89 | return tmp; |
| 90 | } |
| 91 | ArrayIterator operator--(int) { |
| 92 | ArrayIterator tmp(*this); |
| 93 | --index_; |
| 94 | return tmp; |
| 95 | } |
| 96 | |
| 97 | // Arithmetic |
| 98 | difference_type operator-(const ArrayIterator& other) const { |
| 99 | return index_ - other.index_; |
| 100 | } |
| 101 | ArrayIterator operator+(difference_type n) const { |
| 102 | return ArrayIterator(*array_, index_ + n); |
| 103 | } |
| 104 | ArrayIterator operator-(difference_type n) const { |
| 105 | return ArrayIterator(*array_, index_ - n); |
| 106 | } |
| 107 | friend inline ArrayIterator operator+(difference_type diff, |