|
24 | 24 |
|
25 | 25 | #include <array> |
26 | 26 | #include <cassert> |
27 | | -#include <iterator> |
28 | 27 | #include <vector> |
29 | 28 |
|
| 29 | +#include "support/parent_index_iterator.h" |
| 30 | + |
30 | 31 | namespace wasm { |
31 | 32 |
|
32 | 33 | // We don't understand this warning, only here and only on aarch64 and riscv64, |
@@ -183,63 +184,28 @@ template<typename T, size_t N> class SmallVector { |
183 | 184 |
|
184 | 185 | // iteration |
185 | 186 |
|
186 | | - template<typename Parent, typename Iterator> struct IteratorBase { |
187 | | - // TODO: Add remaining things from |
188 | | - // https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator |
189 | | - using iterator_category = std::random_access_iterator_tag; |
| 187 | + struct Iterator : ParentIndexIterator<SmallVector<T, N>*, Iterator> { |
190 | 188 | using value_type = T; |
191 | | - using difference_type = long; |
192 | | - using reference = T&; |
193 | 189 | using pointer = T*; |
| 190 | + using reference = T&; |
194 | 191 |
|
195 | | - Parent* parent; |
196 | | - size_t index; |
197 | | - |
198 | | - IteratorBase(Parent* parent, size_t index) : parent(parent), index(index) {} |
199 | | - |
200 | | - bool operator!=(const Iterator& other) const { |
201 | | - return index != other.index || parent != other.parent; |
202 | | - } |
203 | | - |
204 | | - Iterator& operator++() { |
205 | | - Iterator& self = *static_cast<Iterator*>(this); |
206 | | - index++; |
207 | | - return self; |
208 | | - } |
209 | | - Iterator operator++(int) { |
210 | | - Iterator self = *static_cast<Iterator*>(this); |
211 | | - index++; |
212 | | - return self; |
213 | | - } |
214 | | - |
215 | | - Iterator& operator+=(difference_type off) { |
216 | | - index += off; |
217 | | - return *this; |
218 | | - } |
219 | | - |
220 | | - const Iterator operator+(difference_type off) const { |
221 | | - return Iterator(*this) += off; |
222 | | - } |
223 | | - |
224 | | - difference_type operator-(const Iterator& other) const { |
225 | | - return index - other.index; |
226 | | - } |
| 192 | + Iterator(SmallVector<T, N>* parent, size_t index) |
| 193 | + : ParentIndexIterator<SmallVector<T, N>*, Iterator>{parent, index} {} |
227 | 194 |
|
228 | | - bool operator==(const Iterator& other) const { |
229 | | - return parent == other.parent && index == other.index; |
230 | | - } |
| 195 | + T& operator*() { return (*this->parent)[this->index]; } |
231 | 196 | }; |
232 | 197 |
|
233 | | - struct Iterator : IteratorBase<SmallVector<T, N>, Iterator> { |
234 | | - Iterator(SmallVector<T, N>* parent, size_t index) |
235 | | - : IteratorBase<SmallVector<T, N>, Iterator>(parent, index) {} |
236 | | - value_type& operator*() { return (*this->parent)[this->index]; } |
237 | | - }; |
| 198 | + struct ConstIterator |
| 199 | + : ParentIndexIterator<const SmallVector<T, N>*, ConstIterator> { |
| 200 | + using value_type = const T; |
| 201 | + using pointer = const T*; |
| 202 | + using reference = const T&; |
238 | 203 |
|
239 | | - struct ConstIterator : IteratorBase<const SmallVector<T, N>, ConstIterator> { |
240 | 204 | ConstIterator(const SmallVector<T, N>* parent, size_t index) |
241 | | - : IteratorBase<const SmallVector<T, N>, ConstIterator>(parent, index) {} |
242 | | - const value_type& operator*() const { return (*this->parent)[this->index]; } |
| 205 | + : ParentIndexIterator<const SmallVector<T, N>*, ConstIterator>{parent, |
| 206 | + index} {} |
| 207 | + |
| 208 | + const T& operator*() const { return (*this->parent)[this->index]; } |
243 | 209 | }; |
244 | 210 |
|
245 | 211 | Iterator begin() { return Iterator(this, 0); } |
|
0 commit comments