Skip to content

Commit 3812783

Browse files
authored
[NFC] Use ParentIndexIterator in SmallVector (#7826)
SmallVector previously (partially) implemented its own random access iterators using a pointer to the parent SmallVector and an index, but ParentIndexIterator is a more complete implementation of the same iterator design. Reduce duplication and fix missing iterator functionality by using ParentIndexIterator.
1 parent 4d9f6f5 commit 3812783

1 file changed

Lines changed: 16 additions & 50 deletions

File tree

src/support/small_vector.h

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525
#include <array>
2626
#include <cassert>
27-
#include <iterator>
2827
#include <vector>
2928

29+
#include "support/parent_index_iterator.h"
30+
3031
namespace wasm {
3132

3233
// 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 {
183184

184185
// iteration
185186

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> {
190188
using value_type = T;
191-
using difference_type = long;
192-
using reference = T&;
193189
using pointer = T*;
190+
using reference = T&;
194191

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} {}
227194

228-
bool operator==(const Iterator& other) const {
229-
return parent == other.parent && index == other.index;
230-
}
195+
T& operator*() { return (*this->parent)[this->index]; }
231196
};
232197

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&;
238203

239-
struct ConstIterator : IteratorBase<const SmallVector<T, N>, ConstIterator> {
240204
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]; }
243209
};
244210

245211
Iterator begin() { return Iterator(this, 0); }

0 commit comments

Comments
 (0)