diff options
author | Thomas Lively <7121787+tlively@users.noreply.github.com> | 2022-01-21 11:09:34 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-21 11:09:34 -0800 |
commit | 02ef6b11d740f5a3aa2071b53b35c306d6ddfa7a (patch) | |
tree | 00466c409634df147d52d9400a263f563bd642b4 /src/wasm-type.h | |
parent | 060442225165d0423d06ea33ab865e850b54f61b (diff) | |
download | binaryen-02ef6b11d740f5a3aa2071b53b35c306d6ddfa7a.tar.gz binaryen-02ef6b11d740f5a3aa2071b53b35c306d6ddfa7a.tar.bz2 binaryen-02ef6b11d740f5a3aa2071b53b35c306d6ddfa7a.zip |
Create `ParentIndexIterator` to reduce iterator boilerplate (#4469)
Add a utility class for defining all the common operations like pre- and post-
increment and decrement, addition and subtraction, and assigning addition and
subtraction for iterators that are comprised of a parent object and an index
into that parent object. Use the new utility to reduce the boilerplate in
wasm-type.h. Add a new test of the iterator behavior.
Diffstat (limited to 'src/wasm-type.h')
-rw-r--r-- | src/wasm-type.h | 61 |
1 files changed, 8 insertions, 53 deletions
diff --git a/src/wasm-type.h b/src/wasm-type.h index 48b6f2217..c99113317 100644 --- a/src/wasm-type.h +++ b/src/wasm-type.h @@ -18,6 +18,7 @@ #define wasm_wasm_type_h #include "support/name.h" +#include "support/parent_index_iterator.h" #include "wasm-features.h" #include <optional> #include <ostream> @@ -270,70 +271,24 @@ public: std::string toString() const; - struct Iterator { - // Iterator traits - using iterator_category = std::random_access_iterator_tag; + size_t size() const; + + struct Iterator : ParentIndexIterator<const Type*, Iterator> { using value_type = Type; - using difference_type = std::ptrdiff_t; using pointer = const Type*; using reference = const Type&; - - const Type* parent; - size_t index; - Iterator(const Type* parent, size_t index) : parent(parent), index(index) {} - bool operator==(const Iterator& other) const { - return index == other.index && parent == other.parent; - } - bool operator!=(const Iterator& other) const { return !(*this == other); } - Iterator& operator++() { - ++index; - return *this; - } - Iterator& operator--() { - --index; - return *this; - } - Iterator operator++(int) { - auto it = *this; - index++; - return it; - } - Iterator operator--(int) { - auto it = *this; - index--; - return it; - } - Iterator& operator+=(difference_type off) { - index += off; - return *this; - } - Iterator operator+(difference_type off) const { - return Iterator(*this) += off; - } - Iterator& operator-=(difference_type off) { - index -= off; - return *this; - } - Iterator operator-(difference_type off) const { - return Iterator(*this) -= off; - } - difference_type operator-(const Iterator& other) const { - assert(parent == other.parent); - return index - other.index; - } - const value_type& operator*() const; + reference operator*() const; }; - Iterator begin() const { return Iterator(this, 0); } - Iterator end() const; + Iterator begin() const { return Iterator{{this, 0}}; } + Iterator end() const { return Iterator{{this, size()}}; } std::reverse_iterator<Iterator> rbegin() const { return std::make_reverse_iterator(end()); } std::reverse_iterator<Iterator> rend() const { return std::make_reverse_iterator(begin()); } - size_t size() const { return end() - begin(); } - const Type& operator[](size_t i) const; + const Type& operator[](size_t i) const { return *Iterator{{this, i}}; } }; class HeapType { |