|
5 | 5 | #include <vector> |
6 | 6 | #include <queue> |
7 | 7 | #include <cstdint> |
| 8 | +#include <iterator> |
8 | 9 |
|
9 | 10 | // Tilemaker collects OutputObjects in a list that |
10 | 11 | // - spills to disk |
@@ -32,114 +33,111 @@ namespace AppendVectorNS { |
32 | 33 | using reference = T&; |
33 | 34 |
|
34 | 35 | Iterator(AppendVector<T>& appendVector, uint16_t vec, uint16_t offset): |
35 | | - appendVector(&appendVector), vec(vec), offset(offset) {} |
| 36 | + appendVector(&appendVector), index(difference_type(vec) * APPEND_VECTOR_SIZE + offset) {} |
36 | 37 |
|
37 | 38 | Iterator(): |
38 | | - appendVector(nullptr), vec(0), offset(0) {} |
| 39 | + appendVector(nullptr), index(0) {} |
39 | 40 |
|
40 | 41 |
|
41 | 42 | bool operator<(const Iterator& other) const { |
42 | | - if (vec < other.vec) |
43 | | - return true; |
| 43 | + return index < other.index; |
| 44 | + } |
44 | 45 |
|
45 | | - if (vec > other.vec) |
46 | | - return false; |
| 46 | + bool operator>(const Iterator& other) const { |
| 47 | + return other < *this; |
| 48 | + } |
47 | 49 |
|
48 | | - return offset < other.offset; |
| 50 | + bool operator<=(const Iterator& other) const { |
| 51 | + return !(other < *this); |
49 | 52 | } |
50 | 53 |
|
51 | 54 | bool operator>=(const Iterator& other) const { |
52 | 55 | return !(*this < other); |
53 | 56 | } |
54 | 57 |
|
55 | | - Iterator operator-(int delta) const { |
56 | | - int64_t absolute = vec * APPEND_VECTOR_SIZE + offset; |
57 | | - absolute -= delta; |
58 | | - return Iterator(*appendVector, absolute / APPEND_VECTOR_SIZE, absolute % APPEND_VECTOR_SIZE); |
| 58 | + Iterator operator-(difference_type delta) const { |
| 59 | + Iterator result = *this; |
| 60 | + result -= delta; |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + Iterator operator+(difference_type delta) const { |
| 65 | + Iterator result = *this; |
| 66 | + result += delta; |
| 67 | + return result; |
59 | 68 | } |
60 | 69 |
|
61 | | - Iterator operator+(int delta) const { |
62 | | - int64_t absolute = vec * APPEND_VECTOR_SIZE + offset; |
63 | | - absolute += delta; |
64 | | - return Iterator(*appendVector, absolute / APPEND_VECTOR_SIZE, absolute % APPEND_VECTOR_SIZE); |
| 70 | + friend Iterator operator+(difference_type delta, const Iterator& iterator) { |
| 71 | + return iterator + delta; |
65 | 72 | } |
66 | 73 |
|
67 | 74 | bool operator==(const Iterator& other) const { |
68 | | - return appendVector == other.appendVector && vec == other.vec && offset == other.offset; |
| 75 | + return appendVector == other.appendVector && index == other.index; |
69 | 76 | } |
70 | 77 |
|
71 | 78 | bool operator!=(const Iterator& other) const { |
72 | 79 | return !(*this == other); |
73 | 80 | } |
74 | 81 |
|
75 | | - std::ptrdiff_t operator-(const Iterator& other) const { |
76 | | - int64_t absolute = vec * APPEND_VECTOR_SIZE + offset; |
77 | | - int64_t otherAbsolute = other.vec * APPEND_VECTOR_SIZE + other.offset; |
78 | | - |
79 | | - return absolute - otherAbsolute; |
| 82 | + difference_type operator-(const Iterator& other) const { |
| 83 | + return index - other.index; |
80 | 84 | } |
81 | 85 |
|
82 | 86 | reference operator*() const { |
83 | | - auto& vector = appendVector->vecs[vec]; |
84 | | - auto& el = vector[offset]; |
| 87 | + auto& vector = appendVector->vecs[index / APPEND_VECTOR_SIZE]; |
| 88 | + auto& el = vector[index % APPEND_VECTOR_SIZE]; |
85 | 89 | return el; |
86 | 90 | } |
87 | 91 |
|
| 92 | + reference operator[](difference_type delta) const { |
| 93 | + return *(*this + delta); |
| 94 | + } |
| 95 | + |
88 | 96 | pointer operator->() const { |
89 | | - auto& vector = appendVector->vecs[vec]; |
90 | | - auto& el = vector[offset]; |
| 97 | + auto& vector = appendVector->vecs[index / APPEND_VECTOR_SIZE]; |
| 98 | + auto& el = vector[index % APPEND_VECTOR_SIZE]; |
91 | 99 | return ⪙ |
92 | 100 | } |
93 | 101 |
|
94 | | - Iterator& operator+= (int delta) { |
95 | | - int64_t absolute = vec * APPEND_VECTOR_SIZE + offset; |
96 | | - absolute += delta; |
97 | | - |
98 | | - vec = absolute / APPEND_VECTOR_SIZE; |
99 | | - offset = absolute % APPEND_VECTOR_SIZE; |
| 102 | + Iterator& operator+= (difference_type delta) { |
| 103 | + index += delta; |
100 | 104 | return *this; |
101 | 105 | } |
102 | 106 |
|
103 | | - Iterator& operator-= (int delta) { |
104 | | - int64_t absolute = vec * APPEND_VECTOR_SIZE + offset; |
105 | | - absolute -= delta; |
106 | | - |
107 | | - vec = absolute / APPEND_VECTOR_SIZE; |
108 | | - offset = absolute % APPEND_VECTOR_SIZE; |
| 107 | + Iterator& operator-= (difference_type delta) { |
| 108 | + index -= delta; |
109 | 109 | return *this; |
110 | 110 | } |
111 | 111 |
|
112 | 112 | // Prefix increment |
113 | 113 | Iterator& operator++() { |
114 | | - offset++; |
115 | | - if (offset == APPEND_VECTOR_SIZE) { |
116 | | - offset = 0; |
117 | | - vec++; |
118 | | - } |
| 114 | + index++; |
119 | 115 | return *this; |
120 | 116 | } |
121 | 117 |
|
122 | 118 | // Postfix increment |
123 | | - Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; } |
| 119 | + Iterator operator++(int) { |
| 120 | + Iterator result = *this; |
| 121 | + ++*this; |
| 122 | + return result; |
| 123 | + } |
124 | 124 |
|
125 | 125 | // Prefix decrement |
126 | 126 | Iterator& operator--() { |
127 | | - if (offset > 0) { |
128 | | - offset--; |
129 | | - } else { |
130 | | - vec--; |
131 | | - offset = APPEND_VECTOR_SIZE - 1; |
132 | | - } |
133 | | - |
| 127 | + index--; |
134 | 128 | return *this; |
135 | 129 | } |
136 | 130 |
|
137 | 131 | // Postfix decrement |
138 | | - Iterator operator--(int) { Iterator tmp = *this; --(*this); return tmp; } |
| 132 | + Iterator operator--(int) { |
| 133 | + Iterator result = *this; |
| 134 | + --*this; |
| 135 | + return result; |
| 136 | + } |
139 | 137 |
|
140 | 138 | private: |
141 | 139 | mutable AppendVector<T>* appendVector; |
142 | | - int32_t vec, offset; |
| 140 | + difference_type index; |
143 | 141 | }; |
144 | 142 |
|
145 | 143 | AppendVector(): |
|
0 commit comments