From c5a22baecfdfc2707bcd06b8e9f168348a6afa43 Mon Sep 17 00:00:00 2001 From: Ryoga Date: Wed, 27 Mar 2019 08:47:35 +0900 Subject: [PATCH] Change ArenaVector::Iterator to satisfy standard (Legacy)RandomAccessIterator concept (#1962) --- src/mixed_arena.h | 81 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/mixed_arena.h b/src/mixed_arena.h index 46487b7fc19..5f48f52202d 100644 --- a/src/mixed_arena.h +++ b/src/mixed_arena.h @@ -277,31 +277,102 @@ class ArenaVectorBase { // iteration struct Iterator { + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using reference = T&; + const SubType* parent; size_t index; + Iterator() : parent(nullptr), index(0) {} Iterator(const SubType* 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 index != other.index || parent != other.parent; + return !(*this == other); + } + + bool operator<(const Iterator& other) const { + assert(parent == other.parent); + return index < other.index; + } + + bool operator>(const Iterator& other) const { + return other < *this; + } + + bool operator<=(const Iterator& other) const { + return !(other < *this); + } + + bool operator>=(const Iterator& other) const { + return !(*this < other); } - void operator++() { + Iterator& operator++() { index++; + return *this; } - Iterator& operator+=(int off) { + Iterator& operator--() { + index--; + return *this; + } + + Iterator operator++(int) { + Iterator it = *this; + ++*this; + return it; + } + + Iterator operator--(int) { + Iterator it = *this; + --*this; + return it; + } + + Iterator& operator+=(std::ptrdiff_t off) { index += off; return *this; } - const Iterator operator+(int off) const { + Iterator& operator-=(std::ptrdiff_t off) { + return *this += -off; + } + + Iterator operator+(std::ptrdiff_t off) const { return Iterator(*this) += off; } - T& operator*() { + Iterator operator-(std::ptrdiff_t off) const { + return *this + -off; + } + + std::ptrdiff_t operator-(const Iterator& other) const { + assert(parent == other.parent); + return index - other.index; + } + + friend Iterator operator+(std::ptrdiff_t off, const Iterator& it) { + return it + off; + } + + T& operator*() const { return (*parent)[index]; } + + T& operator[](std::ptrdiff_t off) const { + return (*parent)[index + off]; + } + + T* operator->() const { + return &(*parent)[index]; + } }; Iterator begin() const {